Ted's Linux Kernel Build HOWTO

Ted Felix

Goal

...to get kernel source from kernel.org and build a vanilla kernel.

Get the Source

The latest kernel source can be found at www.kernel.org.

To save bandwidth (both yours and kernel.org's) I recommend downloading the base source and the latest patch. This way for future upgrades, you just need to download the patch, which is generally much smaller than the complete source. As an example, let's say we want to build version 2.6.31.6. We would download the following two files:

linux-2.6.31.tar.bz2
patch-2.6.31.6.bz2

Next we extract the source and apply the patch.

tar -xvf linux-2.6.31.tar.bz2
bzip2 -d patch-2.6.31.6.bz2
cd linux-2.6.31
patch -p1 < ../patch-2.6.31.6

Then give the directory its rightful name.

cd ..
mv linux-2.6.31 linux-2.6.31.6
cd linux-2.6.31.6

Configure the Kernel

Configuring the Linux kernel is probably the most difficult task you'll encounter. However, with a few tricks, it isn't really all that bad.

It helps to have an existing kernel config as a starting point. The best config to start with is one that is either for the same kernel version you are building, or one whose version is as close as possible to the version you are building. Look in /boot for files starting with "config". Also check the online repositories for your distro to see if there is a kernel with a closer version number. In my case, using Debian, I was able to find a 2.6.30 kernel in "backports" that was close to the 2.6.31 that I wanted to build, so I went with it.

Copy the config that you find to the filename ".config" in your kernel source main directory.

cp /boot/config-2.6.30 .config

If you don't do this, the configuration tool will use the config for the currently running kernel, which might be just fine.

"menuconfig" is the preferred kernel configuration tool. It is easy to use, and has minimal dependencies. Run it like this:

make menuconfig

You can now wander through the numerous kernel configuration settings and see if anything grabs your attention. For many of the settings, the help (press "?") will explain what the typical setting is.

Build the Kernel

When you are done looking through the various settings, you're ready to build. First we check the .config file and set up the build.

make oldconfig

Since making a .config is a lot of work, be sure to backup your config as it stands.

cp -p .config ~/config.backup

Now clean up any leftovers from previous builds.

make clean

And build the kernel and the modules.

make all

Install the Kernel

Before you install a kernel, make sure you aren't running the kernel you are about to install. I've not tried it, but I'm betting it won't work. Reboot with one of your distro's standard kernels before proceeding.

Install the new kernel to /boot.

sudo make install

Clear out the old modules. For this example, we'll clear out the modules for 2.6.31.6:

sudo rm -rf /lib/modules/2.6.31.6

Install the new modules to /lib/modules.

sudo make modules_install

Now we need to update the initial ramdisk. Go into the /boot directory and either remove or rename the old initial ramdisk. This step is required or else you will end up with a bad initial ramdisk.

cd /boot
sudo rm initrd.img-2.6.31.6

Build a new initial ramdisk.

sudo mkinitramfs -o initrd.img-2.6.31.6 2.6.31.6

Finally, we must update our bootloader to point to the new kernel and initial ramdisk. For GRUB, take a look at /boot/grub/menu.lst and update it to include the new kernel. For lilo, take a look at /etc/lilo.conf, and make any changes that are needed. Then you must run the "lilo" command before rebooting, even if you make no changes to the lilo.conf. This is because lilo has to figure out where your new kernel is on the hard drive.

Now would be a good time to backup your machine in case something bad happens with the new kernel.

Reboot and try your new kernel.

License

Copyright (C) 2009-2010, Ted Felix

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. See http://www.gnu.org/licenses/fdl.html for the full text of this license.