首页 > 解决方案 > 在 Ubuntu 18.04 / 20.04 上构建自定义内核时异常大的驱动程序/模块目录

问题描述

我目前正在蹑手蹑脚地进行自定义内核构建。

我首先在 VirtualBox 中的一个虚拟机上开始,安装一个全新的 Ubuntu Server 20.04 发行版。

我遵循以下程序:

# getting the archive
cd ~/src/linux/
wget https://cdn.kernel.org/pub/linux/kernel/v5.x/linux-5.4.91.tar.xz
tar -xf linux-5.4.91.tar.xz 
cd linux-5.4.91/

# getting the config of the kernel currently running
cp -v /boot/config-$(uname -r) .config

# adapting the config to the target kernel, selecting the default for everything
make oldconfig

# adding manually a suffix to the kernel name: "-tony"
make menuconfig

# building and installing modules & image
time make -j $(nproc)
sudo make modules_install
sudo make install

一切都很顺利,尽管构建所需的时间比预期的要长一些(在 6 核 AMD Ryzen 5 3600 6 核处理器上大约需要 30 分钟,具有 32 GB 的 RAM;我在 VM 可用的 12 个 CPU 上分配了 6 个 CPU)。

但是,当我检查源目录的大小时,现在包含所有构建的目标文件,drivers目录的大小困扰着我:

user@vm:~/src/linux/linux-5.4.91$ du -hd 1
1.3G    ./fs
544K    ./certs
34M ./block
128K    ./usr
41M ./tools
208K    ./LICENSES
58M ./security
48M ./Documentation
3.4M    ./init
58M ./include
895M    ./sound
73M ./lib
5.1M    ./ipc
95M ./crypto
2.0G    ./net
2.2M    ./samples
446M    ./arch
5.2M    ./scripts
195M    ./.git
4.8M    ./virt
142M    ./kernel
56M ./mm
13G ./drivers
20G .

13 GB 看起来很多。当我查看已安装的文件时,模块看起来也很大(5.6G):

root@vm:/# find -name '*5.4.91*' | xargs du -hs
20G ./home/user/src/linux/linux-5.4.91
105M    ./home/user/src/linux/linux-5.4.91.tar.xz
5.6G    ./usr/lib/modules/5.4.91-tony+
4.0K    ./var/lib/initramfs-tools/5.4.91-tony+
912M    ./boot/initrd.img-5.4.91-tony+
232K    ./boot/config-5.4.91-tony+
12M ./boot/vmlinuz-5.4.91-tony+
4.5M    ./boot/System.map-5.4.91-tony+

特别是当我与随发行版一起安装的香草内核进行比较时:

root@vm:/# find -name '*5.4.0-62*' | xargs du -hs
...
262M    ./usr/lib/modules/5.4.0-62-generic
4.0K    ./usr/lib/modprobe.d/blacklist_linux_5.4.0-62-generic.conf
4.0K    ./var/lib/initramfs-tools/5.4.0-62-generic
...
12M ./boot/vmlinuz-5.4.0-62-generic
79M ./boot/initrd.img-5.4.0-62-generic
236K    ./boot/config-5.4.0-62-generic
4.6M    ./boot/System.map-5.4.0-62-generic

cp -v /boot/config-$(uname -r) .config考虑到我采用了相同的配置文件(命令),262M 与 5.6G 似乎有很大的不同。

我还在 Ubuntu Server 18.04 上重现了相同的结果。此外,我直接在我的主机(没有虚拟机)上尝试了同样的结果。

我显然在这里遗漏了一些东西,但我找不到什么:

预先感谢您的帮助!

标签: buildcompilationlinux-kernelkernelkernel-module

解决方案


在内核配置中禁用 CONFIG_SLUB_DEBUG、CONFIG_DEBUG_INFO 和 CONFIG_DEBUG_MISC 会产生合理的构建性能:

real    22m19.559s                                                                                   
user    119m17.273s                                                                                  
sys     12m15.424s

模块的尺寸如下:

root@vm:/usr/lib/modules# du -hs *
261M    5.10.10-stripped+
262M    5.4.0-64-generic

这甚至比随附的内核更好:)(请注意,我更新了内核源代码,但如果使用相同的内核,它不会改变结果)。

显然,make modules_install INSTALL_MOD_STRIP=1当使用调试符号构建模块时,也可以使用剥离模块,从而减小大小而无需更改构建选项。

如上所述在已经剥离的内核和模块上使用该选项安装会产生以下大小:

user@vm:/usr/lib/modules$ du -hs *
260M    5.10.10-stripped+
262M    5.4.0-64-generic

我们从这个额外的剥离中有效地增加了 1 兆字节。


推荐阅读