19 内核开发-内核源码编译
(1)开始准备
安装好virtual box + ubuntu 系统后,即可下载内核代码,进行编译
历史内核源码地址:https://mirrors.tuna.tsinghua.edu.cn/kernel/v5.x/
下载 linux-5.10.102.tar.gz 的包,可以使用wget 命令
创建编译目录 /home/peach/lkd_kernels/productionk
然后解压到磁盘上 /home/peach/lkd_kernels/productionk
tar -zxf linux-5.10.102.tar.gz --directory=/home/peach/lkd_kernels/productionk/
(2)安装必备编译软件
sudo apt update
sudo apt upgrade
sudo apt install build-essential dkms ssh -y
sudo apt install linux-headers-$(uname –r) ssh -y
(3)进行kernel config 配置
lsmod > /tmp/lsmod.now
make LSMOD=/tmp/lsmod.now localmodconfig
# 备份执行文件:
cp –af .config ~/lkd_kernels/kconfig_prod01
# 执行编译
make -j8
我们已经看到生成了我们需要文件 bzImage和vmlinux 文件。
注意:make LSMOD=/tmp/lsmod.now localmodconfig 该命令将执行以下操作。
设置环境变量 LSMOD 为 /tmp/lsmod.now。
执行 localmodconfig 目标。
localmodconfig 目标通常用于配置内核模块,它将创建一个名为 .config 的文件,其中包含用于编译内核模块的配置选项。
因此,该命令将使用 /tmp/lsmod.now 文件中的选项配置内核模块,然后编译该模块。
请注意,/tmp/lsmod.now 文件必须存在并且包含有效的内核模块配置选项。
(4)遇到错误
错误:一些包没有安装
make[1]: *** No rule to make target 'debian/canonical-certs.pem', needed by 'certs/x509_certificate_list'. Stop.
make: *** [Makefile:1868: certs] Error 2
#解决
scripts/config --disable SYSTEM_TRUSTED_KEYS
错误:
make[1]: *** No rule to make target 'debian/canonical-revoked-certs.pem', needed by 'certs/x509_revocation_list'. Stop.
make: *** [Makefile:1868: certs] Error 2
#解决
scripts/config --disable SYSTEM_REVOCATION_KEYS
报错:
BTF: .tmp_vmlinux.btf: pahole (pahole) is not available
Failed to generate BTF for vmlinux
Try to disable CONFIG_DEBUG_INFO_BTF
make: *** [Makefile:1161: vmlinux] Error 1一般是系统缺少dwarves软件包导致。
#解决
sudo apt-get install dwarves
报错:
FAILED: load BTF from vmlinux: Unknown error -2make: *** [Makefile:1179: vmlinux] Error 255
排查
peach@peach:~/lkd_kernels/productionk/linux-5.10.102$ sudo dmesg | grep pahole
[ 3353.221446] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/user.slice/user-1000.slice/session-6.scope,task=pahole,pid=92508,uid=1000
[ 3353.221459] Out of memory: Killed process 92508 (pahole) total-vm:2031180kB, anon-rss:1651324kB, file-rss:4kB, shmem-rss:0kB, UID:1000 pgtables:4036kB oom_score_adj:0
[ 3565.138510] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/user.slice/user-1000.slice/session-6.scope,task=pahole,pid=97364,uid=1000
[ 3565.138525] Out of memory: Killed process 97364 (pahole) total-vm:2031180kB, anon-rss:1647620kB, file-rss:4kB, shmem-rss:0kB, UID:1000 pgtables:4040kB oom_score_adj:0
[ 3714.551892] oom-kill:constraint=CONSTRAINT_NONE,nodemask=(null),cpuset=/,mems_allowed=0,global_oom,task_memcg=/user.slice/user-1000.slice/session-6.scope,task=pahole,pid=102077,uid=1000
[ 3714.551907] Out of memory: Killed process 102077 (pahole) total-vm:2031180kB, anon-rss:1665776kB, file-rss:4kB, shmem-rss:0kB, UID:1000 pgtables:4032kB oom_score_adj:0
有人说要 10g 内存, 我的版本时 5.10 的,我分配了8g 就编译通过了,其实没有那么夸张。
Just quick FYI: Current linux build needs at least 10GB of RAM!
I was just trying to build kernel 6.5 and noticed the process was being killed after both SWAP+RAM was getting full. I started with 4.0 of RAM on my VM, then increased until it passed.
I noticed the linux building process needing about 10GiB of RAM to finish building without throwing that error. This is crazy on how much RAM it needs.
内存不够 oom killed了
# 调大虚拟机的内存 该为8g
报错:
/bin/sh: 1: zstd: not found
#安装
sudo apt install zstd
(5)总结
建议虚拟机磁盘设计大一点 25G 以上,否则可能编译多个版本内核后,磁盘文件就满了