Sunday, December 26, 2010

Build and Compile your own Linux Kernel


Kernel is the core of the operating system .Linux Kernel mainly comprises of some million lines of code written in C language. It carries out various tasks which include scheduling, Multitasking, Multiprogramming, access to a shared memory and much more. Linux Kernel is Open source and thus the code is available to the public freely. It is this feature which makes Open Source products superior than the closed source ones. With Open Source Products people are given the freedom to modify or remove the parts of the Kernel program and Build and Compile their own  Kernel .




Before starting of our journey to the Kernel compilation and Building, we need to have the Latest stable release of the Linux kernel.To download the Latest Linux kernel ,Visit the website www.kernel.org .
Download the stable release which is 2.6.36.2 as of this date. There are even available release candidate of the upcoming versions of Linux kernel (2.6.37.rc7). 

After downloading the Linux kernel into your home folder, the downloaded file will be in .tar.bz2 format.Extract the file using tar command as

# tar xvfz linux-2.6.36.2.tar.bz2

or by right clicking the file and choosing the option "Extract here" .
After extracting the .tar.bz2 file ,the folder containing various Linux kernel program files will be available.Modify the Linux kernel programs as per your wish.After Modifications we are ready to compile and Build the Linux Kernel.

Switch to super user mode by issuing "su" command before proceeding further .
Compilation and Building of your own  Linux Kernel can be achieved by following these simple steps.
  • Copy the  extracted Linux kernel folder into /usr/src using the following command.Copying of the files into the root directory may be prohibited . To get the write permission to the root use chmod to change the permission settings . Enter the directory as cd /usr and issue the following command chmod -R 777 *  forgranting the write permissions.
           #  cp linux-2.6.36.2  /usr/src
  • Compilation of the Linux kernel needs gcc compiler, to install gcc if not available issue the following command at the terminal :
           #  apt-get install gcc
  • Download and install the libncurses5-dev library which is used for graphical display during configuration phase of the Linux kernel compilation.
          # apt-get install libncurses5-dev
  • After achieving all the pre-compilation steps mentioned above,we are now ready to compile the kernel code. Move into the /usr/src/linux-2.6.36.2 directory by issuing the following command.
         # cd /usr/src/linux-2.6.36.2
  • Issue any one of the following command for getting the configuration menu .
         # make menuconfig
         # make xconfig
         # make gconfig

          Configure the different options to be included during the compilation here.
  • To start compiling and create a compressed image of the linux kernel issue the command : 
         # make

         To start compiling the kernel modules issue the command
          
         # make modules
  • Now install the Kernel modules as
          # make modules_install

        To install the whole of the Linux Kernel compiled issue the command :
         
         # make install
  • It will install three files into your /boot directory which include as well as modified configuration file to your grub folder. Move into the /boot directory as  cd /boot
         System.map-2.6.36.2
         Config-2.6.36.2
         vmlinuz-2.6.36.2
  • The next step is to create and initrd image in the /boot directory using mkinitrd or mkinitramfs command as follows.
         # mkinitrd -o initrd.img-2.6.36.2 2.6.36.2
                        OR
         # mkinitramfs -o initrd.img-2.6.36.2 2.6.36.2

         initrd contains device drivers used to load your operating system later.
  • In this step we need to make an entry into the grub configuration file located in /boot directory.
         # gedit grub/menu.lst

        Make the following entry into the menu.lst configuration file.


    title             Debian GNU/Linux, kernel 2.6.36.2 Default
    root             (hd0,0)
    kernel         /boot/vmlinuz root=/dev/hdb1 ro
    initrd           /boot/initrd.img-2.6.26.2
    savedefault
    boot

       Save it and Exit.
       To automatically update the grub for new lines in the kernel issue the command
        # update-grub
  • Reboot your computer . 
     On Reboot select the option with the version of the Linux Kernel you compiled and here you are with your own Linux kernel which works just the way you configured.

 Any problems or Suggestions do feel free to leave a comment or mail me at rajeev.open.1991@gmail.com

3 comments:

Bharat Balegeer said...

Nice Post.
It would have been nice if you told us which options have to be configured after executing make menuconfig or make xconfig or make gconfig
Can you post a sample config for particular processor and wifi card?
Or maybe post the ideal config for ur laptop?
Did u see any improvements in the speed after compiling the kernel?

TECH GEEK said...

Hi,
As of Sample Configuration, It is as per your wish as to what are the modules you want to load and according to your convenience.The Menu configuration is GUI and easy to read and understand .
As of Ideal Configuration,there is no ideal configuration as such It is user specific.
And as of to my Laptop I installed for example say the drivers pertaining to Intel only and omitted all the drivers which were for AMD and other processors.
As of the speed improvement there is no drastic speed improvement after compilation and building of the new kernel,But it definitely fixed many of the bugs which were found in the earlier versions of the kernel.Bingo hope you got the answers,if still unsatisfied do feel free to mail me at rajeev.open.1991@gmail.com or leave a comment.

TECH GEEK said...

During make menuconfig command,the system might prompt that there is no configuration file in the folder linux-2.6.36.2
What you have to do is go to /boot
There you will be finding the configuration file for the previous kernel just copy it to the folder /usr/src/linux-2.6.36.2 and rename it as .config and then try to run make menuconfig.
After that you will asked for loading or unloading different modules .. continue it and then issue the make command and then everything follows as per the post....