Complete Handbook/Building the Linux kernel

From Gentoo Wiki
Jump to: navigation, search

Kernel configuration procedure

Introduction

The Linux kernel is the core of the Linux operating system. It takes care of resource management (processes, memory, ...), hardware support, networking, file systems, ... and is therefore one of the most vital parts of the system.

Gentoo offers various kernel sources. Each source is based on the vanilla kernel source (the one developed by the main kernel developers) and adds in additional features, hardware support, experimental features, etc. You can pick whatever kernel source you like for your system (as long as your profile allows it of course).

When the source code is installed on your system, you still need to configure and build the kernel before you can use it. The kernel configuration is the trickiest part since a mistake can lead to an unusable kernel, but trying to build in too much leads to irrelevant code in your kernel which you'll never use, take space and might even cause troubles later when there is a kernel bug in that code.

Luckily, Gentoo offers a tool called genkernel which configures, builds and installs a kernel automatically. This might be of interest to you if you have no idea about configuring kernels, you want a basic kernel configuration for all your systems, or you require a kernel that can deal with a majority of hardware.

After configuring the kernel, it is built into an image which your computer loads in memory when you boot the system.

Picking a kernel source

Gentoo maintains a list of Gentoo kernel packages which contains a small introduction about the kernel tree.

You can make your choice from this guide, although you can very well pick a fairly generic one right now and use a different kernel later on - the kernel is completely interchangeable on a Linux environment so you don't have to decide on the kernel right here, right now.

The most default, generic kernel source is the vanilla-sources. This kernel tree is the one released by the Linux Kernel Developers, unmodified. Gentoo offers a patched version itself called gentoo-sources. Using these sources has the advantage that Gentoo can release a new kernel tree whenever it deems it necessary.

If you have made your choice, install the kernel sources using emerge. Just add the kernel source name as an argument to emerge and it will download and extract the kernel sources on your system:

root #emerge --ask <kernelsources>

Building the kernel

Automated build process

If you don't want all the hassle surrounding the kernel installation, you can install genkernel and then have genkernel configure, build, and install the Linux kernel for you. This process is quite simple:

root #emerge --ask genkernel
root #genkernel all

However, genkernel is a lot more powerful than this. With this tool, you can maintain your personal kernel configuration and let the tool rebuild newer kernel versions with your settings. You can enable specific features (like bootsplash, LVM2, EVMS2, RAID, ...) and tweak the compiler settings used during the kernel build process (which differs from the settings placed in make.conf!).

For more information on genkernel, please read the Genkernel guide.

Manual build process

The manual build process consists out of three steps:

  • Configuring the kernel.
  • Building the kernel.
  • Installing the kernel.

To configure the kernel, go to /usr/src/linux and run make menuconfig. You will get a dialog-based interface where you can configure your kernel.

Configuring a kernel isn't hard if you know what hardware you have and what features you want - but then again, if all this is new to you, finding out what features you want or need can be time consuming.

The kernel configuration dialogs has a good Help built-in which even includes search functionality (very useful if you want to search for the location of that network card you have but can't seem to place in the configuration structure).

It is not our intention to describe the kernel configuration process for you - there are several guides about this topic on the Internet and if you really aren't able to successfully configure a kernel, use genkernel for the time being.

When you're finished with the configuration part, build the kernel by running make (hold on, we'll do this together with the next step). make is a tool that reads in a script called Makefile in the directory you are in. Makefiles are very powerful when used for building software since they are able to only (re)build those parts that need to be (re)build instead of building the entire software over and over again.

To finish the kernel build process, you need to copy over the resulting kernel image to the boot partition and install the kernel modules you have selected. The location of the kernel image depends on the architecture you're using. The following table gives an overview of possible kernel images with the commands needed to build and install the kernel:

Architecture Image Location Build Command
alpha arch/alpha/boot/vmlinux.gz
root #make && make install modules_install && make boot
amd64 arch/x86_64/boot/bzImage
root #make && make install modules_install
hppa vmlinux
root #make && make install modules_install
mips vmlinux
root #make && make install modules_install
ppc Apple/IBM vmlinux
root #make && make install modules_install
ppc Pegasos arch/ppc/boot/images/zImage.chrp
root #make && make install modules_install
ppc64 vmlinux
root #make && make install modules_install
sparc32 arch/sparc/boot/image
root #make && make install modules_install
sparc64 arch/sparc64/boot/image
root #make && make image install modules_install
x86 arch/x86/boot/bzImage
root #make && make install modules_install

The build command is divided in two parts - the make instruction we've discussed before and a specific instruction to finish off with some additional steps. The separation is made with the && string. This is a specific operator to the shell, telling the system to continue with the next command if the previous one didn't fail. A similar operator is || which tells the system to execute the next command if the previous one did fail.

Now, execute the build command to create the kernel image.