another voice in the babble on the net

how to build a ubuntu or debian kernel package – the debian way

This note outlines the steps necessary for building a debian or ubuntu kernel into .deb kernel packages suitable for installation. I wrote it largely as an aide-memoire for myself, but I post it here because it may be useful to others. There are a variety of “kernel howtos” on the net, but many of them appear to be out of date or give conflicting advice. In particular, the “make, make modules_install, make install” type of installation is deprecated and mkinitrd is no longer used. This can be confusing – hence this note. The closest I came to finding a simple kernel build “howto” was at the Ubuntu community kernel page on which this note is based.

Assumptions:

1. You are building a kernel for a debian based distro or any of the distros based on ubuntu.

2. You are building the kernel on a ubuntu system of 8.04 or later vintage.

Prerequisites:

You will need the following additional packages installed:

linux-kernel-devel, fakeroot, build-essential

and for make xconfig (recommended)

qt3-dev-tools libqt3-mt-dev

or for make menuconfig (the curses based method)

libncurss5 libncurses5-dev

You can install the packages with synaptic, or from the command line with:

sudo aptitude install packagename

or the old fashioned way

sudo apt-get install packagename

Getting the source:

Depending upon how bleeding edge (or non standard) you wish to be you may get the source code in one of two main ways. The preferred option will be to use the latest source from your distro’s tree, the second option will be to use a pristine source from one of the mirrors of kernel.org.

– from your distro,

sudo aptitude install linux-source

will install the zipped source archive as /usr/src/linux-source-VERSION-tar.bz2

– from the kernel.org mirror, download the version of your choice to the direrctory of your choice.

Unpacking the source:

I prefer to build in /usr/local/src/KERNELNAME, so first ensure that /usr/local/src exists and then create the necessary subdirectory with permissions which allow you as an unprivileged user to build the kernel (do NOT build as root).

Now unpack the new kernel source archive in the in the new subdirectory

(where /path/to/linux/source is the location of the source file and linux-VERSION-tar.bz2 (or linux-source-VERSION-tar.bz2) is the actual name of the archive).

This will give you a new subdirectory of /usr/local/src/KERNELNAME called linux-VERSION-NUM or linux-source-VERSION-NUM (where VERSION-NUM is the number of the kernel you have chosen – e.g. 2.6.29.1

cd to the new directory.

Ensure the sources are clean:

This won’t be necessary on the first build, but it is a good idea to ensure that no old object files or configurations lurk around to confuse you so get into the habit of cleaning the source tree as follows

make mrproper
make-kpkg clean

This WILL destroy any configuration file you may have so if you wish to reuse an old .config, then get into the habit of copying it to the directory above with a date or version identifier as part of the name.

e.g. cp .config ../config-test-version-0.1

you can then copy it back to .config as the basis of your next build (and you will be building a series…….)

Pick your starting configuration:

Install a .config in your build tree. You can use the one distributed with your current kernel (generally a good starting point) by copying the file from the boot directory as follows:

cp /boot/config-`uname -r` .config

or you can use a config from any other source you trust (or one you saved earlier as above).

Now configure your kernel:

I prefer to use xconfig (but the curses based menuconfig is also useful) because it gives a nice user interface including a lot of descriptive help. So type:

make xconfig

and then start configuring your kernel. When you are happy with the results, save the config, exit make xconfig and make a copy of the .config just generated (as described above).

Make the kernel (and a pot of tea):

Now you can make the kernel, its modules, and package the lot up into a .deb package file in one easy command as follows:

“your-version-identifier” should be some string (without quotes) you want tagged on to the end of the linux-kernel name (e.g. -baldric-0.8) in order to identify your versions. “fakeroot” literally fakes root privileges in the build process (I suggest you read “man fakeroot” so that you understand the implications of building packages in a fake root environment). The –initrd switch tells make-kpkg to build the initrd.img boot image whilst the –append-to-version switch allows you to give a build time version stamp to the new kernel image. So for example, if the kernel you are building is 2.6.29.1, passing the string “-baldric-0.1″ will build a kernel image called “linux-image-2.6.29.1-baldric-0.1″

Building a kernel on a newish core 2 duo machine with 4 gig of RAM takes around 20-25 minutes. YMMV. So you do have time to make a pot of tea as well. If you do have a multi core processor, then you can parallelise the make (and speed it up) by setting the the environment variable “CONCURRENCY_LEVEL” to one more than the number of cores you have. So on a core 2 duo:

export CONCURRENCY_LEVEL=3

in the shell before you start the make will speed things up.

Installing the new kernel (and headers if required):

On completion of the above build process, make-kpkg will have created the new packages in the parent directory of your build directory – in this case /usr/local/src/KERNELNAME. You can install the new kenel image, modules, initrd image and modify the grub installation in one command:

If all goes well, on reboot you will have a shiny new kernel installed (check it with uname -r). If something breaks (and it might, it might) then simply reboot and select your earlier known working kernel and remove the offending kernel with:

sudo dpkg -r linux-image-YOUR_VERSION_DETAILS

Do NOT attempt to remove a kernel image which is actually running. There be dragons there.

Enjoy.

Permanent link to this article: http://baldric.net/how-to-build-a-ubuntu-or-debian-kernel-package-the-debian-way/