Using Linux Kernel Source Code

Using Linux Kernel Source Code

This book focuses primarily on using the Red Hat Linux binary software that comes on a set of installation CDs. Although the Linux kernel source code (kernel-source package) comes on the Red Hat Linux install CDs, rebuilding the kernel is not necessary for most users.

This section is particularly applicable for those cases where you want to use the Linux kernel source code to rebuild the Linux kernel.

Reconfiguring the kernel

When you install Red Hat Linux, the kernel (which represents the core of the operating system) is automatically configured for you and ready to run. Many assumptions are built into this kernel, including the types of drivers that you will need to run your hardware and the services that the kernel provides. There are times when you may want to change these assumptions. To do that, you can reconfigure your kernel.


Not all changes to the features in your kernel require that you rebuild it. Many drivers are available to an installed Red Hat Linux system in the form of loadable modules. Loadable modules can be used to add features to a running kernel. For example, the PCMCIA feature uses loadable modules. You can use the insmod and modprobe commands to load modules that you need (as described in Chapter 10).

Reconfiguring your kernel is a tedious job. You will need to answer a lot of questions (some of which may mystify you). To simplify the process of reconfiguring your kernel, the xconfig option to make for rebuilding the kernel offers a graphical interface. Using make xconfig, you can focus on the drivers you want to add and remove instead of having to page through all the drivers.

Deciding to reconfigure the kernel

Sometimes, you may need to reconfigure the kernel so that it works the way you need it to. Many of the features that are turned off by default are off either because they relate to experimental features or because they are needed to support bugs (or limited features) in some older computer hardware. Here are a few examples:

  • Networking options The Network option should be on by default. It should be on even if you are not connected to a network because the X Window System (your graphical desktop) relies on it. If Linux is operating as a router and you want to optimize it as such (by default, it is optimized to act as a host) you will want to reconfigure networking options. Certain experimental options (X.25, SPX, and others) are also turned off by default.

  • Block devices For some older disk drives, you may need to use an old disk-only driver on the primary interface. With that selected, you won't be able to have an IDE/ATAPI CD-ROM attached to the primary IDE interface. However, turning on the disk-only driver may be necessary for some older disk drives.

  • QoS and/or fair queuing If your network devices include some real-time devices that require a minimum quality of service (QoS), you can change some of these QoS values to favor certain interfaces when you have a high demand for service. Turning on QoS lets you try any of a handful of different algorithms.

As you read this and other documents that describe how to use Red Hat Linux, you may notice that certain features can only work if the kernel is changed. By using tools such as the X kernel configuration tool, you can determine which features are turned on and off by default, and build a kernel to suit your needs.


If you need to support a hardware device that is only added temporarily or occasionally to your computer, you should try to configure it as a loadable module. In that way, you can load and unload the module as needed, without making the kernel bear any performance penalty when the device is not needed.

Installing kernel source code

