Monday, April 13, 2009

Vanilla kernel for Debian Lenny

The default Linux kernel of Debian Lenny is of course very stable - but a little outdated. If you're the more adventurous type (or just need support for newer devices) you might like the idea of installing a more recent vanilla kernel? Luckily, Debian makes this task quite easy and you'll end up with real Debian kernel deb-packages! I'll provide a small HOWTO here which you might follow and a small script which will automate everything. I gathered most of the information given here from the official Debian documentation (have a look at this and this).


OK, let's start! First, make sure the following packages are installed:

  • build-essential
  • fakeroot
  • kernel-package
  • libqt3-mt-dev
  • devscripts
  • debhelper
  • dpkg-dev
  • gnupg

If you choose /usr/src as the source directory (which I strongly recommend), add yourself to group 'src' by
su -c "usermod -aG src <USERNAME>"

If you followed this, you don't need to be root to build a new kernel - and that's a good thing. Now import the Kernel Archive signing key to your gpg keyring (see this for details):
gpg --keyserver wwwkeys.pgp.net --recv-keys 0x517D0F0E

We need this to verify the integrity of the source tarball later on. So, download the kernel sources from http://www.kernel.org/ and the corresponding signature file from here to /usr/src. Then check the source tarball integrity by
cd /usr/src
gpg --verify linux-<VERSION>.tar.bz2.sign

and you should see something like this:

gpg: Signature made Mon Oct 9 23:48:38 2000 PDT using DSA key ID 517D0F0E
gpg: Good signature from "Linux Kernel Archives Verification Key <ftpadmin@kernel.org>"


If not, something went wrong during your download and you have to try again.

We may now unpack the source code (still within /usr/src):
tar xvf linux-<VERSION>.tar.bz2

For a start, copy an old .config file to the kernel source tree. You may use the one from /boot of your current kernel. I tend to choose one from sidux' kernel, configured by slh. To do so, download a kernel package from one of sidux' download mirrors and extract the content to a temporary directory, then copy boot/config-<WHATEVER_VERSION> to your kernel source tree as .config. For example, download "linux-image-2.6.29-1.slh.3-sidux-amd64_2.6.29-9_amd64.deb" and extract the content by:
cd /tmp
mkdir test
dpkg-deb -x /path/to/linux-image-2.6.29-1.slh.3-sidux-amd64_2.6.29-9_amd64.deb test
cd test/boot
cp .config-2.6.29-1.slh.3-sidux-amd64 /usr/src/linux-<VERSION>/.config
rm -r test
rm /path/to/linux-image-2.6.29-1.slh.3-sidux-amd64_2.6.29-9_amd64.deb

The sidux kernel tends to have almost every module compiled, so you might want to deselect some modules which you don't need later on.

Now proceed with configuring and compiling the new kernel:
cd /usr/src/linux-<VERSION>
make oldconfig
make xconfig

You may tweak your kernel now. I'd recommend to change at least the processor type to match your actual CPU, i.e., "Generic x86_64" to "Athlon64" in my case. Now save your configuration and exit xconfig. (EDIT: optionally run 'make-kpkg clean', if you build multiple kernel packages with different configuration.) Let's build the kernel!
fakeroot make-kpkg --initrd --append_to_version -<YOUR_CHOICE> --revision=<YOUR_REVISION> kernel_image

The "--append_to_version" switch may be used to append the revision and architecture/CPU type to the kernel name. For example "custom.1-amd64-k8". Replacing <YOUR_REVISION> with a revision number and kernel version is recommended, i.e. "2.6.29.1~custom.1". Both fields may contain alphanumerics and "~ + .", <YOUR_CHOICE> may also contain "-" (without the quotation marks!).

Whatever you choose, the former step may take a while, depending on your machine (it takes almost an hour on my computer - but I tend to compile a lot of modules, even those I might never need!) Once done, I'd recommend running almost the same command again but this time building a kernel header package:
fakeroot make-kpkg --initrd --append_to_version -<YOUR_CHOICE> --revision=<YOUR_REVISION> kernel_headers

You may repeat this with "kernel_doc" and "kernel_source", too. You should end up with deb-packages in /usr/src, which can now be installed by
su -c "dpkg -i /usr/src/<package_name>.deb"

Your grub configuration will be automatically updated, just like with default Debian kernel updates - the Debian way! Once your new kernel is installed, remember to reinstall any proprietary drivers for your new kernel, like Nvidia or Ati.

Reboot to the new kernel and see if everything works... Good luck! ;-)

As I'm kind of lazy, I decided to script this whole procedure, so I don't need to remember everything. You may download this script from here and do with it almost everything you like (I do not know, if a license is required for a small script like this, but if so, it's GPL'd 2, of course!)

This script basically does everything described above automatically. It asks for the desired kernel version, downloads it if required, checks the gpg signature, asks for the revision, starts xconfig and creates the kernel packages. Please open this script first in your favorite editor and have a look at a few configurable variables and maybe adjust them. Please take a look at the README file! You may then run this script (NOT as root) and it *should* work.

Download the script from here:

custom_kernel_0.51.tar.gz (Version 0.51) (md5sum: ddba1f51d33acdbfcc259b7d7b0e3303)
custom_kernel.tar.gz (Version 0.5) (md5sum: aaa81f83d84ccd5b7e9fe14187f302f3)
custom_kernel.tar.gz (Version 0.4) (md5sum: b752d5d7f684d5ffb264b287f6b74472)

Disclaimer: This script is provided "as is", I give no warranty whatsoever.

3 comments:

  1. So glad I found your site! Awesome instructions! I've justed started using Debian and this how-to was a quick way for me to learn how to build my own custom kernel. I look forward to reading some of your other how-to's and articles. Keep up the good work! Thanks!

    ReplyDelete
  2. Thank you very much for your comment, very happy to hear!
    I tend to change distros from time to time (Debian, sidux, Ubuntu and even Fedora), but Debian is definitively my favorite!

    ReplyDelete