用KEXEC迅速切换内核

2015年2月13日09:39:11 评论 8,139

前言

研究这个功能起初目的是要给线上的几台服务器添加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.el5
drwxr-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.img
drwx------ 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/sda
default=0
timeout=5
splashimage=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title 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.img
title 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 -a
Linux 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
weinxin
聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: