Ubuntu 22.04内核代码下载、编译、调试
1 下载Ubuntu Kernel
参考《Kernel/Dev/KernelGitGuide - Ubuntu Wiki》,下载Ubuntu 22.04代码。
优先使用:
apt source linux-image-unsigned-$(uname -r)
或者:
git clone https://git.launchpad.net/~ubuntu-kernel/ubuntu/+source/linux/+git/jammy
2 修改、编译、安装Ubuntu Kernel
参考《Kernel/BuildYourOwnKernel - Ubuntu Wiki》 。
2.1 修改Ubuntu Kernel配置
修改Ubuntu Kernel配置选项:
chmod a+x debian/rules
chmod a+x debian/scripts/*
chmod a+x debian/scripts/misc/*
fakeroot debian/rules clean
fakeroot debian/rules editconfigs # you need to go through each (Y, Exit, Y, Exit..) or get a complaint about config later
2.2 编译Ubuntu Kernel
安装Ubuntu kernel编译所需工具:
sudo apt build-dep linux linux-image-unsigned-$(uname -r)
补充安装如下工具:
sudo apt install libncurses-dev gawk flex bison openssl libssl-dev dkms libelf-dev libudev-dev libpci-dev libiberty-dev autoconf llvm
进行编译:
fakeroot debian/rules clean # quicker build: fakeroot debian/rules binary-headers binary-generic binary-perarch # if you need linux-tools or lowlatency kernel, run instead: fakeroot debian/rules binary
2.2.1 错误:FAILED: load BTF from vmlinux: Invalid argument
编译过程中出现如下错误:
FAILED: load BTF from vmlinux: Invalid argument
参考《kbuild: Add skip_encoding_btf_enum64 option to pahole · intel/linux-intel-lts@b775fbf · GitHub》,在scripts/pahole-flags.sh添加如下修改:
if [ "${pahole_ver}" -ge "124" ]; then extra_paholeopt="${extra_paholeopt} --skip_encoding_btf_enum64" fi
2.3 安装Ubuntu Kernel
在上一层目录查看编译结果:
ls linux*5.15.0-52.58*.deb
安装Ubuntu Kernel,然后重启生效:
sudo dpkg -i linux*5.15.0-52.58*.deb sudo reboot
2.4 查看当前内核
通过uname查看当前内核版本号:
uname -a
查看内核中已安装的内核版本:
dpkg --get-selections |grep linux-image
删除某个已安装内核版本:
sudo apt-get purge linux-image-6.5.0-25-generic linux-image-unsigned-6.5.0-25-generic
2.5 更换默认内核
当Ubuntu kernel存在多个内核时,可以选择默认的启动内核。一是在启动时手动选择内核版本,另一是修改/boot/grub/grub.cfg和/etc/default/grub来选择内核版本。
打开/boot/grub/brub.cfg文件,在submenu 'Advanced options for Ubuntu'菜单中找到对应的menuentry,序号从0开始。
submenu 'Advanced options for Ubuntu' $menuentry_id_option 'gnulinux-advanced-85d24705-70b3-43c7-8af6-cd9524790ecd' { menuentry 'Ubuntu, with Linux 6.5.0-18-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-6.5.0-18-generic-advanced-85d24705-70b3-43c7-8af6-cd9524790ecd' {--0 ... } menuentry 'Ubuntu, with Linux 6.5.0-18-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-6.5.0-18-generic-recovery-85d24705-70b3-43c7-8af6-cd9524790ecd' {--1 ... } menuentry 'Ubuntu, with Linux 5.15.0-52-generic' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-52-generic-advanced-85d24705-70b3-43c7-8af6-cd9524790ecd' {--2,对应GRUB_DEFAULT中序号。 recordfail load_video gfxmode $linux_gfx_mode insmod gzio if [ x$grub_platform = xxen ]; then insmod xzio; insmod lzopio; fi insmod part_gpt insmod ext2 set root='hd0,gpt3' if [ x$feature_platform_search_hint = xy ]; then search --no-floppy --fs-uuid --set=root --hint-bios=hd0,gpt3 --hint-efi=hd0,gpt3 --hint-baremetal=ahci0,gpt3 85d24705-70b3-43c7-8af6-cd9524790ecd else search --no-floppy --fs-uuid --set=root 85d24705-70b3-43c7-8af6-cd9524790ecd fi echo 'Loading Linux 5.15.0-52-generic ...' linux /boot/vmlinuz-5.15.0-52-generic root=UUID=85d24705-70b3-43c7-8af6-cd9524790ecd ro find_preseed=/preseed.cfg auto noprompt priority=critical locale=en_US quiet splash $vt_handoff echo 'Loading initial ramdisk ...' initrd /boot/initrd.img-5.15.0-52-generic } menuentry 'Ubuntu, with Linux 5.15.0-52-generic (recovery mode)' --class ubuntu --class gnu-linux --class gnu --class os $menuentry_id_option 'gnulinux-5.15.0-52-generic-recovery-85d24705-70b3-43c7-8af6-cd9524790ecd' {--3 ... } }
修改/etc/default/grub:
GRUB_DEFAULT="1> 2"--后面的序号0表示使用menuentry作为默认启动选项。2前必须要有空格。
在修改完后,一定要执行sudo update-grub,上述修改才会生效。
3 调试
编译带调试符号表的内核版本:
sudo apt install pkg-config-dbgsym fakeroot debian/rules clean fakeroot debian/rules binary-headers binary-generic binary-perarch skipdbg=false