Monday, July 23, 2012

Begin with Compiling Linux Kernel


I was doing some work and got few issues. I started looking here and there in books, in posts. Then I realized to write a post for some other guy who would be in same situation as I am or I was.

This guide is for those who are new to linux kernel and want to compile kernel for the 1st time. You might get some deviation with coming new versions but once you get what you are doing you can do in newer version too. Well, you can google, why do you want to compile kernel.

Below steps are tested with kernel v2.6.38.2. I am suggesting you to follow each step sincearly otherwise most likely you will get a very common FATAL ERROR: /lib/modules/<kernel>/modules.dep not found. Don't blame me if you won't get success. Most of the people don't get sucess at 1st attempt. And your system configuration, current kernel, gcc, etc.. these all matters when you start.

-Remember that you are compiling kernel, not cross compiling.
-Remember, don't touch your old working kernel image, its Config, System.map, Modules, entry at grub.cfg. Your old kernel will save you if your new kernel dosn't boot.

1) Download kernel source source from kernel.org, throughout the discussion I will refer my kernel version and you should stick to your version in respective places.
$ linux-2.6.38.2.tar.bz2

2) untar the kernel source
$ tar -xvf linux-2.6.38.2.tar.bz2
Check README.txt file from your parent kernel directory. You will find which gcc version you should use and verify it in your system, $ gcc -v

3) If you had already tried compiling kernel, you should clean previosly generated objects and bundels.
$ make clean

4) You have to make kernel configuration. There are number of ways to do it which gives different UI fashion but in the end, you will be generation .config file. Or best way is to copy .config file which was used in your working kernel.
 $make menuconfig
or
 $make xconfig
Or any other way to make kernel configureation, ultimately you are doing same thing with your interest. And then make your choise from various options.
    After you are done, you will see .config file created. This .config file will be present in your parent kernel source tree. Check with command if it get listed out. $ls -a|grep .config
You can directly execute below instruction to copy config of your current working kernel.
$ cp /boot/config-`uname -r` ./.config

5) Now you are going to create kernel image, bzImage. This step may take some time, so you can have your lunch or dinner in meantime. Hopefully you wont get error inbetween ;).
  $make -j4

Here -j4 is optional, which tells compiler to use both the cores of your system if your system is dual core. Usually kernel has two thread so in -jn, n=2x(number of cores) so for quad core, it is 8.

6) If you are using x86 processor, afer above step, you will see bzImage as <source parent>/arch/x86/bzImage. Do below steps, hopefully you understand what you are doing here. You may need root permission. Be very careful about naming version. You had downloaded linux version "2.6.38.2". Most of the time people do mistake at this step.

cp ./arch/x86/bzImage /boot/vmlinuz-2.6.38.2
cp ./.config /boot config-2.6.38.2
cp ./System.map  System.map-2.6.38.2

7) create init ramdisk image.
   initramfs -k -o ./initrd.img-2.6.38.2 2.6.38.2

8) Copy initrdimage to boot folder.

   cp ./initrd.img-2.6.38.2 /boot/

9)  And register your kernel to your bootloader. In may case, GRUB
   update-grub
   you will see some action happening, like grub.cfg...
   Now check grub.cfg
   vi /boot/grub/grub.cfg
----------------------------------------------------------------------------------------------------
   menuentry 'Ubuntu, with Linux 2.6.38.2' --class ubuntu --class gnu-linux --class gnu --class os {
        recordfail
        insmod part_msdos
        insmod ext2
        set root='(hd0,msdos6)'
        search --no-floppy --fs-uuid --set 2fde25ee-ea28-4acf-96d7-e972527cd820
        linux   /boot/vmlinuz-2.6.38.2 root=UUID=2fde25ee-ea28-4acf-96d7-e972527cd820 ro   quiet splash
        initrd  /boot/initrd.img-2.6.38.2
   }
----------------------------------------------------------------------------------------------------
 If you wont find proper entry at "initrd" or "root". Most likely you have done somthing wrong, possibly your initrd.img is not correct. Google and correct.
 I would suggest you to compare above like entry with your working kernel version, and try to understand what that is. It will help you to reach near to the problem.

 10) now check at /lib/modules, you will find kernel modules of working kernel. Now this is the to make your kernel module and install.
  make modules
  make modules_install
 If you are successful, you will find entry of your kernel module at /lib/modules

 11) If you have done all steps properly, you might have built and installed your kernel/kernel modules. Comeon reboot your system and boot with your new kernel.

All the best. Many of the people don't get success at 1st attempt, unless he is luckey.

No comments:

Post a Comment