推荐一个零声学院免费教程,个人觉得老师讲得不错,
分享给大家:[Linux,Nginx,ZeroMQ,MySQL,Redis,
fastdfs,MongoDB,ZK,流媒体,CDN,P2P,K8S,Docker,
TCP/IP,协程,DPDK等技术内容,点击立即学习:
https://ke.qq.com/course/4032547?flowToken=1042705
目录
前言
更换内核,获得一套完整的源码,分析源码,模块开发,大有好处。
这篇的移植的第一篇,也可以参考我内核编译第二篇
kernel编译练习2:给ubuntu22升级5.19.3内核
一.首先看一下当前的内核版本号
cat /proc/version
Linux version 5.15.0-41-generic (buildd@lcy02-amd64-065) (gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #44-Ubuntu SMP Wed Jun 22 14:20:53 UTC 2022
二 看看官网当前最新的内核版本号
内核官网:The Linux Kernel Archives
三 安装一些需要的软件包
$sudo apt-get install libncurses5-dev libssl-dev build-essential openssl
四 下载内核5.19
https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.19.tar.xz
五 解压内核,注意大小写。
方法一:
$ tar -Jxf linux-5.19.tar.xz
方法二:
也可以下面这样,这个是我能记住的,
xz -d linux-5.19.tar.xz
linux-5.19.tar.xz变成了linux-5.19.tar
$ ls
linux-5.19.tar
然后
tar -xf linux-5.19.tar
七 使用当前内核的配置文件
把系统自带的内核配置文件复制过来,我的内核版本是
$ cat /proc/version
Linux version 5.15.0-41-generic (buildd@lcy02-amd64-065) (gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #44-Ubuntu SMP Wed Jun 22 14:20:53 UTC 2022
所以,当前内核的配置文件是/boot/config-5.15.0-41-generic,注意这是一个.config文件不是一个目录。变成新内核的一个配置文件
cp /boot/config-5.15.0-41-generic arch/x86/configs/my_defconfig
然后执行
make my_defconfig
$ make my_defconfig
HOSTCC scripts/kconfig/conf.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/conf
arch/x86/configs/my_defconfig:435:warning: symbol value 'm' invalid for I8K
arch/x86/configs/my_defconfig:2000:warning: symbol value 'm' invalid for MCTP
arch/x86/configs/my_defconfig:8860:warning: symbol value 'm' invalid for VIDEO_ZORAN_DC30
arch/x86/configs/my_defconfig:8861:warning: symbol value 'm' invalid for VIDEO_ZORAN_ZR36060
arch/x86/configs/my_defconfig:8862:warning: symbol value 'm' invalid for VIDEO_ZORAN_BUZ
arch/x86/configs/my_defconfig:8863:warning: symbol value 'm' invalid for VIDEO_ZORAN_DC10
arch/x86/configs/my_defconfig:8864:warning: symbol value 'm' invalid for VIDEO_ZORAN_LML33
arch/x86/configs/my_defconfig:8865:warning: symbol value 'm' invalid for VIDEO_ZORAN_LML33R10
arch/x86/configs/my_defconfig:8866:warning: symbol value 'm' invalid for VIDEO_ZORAN_AVS6EYES
arch/x86/configs/my_defconfig:9957:warning: symbol value 'm' invalid for ANDROID_BINDER_IPC
arch/x86/configs/my_defconfig:9958:warning: symbol value 'm' invalid for ANDROID_BINDERFS
#
# configuration written to .config
#
lkmao@lkmao-virtual-machine:~/linux/linux-5.19$
执行完毕后,就会在内核源码根目录下生成一个.config文件。
然后开始编译内核
先查看一下CPU核心数,可以多线程编译内核
cat /proc/cpuinfo | grep processor | wc -l
4
是四个核心的,看下虚拟机的配置
处理器内核总数是也是4,那就是4了。
然后就是开始编译了。
sudo make -j4
<stdin>:1:10: fatal error: libelf.h: No such file or directory
报错了,解决方法就是安装依赖包
sudo apt-get install libelf-dev
重新编译
sudo make -j4
开始编译了,可以先去刷刷Darren老师的视频了,编译大概需要花费若干分钟左右。很快就编译完了,好可疑啊...,那就开始编译模块吧
sudo make modules,果然报错了
sudo make modules
CALL scripts/checksyscalls.sh
CALL scripts/atomic/check-atomics.sh
DESCEND objtool
CHK include/generated/compile.h
CHK kernel/kheaders_data.tar.xz
make[1]: *** No rule to make target 'debian/canonical-certs.pem', needed by 'certs/x509_certificate_list'. Stop.
make: *** [Makefile:1843: certs] Error 2
在.config文件中搜索debian/canonical-certs.pem,找到
CONFIG_SYSTEM_TRUSTED_KEYS
在次make menuconfig 修改CONFIG_SYSTEM_TRUSTED_KEYS的值为空
找到了,它的默认值,就是红框里的值
还是报错
make[1]: *** No rule to make target 'debian/canonical-revoked-certs.pem', needed by 'certs/x509_revocation_list'. Stop.
grep debian/canonical-revoked-certs.pem .config
CONFIG_SYSTEM_REVOCATION_KEYS="debian/canonical-revoked-certs.pem"
在.config文件中搜索debian/canonical-revoked-certs.pem,找到CONFIG_SYSTEM_REVOCATION_KEYS,在次make menuconfig 修改CONFIG_SYSTEM_REVOCATION_KEYS的值为空
还是在加密模块
看,就是它
把它的值清空
然后保存配置,退出从新编译
sudo make -j4
这次过了好久还在编译,继续等待,盯着... ...,算了,还是先干点别的吧,ubuntu系统默认5分钟不操作自动会锁屏,先关闭锁屏
好了,应该不会锁屏了
第一次更换内核,还是挺激动的,一直忍不住看几眼,时间过的好慢长啊... ...
贴一段内容出来,证明确实编译完成了
LD [M] sound/soc/xilinx/snd-soc-xlnx-i2s.ko
LD [M] sound/soc/xilinx/snd-soc-xlnx-spdif.ko
LD [M] sound/soc/xtensa/snd-soc-xtfpga-i2s.ko
LD [M] sound/soundcore.ko
LD [M] sound/synth/emux/snd-emux-synth.ko
LD [M] sound/synth/snd-util-mem.ko
LD [M] sound/usb/6fire/snd-usb-6fire.ko
LD [M] sound/usb/bcd2000/snd-bcd2000.ko
LD [M] sound/usb/caiaq/snd-usb-caiaq.ko
LD [M] sound/usb/hiface/snd-usb-hiface.ko
LD [M] sound/usb/line6/snd-usb-line6.ko
LD [M] sound/usb/line6/snd-usb-pod.ko
LD [M] sound/usb/line6/snd-usb-podhd.ko
LD [M] sound/usb/line6/snd-usb-toneport.ko
LD [M] sound/usb/line6/snd-usb-variax.ko
LD [M] sound/usb/misc/snd-ua101.ko
LD [M] sound/usb/snd-usb-audio.ko
LD [M] sound/usb/snd-usbmidi-lib.ko
LD [M] sound/usb/usx2y/snd-usb-us122l.ko
LD [M] sound/usb/usx2y/snd-usb-usx2y.ko
LD [M] sound/virtio/virtio_snd.ko
LD [M] sound/x86/snd-hdmi-lpe-audio.ko
LD [M] sound/xen/snd_xen_front.ko
lkmao@lkmao-virtual-machine:~/kernel/linux-5.19$
9$ find -name bzImage
./arch/x86_64/boot/bzImage
./arch/x86/boot/bzImage
有两个bzImage,不知道用的哪一个。应该是64这个吧
$ ls -lsah ./arch/x86_64/boot/bzImage
0 lrwxrwxrwx 1 lkmao lkmao 22 8月 8 21:52 ./arch/x86_64/boot/bzImage -> ../../x86/boot/bzImage
原来64这个是一个链接,真是...,这几天发现了好多都是链接啊。
$ ls -lsah ./arch/x86/boot/bzImage
10M -rw-rw-r-- 1 lkmao lkmao 10M 8月 8 21:52 ./arch/x86/boot/bzImage
bzImage大小是10M
八 开始安装模块
$ sudo make modules_install
这个命令也需要几分钟...,根据输出信息可知,它就是把.ko文件复制到/lib/modules/5.19.0目录
INSTALL /lib/modules/5.19.0/kernel/sound/usb/line6/snd-usb-toneport.ko
SIGN /lib/modules/5.19.0/kernel/sound/usb/line6/snd-usb-toneport.ko
INSTALL /lib/modules/5.19.0/kernel/sound/usb/line6/snd-usb-variax.ko
SIGN /lib/modules/5.19.0/kernel/sound/usb/line6/snd-usb-variax.ko
INSTALL /lib/modules/5.19.0/kernel/sound/usb/misc/snd-ua101.ko
SIGN /lib/modules/5.19.0/kernel/sound/usb/misc/snd-ua101.ko
INSTALL /lib/modules/5.19.0/kernel/sound/usb/snd-usb-audio.ko
SIGN /lib/modules/5.19.0/kernel/sound/usb/snd-usb-audio.ko
INSTALL /lib/modules/5.19.0/kernel/sound/usb/snd-usbmidi-lib.ko
SIGN /lib/modules/5.19.0/kernel/sound/usb/snd-usbmidi-lib.ko
INSTALL /lib/modules/5.19.0/kernel/sound/usb/usx2y/snd-usb-us122l.ko
SIGN /lib/modules/5.19.0/kernel/sound/usb/usx2y/snd-usb-us122l.ko
INSTALL /lib/modules/5.19.0/kernel/sound/usb/usx2y/snd-usb-usx2y.ko
SIGN /lib/modules/5.19.0/kernel/sound/usb/usx2y/snd-usb-usx2y.ko
INSTALL /lib/modules/5.19.0/kernel/sound/virtio/virtio_snd.ko
SIGN /lib/modules/5.19.0/kernel/sound/virtio/virtio_snd.ko
INSTALL /lib/modules/5.19.0/kernel/sound/x86/snd-hdmi-lpe-audio.ko
SIGN /lib/modules/5.19.0/kernel/sound/x86/snd-hdmi-lpe-audio.ko
INSTALL /lib/modules/5.19.0/kernel/sound/xen/snd_xen_front.ko
SIGN /lib/modules/5.19.0/kernel/sound/xen/snd_xen_front.ko
DEPMOD /lib/modules/5.19.0
lkmao@lkmao-virtual-machine:~/kernel/linux-5.19$
好了,完毕了
九 最后一步,安装内核
sudo make install
lkmao@lkmao-virtual-machine:~/kernel/linux-5.19$ sudo make install
INSTALL /boot
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 5.19.0 /boot/vmlinuz-5.19.0
update-initramfs: Generating /boot/initrd.img-5.19.0
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 5.19.0 /boot/vmlinuz-5.19.0
run-parts: executing /etc/kernel/postinst.d/update-notifier 5.19.0 /boot/vmlinuz-5.19.0
run-parts: executing /etc/kernel/postinst.d/xx-update-initrd-links 5.19.0 /boot/vmlinuz-5.19.0
I: /boot/initrd.img.old is now a symlink to initrd.img-5.15.0-41-generic
I: /boot/initrd.img is now a symlink to initrd.img-5.19.0
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 5.19.0 /boot/vmlinuz-5.19.0
Sourcing file `/etc/default/grub'
Sourcing file `/etc/default/grub.d/init-select.cfg'
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-5.19.0
Found initrd image: /boot/initrd.img-5.19.0
Found linux image: /boot/vmlinuz-5.15.0-41-generic
Found initrd image: /boot/initrd.img-5.15.0-41-generic
Found linux image: /boot/vmlinuz-5.15.0-25-generic
Found initrd image: /boot/initrd.img-5.15.0-25-generic
Found memtest86+ image: /boot/memtest86+.elf
Found memtest86+ image: /boot/memtest86+.bin
Warning: os-prober will not be executed to detect other bootable partitions.
Systems on them will not be added to the GRUB boot configuration.
Check GRUB_DISABLE_OS_PROBER documentation entry.
done
看到最后的done了。
完成之后就可以重启系统了
sudo reboot
十 验证结果
lkmao@lkmao-virtual-machine:~$ cat /proc/version
Linux version 5.19.0 (lkmao@lkmao-virtual-machine) (gcc (Ubuntu 11.2.0-19ubuntu1) 11.2.0, GNU ld (GNU Binutils for Ubuntu) 2.38) #1 SMP PREEMPT_DYNAMIC Mon Aug 8 20:26:54 CST 2022
lkmao@lkmao-virtual-machine:~$ uname -r
5.19.0
版本编程5.19.0,编译时间就是刚刚的时间,验证无误。
遇到的报错
gelf.h
/home/lkmao/linux/linux-5.19/tools/objtool/include/objtool/elf.h:10:10: fatal error: gelf.h: No such file or directory
10 | #include <gelf.h>
| ^~~~~~~~
compilation terminated.
make[3]: *** [/home/lkmao/linux/linux-5.19/tools/build/Makefile.build:97: /home/lkmao/linux/linux-5.19/tools/objtool/weak.o] Error 1
make[2]: *** [Makefile:54: /home/lkmao/linux/linux-5.19/tools/objtool/objtool-in.o] Error 2
make[1]: *** [Makefile:73: objtool] Error 2
make: *** [Makefile:1344: tools/objtool] Error 2
make: *** Waiting for unfinished jobs....
解决办法
sudo apt-get install libelf-dev
总结
千里之行,始于足下,更换内核还是需要耐心、经验、热情和勇气。