一、编译安装

1、下载内核源码

https://www.kernel.org/下载longterm 4.14源码

2、准备编译

为了避免复杂的config过程,我决定利用现有内核的config配置

$cp /boot/config-3.10.0-957.1.3.el7.x86_64 ./.config

使用menuconfig工具对内核进行配置

$make menuconfig

在配置过程中可能会提示缺少一些东西,把缺少的包装上再继续配置

menuconfig界面有一个load选项,load上面刚复制过来的config文件,然后再看情况勾选一些我们需要的新特性并保存

默认编译之后的内核名称是4.14.91,可以通过修改Makefile来更改名称

# SPDX-License-Identifier: GPL-2.0
VERSION = 4
PATCHLEVEL = 14
SUBLEVEL = 91
EXTRAVERSION = 这里加上额外的信息,比如.el7.x86_64
NAME = Petit Gorille

修改之后,可以通过下面的命令查看kernelversion

$make kernelversion

3、编译

编译直接make就行,当然为了提高编译速度,最好使用多进程

编译时会产生大量的编译信息,如果你已经非常熟练了,那直接

$make > /dev/null

4、build modules(非必需,可以与步骤5合一起)

$make modules

5、安装

必须以root身份执行

#make modules_install

安装完modules之后,可以通过命令查看:

$ls /lib/modules

此时还不能使用新内核,需要将新内核加到/boot中

#make install

查看执行结果

$ls -lh /boot

CentOS7编译内核安装与卸载-LMLPHP

下次开机就能看到新内核的启动项了

你可能需要更新grub,UEFI系统命令:

#grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg

以及设置默认启动项,参考文章末尾 “ 设置默认启动项 ” 部分

二、卸载

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

$ sudo grub2-mkconfig -o /boot/efi/EFI/centos/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.14.91.el7.x86_64
Found initrd image: /boot/initramfs-4.14.91.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-957.1.3.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-957.1.3.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-7ebbe12473084ddea5bfebf0cf63627f
Found initrd image: /boot/initramfs-0-rescue-7ebbe12473084ddea5bfebf0cf63627f.img
done

在非UEFI系统上,命令是grub2-mkconfig -o /boot/grub2/grub.cfg

[root@host ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-229.14.1.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.14.1.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-229.4.2.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.4.2.el7.x86_64.img
Found linux image: /boot/vmlinuz-3.10.0-229.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-229.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-605f01abef434fb98dd1309e774b72ba
Found initrd image: /boot/initramfs-0-rescue-605f01abef434fb98dd1309e774b72ba.img
done

(3)设置grub默认启动项

A、查看grub列表

UEFI系统:

[root@host ~]##awk -F\' '$1=="menuentry " {print i++ " : " $2}' /etc/grub2-efi.cfg 
0 : CentOS Linux (4.14.91.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 (4.14.91.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)

B、设置默认启动项

[root@host ~]# grub2-set-default 0

“0”对应上面list的编号0
[root@host ~]# grub2-editenv list
saved_entry=0

更多细节请参考CentOS官方wiki

https://wiki.centos.org/zh/HowTos/Grub2

如果安装新内核重启出现Failed to mount /boot/efi,关闭SELinux就好

[root@localhost ~]# vi /etc/selinux/config

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.

// 这里改变为disabled
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected.
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

01-30 14:29