用KEXEC迅速切换内核

前言

研究此功能的最初目的是增加在线服务器iptables监控,但发现iptables状态全部为Firewall is stopped。原因是几台在线机器更新了kernel,但是机器没有重启。因此,您可以使用新的核心文件,而不需要重置启动(冷启动)。

本文应用的系统是centos5.5,内核为2.6.18-194.el5,新内核为2.6.18-371.3.1.el5

操作

1. 首先满足两个必要条件

a、内核版必须是 2.6.13 或以上,因为这个版本内置 kexec 系统呼叫程序。(使用指令可以检查系统是否支持,显示为y即可。?)

# cat /boot/config-2.6.18-194.el5 |grep KEXEC

b、 系统要求装置kexec-tools (运用yum install update晋级内核此工具会自动装置),或者运用yum指令

# yum install kexec-tools

2. 等候yum install updat晋升后,检查boot另外两个内核文件可以在文件夹中找到,2.6.18-371.3.1.el5我们要用kexec切换的新内核

[root@vps172160115 ~]# ll /boot/总计 12355-rw-r--r-- 1 root root 66887 2010-04-03 config-2.6.18-194.el5-rw-r--r-- 1 root root 67858 12-06 02:20 config-2.6.18-371.3.1.el5drwxr-xr-x 2 root root 1024 01-16 05:40 grub-rw------- 1 root root 2729093 01-15 10:17 initrd-2.6.18-194.el5.img-rw------- 1 root root 2774929 01-16 05:40 initrd-2.6.18-371.3.1.el5.imgdrwx------ 2 root root 12288 01-15 18:09 lost found-rw-r--r-- 1 root root 80032 2009-03-17 message-rw-r--r-- 1 root root 112656 2010-04-03 symvers-2.6.18-194.el5.gz-rw-r--r-- 1 root root 118789 12-06 02:21 symvers-2.6.18-371.3.1.el5.gz-rw-r--r-- 1 root root 1242340 2010-04-03 System.map-2.6.18-194.el5-rw-r--r-- 1 root root 1284091 12-06 02:20 System.map-2.6.18-371.3.1.el5-rw-r--r-- 1 root root 1953660 2010-04-03 vmlinuz-2.6.18-194.el5-rw-r--r-- 1 root root 2127660 12-06 02:20 vmlinuz-2.6.18-371.3.1.el5

3. 检查/boot/grub/menu.lst 内核的句子也有两个内核启动项

[root@vps172160115 ~]# cat /boot/grub/menu.lst# grub.conf generated by anaconda## Note that you do not have to rerun grub after making changes to this file# NOTICE: You have a /boot partition. This means that# all kernel and initrd paths are relative to /boot/,eg.# root (hd0,0)# kernel /vmlinuz-version ro root=/dev/sda2# initrd /initrd-version.img#boot=/dev/sdadefault=0timeout=5splashimage=(hd0,0)/grub/splash.xpm.gzhiddenmenutitle CentOS (2.6.18-371.3.1.el5)root (hd0,0)kernel /vmlinuz-2.6.18-371.3.1.el5 ro root=LABEL=/initrd /initrd-2.6.18-371.3.1.el5.imgtitle CentOS (2.6.18-194.el5)root (hd0,0)kernel /vmlinuz-2.6.18-194.el5 ro root=LABEL=/initrd /initrd-2.6.18-194.el5.img

4. 加载新内核,指令如下。可成功加载

[root@vps172160115 ~]# kexec -l /boot/vmlinuz-2.6.18-371.3.1.el5 --append=\"root=LABEL=/\" --initrd=/boot/initrd-2.6.18-371.3.1.el5.img

5.重启内核收效,应用kexec -e指令即可

6.重启后,登录系统检查新内核,切换成功

[root@vps172160115 ~]# uname -aLinux vps172160115 2.6.18-371.3.1.el5 #1 SMP Thu Dec 5 12:47:02 EST 2013 x86_64 x86_64 x86_64 GNU/Linux