Chapter 19. Building a kernel

Table of Contents
19.1. Introduction
19.2. Configuration
19.3. Compilation
19.4. Installation

19.1. Introduction

The Linux kernel is shortly discussed in Section 2.1. One of the advantages of Linux is that the full sources are available (as most of the Slackware Linux system). This means that you can recompile the kernel. There are many situations in which recompiling the kernel is useful. For example:

This chapter focuses on the default kernel series used in Slackware Linux 10.2, Linux 2.4, though most of these instructions also apply to Linux 2.6. Compiling a kernel is not really difficult, just keep around a backup kernel that you can use when something goes wrong. Kernel compilation involves these steps:

In this chapter, we suppose that the kernel sources are available in /usr/src/linux. If you have installed the kernel sources from the "k" disk set, the kernel sources are available in /usr/src/linux-kernelversion, and /usr/src/linux is a symbolic link to the real kernel source directory. So, if you use the standards Slackware Linux kernel package you are set to go.

19.2. Configuration

As laid out above, the first step is to configure the kernel source. To ease the configuration of the kernel, it is a good idea to copy the default Slackware Linux kernel configuration to the kernel sources. The Slackware Linux kernel configuration files are stored on the distribution medium as kernels/<Kernel>/config. Suppose that you use the bare.i kernel (which is the default kernel), and that you have a Slackware Linux CD-ROM mounted at /mnt/cdrom, you can copy the Slackware Linux kernel configuration with:


# cp /mnt/cdrom/kernels/bare.i/config /usr/src/linux/.config
    

At this point you can start to configure the kernel. There are three configuration front-ends to the kernel configuration. The first one is config, which just asks you what you want to do for each kernel option. This takes a lot of time. So, normally this is not a good way to configure the kernel. A more user friendly approach is the menuconfig front-end, which uses a menuing system that you can use to configure the kernel. There is also an X front-end, named xconfig. You can start a configuration front-end by going to the kernel source directory, and executing make <front-end>. For example, to configure the kernel with the menu front-end you can use the following commands:


# cd /usr/src/linux
# make menuconfig
    

In the kernel configuration there are basically three options for each choice: "n" disables functionality, "y" enables functionality, and "m" compiles the functionality as a module. The default Slackware Linux kernel configuration is a very good configuration, it compiles only the bare functionality needed to boot the system in the kernel, the rest is compiled as a module. Whatever choices you make, you need to make sure that both the driver for your IDE/SCSI chip set is available in the kernel and the filesystem driver. If they are not, the kernel is not able to mount the root filesystem, and no further modules can be loaded.

19.3. Compilation

The first step of the kernel compilation is to let the kernel build infrastructure check the dependencies. This can be done with make depend:


# cd /usr/src/linux
# make depend
    

If make depend quits because there are errors, you have to recheck the kernel configuration. The output of this command will usually give some clues where things went wrong. If everything went fine, you can start compiling the kernel with:


# make bzImage
    

This will compile the kernel and make a compressed kernel image named bzImage in /usr/src/linux/arch/i386/boot. After compiling the kernel you have to compile the modules separately:


# make modules
    

When the module compilation is done you are ready to install the kernel and the kernel modules.

19.4. Installation

19.4.1. Installing the kernel

The next step is to install the kernel and the kernel modules. We will start with installing the kernel modules, because this can be done with one command within the kernel source tree:


# make modules_install
      

This will install the modules in /lib/modules/<kernelversion>. If you are replacing a kernel with the same version number, it is a good idea to remove the old modules before installing the new ones. E.g.:


# rm -rf /lib/modules/2.4.26
      

You can "install" the kernel by copying it to the /boot directory. You can give it any name you want, but it is a good idea to use some naming convention. E.g. vmlinuz-somename-version. For instance, if we would name it vmlinuz-custom-2.4.28, we can copy it from within the kernel source tree with:


# cp arch/i386/boot/bzImage /boot/vmlinuz-custom-2.4.28
      

At this point you are almost finished. The last step is to add the new kernel to the Linux boot loader.

19.4.2. Configuring LILO

LILO (Linux Loader) is the default boot loader that Slackware Linux uses. The configuration of LILO works in two steps; the first step is to alter the LILO configuration in /etc/lilo.conf. The second step is to run the lilo, which will write the LILO configuration to the boot loader. The LILO configuration already has an entry for the current kernel you are running. It is a good idea to keep this entry, as a fall-back option if your new kernel does not work. If you scroll down to the bottom of /etc/lilo.conf you will see this entry, it looks comparable to this:


# Linux bootable partition config begins
image = /boot/vmlinuz
  root = /dev/hda5
  label = Slack
  read-only # Non-UMSDOS filesystems should be mounted read-only for checking
# Linux bootable partition config ends
      

The easiest way to add the new kernel is to duplicate the existing entry, and then editing the first entry, changing the image, and label options. After changing the example above it would look like this:


# Linux bootable partition config begins
image = /boot/vmlinuz-custom-2.4.28
  root = /dev/hda5
  label = Slack
  read-only # Non-UMSDOS filesystems should be mounted read-only for checking

image = /boot/vmlinuz
  root = /dev/hda5
  label = SlackOld
  read-only # Non-UMSDOS filesystems should be mounted read-only for checking
# Linux bootable partition config ends
      

As you can see the image points to the new kernel in the first entry, and we changed the label of the second entry to "SlackOld". LILO will automatically boot the first image. You can now install this new LILO configuration with the lilo command:


# lilo
Added Slack *
Added SlackOld
      

The next time you boot both entries will be available, and the "Slack" entry will be booted by default.

Note

If you want LILO to show a menu with the entries configured via lilo.conf on each boot, make sure that you add a line that says


prompt
        

to lilo.conf. Otherwise LILO will boot the default entry that is set with default=<name>, or the first entry when no default kernel is set. You can access the menu with entries at any time by holding the <Shift> key when LILO is started.