Streamline your kernel config

Posted by Marcus Folkesson on Thursday, February 15, 2024

Streamline your kernel config

When working with embedded systems, it's not that uncommon that you need to compile your own kernel for your hardware platform.

The configuration file you use is probably based on some default configuration that you borrowed from the kernel tree, a vendor tree, a SoM-vendor or simply from your collegue.

As the configuration is somehow generic, it probably contains tons of kernel modules that is not needed for your application. Walking through the menuconfig and tidy up among all modules could be a lot of work.

What you want is to only enable those modules that are currently in use and disable all other. Maybe you have used make localmodconfig or make localyesconfig during compilation of your host kernel, but did you know that those targets are just as applicable for embedded systems?

streamline_config.pl

Both localmodconfig and localyesconfig make use of ./scripts/kconfig/streamline_config.pl [1] which usually execute lsmod, look into your .config and remove all modules that are currently not loaded.

Instead of execute the default lsmod binary, you could set the LSMOD environment variable to either point to an executable file, OR a text file containing the output from lsmod.

Get your current config

In order to streamline your configuration file, you will of course need to know the configuration. Hopefully you already have it, otherwise it could be extracted from your running kernel if CONFIG_IKCONFIG is set.

Either directly from the target:

1 $ zcat /proc/config.gz > /tmp/.config

Or by helper scripts in the kernel source

1 $ ./scripts/extract-ikconfig ./vmlinux > /tmp/.config

and make it available to the host.

Get a list of loaded modules

Save the output from lsmod (run on target) to a file:

1 $ lsmod > /tmp/mymodules

And make it available to the host.

Streamline

Time to streamline. Assuming you now have the configuration file stored as .config in the root of your kernel tree, you may now run

1 $ make LSMOD=/path/to/mymodules localmodconfig
To update the current config and disable all modules not specified in /path/to/mymodules.

Or by

1 $ make LSMOD=/path/to/mymodules localyesconfig
To update the current config converting all modules specified in /path/to/mymodules to core.