Friday, March 29, 2013

Compiling and installing new kernel in Ubuntu 12.04 LTS




Two months back I started playing with Linux Kernel. As a beginner in Linux kernel, first thing to go for is to compile and install a new kernel in my system. In this post, I will be describing the very basic steps to just compile a new kernel and install it in your system and boot from that kernel. Steps mentioned below is somewhat general and will probably work in most Linux distributions. I am using Ubuntu 12.04 64 bit Desktop edition. Lets start now.


Get the required kernel source. If you don't have anything, go to www.kernel.org and download a kernel source. Mostly kernel sources will be provided as compressed tar balls with extensions .tar.gz or .tar.bz2.


Now navigate to the folder which contains source package. Now decompress the source package. Use the following commands depending on the type of compression.
    gzip -cd linux-3.X.tar.gz | tar xvf -
    or
    bzip2 -dc linux-3.X.tar.bz2 | tar xvf -
replace x with the actual version number which you are using.

Now navigate to the uncompressed source folder and run

make oldconfig

If you are using a newer kernel than the existing one, then probably once you enter the above command , you will get many yes or no questions. For the time being just ignore those by pressing enter till those questions are over. If every thing works fine, you will get a message similar to shown below.

#
# configuration written to .config
#

What we have done by typing the command 'make oldconfig' is that we automated the process of configuring the kernel for your system. This will configure the new kernel with the existing configuration of the currently running kernel. This the most safest configuring option for first time kernel compilation.

Now we have configured our kernel. Next we need to compile it. As you know kernel compilation is a time consuming process. It is worth to describe parallel compilation options.'make' command is used to compile a configured kernel. Along with make command we use the -j option to schedule the compilation in multiple parallel jobs. Depending on the number of cores your processor have, you can give that many parallel jobs. An optimum number for parallel jobs is twice the number of CPU cores. Giving more than twice could degrade the performance due to heavy switching overhead.

For example

make -j4 will compile the kernel with 4 parallel jobs

Now you could start compiling the your kernel with suitable number of parallel jobs. Wait patiently for the build to complete.

Once the built is over without any errors, its time to install it.

Installation is in two stages. First we will install the kernel modules and then will install the kernel image.

To install the modules enter the following command

make modules_install

Once modules are installed run

make install

to install the kernel image.

Its over now. The new kernel is now ready to boot. You can select the new kernel from the grub menu which will be automatically updated.

Note: I recommend you to use virtual machines for your experiments with kernel.



0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | coupon codes