theMorgue.org
Pushing the Limits of Home Electronics  

Building a Customized Kernel

If you need some feature in the latest kernel release, or you simply want to performance tune the kernel to your specific hardware, you're probably going to have to configure and compile it yourself. Building a kernel can seem like a pretty daunting task, and in some sense, it is. If you choose the wrong options or options that are incompatible with your hardware, you may accidentally disable the support of some piece of hardware that you need, or your machine just may not boot at all. Luckily there are ways to safeguard against these things by always creating a backdoor to the original setup.

This document will give you step-by-step instructions on how to compile and install your own customized kernel with the minimum amount of stress and anxiety. Keep in mind that this is a very condensed "How To" document that does not handle all of the fine details of kernel configuration and compiling. If you finish this document and you want more information, please read the Kernel-HOWTO supplied by the Linux Documentation Project.

Installing the Kernel Source

The first thing you need to build a kernel is the source code. You might already have the source to your current kernel installed. You will probably want to download the latest kernel source to get all of the most recent benefits and to avoid incompatibilities in your current modules with the modules for the new kernel. You can determine the version of your current kernel with the command:

 
   uname -r 

The kernel source can be obtained from your favorite RPM repository (such as rpmfind.net). You will want the RPMs for the kernel source and header files. They will have names like kernel-source-x.x.x-y and kernel-headers-x.x.x-y where the x.x.x is the version number and y is the build number. Download one of each of these RPMs that has a newer version than the one you have installed. Make sure that you get source and headers from the same version and build.

Installing the source is quite simple. Log into your root account and issue the following command (substituting in the appropriate version and build numbers, of course).

 
   rpm -Uvh kernel-source-x.x.x-y.rpm kernel-headers-x.x.x-y.rpm

The files should now be in /usr/src/linux-x.x.x. Before you go any further, change directory to /usr/src and make sure that the symbolic link linux points to your new source directory. In other words, make sure that when you run the command ls -l linux. You get the following output:

 
   lrwxrwxrwx   1 root     root           12 Feb 11 22:08 linux -> linux-x.x.x/

If you don't get this output or you don't have a file called linux then create a symbolic link to your new source directory as follows.

 
   ln -sf linux-x.x.x linux

NOTE: If you decide to go back to your old kernel, you will need to link this file back to your original source directory.

Configuring the Kernel

Now for the fun part, customizing. If you're not logged in as root, do it now and change directory to /usr/src/linux. Just to get the ball rolling and to make sure that there are no remnants of a previous build to screw you up, run the command:

 
   make mrproper

There are couple of nice interfaces to help you get through the configuration easily. They are invoked with the commands make menuconfig and make xconfig. Menuconfig is a text based interface to the kernel configuration, and xconfig is an X based interface. For the sake of discussion, I'll assume that you chose the xconfig interface.

Since most kernels are built assuming a 386 processor, you can possibly increase the performance of your kernel by choosing a more appropriate configuration for your generation of processor. The default configuration files for the most common processors are in the /usr/src/linux/configs directory. Basically, Pentium class machines would benefit from the 586 configuration and Celeron/Pentium Pro/II/III+ machines would benefit from the 686 configuration. Don't choose a configuration file from an architecture greater than the one you have, or the kernel may not run on your hardware.

NOTE: The configuration files with "smp" in the name are for multiprocessor systems.

If you haven't already done so, fire up the configuration interface with the command make xconfig. It should look something like the figure below.

Click on the "Load Configuration from File" button and enter the name of the configuration file from the configs directory that you chose earlier. Keep in mind that you have to enter the "configs/" part of the path also.

Now we get to the trickiest part, figuring out what to customize. For the most part, anything that isn't currently built into the kernel is built as a loadable module. You can tell what is currently configured by which column is checked in the subsystem configuration window. For example, click on the "Mice" button. It should look similar to the figure below.

On most items, you will notice that you can choose either "y", "m", or "n". "y" means that you want support for this built into the kernel, "m" means that you want support for this item, but as a loadable module, and "n" means that you don't want any support for this item.

You may just want to leave the default configuration alone. But if you want to get real fancy, you can go through all of the sections and customize at will. I usually don't mess around with a lot, but I do turn off SCSI and RAID support since I don't use either at the moment. You can usually get a helpful tip by clicking the "Help" button next to the module description.

Once you have the kernel configured, click "Save and Exit" in the main window.

Building the Kernel

If you've gotten this far, you should be ready to actually build the kernel. This, of course, requires a C compiler which I will assume you have installed. You will want to run the following commands first.

 
   make dep
   make clean

"make dep" will make sure that any dependencies (such as header files) are in place, and "make clean" will clean out any files from previous builds that may cause problems. If you did "make mrproper" as I suggested earlier, this probably won't do much, but it never hurts to clean up again.

