Skip to content

Commit

Permalink
add post085
Browse files Browse the repository at this point in the history
  • Loading branch information
hilyso committed Sep 9, 2024
1 parent 00c7d30 commit a7a9690
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 0 deletions.
107 changes: 107 additions & 0 deletions source/_posts/085.kexec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: 不重启系统切换内核
date: 2024-4-17 08:59:26
categories:
- [docker]
tags:
- kexec-tools
- 内核切换
- pve
---

## 一、 重要说明:

> kexec 实现的是一种**快速重启**,它可以避免传统的 BIOS/UEFI 启动过程,但实际上系统内的所有进程确实会终止并重新启动。因此,尽管使用 kexec 可以跳过硬件初始化的过程,**系统层面的重启依然会发生**
- 1. **加载新内核**`kexec -l` 命令会将新内核加载到内存中,同时准备好初始内存盘(initrd)等启动所需的数据。
- 2. **停止当前进程并执行新内核**:当执行 `kexec -e` 时,当前正在运行的系统会立即终止所有用户态进程,清理掉旧内核占用的资源,然后切换到新内核,重新启动整个系统。
- 这个过程跳过了 BIOS/UEFI 等硬件初始化步骤(如自检、硬盘探测等),直接由 CPU 开始执行新加载的内核。
- 但是,所有进程仍然会终止并重新启动,因为新内核需要一个干净的状态。换句话说,从操作系统的角度来看,所有进程都会被关闭,只不过跳过了硬件层面的重启。


## 二、 安装新内核

### 2.1 添加公钥
` rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org `

### 2.2 添加源

``` bash

### Name: ELRepo.org Community Enterprise Linux Repository for el7
### URL: https://elrepo.org/

[elrepo]
name=ELRepo.org Community Enterprise Linux Repository - el7
baseurl=https://mirrors.nju.edu.cn/elrepo/archive/elrepo/el7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

[elrepo-testing]
name=ELRepo.org Community Enterprise Linux Testing Repository - el7
baseurl=http://elrepo.org/linux/testing/el7/$basearch/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

[elrepo-kernel]
name=ELRepo.org Community Enterprise Linux Kernel Repository - el7
baseurl=https://mirrors.nju.edu.cn/elrepo/archive/kernel/el7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

[elrepo-extras]
name=ELRepo.org Community Enterprise Linux Extras Repository - el7
baseurl=https://mirrors.nju.edu.cn/elrepo/archive/extras/el7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-elrepo.org
protect=0

```

### 2.3 安装TL版本

` yum install -y kernel-lt kernel-lt-devel `

### 2.4 查看已安装内核

` awk -F\' '$1=="menuentry " {print $2}' /etc/grub2.cfg `


## 三、 切换内核


### 3.1 更新 GRUB 配置

` grub2-mkconfig -o /boot/grub2/grub.cfg `


### 3.2 确保安装了 kexec

` yum install kexec-tools`

### 3.3 找到新安装的内核路径

` ls -ahl /boot/vmlinuz-* `
![](/images/085.kexec.md.01.png)
` ls -ahl /boot/initramfs-* `
![](/images/085.kexec.md.02.png)

### 3.4 加载新内核

` kexec -l /boot/vmlinuz-5.4.278-1.el7.elrepo.x86_64 --initrd=/boot/initramfs-5.4.278-1.el7.elrepo.x86_64.img --reuse-cmdline `

### 3.5 切换内核

` kexec -e `

### 3.6 检查系统

![](/images/085.kexec.md.03.png)

Binary file added source/images/085.kexec.md.01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/images/085.kexec.md.02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added source/images/085.kexec.md.03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit a7a9690

Please sign in to comment.