Unixbhaskar's Blog

Wednesday, February 17, 2010

Compiling custom kernel for different GNU/Linux distributions

Compiling kernel is not for the faint heart's toy. One have to be ready for the pitfall they face when things are not working as they expected from a preconceived notion in their mind.

But having said that I am not trying you to get scared of it or keep you away from to get the best out of your own GNU/Linux distro as per your liking.

In this article I will step you through the process that how can build your own kernel step by step and installed it ..later use it .

I do not issue any guarantee that this will work for you.

I have pick up three distribution for this purpose; the peoples choice comes first as Ubuntu. Then a bit mid way distribution for geek and normal people ;called openSUSE. And finally the my favorite and source based one called Gentoo.

Now a very important point :why would anyone want to compile a kernel??(I have had the experience to hear from newbie administrator and GNU/Linux enthusitst that they want to compile the kernel; because they want to show the world that they know GNU/Linux more then anybody else) ,which is an absolute bogus and rubbish idea ...only expect from those who jump from different stream or just want to be highlighted by saying " I am a Linux guy".

Compiling kernel should be necessary if and only if the below thing one wants:

  • The stock kernel doesn't provide the feature that they want.
  • You should know what you are trying to do with the kernel.
  • If you are an avid kernel developer looking inside the kernel offering.
  • If you want fix some kernel related problem by enabling a debug feature in the kernel.
  • Some specific hardware feature want to activate so the underlying hardware react properly.
  • you should have the capability or knowledge iff something goes wrong after compilation of the kernel can bring back the system in previous pristine mode to function it properly.

Compiling kernel in Ubuntu way( Intrepid, Jaunty, and Karmic)

Step 1:

Get the kernel source from the .Whatever the version you want to compile;stable version would be a better choice.

Step 2:
open a terminal and fire the below command

sudo apt-get install fakeroot kernel-wedge build-essential makedumpfile kernel-package

Step 3:

The linux-kernel-devel does not exists in intrepid,jaunty and newer version.For that reason to compile the kernel also need to run:

sudo apt-get build-dep linux

Step 4:

You might get the kernel source in ubuntu way like this:( for Hardy,intrepid Ibex,Jaunty)
sudo apt-get build-dep --no-install-recommends --only-source linux

For Kermic Koala:

sudo apt-get build-dep --no-install-recommends linux-image-$(uname -r)
apt-get source linux-image-$(uname -r)

Step 5:

Now you have to modify the source config file of the downloaded lets find the that file
Ubuntu store this file /debian/config/ARCH or for newer release it is in debian.master/config/ARCH
You can get a snapshot of that particular dir like this

ls -l debian/config/i386/

total 108
-rw-r--r-- 1 root src 73962 2007-08-13 01:29 config
-rw-r--r-- 1 root root 1369 2007-08-13 01:29 config.386
-rw-r--r-- 1 root root 1330 2007-08-13 01:29 config.generic
-rw-r--r-- 1 root root 1395 2007-08-13 01:29 config.server
-rw-r--r-- 1 root root 1756 2007-08-13 01:29 config.server-bigiron
-rw-r--r-- 1 root root 8 2007-08-13 01:25 lowlatency
-rw-r--r-- 1 root root 194 2007-08-13 01:25 vars.386

If you don't find the file then search in /boot section of tree.

Step 6:

Now change the config file according to your choice ...once done then please do the following
debian/rules updateconfigs

yes that is a command you have to fire once you are done with the config file.That command reside in debian/scripts/misc

Step 7:

Now you have run the below command in the source dir of the downloaded kernel:
You have to take the current configuration file as the basis of the new one. So doing so..

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

Step 8:

make menuconfig

It will open up an tui window full of options to choose from.Once you finish the your selective session with that

dialog window( you have manuvaour to with TAB key to move and spacebar to select).
Then go to bottom of that window and select " Load an alternate configuration file" opention and enter
Then press "ok" to save the config file.

Step 9:

Once done the previous steps you have your own custom config file for the kernel. Now time to build the kernel
Do it in the terminal:

$ make-kpkg clean

$ fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers

Now here you need bit of explanation of the "--append-to-version" thing..which can be selected from the

previous step when the window came select it from "general section" (top of the window option) and
it should start with a "-" minus signto separate form the kernel version number.
Now please help yourself to get a cup of coffee and will take some to compile the kernel to build .deb file for your system.

Step 10:

