编译内核是一项很简单的事情,但却是进入Linux内核世界的第一步,想要开发内核代码,想要了解内核的运行机制,第一步就是编译Linux内核,以下是在centos7.5环境下编译内核的具体流程。
内核的源码的官方网站为The Linux Kernel Archives,在此网站下载最新内核源码。
[root@rt7_node03 src]# tar xvf linux-5.16.14.tar.xz -C /usr/src/
[root@rt7_node03 src]# ln -sv linux-5.16.14 linux
'linux' -> 'linux-5.16.14'
[root@rt7_node03 tmp]# cd /usr/src/
[root@rt7_node03 src]# ls
debug kernels linux linux-5.16.14
[root@rt7_node03 linux]# yum groupinstall Development Tools
[root@rt7_node03 linux]# yum install ncurses-devel
[root@rt7_node03 linux]# yum install elfutils-libelf-devel-0.176-5.el7.x86_64
[root@rt7_node03 linux]# yum install openssl-devel
[root@rt7_node03 linux]# yum install bc-1.06.95-13.el7.x86_64
在进行编译的时候提供依赖的文件,我当前的环境是系统版本为CentOS7.5,为最小化安装,实际情况根据编译的时候提示缺少了哪些文件,安装相应的软件包即可。
[root@rt7_node03 linux]# make menuconfig
UPD scripts/kconfig/mconf-cfg
HOSTCC scripts/kconfig/mconf.o
HOSTCC scripts/kconfig/lxdialog/checklist.o
HOSTCC scripts/kconfig/lxdialog/inputbox.o
HOSTCC scripts/kconfig/lxdialog/menubox.o
HOSTCC scripts/kconfig/lxdialog/textbox.o
HOSTCC scripts/kconfig/lxdialog/util.o
HOSTCC scripts/kconfig/lxdialog/yesno.o
HOSTCC scripts/kconfig/confdata.o
HOSTCC scripts/kconfig/expr.o
LEX scripts/kconfig/lexer.lex.c
YACC scripts/kconfig/parser.tab.[ch]
HOSTCC scripts/kconfig/lexer.lex.o
HOSTCC scripts/kconfig/menu.o
HOSTCC scripts/kconfig/parser.tab.o
HOSTCC scripts/kconfig/preprocess.o
HOSTCC scripts/kconfig/symbol.o
HOSTCC scripts/kconfig/util.o
HOSTLD scripts/kconfig/mconf
配置完成后会生成.config的隐藏文件,如果不知道该开启哪些功能也可以直接选用当前系统的配置文件,这里直接使用当前系统的配置文件。
[root@rt7_node03 linux]# cp /boot/config-3.10.0-514.el7.x86_64 ./.config
[root@rt7_node03 linux]# make -j 8 # "-j 8" 代表使用8颗cpu同事编译,这里可根据实际情况而定
/usr/src/linux-5.16.14/Makefile:661: include/config/auto.conf: No such file or directory
/usr/src/linux-5.16.14/Makefile:709: include/config/auto.conf.cmd: No such file or directory
SYNC include/config/auto.conf.cmd
HOSTCC scripts/kconfig/conf.o
HOSTLD scripts/kconfig/conf
...
此步骤会对.config文件中的每一个选项再次确认,全部确认即可(我这里是8颗cpu同时在编译大概需要二十多分钟,如果cpu少的话,时间会更久一些)
[root@rt7_node03 linux]# make modules_install
...
SIGN /lib/modules/5.16.14/kernel/sound/usb/misc/snd-ua101.ko
INSTALL /lib/modules/5.16.14/kernel/sound/usb/snd-usb-audio.ko
SIGN /lib/modules/5.16.14/kernel/sound/usb/snd-usb-audio.ko
INSTALL /lib/modules/5.16.14/kernel/sound/usb/snd-usbmidi-lib.ko
SIGN /lib/modules/5.16.14/kernel/sound/usb/snd-usbmidi-lib.ko
INSTALL /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-us122l.ko
SIGN /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-us122l.ko
INSTALL /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-usx2y.ko
SIGN /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-usx2y.ko
INSTALL /lib/modules/5.16.14/kernel/virt/lib/irqbypass.ko
SIGN /lib/modules/5.16.14/kernel/virt/lib/irqbypass.ko
DEPMOD /lib/modules/5.16.14
安装完成后在/lib/modules/目录下会生成一个同内核版本号的目录,目录下便是新内核的模块。
[root@rt7_node03 linux]# make install
sh ./arch/x86/boot/install.sh 5.16.14 \
arch/x86/boot/bzImage System.map "/boot"
[root@rt7_node03 linux]#
安装完后会在/boot目录下生成新的内核文件。
[root@rt7_node03 linux]# ll /boot/
total 187028
lrwxrwxrwx 1 root root 24 Mar 23 16:32 System.map -> /boot/System.map-5.16.14
-rw-------. 1 root root 3113253 Nov 23 2016 System.map-3.10.0-514.el7.x86_64
-rw-r--r-- 1 root root 4659281 Mar 23 16:32 System.map-5.16.14
-rw-r--r-- 1 root root 4659281 Mar 23 16:25 System.map-5.16.14.old
-rw-r--r--. 1 root root 137696 Nov 23 2016 config-3.10.0-514.el7.x86_64
drwxr-xr-x. 2 root root 27 Nov 7 2018 grub
drwx------. 6 root root 111 Mar 23 16:34 grub2
-rw-------. 1 root root 48321021 Nov 7 2018 initramfs-0-rescue-33c42a62363749ea9b8fa6116acbeb3e.img
-rw-------. 1 root root 20732112 Nov 7 2018 initramfs-3.10.0-514.el7.x86_64.img
-rw-------. 1 root root 18546695 Nov 7 2018 initramfs-3.10.0-514.el7.x86_64kdump.img
-rw------- 1 root root 62203659 Mar 23 16:34 initramfs-5.16.14.img
-rw-r--r--. 1 root root 613751 Nov 7 2018 initrd-plymouth.img
-rw-r--r--. 1 root root 277953 Nov 23 2016 symvers-3.10.0-514.el7.x86_64.gz
lrwxrwxrwx 1 root root 21 Mar 23 16:32 vmlinuz -> /boot/vmlinuz-5.16.14
-rwxr-xr-x. 1 root root 5392080 Nov 7 2018 vmlinuz-0-rescue-33c42a62363749ea9b8fa6116acbeb3e
-rwxr-xr-x. 1 root root 5392080 Nov 23 2016 vmlinuz-3.10.0-514.el7.x86_64
-rw-r--r-- 1 root root 8720224 Mar 23 16:32 vmlinuz-5.16.14
-rw-r--r-- 1 root root 8720224 Mar 23 16:25 vmlinuz-5.16.14.old
新内核会自动写入到引导文件中,直接重启系统,在选择内核的界面选中新的内核即可。
[root@rt7_node03 linux]# cat /boot/grub2/grub.cfg
...
### BEGIN /etc/grub.d/10_linux ###
menuentry 'CentOS Linux (5.16.14) 7 (Core)' --class centos --class gnu-linux --class gnu --class os --unrestricted $menuentry_id_option 'gnulinux-3.10.0-514.el7.x86_64-advanced-bc95c848-a340-412c-92f1-09f6a8ed94ec' {
load_video
set gfxpayload=keep
insmod gzio
insmod part_msdos
insmod xfs
set root='hd0,msdos1'
if [ x$feature_platform_search_hint = xy ]; then
search --no-floppy --fs-uuid --set=root --hint='hd0,msdos1' f0b3417a-c9b1-4d04-9c2a-77ea4a0abb96
else
search --no-floppy --fs-uuid --set=root f0b3417a-c9b1-4d04-9c2a-77ea4a0abb96
fi
linux16 /vmlinuz-5.16.14 root=/dev/mapper/cl-root ro biosdevname=0 net.ifnames=0 console=ttyS0,115200n8 LANG=en_US.UTF-8
initrd16 /initramfs-5.16.14.img
}
...
[root@rt7_node03 linux]# uname -a
Linux rt7_node03 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
[root@rt7_node03 linux]# uname -a
Linux rt7_node03 5.16.14 #1 SMP PREEMPT Wed Mar 23 15:56:47 CST 2022 x86_64 x86_64 x86_64 GNU/Linux
内核已升到5.16.14
这个实验只是体验了内核编译的过程,并未修改内核的源码,后续将对内核源码进行解读,并插入自己的内核代码,将自己的内核代码运行在操作系统中。