不重启系统切换内核

一、 重要说明:

kexec 实现的是一种快速重启,它可以避免传统的 BIOS/UEFI 启动过程,但实际上系统内的所有进程确实会终止并重新启动。因此,尽管使用 kexec 可以跳过硬件初始化的过程,系统层面的重启依然会发生

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

二、 安装新内核

2.1 添加公钥

rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org

2.2 添加源


### 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-*
图
ls -ahl /boot/initramfs-*
图

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 检查系统

图