User:Douglarek/Tutorials/Streamlining kernel

From Gentoo Wiki
Jump to:navigation Jump to:search
Warning
When you start customizing your kernel, you'll find yourself spending (or "learning") considerable time on it.

This article was inspired by a Gentoo user's post on Reddit[1], which led to his article on the Gentoo Wiki. While his article is concise, some aspects weren't fully explained and didn't entirely align with Gentoo's standard practices. Additionally, the article's focus on gentoo-sources seems unnecessary when the Distribution Kernel can suffice. The main difference between these two is that gentoo-sources is preferred for manual compilation, while the Distribution Kernel is ideal for standard emerge installations and benefits from various portage hooks. This guide doesn't delve deep into kernel customization details (as the author is also relatively new to this). Instead, it focuses on providing a clear, straightforward process to achieve significantly reduced compilation times (approximately 2x faster in testing). Boot configuration details are omitted since both GRUB and Systemd-boot have relevant portage hooks (manual configuration may be required if these aren't set up). The following steps use sys-kernel/gentoo-kernel as an example (assuming you currently use sys-kernel/gentoo-kernel-bin).

Building Kernel Configuration

Using gentoo-kernel 6.11.9 as an example:

root #ebuild /var/db/repos/gentoo/sys-kernel/gentoo-kernel/gentoo-kernel-6.11.9.ebuild clean configure
root #cd /var/tmp/portage/sys-kernel/gentoo-kernel-6.11.9/work/modprep
root #make localmodconfig # For module-based build, or make localyesconfig for built-in
Tip
This step relies on currently loaded hardware information, which has a limitation: it might miss some hardware. For example, USB modules won't be included if external drives aren't connected. A better approach is using modprobed-db. While connecting all hardware before configuration works, it doesn't account for future hardware additions. After installing modprobed-db, it collects hardware information every 6 hours, updating the modprobed.db file. Use make LSMOD=$HOME/.config/modprobed.db localmodconfig for kernel customization, ideally after running modprobed-db for about two months. Without modprobed-db, you can manually configure kernel options via make menuconfig, following this USB example.
Note
These steps aren't suitable for major version upgrades (e.g., 6.11.x to 6.12.x), which often involve kernel option changes. For such upgrades, either:
  • Use make LSMOD=$HOME/.config/modprobed.db localmodconfig on the new kernel with custom configurations
  • Use make olddefconfig to retain old .config options while setting new options to defaults

Custom Configuration and Compilation

Save your custom configuration for future kernel builds:

root #mkdir -p /etc/portage/savedconfig/sys-kernel/
root #cp /var/tmp/portage/sys-kernel/gentoo-kernel-6.11.9/work/modprep/.config /etc/portage/savedconfig/sys-kernel/gentoo-kernel
Note
Like other portage options, savedconfig follows consistent configuration patterns and priority rules. Refer to the Savedconfig wiki for detailed information.

Enable custom configuration through USE flags:

root #mkdir /etc/portage/package.use/
root #echo 'sys-kernel/gentoo-kernel savedconfig' > /etc/portage/package.use/kernel
Tip
When the savedconfig USE flag is enabled, gentoo-kernel compilation will use your custom configuration; otherwise, it will use the default configuration.

Rebuilding the Kernel

root #emerge -W gentoo-kernel-bin
root #emerge -av gentoo-kernel
root #emerge -av @module-rebuild
Note
1. We remove gentoo-kernel-bin before installing gentoo-kernel because they cannot coexist. gentoo-kernel is compiled during emerge, while gentoo-kernel-bin is pre-compiled (though both include source code and Gentoo patches).
2. For NVIDIA users, the @module-rebuild step is almost mandatory. It's easy to forget this step when frequently customizing the same kernel version; Dracut only triggers @module-rebuild during kernel upgrades.

Before and After Comparison

Build time[2] (qlop -vHt gentoo-kernel):

Before: 2024-11-15T21:26:12 >>> sys-kernel/gentoo-kernel-6.11.8: 40 minutes, 8 seconds
After:  2024-11-15T23:33:06 >>> sys-kernel/gentoo-kernel-6.11.8: 11 minutes, 28 seconds

Installation size[3] (qsize gentoo-kernel):

Before: sys-kernel/gentoo-kernel: 24177 files (24175 unique), 4500 non-files, 1.0G
After:  sys-kernel/gentoo-kernel: 14565 files (14563 unique), 3550 non-files, 586.0M

Troubleshooting

Is this the most minimal kernel configuration possible

No, even after using make localmodconfig, there's still significant room for optimization (e.g., filesystems, networking). The full extent of kernel customization is available through make menuconfig.

What if I can't boot into the desktop environment after compilation

This can happen. There are two main solutions:

  1. Try accessing a TTY
  2. Boot from a minimal CD

Then restore the generic pre-compiled kernel by running emerge -W gentoo-kernel followed by emerge -av gentoo-kernel-bin.

See also

  1. [1],Reddit: Using make localmodconfig for kernel?
  2. CPU: AMD Ryzen 5 5600X (12) @ 4.65 GHz,MAKEOPTS="--jobs 8 --load-average 9"
  3. The large installation size is due to debug symbols being enabled by default in gentoo-kernel. If you disable the debug USE flag, the size can be further reduced (on my system, it's 152.5M).