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?
How to build a custom kernel with localmodconfig that support hardware of multiple machines?
linux kernel;kernel modules;make
The make localmodconfig command is still the right tool for the job. In fact make localmodconfig runs scripts/kconfig/streamline_config.pl. File inputWhen reading the streamline_config.pl (perl) source code, there is an undocumented feature my $lsmod_file = $ENV{'LSMOD'}; that allows file input for loaded module detection instead of the output from the lsmod command.Live CDBecause 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), run lsmod and save its output.Concatenate outputBy 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.txtcreates a text file with your loaded modules$ nano lsmod.txtwill open the nano text editor, of course you can use your favorite editor applicationAppend 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 localmodconfigthis will tell localmodconfig to use your lsmod.txt file as input for loaded modules detectionWith 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:x86_pkg_temp_thermal   13810  0intel_powerclamp       14239  0But we don't want to run virtual machines on this hardware, that is why we skip these lines:kvm_intel             128218  0kvm                   364766  1 kvm_intelIt has an Apple (Broadcom) Gibabit ethernet adapter connected to its Thunderbolt port, so we append:tg3                   152066  0ptp                    18156  1 tg3pps_core               18546  1 ptpWe think we don't need volume mirroring, and therefor do not add:dm_mirror              21715  0dm_region_hash         15984  1 dm_mirrordm_log                 18072  2 dm_region_hash,dm_mirrorAnd we also don't need graphics output (text will do on a headless server), so we do not include:i915                  589697  3i2c_algo_bit           13197  1 i915drm_kms_helper         46867  1 i915drm                   242354  4 i915,drm_kms_helperFor another machine we need this Realtek ethernet driver aditionally:r8169                  61434  0mii                    13654  1 r8169