To reconfigure the kernel, you need the Linux source code. You can install that source code from the second installation CD that accompanies this book (CD #2). If the source code is already installed on your Linux system, it should be in the /usr/src/linux* directory (for example, /usr/src/linux-2.4).

When I checked the disk space, the kernel-source package consumed about 189MB of disk space in /usr/src/linux-2.4 (which is a link to a directory that has a name reflecting the kernel release, such as linux-2.4.21). To install the kernel source code from the RPMS directory, type:

# rpm -i kernel-source*

Starting to reconfigure the kernel

The steps for reconfiguring your kernel include protecting your system, setting up the configuration, making the configuration, checking for dependencies, preparing the source tree, and compiling the new kernel. Each of these steps is fairly straightforward and described in the text that follows.

Protecting your system

During the Red Hat Linux installation, you should have created an emergency boot disk. This floppy disk enables you to boot your Linux system in case the new kernel doesn't boot or in case your grub.conf file doesn't work (so that the boot loader fails). If you did not create one, or if you can't find it, insert a blank floppy disk in the first floppy drive and type the following:

# mkbootdisk --device /dev/fd0 2.4.21-20

Replace the number 2.4.21-20 with the version number of your kernel. Press Enter to continue, as prompted.

Setting up the configuration

To begin reconfiguring the kernel, you should be in a bash shell. (If you are not sure, type bash at the command line.) Then go to the /usr/src/linux-2.4* directory and type:

# make mrproper

This prepares you to create the new kernel configuration. It thoroughly cleans up the kernel configuration directories.

Making the configuration

To choose the kernel options that you want, you must run make with the config, menuconfig, or xconfig options. By far, the easiest option is xconfig, though it requires that you run an X desktop (such as GNOME or KDE). Here is an example:

# make xconfig

If you are not running X, you can use config or menuconfig. Using make config presents you with a pure text-based, question-and-answer way of rebuilding your kernel. Typing make menuconfig from /usr/src/linux-2.4* provides a menu-driven interface (which runs in a character terminal).

Besides being a bit nicer to look at, the menuconfig and xconfig kernel configuration tools enable you to select only those areas of the kernel that you want to change. With make config, you have to step through each category. With the xconfig tool, you see all selections available in a category. Keep in mind, though, that if a category is turned off, selections within that category are grayed out. Also, if you make a mistake, just reselect the category.


You might want to edit the Makefile file at this point and change the value of EXTRAVERSION. This can protect you from writing over your original kernel files by mistake. After the new kernel is booted, you can type uname -a to see the value of EXTRAVERSION appended to the kernel name that is running.

Checking for dependencies

When you have configured the kernel, save the changes. After you have made the kernel configuration changes that you want, you must return to the /usr/src/linux* directory and type the following:

# make dep

The make dep command checks that dependencies of the packages are met. Next, you must prepare the source tree.

Preparing the source tree

To prepare the source tree for the new kernel build, run the make clean command as follows:

# make clean
Compiling the new kernel

You have several choices for compiling the new kernel. You can create the new kernel boot image so that it is stored on your hard disk or have a copy placed on floppy disk. The latter is good for testing the new kernel. You can boot the image from floppy before you install it, so if the new kernel doesn't work, you can refrain from installing it.

To compile the kernel and save it to a floppy disk (as well as to the hard disk), place a floppy disk in the disk drive and run: make zdisk. To save the new image only to hard disk, type make zImage to compile the kernel. If the image created by make zImage is too large, type the following to create a more compressed image:

# make bzImage

This process takes a while, so be patient. In any case, the image is placed in the arch/i386/boot directory. The resulting file is a compressed boot image.

Making modules

Modules that are not compiled into the kernel can be compiled as loadable modules using the modules option. Once the modules are compiled, you can install them in /lib/modules/kernel (where kernel is replaced by the number representing the current kernel) with the modules_install option. Type the following:

# make modules
# make modules_install

To add a module when your system is running, you could use the modprobe or insmod commands.

Creating an initrd image

If your computer does not have a SCSI adapter, skip this step. However, if your computer has one and needs to load the SCSI module at boot time, you must create an initrd image. First, check that your /etc/modules.conf file contains a line for your SCSI adapter. For example:

alias scsi_hostadapter aic7xxx

Next, use the mkinitrd command to build the new image. The mkinitrd command takes two options. The first indicates the name of the new image. The second indicates the kernel from which the modules are taken (such as /lib/modules/2.4.18-11). For example:

# mkinitrd /boot/newinitrd-image 2.4.18-11

When you create an entry for this new kernel in /etc/lilo.conf, make sure you add the new initrd image to that entry. For example:


Installing the new kernel

Different ways to set up boot images in the GRUB or LILO boot loader are described in the GRUB and LILO sections of Chapter 2. I recommend using those procedures to add the new boot image in such a way that it doesn't replace the old one. That way, you can test it without the risk of being stuck with a potentially unbootable system.

Part IV: Red Hat Linux Network and Server Setup