When configuring a server to run a single task, like an appliance, there might rise the need to build a custom kernel. For example to save disk space.
Ubuntu Core 13.10 amd64 root filesystem is a 38 MB download, where the "linux-image-generic" meta package including required dependencies will
download 79 MB of archives, and the size of a
kernel-image-x.y.z-generic package is still 14 MB.
For building a kernel that is stripped from modules that your hardware will not use, the make localmodconfig
command is the right tool for the job. However your virtual machine that runs your build and test environment loads different modules than your target deployment hardware. And/or after a while your deployment hardware might become eol, and different hardware is used. And at the frequency where kernel updates are released it might become too cumbersome to build a custom kernel for every different hardware configuration.
How to quickly build a custom Linux kernel that runs on a few different hardware boxes?
Best Answer
The
make localmodconfig
command is still the right tool for the job. In factmake localmodconfig
runsscripts/kconfig/streamline_config.pl
.File input
When reading the
streamline_config.pl
(perl) source code, there is an undocumented featuremy $lsmod_file = $ENV{'LSMOD'};
that allows file input for loaded module detection instead of the output from thelsmod
command.Live CD
Because localmodconfig uses the output
lsmod
to detect the loaded modules. We run a Ubuntu Live CD on each of the different hardware setups, open a terminal (Ctrl+Alt+T), runlsmod
and save its output.Concatenate output
By concatenating the
lsmod
output files while stripping consecutive headers lines you can quickly create an input file that covers all your required kernel modules. We like to review the module list by hand and use a more manual recipe:$ cd linux-3.11.0/
or go the directory where you will run your make command
$ lsmod > lsmod.txt
creates a text file with your loaded modules
$ nano lsmod.txt
will open the nano text editor, of course you can use your favorite editor application
Append your desired modules that are not already there, to the bottom of this file (see for an example the bottom of this anwer), and save it when you are ready.
Note: use spaces not tabs to match the column tabulator positions.
$ make LSMOD="lsmod.txt" localmodconfig
this will tell localmodconfig to use your lsmod.txt file as input for loaded modules detection
With regards to Steven Rostedt - the author of steamline_config.pl - for suggesting a shorter notation in step 5.
Example for what to append and not append to lsmod.txt (step 4):
Because the Intel D33217CK main board has Intel thermal sensors that we would like to read, we append these lines:
But we don't want to run virtual machines on this hardware, that is why we skip these lines:
It has an Apple (Broadcom) Gibabit ethernet adapter connected to its Thunderbolt port, so we append:We think we don't need volume mirroring, and therefor do not add:
And we also don't need graphics output (text will do on a headless server), so we do not include:For another machine we need this Realtek ethernet driver aditionally: