Tuesday, February 16, 2010

Why Should You Compile Your Kernel and How

If you are running Mepis, Mint, PCLinuxOS or other desktop oriented distribution, and your installation detects all your devices and works perfectly fine, you don't need to waste your time compiling you own kernel. That's what I had mentioned earlier in this blog.

But sometimes you may require to add/remove some features, drivers or change some settings, then you should go for compiling your own. Take my case for example. A week ago, I had to setup a home desktop for a total noob (doesn't know L of Linux). He was just interested in running a system that works after some initial configuration. He is not supposed to do updates, any sort of fixing and he is connected to web on his desktop through broadband. After much thought I settled on installing a customized Debian Lenny that would just work without any hitch or instability.

And I set on to recompile a kernel totally meant for boosting desktop performance. I prepared a .config file shedding every bit of unnecessary components and some setting changes. They are:

1. Changed kernel to "Low Latency Preemptible for Desktop" (Debian kernel is optimized for server, you can't change it even if you choose a desktop install), it's a must for desktop kernels
2. Changed Processor type to "Core2Duo or Newer" (my friend has a core2 machine)
3. Removed support for almost hundreds of drivers for old/legacy and modern devices, except for those which are built into that desktop
4. Removed support for unnecessary block-devices, esoteric filesystems, etc.
5. Changed memory settings
6. Removed ipv6 support
7. Tweaked CPU scaling features

How Did I Compile it

First I installed the following packages

#apt-get install kernel-package ncurses-dev bzip2 module-init-tools initramfs-tools procps fakeroot zlib1g-dev libgtk2.0-dev libglib2.0-dev libglade2-dev

Checked the kernel version using the following command

#uname -r

Downloaded linux source matching the version of installed kernel from Debian repository. The reason behind it is that you can modify the .config file and the recompiled kernel will integrate well with the existing set of userspace packages and applications. Of course you can put a newer or older kernel source for recompiling. But then, you should be more cautious while creating a .config file through "make menuconfig".

Whether newer or older kernel source, try to pull that from your distribution repository. It's will suit better and it may bear some patches specific to your distribution.

Copied source to newly created folder

#cp /usr/src/linux-source-2.6.26.tar.bz2 ~/newkernel/

Extracted the source and moved to the sources directory

#tar xjf linux-source-2.6.26.tar.bz2
#cd linux-source-2.6.26

Then copied a working template for the kernel .config to this directory. You should do this unless you want to drastically change the kernel .config file.

#cp /boot/config-$(uname -r) ./.config

Checked processor (it'll be useful in choosing cpu type and settings)

#cat /proc/cpuinfo

Configured kernel options (Don't change settings aggressively if you don't know what you are doing)

#make menuconfig

Now cleaned the bed for real work

#make-kpkg clean

Then started the real compilation

#make-kpkg --rootcmd fakeroot --initrd --revision=dev.1 kernel_image kernel_headers

Note:
1.You can change “dev.1” with yours.
2.You may skip building “kernel_headers” if you don't need to build kernel modules


Then moved to the directory where I had freshly cooked kernel images.

#cd ..

Installed .deb package using the following command

#dpkg -i *.deb

After installation, I checked the menu.lst and /boot directory to ensure the initrd, vmlinuz files are properly created and linked in the grub menu.

Benefits of this compilation:
1. My custom kernel weighed just 8MB.
2. It takes 8-10sec less to boot
3. The desktop feels snappier than the default Lenny Desktop install
4. It takes much less memory and resources.
5. The boot screen is quite neat even in verbose mode.

The above procedure works well on Debian Lenny and Mepis 8. You might have to google the web for more appropriate help relevant to your particular distribution.

2 comments:

Anonymous said...

Good stuff. A couples notes though. :)

You only need to specify 'make-kpkg clean' if you are rebuilding a kernel.

You forgot 'modules_image'.
eg. make-kpkg --initrd --append-to-version=-candela kernel_image kernel_headers modules_image

Also while the kernel 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.

If you're looking for a good kernel building script go here..http://ubuntuforums.org/showpost.php?p=8013356&postcount=1326

==

manmath sahu said...

Thanks for your suggestion. But I have removed module_images deliberately. Because if one a compiling a kernel he/she will build everything into it.

As for me. I just build a kernel_image including everything into it and forget the rest.

How about this