Building a Custom Kernel

Many people new to Linux often ask, "why should I build my own kernel?" Given the advances that have been made in the use of kernel modules, the most accurate response to that question is, "unless you know why you need to build your own kernel, you probably don't." So unless you have a specific reason to build a customized kernel (or you're just the curious sort), you may skip ahead to the section called Sendmail.

In the past, you would need to recompile the kernel if you added new hardware on your system. The kernel was, in other words, static. Improvements in the Linux 2.0.x kernels allowed for much of the drivers for hardware to be modularized into components that could only be inserted on demand. However, there were major problems with having multiple kernels on your system that had been compiled for different advancements (a good case being SMP versus UP kernels). Further advancements with the modularization of the Linux 2.2.x kernel have allowed for multiple kernels to more easily co-exist (though not share modules).

For information on handling kernel modules see the section called Loading Kernel Modules in Chapter 3. Most of the changes are hidden except when recompiling a customized kernel for your system.

Building a modularized kernel

These instructions enable you to take advantage of the power and flexibility available through kernel modularization. If you do not wish to take advantage of modularization, please see the section called Building a monolithic kernel for an explanation of the different aspects of building and installing a monolithic kernel. It's assumed that you've already installed the kernel-headers and kernel-source packages and that you issue all commands from the /usr/src/linux directory.

The most important step is to make sure that you have a working emergency boot disk in case you make a mistake below. If you didn't make a boot disk during the installation, use the mkbootdisk command to make one. The standard command is similar to mkbootdisk --device /dev/fd0 2.2.x. Once done, test the boot disk to make sure that it will boot the system.

It is important to begin a kernel build with the source tree in a known condition. Therefore, it is recommended that you begin with the command make mrproper. This will remove any configuration files along with the remains of any previous builds that may be scattered around the source tree. Now you must create a configuration file that will determine which components to include in your new kernel. Available methods for kernel configuration are listed below:

NotePlease Note
 

In order to use kmod (see the section called Loading Kernel Modules in Chapter 3 for details) and kernel modules you must answer Yes to kmod support and module version (CONFIG_MODVERSIONS) support in the configuration.

If you wish to build a kernel with a configuration file (/usr/src/linux/.config -- this file is created once one of the above methods has been performed) that you have already created with one of the above methods, you can omit the make mrproper and make config commands and use the command make dep followed by make clean to prepare the source tree for the build.

The next step in making a modularized kernel is to simply edit /usr/src/linux/Makefile and compile the source code components into a working program that your machine can use to boot. The method described here is the easiest to recover from in the event of a mishap. If you are interested in other possibilities, details can be found in the Kernel-HOWTO or in the Makefile in /usr/src/linux on your Linux system.

If you have a SCSI adapter and made your SCSI driver modular, build a new initrd image (see the section called Making an initrd image; note that there are few practical reasons to make the SCSI driver modular in a custom kernel). Unless you have a specific reason to create an initrd image, do not create one and do not add it to lilo.conf.

In order to provide a redundant boot source to protect from a possible error in a new kernel you should keep the original kernel available. Adding a kernel to the LILO menu is as simple as renaming the original kernel in /boot, copying the new kernel to /boot, adding a few lines in /etc/lilo.conf and running /sbin/lilo. Here is an example of the default /etc/lilo.conf file shipped with Red Hat Linux:

boot=/dev/hda
map=/boot/map
install=/boot/boot.b
prompt
timeout=50
default=linux

linear

image=/boot/vmlinuz-2.2.15-2.5.0
     label=linux
     initrd=/boot/initrd-2.2.15-2.5.0.img
     read-only 	      
     root=/dev/hda8

other=/dev/hda1
        label=dos
	

Now you must update /etc/lilo.conf. If you built a new initrd image you must tell LILO to use it. In this example of /etc/lilo.conf we have added four lines in the middle of the file to indicate another kernel to boot from. We have renamed /boot/vmlinuz to /boot/vmlinuz.old and changed its label to old. We have also added an initrd line for the new kernel:

boot=/dev/hda
map=/boot/map
install=/boot/boot.b
prompt
timeout=50
default=linux

linear

image=/boot/vmlinuz-2.2.15-2.5.0
     label=linux
     initrd=/boot/initrd-2.2.15-2.5.0.img
     read-only
     root=/dev/hda8

image=/boot/vmlinuz-2.2.15-2.5.0sjs
        label=test
        initrd=/boot/initrd-2.2.15-2.5.0sjs.img
        read-only
        root=/dev/hda8

other=/dev/hda1
        label=dos
	

Now when the system boots and you press Tab at the LILO boot: prompt, available choices will be shown;

LILO boot:
linux  test  dos
        

To boot the old kernel (linux) simply press Enter, or wait for LILO to time out. If you want to boot the new kernel (test), type test and press Enter.

Here is a summary of the steps:

You can begin testing your new kernel by rebooting your computer and watching the messages to ensure your hardware is detected properly.

Making an initrd image

An initrd image is needed for loading your SCSI module at boot time. If you do not need an initrd image, do not make one and do not edit lilo.conf to include this image.

The shell script /sbin/mkinitrd can build a proper initrd image for your machine if the following conditions are met:

To build the new initrd image, run /sbin/mkinitrd with parameters such as this:

/sbin/mkinitrd /boot/newinitrd-image 2.2.15
	

Where /boot/newinitrd-image is the file to use for your new image, and 2.2.15 is the kernel whose modules (from /lib/modules) should be used in the initrd image (not necessarily the same as the version number of the currently running kernel).

Building a monolithic kernel

To build a monolithic kernel you follow the same steps as building a modularized kernel with a few exceptions.