Gentoo Installation: Kernel Compilation
Status: In Progress

The official guide from Gentoo.

The guide says you have 3 options, but they're actually 2:

  • Install a precompiled generic kernel for your architecture (sys-kernel/gentoo-kernel)

  • Compile your own (sys-kernel/gentoo-sources)

Preparing sources:

The first option is faster, but it goes against the Gentoo Spirit. However, you can use it to retrieve a kernel configuration file to start from.

When it comes to compiling your own kernel, you can make it or genkernel it. This latter is a tool provided by Gentoo to manage kernels and configuration files.

When you install the kernel sources, they'll be located in /usr/src folder.

ls /usr/src/
# > linux  linux-6.1.28-gentoo  linux-intel-lts
eselect kernel list
# > Available kernel symlink targets:
# >   [1]   linux-6.1.28-gentoo # from gentoo-sources package
# >   [2]   linux-intel-lts *   # from GitHub, has intel GPU SR-IOV modules

When you use eselect kernel set 1 the symbolic link /usr/src/linux is automatically set to the corresponding kernel folder (linux-6.1.28-gentoo).

Kernel config

  • From within /usr/src/linux/, you can start with an initial config based on the current loaded kernel from the LiveUSB medium:
# enable modules from lsmod
make localmodonfig

This would create a .config file (ls -a) that activates the current loaded modules. You can start from different configs: defconfig for one that is based on the ARCH (more here).

  • Edit the configuration:
make menuconfig

You do not want to change .config directly, as many options have dependencies and conditions defined in the Kconfig files and the menu allows to navigate these setting: use "/" to start a search, "H" to see the documentation of each config, and it provides the "DEPENDS" arguments that contain conditions for the setting to be available/editable.

Kernel compilation & Installation

  • Compile the modules
# choose a number of workers to use
make -j8

Before you start this process, take a look at Custom Initramfs to include a rescue mode in your kernel.

  • Install the modules to /lib/modules/{kernel_name}
make -j8 modules_install
  • Install the kernel to /boot
make install

The last step is to update Grub menu: grub-mkconfig -o /boot/grub/grub.cfg.

When the new kernel has the same name as an old one, old extension will be automatically added to the current one. But I like to change General setup -> Local version to use a different extension for the kernel name.

The first compilation is usually the slowest as small changes in the config should require less compilation.

If you get the hang of this, then "genkernel" tool would be easy to understand. It's just a tool to automatically perform some of the steps above when you have many kernels/architectures to compile.

Happy compiling!