Saturday, November 14, 2009

How To Compile A Linux Kernel - on ubuntu?

First I prefer to do all the steps here as the root user. So if you haven't already created a root login, you should do so now:
#sudo passwd root

Afterwards, log in as root:
#su

Then update :
#apt-get update

Then we install all needed packages like this:
#apt-get install kernel-package libncurses5-dev fakeroot wget bzip2

Download The Kernel Sources 

Next we download our desired kernel to /usr/src. Go to www.kernel.org and select the kernel you want to install, e.g. linux-2.6.18.1.tar.bz2 (you can find all 2.6 kernels here: http://www.kernel.org/pub/linux/kernel/v2.6/). Then you can download it to /usr/src like this:

#cd /usr/src
#wget http://www.kernel.org/pub/linux/kernel/v2.6/linux-2.6.31.6tar.bz2

 Then we unpack the kernel sources:

#tar xjf linux-2.6.31.6.tar.bz2
#ln -s linux-2.6.31.6 linux
#cd /usr/src/linux

Configure The Kernel

It's a good idea to use the configuration of your current working kernel as a basis for your new kernel. Therefore we copy the existing configuration to /usr/src/linux:

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

Then we run
#make menuconfig

which brings up the kernel configuration menu. Go to Load an Alternate Configuration File and choose .config (which contains the configuration of your current working kernel) as the configuration file:









Then browse through the kernel configuration menu and make your choices. When you are finished and select Exit, answer the following question (Do you wish to save your new kernel configuration?) with Yes:





Build The Kernel

To build the kernel, execute these two commands:
#make-kpkg clean
#fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers

Now be patient, the kernel compilation can take some hours, depending on your kernel configuration and your processor speed.

Install The New Kernel

After the successful kernel build, you can find two .deb packages in the /usr/src directory.
#cd /usr/src
#ls -l

On my test system they were called linux-image-2.6.31.6-custom_2.6.31.6-custom-10.00.Custom_i386.deb (which contains the actual kernel) and linux-headers-2.6.31.6-custom_2.6.31.6-custom-10.00.Custom_i386.deb (which contains files needed if you want to compile additional kernel modules later on). I install them like this:

#dpkg -i linux-image-2.6.31.6-custom_2.6.31.6-custom-10.00.Custom_i386.deb
#dpkg -i linux-headers-2.6.31.6-custom_2.6.31.6-custom-10.00.Custom_i386.deb


That's it. You can check /boot/grub/menu.lst now, you should find two stanzas for your new kernel there:
#vi /boot/grub/menu.lst
The stanzas that were added on my test system see on this file.

Now reboot the system:
#shutdown -r now

If everything goes well, it should come up with the new kernel. You can check if it's really using your new kernel by running
#uname -r

This should display something like
2.6.31.6-custom
If the system doesn't start, restart it, and when you see this:


No comments:

Post a Comment