需求:通过内核编译的方式,升级内核版本,并删除无用的kernel开机项。
一 安装
当前版本:
Linux cli-1 3.10.0-514.el7.x86_64 #1 SMP Tue Nov 22 16:42:41 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
1、下载目标版本的kernel源码:linux-3.15.tar.gz(https://www.kernel.org)
2、解压tar包:
# tar -xvzf linux-3.15.tar.gz -C /usr/src/kernels/
3、安装编译内核所需依赖包:
# yum install ncurses-devel
4、切换到/usr/src/kernels/linux-3.15目录下
# cd /usr/src/kernels/linux-3.15
5、配置内核:
5.1 保持源码干净
[root@cli-1 /usr/src/kernels/linux-3.15]
# make mrproper
注:此操作会将历史编译过程中配置的内核功能配置文件清除掉,所以只有第一次执行内核编
译前才进行此操作,其它情况若需要清除前一次编译过程的残留数据,只需用下面的命令:
[root@cli-1 /usr/src/kernels/linux-3.15]
# make clean
此命令仅删除编译过程产生的中间文件和目标文件,而不会删除历史配置。
5.2 设置内核功能选项:
设置内核功能选项时,可以手动配置也可从拷贝配置文件。
a、手动配置:
#make menuconfig
b、从其它路径下拷贝.config
[root@cli-1 /usr/src/kernels/linux-3.15]
# cp /boot/config-3.10.0-514.el7.x86_64 ./.config
5.3 在配置过程中可能会提示缺少一些东西,把缺少的包装上再继续配置。
menuconfig界面有一个load选项,load上面刚复制过来的config文件,然后再看情况勾选一些我们需要的新特性并保存
5.4 默认编译之后的内核名称是3.15.0,可以通过修改Makefile来更改名称:
VERSION = 3
PATCHLEVEL = 15
SUBLEVEL = 0
EXTRAVERSION = 这里加上额外的信息,比如.el7.x86_64
NAME = Shuffling Zombie Juror
5.5 可以通过下面的命令查看kernelversion
[root@cli-1 /usr/src/kernels/linux-3.15]
# make kernelversion
3.15.0
[root@cli-1 /usr/src/kernels/linux-3.15]
可在字符界面中显示功能配置的图形界面,如下图:
6、编译内核
6.1 清除暂存的缓存:
# make clean
6.2 编译内核:
# make -j8 bzImage
注:其中-j8表示同时编译的任务数。如果操作系统中有 4个 CPU 核心,可通过-j4加快编译的 速度。此编译过程较长,请耐心等候。
6.2 编译内核模块:
# make modules
7、安装内核
7.1 安装内核模块(必须以root身份安装内核模块)
# make modules_install
安装完modules之后,可以通过命令查看:
[root@srv-1 ~]# ls /lib/modules
3.10.0-514.el7.x86_64 3.15.0
此时还不能使用新内核,需要将新内核加到/boot中。
7.2安装内核
# make install
查看执行结果:
[root@srv-1 ~]# ls -alh /boot/
total 189M
dr-xr-xr-x. 4 root root 4.0K Mar 21 15:48 .
dr-xr-xr-x. 17 root root 224 Mar 21 2018 ..
-rw-r--r--. 1 root root 135K Nov 23 2016 config-3.10.0-514.el7.x86_64
drwxr-xr-x. 2 root root 27 Mar 21 2018 grub
drwx------. 6 root root 111 Mar 21 15:49 grub2
-rw-------. 1 root root 47M Mar 21 2018 initramfs-0-rescue-885d6e6754a14ad6ab19e8294d4b2c1e.img
-rw-------. 1 root root 20M Mar 21 2018 initramfs-3.10.0-514.el7.x86_64.img
-rw-------. 1 root root 18M Mar 21 2018 initramfs-3.10.0-514.el7.x86_64kdump.img
-rw-------. 1 root root 39M Mar 21 14:30 initramfs-3.15.0.img
-rw-------. 1 root root 37M Mar 21 14:34 initramfs-3.15.0kdump.img
-rw-r--r--. 1 root root 600K Mar 21 2018 initrd-plymouth.img
-rw-r--r--. 1 root root 272K Nov 23 2016 symvers-3.10.0-514.el7.x86_64.gz
lrwxrwxrwx. 1 root root 23 Mar 21 14:27 System.map -> /boot/System.map-3.15.0
-rw-------. 1 root root 3.0M Nov 23 2016 System.map-3.10.0-514.el7.x86_64
-rw-r--r--. 1 root root 2.9M Mar 21 14:27 System.map-3.15.0
-rw-r--r--. 1 root root 2.9M Mar 19 23:32 System.map-3.15.0.old
lrwxrwxrwx. 1 root root 20 Mar 21 14:27 vmlinuz -> /boot/vmlinuz-3.15.0
-rwxr-xr-x. 1 root root 5.2M Mar 21 2018 vmlinuz-0-rescue-885d6e6754a14ad6ab19e8294d4b2c1e
-rwxr-xr-x. 1 root root 5.2M Nov 23 2016 vmlinuz-3.10.0-514.el7.x86_64
-rw-r--r--. 1 root root 166 Nov 23 2016 .vmlinuz-3.10.0-514.el7.x86_64.hmac
-rw-r--r--. 1 root root 5.0M Mar 21 14:27 vmlinuz-3.15.0
-rw-r--r--. 1 root root 5.0M Mar 19 23:32 vmlinuz-3.15.0.old
8、更新开机内核启动项
a、在非UEFI系统上,命令是
# grub2-mkconfig -o /boot/grub2/grub.cfg
b、在UEFI系统上,命令是
# grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
9、重启就可以看到已经安装成功的内核。
# reboot
二 卸载
安装完新的内核,旧的内核就可以删除了。
1、卸载通过yum安装的内核:
#列出所有的kernel
$rpm -qa|grep kernel
#然后卸载对应的rpm就行
$sudo yum remove xxxxx
建议至少保留一个官方内核,万一系统出问题,可以从官方内核启动。
2、卸载编译安装的内核
(1)首先从待删除内核以外的内核启动,然后删除/lib/modules下对应的文件夹
(2)删除/boot/下面对应的项
(3)删除grub开机残留项
在UEFI系统上,命令是:
# grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
在非UEFI系统上,命令是:
# grub2-mkconfig -o /boot/grub2/grub.cfg
3、设置grub默认启动项
3.1、查看grub列表
UEFI系统:
[root@host ~]##awk -F’ ‘$1=="menuentry " {print i++ " : " $2}’ /etc/grub2-efi.cfg
0 : CentOS Linux (3.15.0.el7.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-957.1.3.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-7ebbe12473084ddea5bfebf0cf63627f) 7 (Core)
非UEFI系统:
[root@host ~]# awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2.cfg
0 : CentOS Linux (3.15.0.el7.x86_64) 7 (Core)
1 : CentOS Linux (3.10.0-957.1.3.el7.x86_64) 7 (Core)
2 : CentOS Linux (0-rescue-7ebbe12473084ddea5bfebf0cf63627f) 7 (Core)
2、设置默认启动项
# grub2-set-default 0
注:“0”对应上面list的编号0
# grub2-editenv list
saved_entry=0
[root@host ~]# grub2-editenv list
saved_entry=0