Linux 内核编译过程
下载 linux 内核源代码
首先到 https://www.kernel.org/
官网下载源代码,这里以4.19.186
长期支持版本为例,展示编译过程。
安装编译依赖组件
yum makecache
yum install ncurses-devel make gcc bc openssl-devel
yum install elfutils-libelf-devel
# 将生成的内核打包为 rpm 包时使用
yum install rpm-build
查看现有的 linux 内核配置
可以到 /boot/
目录下查看现在的 linux 配置文件,如下, config-3.10.0-1127.el7.x86_64
和 config-3.10.0-1160.24.1.el7.x86_64
即为不同版本的内核对应的配置文件,相对应的内核文件为vmlinuz-3.10.0-1127.el7.x86_64
和 vmlinuz-3.10.0-1160.24.1.el7.x86_64
。
[root@bogon modules]# cd /boot/
[root@bogon boot]# ls -alh
total 141M
dr-xr-xr-x. 5 root root 4.0K Apr 9 11:39 .
dr-xr-xr-x. 17 root root 224 Feb 2 16:56 ..
-rw-r--r--. 1 root root 150K Mar 31 2020 config-3.10.0-1127.el7.x86_64
-rw-r--r--. 1 root root 150K Apr 8 15:55 config-3.10.0-1160.24.1.el7.x86_64
drwxr-xr-x. 3 root root 17 Oct 10 04:50 efi
drwxr-xr-x. 2 root root 27 Oct 10 04:50 grub
drwx------. 5 root root 97 Apr 9 11:35 grub2
-rw-------. 1 root root 60M Oct 10 04:52 initramfs-0-rescue-6576104afcd64705883f5035a7a6f4d4.img
-rw-------. 1 root root 21M Apr 9 11:36 initramfs-3.10.0-1127.el7.x86_64.img
-rw-------. 1 root root 14M Apr 9 11:34 initramfs-3.10.0-1127.el7.x86_64kdump.img
-rw-------. 1 root root 21M Apr 9 11:35 initramfs-3.10.0-1160.24.1.el7.x86_64.img
-rw-r--r--. 1 root root 313K Mar 31 2020 symvers-3.10.0-1127.el7.x86_64.gz
-rw-r--r--. 1 root root 314K Apr 8 15:55 symvers-3.10.0-1160.24.1.el7.x86_64.gz
-rw-------. 1 root root 3.5M Mar 31 2020 System.map-3.10.0-1127.el7.x86_64
-rw-------. 1 root root 3.5M Apr 8 15:55 System.map-3.10.0-1160.24.1.el7.x86_64
-rwxr-xr-x. 1 root root 6.5M Oct 10 04:52 vmlinuz-0-rescue-6576104afcd64705883f5035a7a6f4d4
-rwxr-xr-x. 1 root root 6.5M Mar 31 2020 vmlinuz-3.10.0-1127.el7.x86_64
-rw-r--r--. 1 root root 167 Mar 31 2020 .vmlinuz-3.10.0-1127.el7.x86_64.hmac
-rwxr-xr-x. 1 root root 6.5M Apr 8 15:55 vmlinuz-3.10.0-1160.24.1.el7.x86_64
-rw-r--r--. 1 root root 172 Apr 8 15:55 .vmlinuz-3.10.0-1160.24.1.el7.x86_64.hmac
解压内核源代码
[root@bogon linux-kernel]# tar xf linux-4.19.186.tar.xz
[root@bogon linux-kernel]# cd linux-4.19.186/
[root@bogon linux-4.19.186]# ls
arch COPYING Documentation fs ipc kernel MAINTAINERS modules.builtin README security usr
block CREDITS drivers include Kbuild lib Makefile modules.order samples sound virt
certs crypto firmware init Kconfig LICENSES mm net scripts tools
配置内核
如果要保持内核的各项配置不变,可以将 /boot/
目录下的内核配置文件拷贝到源码编译的目录下,修改文件名称为.config
,然后输入 make menuconfig
进入内核配置界面。
cp -v /boot/config-3.10.0-693.el7.x86_64 .config
make menuconfig
编译
内核编译需要较长的时间,在编译之前,要保证编译的硬盘存储空间要保留至少20GB
的空间大小。
# 直接编译
# 使用 4个线程进行编译
make -j 4
# 将内核编译打包为 rpm包
make rpm-pkg