Ok, I guess i'll write a series of tutorials as i've been the one pining for this forum for a long time.

Lets begin, basic compilation of the kernel

Step 1 - Get your kernel
Depending on your distribution, there are different ways to obtain your kernels:
  • Redhat/Fedora - Supplied by the manufacturer, also available via rpmfind.net
  • Debian - Provided in .deb format, either apt-cache search for it or check out the online repository
  • Gentoo - Under the sys-kernel tree of portage (/usr/portage/sys-kernel/gentoo-sources is the more common ebuild)
  • Any Linux admin can get kernel tarballs from http://www.kernel.org but they're plain vanilla, no optimisations for any distribution etc
  • FreeBSD - Will be covered in a seperate tutorial


Step 2 - Unpack it
Ok, a handy lesson in the tar command to go along with this, for this example our kernel is called linux-2.4.24.tar.bz2. We can see this is a bzipped image so we use the following command to untar and unzip it:
tar xvjfp linux-2.4.24.tar.bz2
Here is a breakdown of this command:
  • x - extract files from archive
    v - be verbose about files (list every file extracted)
    j - parse the file through the bzip2 decompressor
    f - simply means, use the file supplied on the command line
    p - preserve permissions (instead of creating files in reference to the umask.. If you are running as root this behaviour is default anyway)


You should run this command inside /usr/src/ .. This is the most common directory for source code compilation, you may need to provide a full path if the file is not present in that directory. You should end up with a directory labelled after the kernel, for example, /usr/src/linux-2.4.24/.

Step 3 - Symlink directory
You will need to link your current kernel source tree (which we just extracted) to /usr/src/linux .. this is to avoid clumsy searching and pointless configuration, the appropriate command is ln -sf /usr/src/<kernel directory> /usr/src/linux .. Absolute paths can be substituted depending on your preference. The s option indicates this link is symbolic only, the f option overwrites any old existing link.

Step 4 - Begin configuration
Assuming you have ncurses installed (which you should have regardless, hell it's a system utility on Gentoo), we will use the menuconfig setup (X users, substitute 'xconfig' wherever 'menuconfig' is mentioned, non-ncurses users use 'config')

First, change to the source tree's directory: cd /usr/src/linux
Second, start the menu configuration program: make menuconfig

Several compilations should occur then a nice menu should pop up with all your kernel options, these options will be discussed at a later date, for now we'll assume you know how to configure your kernel

Step 5 - Compilation
Once you have finished your configuration, exit and save your configuration file.

You will now need to run two commands, the first is make dep - this will make all the basic dependancies needed for the kernel. Please note, this is not required for 2.6 kernels

Once make dep has finished, you will need to build the rest of the kernel

2.4 kernels:
make bzImage modules modules_install - This will compile the kernel image (bzImage), the modules and also install the modules into /lib/modules/<kernel-version>/
2.6 kernels:
make && make modules_install - Does the same as listed before, just using the new modifications to the makefile

Step 6 - Install your kernel
This step is only generalised as it's impossible to predict how you want your kernel to be configured, plus how you have your bootloader setup, so here is how I do it.

Copy the kernel image into /boot, labelled appropriately: cp arch/i386/boot/bzImage /boot/kernel-2.4.24
Copy the System.map file into /boot, again labelled with the kernel version: cp System.map /boot/System.map-2.4.24
Copy the config you have used to /boot: cp .config /boot/config-2.4.24

This ensures you have enough information at any time to find the kernel version used and all configuration options, even if you have disabled /proc/config support and/or the kernel isn't running.

You should be able to reboot your system (after reconfiguring your bootloader) and boot quite happily into your new kernel

Not covered by this tutorial:
  • FreeBSD Kernels
  • Linux kernel configuration
  • initrd usage
  • vmlinux usage
  • bootloader configuration


I will attempt to touch on these in the future.