[Linux服务器] CentOS下编译Linux内核

1316 0
Honkers 2025-3-10 20:46:53 | 显示全部楼层 |阅读模式

目录

前言

1、下载内核

2、解压内核源码

3、配置依赖的环境

4、进入源码目录,使用make menuconfig开启菜单选项,手动选择内核功能

5、编译内核

6、安装模块

7、安装内核

 7、验证新内核版本。

总结


前言

编译内核是一项很简单的事情,但却是进入Linux内核世界的第一步,想要开发内核代码,想要了解内核的运行机制,第一步就是编译Linux内核,以下是在centos7.5环境下编译内核的具体流程。

1、下载内核

内核的源码的官方网站为The Linux Kernel Archives,在此网站下载最新内核源码。

2、解压内核源码

  1. [root@rt7_node03 src]# tar xvf linux-5.16.14.tar.xz -C /usr/src/
  2. [root@rt7_node03 src]# ln -sv linux-5.16.14 linux
  3. 'linux' -> 'linux-5.16.14'
  4. [root@rt7_node03 tmp]# cd /usr/src/
  5. [root@rt7_node03 src]# ls
  6. debug kernels linux linux-5.16.14
复制代码

3、配置依赖的环境

  • 确保根分区可用空间在15G以上(扩容根分区可参考Linux “/“ 分区扩容_巭犇的博客-CSDN博客)
  • 确保gcc版本在5.0以上(升级gcc可参考CentOS下快速升级gcc版本_巭犇的博客-CSDN博客)
  • 安装依赖的软件包。
  1. [root@rt7_node03 linux]# yum groupinstall Development Tools
  2. [root@rt7_node03 linux]# yum install ncurses-devel
  3. [root@rt7_node03 linux]# yum install elfutils-libelf-devel-0.176-5.el7.x86_64
  4. [root@rt7_node03 linux]# yum install openssl-devel
  5. [root@rt7_node03 linux]# yum install bc-1.06.95-13.el7.x86_64
复制代码

 在进行编译的时候提供依赖的文件,我当前的环境是系统版本为CentOS7.5,为最小化安装,实际情况根据编译的时候提示缺少了哪些文件,安装相应的软件包即可。

4、进入源码目录,使用make menuconfig开启菜单选项,手动选择内核功能

  1. [root@rt7_node03 linux]# make menuconfig
  2. UPD scripts/kconfig/mconf-cfg
  3. HOSTCC scripts/kconfig/mconf.o
  4. HOSTCC scripts/kconfig/lxdialog/checklist.o
  5. HOSTCC scripts/kconfig/lxdialog/inputbox.o
  6. HOSTCC scripts/kconfig/lxdialog/menubox.o
  7. HOSTCC scripts/kconfig/lxdialog/textbox.o
  8. HOSTCC scripts/kconfig/lxdialog/util.o
  9. HOSTCC scripts/kconfig/lxdialog/yesno.o
  10. HOSTCC scripts/kconfig/confdata.o
  11. HOSTCC scripts/kconfig/expr.o
  12. LEX scripts/kconfig/lexer.lex.c
  13. YACC scripts/kconfig/parser.tab.[ch]
  14. HOSTCC scripts/kconfig/lexer.lex.o
  15. HOSTCC scripts/kconfig/menu.o
  16. HOSTCC scripts/kconfig/parser.tab.o
  17. HOSTCC scripts/kconfig/preprocess.o
  18. HOSTCC scripts/kconfig/symbol.o
  19. HOSTCC scripts/kconfig/util.o
  20. HOSTLD scripts/kconfig/mconf
复制代码

配置完成后会生成.config的隐藏文件,如果不知道该开启哪些功能也可以直接选用当前系统的配置文件,这里直接使用当前系统的配置文件。

  1. [root@rt7_node03 linux]# cp /boot/config-3.10.0-514.el7.x86_64 ./.config
复制代码

5、编译内核

  1. [root@rt7_node03 linux]# make -j 8 # "-j 8" 代表使用8颗cpu同事编译,这里可根据实际情况而定
  2. /usr/src/linux-5.16.14/Makefile:661: include/config/auto.conf: No such file or directory
  3. /usr/src/linux-5.16.14/Makefile:709: include/config/auto.conf.cmd: No such file or directory
  4. SYNC include/config/auto.conf.cmd
  5. HOSTCC scripts/kconfig/conf.o
  6. HOSTLD scripts/kconfig/conf
  7. ...
复制代码

 此步骤会对.config文件中的每一个选项再次确认,全部确认即可(我这里是8颗cpu同时在编译大概需要二十多分钟,如果cpu少的话,时间会更久一些)

