Although the general consensus is that it’s difficult to compile a Linux kernel, it's really not that hard, as you will see. After reading this brief tip, I hope you and I will share the title of Kernel Compiler!
What I am going to do is simply run through the steps of the actual compilation of my existing kernel. I won't make any changes (since changes and needs are so unique to each system) but I will walk through all the steps. I will also be approaching this with a path-of-least-resistance approach (read: we're going to do this the easy way).
Just to be safe...
Before I get into the compilation of my kernel I’m going to make a boot floppy just to be on the safe side. To do this, slap a floppy in your drive, su to root, and run the command:
dd if=/boot/bzImage of=/dev/fd0 bs=8192
where bzImage is the kernel image that you need to insert. For example, in my stock Red Hat 7.0 system the kernel image is /boot/vmlinuz-2.2.16-22. For this image, the command would be:
dd if=/boot/vmlinuz-2.2.16-22 of=/dev/fd0 bs=8192
Back to compiling the kernel
The first step is to locate the source code for your particular distribution. Typically, these files are located in /usr/source/linux. (This holds true for most Linux distributions).
Once you've located the source, you are ready to actually start the compilation process.
Because I am doing a simple recompile of my current kernel I am leaving out some major steps necessary when compiling a new kernel. I'll discuss those steps later in this Daily Feature.
Since I am going to take the easy route, let’s stick with the simplest (and sanest) kernel configuration tool. The xconfig tool is a graphical tool offering simple buttons that, when clicked, present the many kernel configuration choices. The main xconfig window looks like the one shown in Figure A.
Each time you click on a button within the xconfig window, you will be offered a list of choices. In Figure B, you can see where I've chosen the Character Devices button. Each choice will have three selections: yes (y), no (n), and modular (m). The first two choices are obvious: yes means add the module; no means do not add the module. The third choice isn’t so obvious. When modularly adding a kernel module, you are, in effect, saying you want the modules available and will load or unload them as you see fit.
Go ahead and run through the other buttons to see what's there. It's quite a hefty assortment of modules (giving you an idea of how complete the Linux kernel really is). Once you've run the gamut, you can click Save And Exit and move on to the next step of the compilation!
|Above you see a small sample of possible options to choose while compiling your kernel.|
Make and its options
You will now journey into the sometimes-maddening world of make. There are many ways to go about this. I am, however, going to try to outline the most universal process as well as the simplest.
While in the /usr/src/linux directory, you are going to run the following commands (as root):
Each of the above commands performs a very different and very critical role in the compiling of the Linux kernel. Once you've run the above set of commands without error, your kernel is compiled!
Remember that this is the first shot at compilation. I'll discuss various issues that surround compiling a new kernel later in this Daily Feature.
Now, you are ready for the final steps of your kernel compilation. These steps are very important, and it's crucial that you follow them closely.
BzImage to boot image
First, open up a second console window and cd to the /boot directory. Once you’re in this directory, run the ls command to see what's there. On my Red Hat 7.0 system, I see the following ls output:
What you’re actually looking for is your current kernel image and the System.map file. In my case, the image is vmlinuz-2.2.16-22, and the System.map is a symlink to System.map-2.2.16-22.
Sticking with the above example, rename the file vmlinuz-2.2.16-22 to vmlinuz-2.2.16-22-old, and the System.map-2.2.16-22 to System.map-2.2.16-22-old. To do this, run these commands:
mv vmlinuz-2.2.16-22 vmlinuz-2.2.16-22-old
mv System.map-2.2.16-22 System.map-2.2.16-22-old
With these files renamed, you’re ready to move the new files into /boot.
Now, change to the directory holding the new kernel image with:
cd /usr/src/linux/arch/i386/boot/bzImage /boot/
and then copy the bzImage file into /boot with the command:
cp bzImage /boot/vmlinuz-2.2.16-22
Then, it's time to do the same with the System.map file. Change to the /usr/src/linux directory and run the command:
cp System.map /boot/System.map-2.2.16-22
You are nearly done!
It's time to edit your /etc/lilo.conf file. In reality, you are going to simply add a new entry into this file to reflect the new kernel image. The section you want to edit looks like:
and will finally (in our example) look like:
Save this file and rerun lilo with the command:
You should receive a return with something like:
Added linux *
and you're ready to rock and roll!
With your boot floppy in hand (just in case), it's time to reboot. At the lilo: prompt, you can hit [Tab] and see both the linux and the test entry.
Of course, not every situation is going to be the same. You may be compiling a new kernel. The biggest difference here will be the location of the source files. If you have downloaded a new kernel, and plan to compile it, you will want to move the source of your current kernel so as not to overwrite the source of the working kernel.
Before you unpack the new kernel, check to see if the /usr/src/linux directory is not merely a symbolic link to /usr/src/linux-2.*.* (where 2.*.* is the name of your current, running kernel). To check this, run the command:
ls -l /usr/src/linux
If you receive something like:
lrwxrwxrwx 1 root root 12 Nov 4 12:08 linux -> linux-2.2.16
you know it's a symbolic link. With this in mind, you will want to remove the symbolic link with the command:
Now, you can unpack your new kernel, which will create a new directory. Let's say you are planning on compiling a 2.2.17 kernel. When you unpack the source, you will have a new /usr/src/linux directory. You’ll want to rename that directory with the command:
mv /usr/src/linux /usr/src/linux-2.2.17
and then create a symbolic link to /usr/src/linux with the command:
ln -s /usr/src/linux-2.2.17 /usr/src/linux
You can now move into the /usr/src/linux directory and start the compilation process!
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 getjackd.net.