Building a Kernel from Source

Setting up a proper kernel yourself - lean, mean and tailored to your hardware, is the challenge by which a linux user can graduate to becoming a Funtoo knight ;-)

Even though many of us are using enterprise-ready kernels in datacenters, there is almost nobody who hasn't at least considered building a kernel for his laptop / PC.
We are showing here how an intermediate Linux user can use an alternative to the standard beginners "genkernel" approach, to compile a custom kernel, in a relatively speedy and easy set up.

Minimum Requirements

Understand the command line

Know where the kernel files are located

Assumptions

You start from an installed Funtoo system on the disk, or at least, you are on stage3 in a chrooted environment from a live cd, following somehow the Funto Installation Tutorial.

Less advanced version

Emerging the kernel sources

To begin, we have to figure out which kernel sources we will use. If you are unsure about which sources are available and what their benefits and drawbacks are, check out the Kernels page.

After you have made a decsion as to which kernel you want to install, emerge it:

# emerge sys-kernel/[kernel-name]

Portage will now go about installing the sources to /usr/src. However, it is preferable to also create a symlink with eselect as such:

# eselect kernel set 1

You may use

# eselect kernel list

to display a list of kernels currently installed.

Using the above command is useful, because it allows you to always be able to go to the directory "/usr/src/linux" and find your current working kernel and build files.
The "/usr/src/linux" directory is simply a symlink to the kernel you selected with eselect, in the "/usr/src" directory.

Note that portage can also set this up for you automatically, by setting the "symlink" USE flag for your kernel.

Configuring the kernel

Now that the kernel sources are on your system, you should configure them. To do this, change your directory to /usr/src/linux

# cd /usr/src/linux

We are now in the kernel sources directory, next sanitize the sources:

Warning

this will delete your .config

# make clean && make distclean && make mrproper

Copy configurations if you have previous configurations to copy:

# cp /boot/config-3.13.7 .config

We can run a script that allows us to modify the configuration. Run:

# make menuconfig

or

# make nconfig

here is where you insert kernel configs, or press / to search for kernel modules by name

While you edit the sources, keep the following in mind:

To build something into your kernel, press y when you have it selected.

To exclude something from your kernel, press n when you have it selected.

To build something as a module, press m.

Things that you may need to include in your kernel:

as modules:

Wireless/LAN drivers

Support for your graphics card

Support for your audio card

Support for USB devices

as built in:

scsi sata & or ata controllers

file system used

Note

Many pages on the wiki will tell you the kernel requirements for the application that they are about. Keep your eyes open for the blue background, white text sections of pages. Like on this one: uvesafb

Building and installing the kernel sources

After you finish configuring your kernel sources, you will need to build them. To build your sources, run the following:

# make

Tip

You can add -j<number of processing cores + 1> after make to build the kernel more quickly.

When the kernel and its modules finish building, install them:

# make modules_install && make install

Now that you have installed your kernel and modules, it is a good idea to install an Initramfs.

If your system has a separate /usr partition, is encrypted, or uses some other non-standard configuration, it will probably not boot without an initramfs.

If your system is unencrypted, with file system, and hard drive controllers compiled in rather than as modules, it will not need an initramfs.

Advanced version

Getting ready to start

Note

In this case we are building a kernel that is booting root in LVM over encrypted LUKS container.
If you don't have this setup, don't worry, you just don't need all the modules, but everything else is similar.

First, there is the decision which linux kernel sources we need.
There are plenty of them in the repositories around, often it is not easy to distinguish between them.

I would always trust my distribution of choice and take what is has to offer - and funtoo has a lot to offer!

I really do recommend (especially if it is your first time) to build a debian-sourced genkernel like described in chapter 5 "Using Debian-Sources with Genkernel" in the Funtoo Kernels Tutorial.

From there you should have a running system booting nicely from your own build (just little bit bloated) kernel. This is more than you can expect from any other ready to go distribution.

Note

We are using Red Hat's dracut in order to build a nice initramfs (containing all the necessary tools and extra drivers our kernel might need to start the system). Although dracut is the way to go, more sophisticated and not as buggy as gentoo's genkernel approach, more and more funtoo geeks start using slashbeast's better-initramfs, which we will cover at the end of this howto! So after having set up a genkernel from debian or gentoo sources we are going to build a kernel with either (or both) dracut or/and better-initramfs. So gentoo sources with genkernel is always my backup if anything is not working correctly on my system. For the slightly more geeky approach with my own initram I am using pf-sources, ck-sources or any other more or less heavily patched sources.

Let's go!

Kernel Sources

The source you use on your system is up to you. For a laptop or desktop system, the following are recommended:

If you are unsure of which sources you would like to use, emerge gentoo-sources. That's always a safe bet for a general system. For more information on available kernels, check out: Funtoo Linux Kernels

Prerequisites

Regardless of the tools you already have installed, it is recommended to follow the steps below, even if you find them to be redundant.
First, we edit our /etc/portage/make.conf:

Next, we run make localmodconfig. You will get some questions which you can answer mostly with either M (compiled as a module) or Y (compiled directly into the kernel). If you are not sure what to choose, press enter, and the default option will be selected.