Things You'll Need
Not surprisingly, building a new kernel from source requires that you have a copy of the source on hand. The official repository for kernel source code is the Linux Kernel Archives (see Figure 1). Many Linux distributions also offer the kernel source in a native package format (such as RPM). If packages are available for your platform, this may be the best way to go, as everything should mesh nicely with the installed system. Additionally, you'll need recent versions of the GNU Compiler Collection (GCC) and various support utilities. We'll check these requirements once we have the kernel source. Since I just need to customize the existing 2.4 kernel on my Red Hat 9 system, I installed the kernel source RPM package from my install CDs.
Figure 1 The Linux Kernel Archives.
If you choose to download the source from the Linux Kernel Archives, you'll need bunzip2 to extract the tar.bz2 archive. To extract the source, use the following commands:
[hunter@heinz hunter]$ bunzip2 linux-2.4.20-8.tar.bz2 [hunter@heinz hunter]$ tar xf linux-2.4.20-8.tar [hunter@heinz hunter]$ ll total 182740 drwxrwxr-x 18 hunter hunter 4096 Oct 8 12:24 linux-2.4.20-8 -rw-r----- 1 hunter hunter 186931200 Oct 12 17:03 linux-2.4.20-8.tar
These actions produce a directory containing all the source code for the kernel. You'll find a README file in the main source directory; the Documentation directory contains information for a wide array of situations. The Documentation/00-INDEX file provides a quick guide to the included documentation. Be sure to look at the Documentation/Changes file. This file lists all the software you'll need to compile your kernel, and includes the commands to check versions. URLs are included for each software package.
Make sure that you have everything you need before you start compiling your kernel! This strategy will save a great deal of frustration later on.
If you need to inspect the source, now is the time. Once you've confirmed that everything in the source code is as it should be, move it into the /usr/src directory and set ownership to root:root:
[hunter@heinz kernel]$ su - Password: [root@heinz root]# mv /home/hunter/linux-2.4.20-8 /usr/src/ [root@heinz root]# chown -R root:root /usr/src/linux-2.4.20-8 [root@heinz root]# ll /usr/src total 12 drwxr-xr-x 2 root root 4096 Jan 24 2003 debug drwxrwxr-x 18 root root 4096 Oct 8 12:24 linux-2.4.20-8 drwxr-xr-x 7 root root 4096 Jul 17 02:12 redhat
If space is limited, place the source on another partition and create a symlink in /usr/src.
Now that the source is safe and secure, some preparation is in order. Making a backup of the system is an excellent idea at this point, unless it's a test system that you don't care about. At the very least, back up the contents of /boot and /lib/modules, as these are the two directories where your new kernel and associated modules will reside:
[root@heinz linux-2.4.20-8]# cp rp /boot /boot.bak [root@heinz linux-2.4.20-8]# cp rp /lib/modules/2.4.20-8 /lib/modules/2.4.20-8.bak
You should also print out your /etc/fstab. You'll need to know your partition mappings to set up the bootloader later on, and this info is also nice to have in case of any problems. Finally, have a boot floppy handy. If you can't boot any of the kernels on the system, the boot floppy will let you restore /boot and /lib/modules to a usable state.
Once all this preparation is finished, you're ready to configure the kernel.