After the sucessfull build you can find the .deb file in /usr/src
there should be two file ;one for linux-image and another for linux-header..install both of them
dpkg -i linux-image-
dpkg -i linux-headers-

Step 11:

You have to put an entry in the grub(the bootloader) conf file to get it booted.
You can find more detailed description of it here : and here:

Compiling the kernel openSUSE way:

Step 1:

Get the kernel source from the ; whatever the version you want to compile;stable version would be a better choice.

Step 2:

Now get the required package for kernel build
yast -i ncurses-devel
This package will help to get the "make menuconfig" dialog to appear in terminal.

Step 3:

Now you need to get some backup and modify the few tool to get kernel compile properly.Otherwise it
won't allow us to install the kernel rpm in the system.
cp /usr/lib/rpm/find-provides.ksyms /usr/lib/rpm/find-provides.ksyms_orig
cp /usr/lib/rpm/find-requires.ksyms /usr/lib/rpm/find-requires.ksyms_orig
cp /usr/lib/rpm/find-supplements.ksyms /usr/lib/rpm/find-supplements.ksyms_orig
Then we open each of these scripts and replace kernel-*) is_kernel_package=1 ;; with kernel*) is_kernel_package=1 ;;:

Step 4:

Now go to the kernel source dir(where you unpack the kernel source tree) make a symlink to the "linux"
dir with the unpacked kernel source tree.
ln -s linux- linux

Step 5:

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

step 6:

make menuconfig

Step 7:

Select "Load an alternate configuration file" and save by pressing ok.

Step 8:

It is important to have different version for your custom kernel ..for that you need to the "general setup" option of and select the second entry started as default.
You must start with a "-" with your choice of name and save it when promted.

Step 9:

Now build the rpm for the system:
make rpm
Please be patience will take some time to compile the rpm( mostly about the choice you made)

Step 10:

You can view the source rpms here :
ls -la /usr/src/packages/SRPMS/

Step 11:

For the actual kernel rpm look in here:
ls -al /usr/src/packages/RPMS/i386

Step 12:

Get into the above dir and install the kernel;
rpm -ivh new-customized-kernel.rpm

Step 13:

Now create the initrd image to boot the system for the new kernel. For that

Step 14:

Now configure the boot loader grub to boot new kernel.
For that edit the /boot/grub/menu.lst and put a stanza like below;
title new-customized-kernel
root (hd0,0) ----> depend where you load the boot loader
kernel path_to_the_kernel_img_file(usually /boot/
initrd path_to_the_initrd_img_file (usually in /boot
Save it and reboot and enjoy the new kernel.

Compiling the kernel in Gentoo way:

Step 1:

You can get he kernel source two ways in Gentoo. First as normal to get the .bzip from the
Second is the gentoo way
sudo emerge -av gentoo-sources

Step 2:

Go to the /usr/src/ dir ,where the new kernel reside and make a link to the linux dir with the new
kernel unpacked dir.
ln -s linux-new-source linux

Step 3:

Then as usual
make menuconfig
select the options you need.You can stright way copy the .config file from the previous kernel source tree to the new one.
cp -v old-kernel-dir/.config new-kernel-dir/

Step -4:

You can compile kernel in two different way in Gentoo.
First is the tradition way and elaborate way ;like in above menthod (make menuconfig;make;make module_install, make install) or
Gentoo provide an easy way to hide the scrolling kernle compilation msg on the screen by using a tool called "genkernel",which essentially does the same thing.
For using the genkernel method need to install it first like this:
sudo emerge -av genkernel
once install ... you are ready to use it.

Step 5:

Go to the specified kernel source dir and fire:
genkernel # without any argument
genkernel --save-config --install all

Now a bot of explanation of second command: the option "--save-config" saves a kenerl config file in

/etc/kenerl/kernel-version.config ,which you can use later and view.
Next option "--install" instruct the genkernel to install the kernel in the system .
Then option "all"...which define all the steps require to compile the kernel i.e make ,make module_install
make bzImage install make initrd or ramfs images
All will be done in the scrolling out put on the terminal.If you are really curious about the ongoing thing
You can have a look at the /var/log/genkernel.log file for the compilation process.
It will create less noise but it will take long .

Hope this guide will help.

1 comment:

  1. Also while the kernel(Debian/Ubuntu) is building you'll need to move the files in '/whateverlinuxsource/include/generated' to /whateverlinuxsource/include/linux' otherwise the kernel image and headers will fail with new *.33-rc kernels and make-kpkg.

    If you're looking for a good kernel building script go here..