Handbook:Alpha/Blocks/Kernel

Now it is time to configure and compile the kernel source. It is possible to use genkernel for this, which will build a generic kernel as used by the installation CD. We explain the "manual" configuration first though, as it is the best way to optimize an environment.

To manually configure your kernel, continue now with Default: Manual configuration. To use genkernel, read Alternative: Using genkernel instead.

Introduction
Manually configuring a kernel is often seen as the most difficult procedure a Linux user ever has to perform. Nothing is less true -- after configuring a couple of kernels users don't even remember that it was difficult ;)

However, one thing is true: it is necessary to know the system when starting to configure a kernel manually. Most information can be gathered by emerging pciutils (emerge pciutils) which contains lspci. With that installed, users will now be able to use lspci within the chrooted environment. pcilib warnings (like pcilib: cannot open /sys/bus/pci/devices) that lspci throws out can be safely ignored for now.

Alternatively, one can run lspci from a non-chrooted environment. The results are the same. Another interesting source is to use lsmod to see what kernel modules the installation CD uses (it might provide a nice hint on what to enable).

Now go to the kernel source directory and execute make menuconfig. This will fire up an ncurses-based configuration menu.

Let's first list some options that must be activated (otherwise Gentoo will not function, or not function properly without additional tweaks). We also have a Gentoo Kernel Configuration Guide on the Gentoo wiki that might help out further.

Activating required options
First go to File Systems and select support for the filesystems used by the system. Don't compile the file system for the root filesystem as module, otherwise Gentoo will not be able to mount the partition. Also select Virtual memory and /proc file system.

When using PPPoE to connect to the Internet, or when using a dial-up modem, enable the following options:

The two compression options won't harm but are not definitely needed, neither does the PPP over Ethernet option, that might only be used by ppp when configured to do kernel mode PPPoE.

If required, don't forget to include support in the kernel for the Ethernet card.

The following options are recommended as well:

Next select Maintain a devtmpfs file system to mount at /dev so that critical device files are already available early in the boot process.

Compiling and installing
With the kernel configured, it is time to compile and install it. Exit the configuration and start the compilation process:

When the kernel has finished compiling, copy the kernel image to. Recent kernels might create instead of. Keep this in mind when copying the kernel image.

Optional: Building an initramfs
When using a specific partition layout where important file system locations (like or ) are on separate partitions, setup an initramfs so that this partition can be mounted before it is needed.

Without an initramfs, the system might not boot up properly as the tools that are responsible for mounting the file systems need information that resides on those file systems. An initramfs will pull in the necessary files into an archive which is used right after the kernel boots, but before the control is handed over to the init tool. Scripts on the initramfs will then make sure that the partitions are properly mounted before the system continues booting.

To install an initramfs, install genkernel first, then have it generate an initramfs:

If specific support in the initramfs is necessary, such as lvm or raid, add in the appropriate options to genkernel. See genkernel --help for more information, or the next example which enables support for LVM and software raid (mdadm):

The initramfs will be stored in. Find the file by simply listing the files starting with initramfs:

Now continue with Kernel modules.

Alternative: Using genkernel
With the kernel source tree installed, it's now time to compile the kernel by using our genkernel script to automatically build a kernel. genkernel works by configuring a kernel nearly identically to the way our installation CD kernel is configured. This means that when genkernel is used to build a kernel, the system will generally detect all available hardware at boot-time, just like our installation CD does. Because genkernel doesn't require any manual kernel configuration, it is an ideal solution for those users who may not be comfortable compiling their own kernels.

Now, let's see how to use genkernel. First, emerge the genkernel ebuild:

Now, compile the kernel sources by running. Be aware though, as genkernel compiles a kernel that supports almost all hardware, this compilation will take quite a while to finish!

Note that, if the boot partition doesn't use ext2 or ext3 as filesystem then one might need to manually configure the kernel using  and add support for the filesystem in the kernel (i.e. not as a module).

Once genkernel completes, a kernel, full set of modules and initial ram disk (initramfs) will be created. We will use the kernel and initrd when configuring a boot loader later in this document. Write down the names of the kernel and initrd as those will be used when writing the bootloader configuration file. The initrd will be started immediately after booting to perform hardware autodetection (just like on the Installation CD) before the "real" system starts up.

Configuring the modules
List the modules that need to be automatically loaded in. Extra options can be added to the modules too if needed.

To view all available modules, run the following find command. Don't forget to substitute " " with the version of the kernel just compiled:

For instance, to automatically load the 3c59x.ko module (which is the driver for a specific 3Com network card family), edit the file and enter the module name in it.