If you read my last article on kernel compilation, "Compiling (or recompiling) your Linux kernel," you should be able to recompile your existing kernel without a hitch. But what if you wanted to test a newer kernel or make changes to your existing kernel without destroying your currently running-just-fine kernel? Where there is a will, there’s almost always a way when it comes to Linux.
I was once faced with a sticky situation where I wanted to take advantage of a CD-RW, but I knew that compiling that into the kernel would break another function of the kernel. In order to get around that mess, I decided to compile a kernel to take advantage of the CD-RW, leaving the current kernel intact. With this set up, I was able to reboot and choose which kernel (which functionality) I wanted to use, depending on the task at hand.
This Daily Feature is going to show you how to create the same set up by adding a second (or third, or however many you wish) kernel to your Linux box.
What you will need
You will need a working Linux machine (of course), a connection to the network (if you plan to download a new kernel), the full source for the kernel you wish to compile, root access to the Linux box, and a little time (depending on the speed of your machine, this could be half an hour to an hour, approximately). You will also need a working X graphical environment if you want to take advantage of the easier xconfig tool (not necessary for kernel compilation).
There are a number of places to grab the source for the Linux kernel. The most popular place is Kernel.org.
Before you start
In order to preserve your current working kernel, it's always best to take a few preventive steps. Before I unpack a kernel in /usr/src, I always copy the tar file to the /tmp directory, unpack it, and check to make sure the base directory of the package isn't linux. The reason for this step is that many distributions use a /usr/src/linux sym link to point to the current kernel's source. Overwrite that sym link and you could find yourself in big trouble. So, if the new kernel unpacks in a directory akin to linux-2.4.17, you are in good shape and can unpack that file in /usr/src.
If you unpack the kernel and see that the base directory is linux, you will want to unpack the new kernel in the /tmp directory and then move it to /usr/src, renaming it to (in this case) linux-2.4.17 with the commands:
mv linux-2.4.17.tar.gz /tmp
tar xvzf linux-2.4.17.tar.gz
mv linux /usr/src/linux-2.4.17
You will also need to know what boot loader you are using. You will have chosen this during installation. If you use Red Hat Linux and you are using any version prior to 7.2, your boot loader will most likely be LILO. If you are using Red Hat 7.2, you will probably be using GRUB. If you are using a newer Mandrake Linux, you will be using GRUB. SuSE Linux takes advantage of LILO, as well as Debian.
The last and most important thing is to make sure you have a working copy of your boot floppy around. If not, take a look at my article "Creating Linux boot disks from both Linux and DOS" and create a boot disk for your current working kernel.
Let's get started
For the purpose of this article, you are going to be compiling a 2.4.17 kernel. The first thing you are going to do is unpack the kernel archive into /usr/src. You will need root access to do this (as well as the rest of the steps outlined in this article).
If you haven't already unpacked your kernel, do so (as outlined above). Once that kernel is unpacked, change to the linux-2.4.17 directory and get ready to compile.
The most time-consuming step in kernel compilation is choosing what you want to add or remove from the kernel. To do this, you will run one of the various available styles of make, which are:
- make config
This is a text-only selection process. This process will be a long series of questions that you answer with a Y (for "Yes, install"), an N (for "No, do not install"), or an M (for "Install as a module").
- make menuconfig
This style is an ncurses-based menu selection process. This type of make is much easier than make config, but it is not point-and-click but rather tab-and-select.
- make xconfig
This is the most common kernel configuration. It is a total GUI point-and-click experience. This style is the least time-consuming and the most user friendly.
Let me introduce you to Mr. Proper
If this is the first time this kernel is being compiled, you can immediately move to the make dep command. If, however, this is either a second attempt at compiling or a recompile, you'll need to run the make mrproper command before running make xconfig.
During this process, you will go through the various sections (and subsections) selecting (or unselecting) the various modules available in that kernel.
Once you have made all your selections, save the new kernel configuration and get ready for the compilation.
Compiling the kernel
The kernel compilation process is actually just a set of commands that are run in a very specific order. As root, you will want to run the following commands:
Some of the above steps will take a bit longer than others and, depending upon the machine, could take anywhere from two minutes to 30 (my best and worst times using a p200pro and athlong 1-GHz processor, respectively). Once the compilation is complete, you need to copy a couple of files over to the /boot directory, add entries into your boot loader configuration file (and run the boot loader command if needed), and then you are ready to boot into your new kernel.
Copying the correct files
If you change into the /usr/src/linux-2.4.17/arch/i386/boot directory, you will find a file called bzImage. This file needs to be moved over to the /boot directory and renamed vmlinuz-2.4.17.This can be achieved with:
cp bzImage /boot/vmlinuz-2.4.17
The next file to copy is the System.map file. This file is located in /usr/src/linux-2.4.17/ directory. Like the bzImage file, the System.map file must be renamed when moved. Follow these commands:
mv System.map /boot/System.map-2.4.17
You are nearly finished.
These boots are made for...
The final step is making your boot loader aware of the new kernel. If you are using LILO, you will need to add a new entry to /etc/lilo.conf and then rerun /sbin/lilo (as root). The lilo.conf entry will look like:
With this addition to the lilo.conf file in place, run (as root):
and the process is complete. You can now boot up and select 2.4.17 as your kernel.
If you are using the GRUB boot loader, you only have to add the new entry into the /boot/grub/menu.1st file. This entry looks like:
With this entry in place, you are ready to reboot.
Wrap it up
This method of kernel compilation will ensure that not only will you be able to test newer kernels or newer features (or even add some older features) but you will also have a working kernel on your machine. Granted, it's ill-advised to tinker around with the kernel of a production machine (unless you know exactly what you are doing) but the process outlined above will make kernel compilation a whole heck of a lot safer.
Jack Wallen is an award-winning writer for TechRepublic and Linux.com. He’s an avid promoter of open source and the voice of The Android Expert. For more news about Jack Wallen, visit his website jackwallen.com.