This next command will build the actual kernel and will probably take a while. Of course, "a while" is a very relative term and depends greatly on your processor. To start building the kernel, execute the following:

 
   make bzImage

Other sources will probably tell you to run "make zImage", but the kernels nowadays seem to be too big to do this. You can try it, but after waiting "a while" you'll probably come back to a message complaining about the kernel being too big.

Once the previous command has finished, you should find your new kernel in /usr/src/linux/arch/i386/boot/bzImage. Don't do anything with it just yet, you still have to build the modules. And you do that with this command.

 
   make modules

This one may take even longer than building the kernel, so this is probably a good time to go to the fridge and get a nice cool beverage.

If you've gotten this far and you haven't gotten any horrible error messages from the compiler, you are ready to install your kernel and modules.

Installing Your New Kernel

There is one more "make" command to run that will install your modules.

 
   make modules_install

This command will put your modules into /lib/modules/x.x.x-y. Where, once again, x.x.x and y are the version and build numbers for the kernel. To install the kernel, you'll need to copy two file into the /boot directory as follows.

 
   cp /usr/src/linux/arch/i386/boot/bzImage /boot/vmlinuz-x.x.x-y 
   cp /usr/src/linux/System.map /boot/System.map-x.x.x-y 

You'll also want to recreate a couple of symbolic links in the /boot directory to point to your new files as shown below.

 
   ln -sf /boot/System.map-x.x.x-y /boot/System.map
   ln -sf /boot/vmlinuz-x.x.x-y /boot/vmlinuz

You should now have your kernel properly installed, but the job isn't quite finished. There is one step left, so read on.

Configuring LILO for Multiple Kernels

LILO is the program that actually loads the kernel, so you need to make sure that it knows about your new kernel. You'll also want to make sure that it still knows about the old one so you can get back to it if the new one doesn't work as well as planned.

This is actuall pretty simple. Open the file /etc/lilo.conf in your favorite text editor (possible kedit or gedit). It should look something like this:

 
   boot=/dev/hda
   map=/boot/map
   install=/boot/boot.b
   prompt
   timeout=50
   image=/boot/vmlinuz-2.2.5-15
           label=linux
           root=/dev/hda2
           read-only

All you need to do is make a copy of the lines from "image=" to the end so it looks like this.

 
   boot=/dev/hda
   map=/boot/map
   install=/boot/boot.b
   prompt
   timeout=50
   image=/boot/vmlinuz-2.2.5-15
           label=linux
           root=/dev/hda2
           read-only
   image=/boot/vmlinuz-2.2.5-15
           label=linux
           root=/dev/hda2
           read-only

Then change the first "image=" to point to your new kernel version. You'll also want to change the "label" on the second one to something like "orig" to remind you that this was the original kernel. Your file should now look like the following.

 
   boot=/dev/hda
   map=/boot/map
   install=/boot/boot.b
   prompt
   timeout=50
   image=/boot/vmlinuz-x.x.x-y label=linux
           root=/dev/hda2
           read-only
   image=/boot/vmlinuz-2.2.5-15
           label=orig
           root=/dev/hda2
           read-only

Now save the file, exit your editor, and run:

 
   /sbin/lilo

This will inform LILO to reload this configuration file.

WARNING: If you don't do this, the configuration changes won't take effect.

When you reboot and get the "LILO:" prompt, you can type any of the labels (i.e. "linux" or "orig") for the system to boot that kernel. LILO chooses the first one by default. So if you reboot and the new kernel doesn't work, you can reboot again and type "orig" at the prompt to get you back to the original kernel.

NOTE: If you do this, you will probably get some messages about the System.map being the wrong version, but it shouldn't break anything.

You should be all set now, so reboot your machine and reap the benefits of your new kernel. If you find that the new kernel isn't all it's cracked up to be, read the following section for instructions on uninstalling it.

Uninstalling the Kernel if it Doesn't Work

Sometimes the new kernel doesn't work as well as we would like and the original one worked better. This is usually caused by getting a little overzealous in the configuration section. But no matter what the reason, uninstalling is usually fairly simple. The following commands will assume that your previous kernel was version 2.2.5-15 (this version will be the one that you got from uname -r way back when you were installing the source for your new kernel).

 
   ln -sf /boot/vmlinuz-2.2.5-15 /boot/vmlinuz
   ln -sf /boot/System.map-2.2.5-15 /boot/System.map
   ln -sf /usr/src/linux-2.2.5 /usr/src/linux

Then you need to return your /etc/lilo.conf file to it's original state by removing the first "image=" section and changing the label on the section image back to "linux". Once again, make sure that you run /sbin/lilo to inform LILO of your changes.

Copyright © 2000 theMorgue.org. All Rights Reserved.