Compiling Linux 2.6 kernel from source


In my last post , we had seen how to upgrade the Linux kernel  using YUM . We had seen how cool it is to upgrade the kernel by executing only one step, if we use YUM . In this article , we will see how to upgrade the Linux kernel by compiling the kernel from source . As said earlier , upgrading the Linux Kernel by compiling from source is always a difficult task if we are doing for the first time . But if we follow the steps carefully and select the proper options , then we can successfully compile the kernel and it is always a geeky feeling after that . Lets now start with the task .

Before we start with the compilation ,we first need to download the source . Get the latest stable kernel source from www.kernel.org. As of writing , the latest stable kernel version is 2.6.31.6 

Compiling  Linux Kernel 2.6 

1.  sudo tar jxvf linux-2.6.31.6.tar.bz2  -C /usr/src/kernels/

Initially extract the kernel package under /usr/src/kernels/  directory .  This directory contains the various kernels  that are available in the system. In my system it looks like this after the extraction  of the latest kernel. 2.6.18-53.el5-i686 is the default kernel running on my system.

$ ls
2.6.18-53.el5-i686  linux-2.6.31.6
$ pwd
/usr/src/kernels


2.  Now run any of the below command to configure the kernel . Here we select the various functionality that we need in our kernel . We can build our kernel either as a monolithic kernel or as a modular kernel .  In this post , we will concentrate on configuring a modular kernel.The various commands available to configure the kernel are: 
  • make config — This will bring a  text based program to configure the kernel .  This method does not require the X Window System
  • make menuconfig —  This will present a  text based menu driven program . Components are presented in a menu of categories and it can run without  the X Window System.
  • make oldconfig — Using this we can build our kernel using the configuration file (.config) of our old kernel. This is useful if we want to build our kernel using the old settings and turning off features not needed. 
  • make xconfig : This provides a graphical interface and needs X window system . 
 Now, let's put the command into practice. Before that, it is a good practice  to identify the hardware of your system. lspci or dmidecode will help us to gather the hardware information.  After gathering the hardware information , we will start with configuring the kernel.

       $sudo make menuconfig

This will open up a menu with various components . To build up a modular kernel , we need to enable the following component   "Enable loadable module support " . To add any feature , press to Y build it in, press M to make it available as a module or Y  to remove it. . Now activate the necessary features either as a module or built in . Help is always there to guide you . Some of the important options you would like to activate are: 
Processor type and features         :   Select your correct processor family.
Networking sup                             :   If your host is in the network     
File systems                                 :   For file system support such as ext3 , NTFS etc
Virtualization                                 :   For adding virtualization feaure.
BUS Options(PCI etc)                   :   If you are compiling for a Laptop, you may want to enable   
                                                          PCMCIA support

    3.  After we are done with configuring the kernel, now its time to build the kernel . The following command below will build a compressed kernel.

                                       $sudo make bzImage

    These are the last few lines after the build is complete . We can see that the build image is available at arch/x86/boot/bzImage .

    BUILD   arch/x86/boot/bzImage
    Root device is (9, 0)
    Setup is 13944 bytes (padded to 14336 bytes).
    System is 2263 kB
    CRC 5572f16c
    Kernel: arch/x86/boot/bzImage is ready  (#1)

    4. Since we are building a modular kernel , we need to now compile the modules that we have enabled while configuring the kernel. The following command will help us to do this.

                                               $sudo make modules

    This will create all the necessary modules with .ko extension. Since one of the options we have selected is NTFS support while building the kernel ,we will see a  ntfs.ko module created under fs directory. The following is the module.
                                    $ find /usr/src/kernels/  -name ntfs.ko
                                /usr/src/kernels/linux-2.6.31.6/fs/ntfs/ntfs.ko


    5. Now its time to install the modules . We can install the module using the following command. After installing the modules , the modules will be installed under " /lib/modules/2.6.31.6/ " directory. We can also see the module for NTFS support got installed.
                                         $ find /lib/modules/  -name ntfs.ko
                                 /lib/modules/2.6.31.6/kernel/fs/ntfs/ntfs.ko

    6. We have reached the final step,to install the kernel. The command below installs the kernel. But before that, take a backup of bootloader config file (grub.conf or lilo.conf )

    $ sudo make install
    sh /usr/src/kernels/linux-2.6.31.6/arch/x86/boot/install.sh 2.6.31.6 arch/x86/boot/bzImage \ System.map "/boot"

    The command does the following. 1) copy the  kernel image  to the /boot directory ,2)  update the bootloader  config file with new kernel information 3) builds a new initrd image  and other necessary things to make the new kernel bootable.We can try a diff between the old and the new grub.conf to view entries of the new kernel . The following is the output in my case .

    $ sudo diff /boot/grub/grub.conf /boot/grub/grub.conf.orig.today
    10c10
    <; default=2
    ---
    >; default=1
    14,17d13
    <; title Red Hat Enterprise Linux Server (2.6.31.6)
    <;     root (hd0,2)
    <;     kernel /vmlinuz-2.6.31.6 ro root=/dev/md0 pci=nommconf rhgb quiet
    <;     initrd /initrd-2.6.31.6.img

    Testing the new Kernel

    Now, let's put the new kernel into test.  Reboot the host and boot Linux from the new kernel . If you are able to boot Linux from the new kernel and everything looks fine, then you probably would like to make this kernel as the default kernel . Edit bootloader conf (/boot/grub/grub.conf) and change default=0 so that Linux boots from the new kernel by default.

    Comments

    Popular Posts