Thursday, August 27, 2009

Compiling 2.6.x kernel on Debian Lenny

Building and using a custom kernel will make it very difficult to get support for your system. While it is a learning experience to compile your own kernel, you will not be allowed to file bugs on the custom-built kernel (if you do, they will be Rejected without further explanation).

Reasons for compiling a custom kernel

a.You are a kernel developer.

b.You need the kernel compiled in a special way, that the official kernel is not compiled in (for example, with some experimental feature enabled).

c.You are attempting to debug a problem in the stock Ubuntu kernel for which you have filed or will file a bug report.

d.You have hardware the stock Ubuntu kernel does not support.

Tools needed

apt-get install linux-kernel-devel fakeroot build-essential


Get the kernel source


download latest kernel from kernel.org

Build the Kernel

Move the Downloaded source file to /usr/src

mv linux-source-.tar.bz2 /usr/src

extract the linux-source-.tar.bz2

tar -xvf linux-source-.tar.bz2

cd linux-source-

Now you are in the top directory of a kernel source tree. Before building the kernel, you must configure it. If you wish to re-use the configuration of your currently-running kernel, start with

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

Before you run 'make menuconfig' or 'make xconfig' (which is what the next step tells you to do), make sure you have the necessary packages

sudo apt-get install qt3-dev-tools libqt3-mt-dev # if you plan to use 'make xconfig'
sudo apt-get install libncurses5 libncurses5-dev # if you plan to use 'make menuconfig'
Then, regardless of whether you're re-using existing configuration or starting from scratch:

make menuconfig # or "make xconfig" if you prefer

Now you can compile the kernel and create the packages:

make-kpkg clean # only needed if you want to do a "clean" build
fakeroot make-kpkg --initrd --append-to-version=-some-string-here kernel-image kernel-headers

You can enable parallel make (just like make -j) by defining the CONCURRENCY_LEVEL environment variable. Don't try to add the -j option to make-kpkg. Try 1+number of processor cores, e.g. 3 if you have a dual core processor.

export CONCURRENCY_LEVEL=3
The *.deb packages will be created in the parent directory of your Linux source directory (in this example, it would be placed in ~/src because our Linux source directory is ~/src/linux-source-).

Install the new kernel

Now that you've told initramfs-tools what modules it should include and the build is complete, you can install the generated debs using dpkg:

sudo dpkg -i linux-image-2.6.20-16-2be-k7_2.6.20-16_i386.deb
sudo dpkg -i linux-headers-2.6.20-16-2be-k7_2.6.20-16_i386.deb

Speeding Up The Build

Use distcc and, if you're rebuilding often, ccache. A good overview of using distcc on a debian-based system is available at  http://myrddin.org/howto/using-distcc-with-debian. If you have AMD64 machines available on your local net, they can still participate in building 32-bit code; distcc seems to handle that automatically. However, with distcc taking over all compiles by default, you will need to set HOSTCC so that when kernel builds want to use the compiler on the host itself, they don't end up distributing jobs to the 64-bit server. If you fail to do that, you'll get link-compatibility failures between 64-bit and 32-bit code. My make-kpkg command, with /usr/lib/ccache at the head of my $PATH, looks like:

MAKEFLAGS="HOSTCC=/usr/bin/gcc CCACHE_PREFIX=distcc" make-kpkg --rootcmd fakeroot --initrd --append-to-version=-suspend2 kernel-image kernel-headers kernel-source

No comments: