Tuesday, 26 April 2011

Compiling your first Linux Kernel


I'm going to explain you on how to compile the linux kernel and get it running on your system. First time experience of Kernel compilation is always an exciting one. Here, I'm sharing on how I compiled my first kernel (and this.. I did it long time ago. But to help you folks, I'm starting afresh so that you'll understand better from a beginners point of view) :-) Sometimes, beginners like you and me tend to get distracted, feel out of place when compiling the kernel. Many feel that we are just not so much of a geek to handle the Kernel level development stuff. Also, as there is simply many fundamental processes to learn before the actual compilation, it may even put you off. But, don't panic. Compilation of Linux Kernel can be done by beginners like you and me too. Please follow this instruction set carefully and if you have any doubts, do let me know!

------

First things First - Checklist :
1. You should have a linux system. In my case I use, Ubuntu 10.10
2. Make sure you've free space well above 1.5 GB to make sure you don't end up with less hard disk space in the middle of the process

------

1. Getting the linux Kernel Source
The first steps in compiling the source is obtaining the source. It's always a wise decision to opt for the latest stable kernel. Hence, go to www.kernel.org and download the latest stable kernel. It should be weighing around ~71 MB.

2. Extracting the Source
The source comes as a tar.bz2 file. Extract it to a proper location. I've extracted the linux kernel in the following path. /home/sanjeev/development/

3. Exploring the Kernel Source (Optional)
It's such a wonderful feel to check out the Kernel source. Being one of the most popular and powerful operating system, seeing its source code is such a bliss. If you are a beginner, it'll make you go ga-ga over it! Just check out the basic directory structure followed, open couple of files, try following the code.

4. Making the Kernel
This is one of the most important steps in the whole process, and this is what that makes compiling the Linux kernel such an easy process. Its whole a lot of complicated stuff, which is available for us in a very simplified manner. Being beginner, lets not get into the depth of which modules we need and which we don't. There is an easy way to just make the default modules only.
So, open terminal and use cd command to navigate to the directory where you extracted Linux source, in my case, it is cd /home/sanjeev/linux-2.6.38.4
now, type make defconfig in the terminal. This command is to create a configuration based on the defaults for your architecture. Configuring this way is the most easiest way and is recommended for beginners. During this configuration process, there are many modules in the kernel which are turned ON and OFF so that you compile just what is required for your machine. If you want to play some more, try make gconfig. But while writing this, I used make defconfig only!
Once the configuration is set in place, we need to Make the kernel. Make of Kernel is so powerful that we can even have multiple threads doing the job for us. This significantly reduces the time of build. You'll have your two cores in a dual core to run in 100% CPU utilization. So, you may want to give the command,
make -jN where N stands for the number of threads you want to spawn. In a dual core case, just type make -j4
This is a time consuming process. Go grab some nap.
After few hours, you'll get the message, Build Successful and a path information about where you can find the Kernel Image.

5. Installing the Kernel
We are almost done with 80% of our job, this step will be the last mile. The general path, where you can find the Kernel image is, /arch/i386/boot/bzImage, in my case, I found it in, /arch/x86/boot/bzImage.
Copy this Image file into your /boot directory. You can achieve this by using 
cp /arch/i386/boot/bzImage /boot

You may want to rename the Kernel image to a different name. So, try mv /boot/bzImage /boot/vmlinuz-

Now, we need to get our Grub find our latest Kernel. So, type in the Terminal the following command, update-grub and then type grub-mkconfig.
The first command, helps you find your kernel and add it to the template files that generate the grub.cfg file. The next command helps you generate the new grub.cfg file with your Kernel updated in it.
Now, restart the system and let me know your experience! :-)
Hope you found it a real pleasant way to build your first Kernel!

2 comments:

Vignesh said...

Cool.. Will try it out and share my experience soon! :)

Thanks for this post!

Shailesh said...

I build linux kernel already back in 2009 for solving wireless and sound problem
but how to modify the existing linux kernel? Write one blog on that.