Blog by Edo Frederix edofrederix@gmail.com RSS

Recompiling the Linux kernel on Debian Etch

March 19, 2008

Abstract

In lots of cases it can be beneficial to run your operating system with a custom kernel - for various reasons. In this post you can read about how to quickly do this in Debian.

First of all, install some packages:

apt-get install wget make build-essential ncurses-dev ibncurses5-dev 
    kernel-package fakeroot bzip2

In order to recompile the linux kernel we must download the most recent version first. We can find it at kernel.org. For convience we shall unpack the source files in /usr/src

tar xjf linux-2.6.21.3.tar.bz2

Next we create a symbolic link pointing to /usr/src/linux

cd /usr/src
ln -s linux-2.6.21.3 linux

Now we’re in the right place to start configuring the kernel. We start off with the commands:

make clean
make mrproper

These commands will clean up the directory so we can make a fresh build. We will save a lot of time if we edit our previous kernel configuration. You can find a copy of it in the /boot/ directory. In particular, we can do:

cp /boot/config-`uname -r` /usr/src/linux/.config

Note that we must copy this file after we’ve done ‘make mrproper’, because this command will delete the file again.

Then we run the menuconfig of the kernel. With this tool you can create a .config file, which specifies all the modules that should or should not be compiled into the kernel. This step is an art in its own. What can we leave behind, and what is required? Run the command:

make menuconfig

Up till know, nothing has really happened. We still have to actually compile and build our new kernel. Run the following commands to do this:

make-kpkg clean
fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers

After the "--append-to-version=" option you can define your own kernel name. This is convenient, as you will have to choose which kernel you load in Grub or Lilo. If the previous two steps were finished correctly (and this can take a while depending on your hardware), there will now be two Debian packages present in the /usr/src directory. The linux-image*.deb file contains the actual kernel. The linux-headers*.deb file needs files which are needed if you want to compile additional kernel modules later on. You can install the kernel easily with the Debian package manager:

dpkg -i linux-image-*.deb
dpkg -i linux-headers-*.deb

Check if there is an initrd in /boot/ agreeing with the proper kernel name. If there isn’t one, perform the following code:

update-initramfs -k kernel-version-custom -c

Also make sure that the initrd is enabled in /boot/grub/menu.lst. This is extremely important, as Grub will decide which Kernel will be loaded. Any mistake here, and your OS will not load. In this case you cal always still load your old kernel, and change Grub.

Now, everything is done. The Grub bootloader configuration file /boot/grub/menu.lst has been modified automatically, and a ramdisk for the new kernel has been created in /boot. So we can reboot into our new kernel.

This article is based on the following two documents: http://howtoforge.com/kernel_compilation_debian_etch and http://www.mulix.org/lectures/tau_linux_workshop/kernel_compilation.pdf