Monday, August 22, 2011

Compiling vanilla Linux kernel in Ubuntu

This article describes how to compile a kernel downloaded from kernel.org in Ubuntu. We are going to compile the latest kernel as on today which is linux-3.0.3. Lets get started...

1. Pre-requisites: Install the following packages
$sudo apt-get install kernel-package libncurses5-dev fakeroot wget bzip2

2. Create a directory in your home directory for compilation (it is not recommended to this work inside /usr/src)
$mkdir ~/linux-kernel
$cd ~/linux-kernel

3. Download and extract the linux kernel sources from kernel.org
$wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.0.3.tar.bz2
$tar jxvf linux-3.0.3.tar.bz2
$cd linux-3.0.3
Note : You can apply any kernel patches at this stage if you want. We will skip this step.

4. Configure the kernel as per your requirements. There are many ways to do this, but the recommended method is to use your current configuration to start from.
$cp /boot/config-`uname -r` ./.config
You need to update your old configuration and bring it to the current kernel source structure.
$make oldconfig
It will ask a few questions which relate to updates that are made in the kernel sources. If you dont know just press "ENTER" to select the default options.

Now, you can customize the kernel configuration as per your requirements by doing
$make menuconfig
This will open up a ncurse based kernel configuration tool. When you are done, save and exit.

5. Build the kernel
$make-kpkg clean
$fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers 
Here you can specify your own version string by changing the "--append-to-version=" option in the previous command. Your string MUST always start with a "-". In our case it is "-custom" so the kernel will be called "3.0.3-custom"

It will take some time to build the kernel, so have a cup of coffee till then :)

6. Once the kernel has been compiled there will be two .deb packages in the parent directory. In our case they are :
linux-headers-3.0.3-custom_3.0.3-custom-10.00.Custom_i386.deb
linux-image-3.0.3-custom_3.0.3-custom-10.00.Custom_i386.deb

Go back to the parent directory and list the files to locate the two .deb packages
$cd ..
$ls

7. The final step is to install the new kernel by using "dpkg" command. You need to be sudo to install the packages.
$sudo dpkg -i linux-image-3.0.3-custom_3.0.3-custom-10.00.Custom_i386.deb
$sudo dpkg -i linux-headers-3.0.3-custom_3.0.3-custom-10.00.Custom_i386.deb

This will also automatically copy the kernel and initrd images to /boot, copy the modules to /lib/modules and also add a entry in the GRUB boot loader. Now you are ready to boot into your new kernel ! Just restart the machine and select the new kernel from the GRUB menu. If there is any problem with booting you can go back to the old kernel which is still there in the GRUB menu list.

8. Once you have booted into the new kernel, you can verify the kernel version by
$uname -r

Also as a bonus you can use that deb file generated - in any other Ubuntu machine for installing the new kernel.

Note : You can also use the -j option to specify the number of threads to use to compile the kernel.
$fakeroot make-kpkg --initrd --append-to-version=-custom kernel_image kernel_headers -j 3
Links :
http://www.howtoforge.com/kernel_compilation_ubuntu

No comments:

Post a Comment