Friday, 12 December 2014

How to compile Linux kernel in Debian?

There are some ways to compile Linux kernel, but why don't do it in debian-style, when using Debian Linux? There are some advantages of this strategy – the structure of a system is safe, the risk of an unsuccesful reboot is decreased and – of course – you get the standalone package which may be used on the similar linux boxes.

So, stop talking about it and let's do it. First, you have to download the latest version of Linux kernel. The easiest way is to download it from the Debian repository, but it is not the best solution in every case. In general, kernel sources from this repository are a little bit outdated.

But, for the most cases, these sources are good enough. The good thing is that they are compatible, as much as possible, with Debian kernel config, so you don't have to make many modifications. So, let's download the sources:

cd /usr/src
apt-get install kernel-sources

Now, unpack them:

tar -jxvf linux-source-2.6.32.tar.bz2
cd linux-source-2.6.32

In this excample, we work on Squeeze and 2.6.32 kernel. If you use another kernel version, you should modify names and paths.

Than, copy the stock Debian config from /boot directory:

cp /boot/config-2.6.32-5-amd64 /usr/src/linux-source/.config

Now, generate the new config, based on the old one. You may be asked for decision about the options, which were added in the new kernel version.

make oldconfig

Than, you can modify some other options, if you want. The easiest way to do it is using "menuconfig":

make menuconfig

It is not mandatory, but I recommend to choose, at least, a most suitable CPU type to optimize your kernel for the specific hardware. But remember, that you couldn't run such kernel on the incompatible CPUs. CPU type can be selected in the Procesor type and features/Procesor family submenu.

Now it's time to compile your new kernel. Type:

make-kpkg linux-image linux-headers --initrd

and wait, while kernel is being compiled. Than, just install the packages:

cd ..
dpkg -i linux-image-2.6.32_2.6.32-10.00.Custom_amd64.deb linux-headers-2.6.32_2.6.32-10.00.Custom_amd64.deb

Reboot. That's all.