Most people uses the kernel shipped by distros - and that's good. But some people like to compile their own kernels from kernel.org, or maybe they like following the Linux development and want to try it. Configuring your own kernel, however, has become a very difficult and tedious task - there're too many options, and some times userspace software will stop working if you don't enable some key option. You can use a standard distro .config file, but it takes too much time to compile all the options it enables.
To make the process of configuration easier, a new build target has been added: make localmodconfig. It runs "lsmod" to find all the modules loaded on the current running system. It will read all the Makefiles to map which CONFIG enables a module. It will read the Kconfig files to find the dependencies and selects that may be needed to support a CONFIG. Finally, it reads the .config file and removes any module "=m" that is not needed to enable the currently loaded modules. With this tool, you can strip a distro .config of all the unuseful drivers that are not needed in our machine, and it will take much less time to build the kernel. There's an additional "make localyesconfig" target, in case you don't want to use modules and/or initrds.
以上内容摘自:Kernel Newbies。
大概意思是说, make localmodconfig 会执行 lsmod 命令查看当前系统中加载了哪些模块 (Modules), 并最后将原来的 .config 中不需要的模块去掉,仅保留前面 lsmod 出来的这些模块,从而简化了内核的配置过程。
这样做确实方便了很多,但是也有个缺点:该方法仅能使编译出的内核支持当前内核已经加载的模块。
因为该方法使用的是 lsmod 的结果,如果有的模块当前没有加载,那么就不会编到新的内核中。
例如,我有的时候需要制作 squashfs , 因此在当前的内核中,将 squashfs 编译成了模块。 当使用 make localmodconfig 来配置 Kernel 的时候,如果当前系统中没有加载这个模块, 那么新编出来的内核中就不会将 squashfs 编译成模块,在新的内核中就没办法使用这个模块了。
所以建议在使用 make localmodconfig 之前,首先折腾一下系统,插个优盘,开开摄像头之类, 以便让内核加载上平时使用时候所需要的模块;执行 make localmodconfig 之后,再执行一下 make menuconfig 来手动检查一下, 是否还有其他模块需要手动选择。


中文 





使用localmodconfig缩短编译时间
本节介绍使用make localmodconfig生成精简的.config文件,缩短内核编译时间的方法。
为了能够应对各种各样的环境,发布版的内核包含很多内核模块。但是在某个特定机器,例如,大家自己平时使用的PC上实际用到的模块只是其中的极小一部分。重新构建内核时,对不使用的模块进行编译就会浪费时间。编译后的模块存放在磁盘里,因此也会造成磁盘空间的浪费。
将localmodconfig作为make的目标,就可以生成仅以正在使用的内核模块为对象的.config文件,可以在Linux内核2.6.32以后的版本中使用它。使用这条命令,可以生成仅启用必要模块的.config文件,从而缩短内核的编译时间。
localmodconfig的使用方法
将运行中的内核源代码解压缩,并在源码树的根下执行下列命令。
# make localmodconfig
如果是一般的发布版,只需进行这一操作就可以生成.config文件,将要编译的内核模块缩减到最少。此后只需执行下列命令,照常进行内核的编译、安装。
# make
# make modules_install
# make install
localmodconfig的效果
使用两种.config文件对上游内核(Linux 2.6.34)进行了编译,并记录分别花费的时间。
第一次是使用Fedora13的默认内核.config文件对Linux 2.6.34进行了编译,将其记为“2.6.34-fc13”。第二次使用的是在2.6.34-fc13的.config文件的基础上使用localmodconfig生成的.config文件,编译后的文件记为“2.6.34-localmod”。两次花费的编译时间如下所示。
2.6.34-fc13:26分13秒
2.6.34-localmod:8分20秒
通过使用localmodconfig生成的.config文件,将编译时间缩短到了1/3以内。
另外,还对内核模块的数量以及它们所在目录的总容量进行了比较。
# du -sh /lib/modules/2.6.34-fc13/kernel/
75M /lib/modules/2.6.34-fc13/kernel
# find /lib/modules/2.6.34-fc13/kernel -name '*.ko' | wc -l
2046
# du -sh /lib/modules/2.6.34-localmod/kernel/
9.2M /lib/modules/2.6.34-localmod/kernel/
# find /lib/modules/2.6.34-fc13-localmod1/kernel -name '*.ko' | wc -l
138
localmodconfig使内核模块的数量减少到约1/15,占用的磁盘空间也减少到约1/8。
localmodconfig的结构
localmodconfig是通过内核源码树的下列脚本执行的。
scripts/kconfig/streamline_config.pl
localmodconfig首先会尝试提取一套配置选项作为模型。使用的模型为源码树的.config文件或者/boot下正在运行的内核的.config文件(/boot/config-<内核版本>)。当这些不存在时,将从正在运行的内核映像(/boot/vmlinuz-<内核版本>)、保存了设置信息的内核模块(configs.ko)等提取信息。
另外,要从内核映像或configs.ko提取出配置选项的信息,内核必须是在指定CONFIG_IKCONFIG选项的情况下编译的。当无法提取用做模型的配置选项时,即,找不到.config文件,且正在运行的内核是在未指定CONFIG_IKCONFIG选项的情况下编译的,就会导致localmodconfig失败。
然后,localmodconfig通过lsmod获取当前安装到内核中的内核模块列表,找出对它们进行编译所需的配置选项并记录下来。
最后,localmodconfig将模型的配置选项中指定要作为内核模块进行编译的部分(CONFIG_*=m)进行如下修改,并输出为.config文件。
当前安装到内核的内核模块所需要的选项:不修改
此外:改为禁用
模型中指定要静态安装到内核的选项(CONFIG_=y)、设置为禁用的选项(# CONFIG_ is not set)不进行修改,直接输出。
小贴士:通过把作为模型的配置选项指定到模块中,却未安装到内核的内核模块中,导致其配置选项失效,无法编译。因此,在执行localmodconfig命令之前,可以将需要编译的内核模块手动安装到内核中。例如,可以使用下列命令,将用来虚拟化的模块kvm.ko安装到内核中。
# modprobe kvm
当然,localmodconfig生成了.config文件后,也可以使用make menuconfig等手动对.config文件进行修改。
localyesconfig
localyesconfig是与localmodconfig相似的make目标。使用这条命令,通过localmodconfig设置为内核模块的配置选项,将设置为在无提示的情况下安装到内核中。
在编写不使用initramfs启动的内核时localyesconfig非常方便。
小结
对于不是很清楚的配置选项,应当先启用,以避免出现内核无法启动的情况。相信凡是自己构建(make)过内核的人对这一点都深有体会。如果使用localmodconfig就不再需要担心这个问题。localmodconfig既能够节约详细检查config选项的时间,又能缩短编译所花费的时间,为我们提供了强有力的支持。