6、安装模块

  1. [root@rt7_node03 linux]# make modules_install
  2. ...
  3. SIGN /lib/modules/5.16.14/kernel/sound/usb/misc/snd-ua101.ko
  4. INSTALL /lib/modules/5.16.14/kernel/sound/usb/snd-usb-audio.ko
  5. SIGN /lib/modules/5.16.14/kernel/sound/usb/snd-usb-audio.ko
  6. INSTALL /lib/modules/5.16.14/kernel/sound/usb/snd-usbmidi-lib.ko
  7. SIGN /lib/modules/5.16.14/kernel/sound/usb/snd-usbmidi-lib.ko
  8. INSTALL /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-us122l.ko
  9. SIGN /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-us122l.ko
  10. INSTALL /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-usx2y.ko
  11. SIGN /lib/modules/5.16.14/kernel/sound/usb/usx2y/snd-usb-usx2y.ko
  12. INSTALL /lib/modules/5.16.14/kernel/virt/lib/irqbypass.ko
  13. SIGN /lib/modules/5.16.14/kernel/virt/lib/irqbypass.ko
  14. DEPMOD /lib/modules/5.16.14
复制代码

 安装完成后在/lib/modules/目录下会生成一个同内核版本号的目录,目录下便是新内核的模块。

7、安装内核

  1. [root@rt7_node03 linux]# make install
  2. sh ./arch/x86/boot/install.sh 5.16.14 \
  3. arch/x86/boot/bzImage System.map "/boot"
  4. [root@rt7_node03 linux]#
复制代码

安装完后会在/boot目录下生成新的内核文件。

  1. [root@rt7_node03 linux]# ll /boot/
  2. total 187028
  3. lrwxrwxrwx 1 root root 24 Mar 23 16:32 System.map -> /boot/System.map-5.16.14
  4. -rw-------. 1 root root 3113253 Nov 23 2016 System.map-3.10.0-514.el7.x86_64
  5. -rw-r--r-- 1 root root 4659281 Mar 23 16:32 System.map-5.16.14
  6. -rw-r--r-- 1 root root 4659281 Mar 23 16:25 System.map-5.16.14.old
  7. -rw-r--r--. 1 root root 137696 Nov 23 2016 config-3.10.0-514.el7.x86_64
  8. drwxr-xr-x. 2 root root 27 Nov 7 2018 grub
  9. drwx------. 6 root root 111 Mar 23 16:34 grub2
  10. -rw-------. 1 root root 48321021 Nov 7 2018 initramfs-0-rescue-33c42a62363749ea9b8fa6116acbeb3e.img
  11. -rw-------. 1 root root 20732112 Nov 7 2018 initramfs-3.10.0-514.el7.x86_64.img
  12. -rw-------. 1 root root 18546695 Nov 7 2018 initramfs-3.10.0-514.el7.x86_64kdump.img
  13. -rw------- 1 root root 62203659 Mar 23 16:34 initramfs-5.16.14.img
  14. -rw-r--r--. 1 root root 613751 Nov 7 2018 initrd-plymouth.img
  15. -rw-r--r--. 1 root root 277953 Nov 23 2016 symvers-3.10.0-514.el7.x86_64.gz
  16. lrwxrwxrwx 1 root root 21 Mar 23 16:32 vmlinuz -> /boot/vmlinuz-5.16.14
  17. -rwxr-xr-x. 1 root root 5392080 Nov 7 2018 vmlinuz-0-rescue-33c42a62363749ea9b8fa6116acbeb3e
  18. -rwxr-xr-x. 1 root root 5392080 Nov 23 2016 vmlinuz-3.10.0-514.el7.x86_64
  19. -rw-r--r-- 1 root root 8720224 Mar 23 16:32 vmlinuz-5.16.14
  20. -rw-r--r-- 1 root root 8720224 Mar 23 16:25 vmlinuz-5.16.14.old
复制代码

新内核会自动写入到引导文件中,直接重启系统,在选择内核的界面选中新的内核即可。

  1. [root@rt7_node03 linux]# cat /boot/grub2/grub.cfg
  2. ...
  3. ### BEGIN /etc/grub.d/10_linux ###
  4. 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' {
  5. load_video
  6. set gfxpayload=keep
  7. insmod gzio
  8. insmod part_msdos
  9. insmod xfs
  10. set root='hd0,msdos1'
  11. if [ x$feature_platform_search_hint = xy ]; then
  12. search --no-floppy --fs-uuid --set=root --hint='hd0,msdos1' f0b3417a-c9b1-4d04-9c2a-77ea4a0abb96
  13. else
  14. search --no-floppy --fs-uuid --set=root f0b3417a-c9b1-4d04-9c2a-77ea4a0abb96
  15. fi
  16. linux16 /vmlinuz-5.16.14 root=/dev/mapper/cl-root ro biosdevname=0 net.ifnames=0 console=ttyS0,115200n8 LANG=en_US.UTF-8
  17. initrd16 /initramfs-5.16.14.img
  18. }
  19. ...
复制代码

 

 7、验证新内核版本。

  1. [root@rt7_node03 linux]# uname -a
  2. 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
  3. [root@rt7_node03 linux]# uname -a
  4. 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

总结

这个实验只是体验了内核编译的过程,并未修改内核的源码,后续将对内核源码进行解读,并插入自己的内核代码,将自己的内核代码运行在操作系统中。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行