Monday 9 January 2017

Kernel Compilation in Easiest Step

Linux kernel is the life force of all Linux family of operating systems including Ubuntu, CentOS, and Fedora.
For most part, you don’t need to compile the kernel, as it is installed by default when you install the OS. Also, when there is a critical update done to the kernel, you can use yum, or apt-get to update the kernel on your Linux system.

However you might encounter certain situation, where you may have to compile kernel from source. The following are few situation where you may have to compile Kernel on your Linux system.

Need of Kernel Compilation :

  • To enable experimental features that are not part of the default kernel.
  • To enable support for a new hardware that is not currently supported by the default kernel.
  • To debug the kernel
  • Or, just to learn how kernel works, you might want to explore the kernel source code, and compile it on your own.
In this tutorial, we’ll explain how to compile Linux kernel from source.

1. Download the Latest Stable Kernel

The first step is to download the latest stable kernel from kernel.org.

# cd /usr/src/ 
 
OR you can directly go through command line 
 
# wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.9.3.tar.xz

2. Untar the Kernel Source

The second step is to untar the kernel source file for compilation.

# tar -xvJf linux-3.9.3.tar.xz
 
Or you can directly extract it by double click on tar file 

3. Configure the Kernel

The kernel contains nearly 3000 configuration options. To make the kernel used by most people on most hardware, the Linux distro like Ubuntu, Fedora, Debian, RedHat, CentOS, etc, will generally include support for most common hardware. You can take any one of configuration from the distro, and on top of that you can add your own configuration, or you can configure the kernel from scratch, or you can use the default config provided by the kernel.

Do it as per your kernel version 
e.g. 
If you have downloaded linux 4.9.2 then 
# cd linux-4.9.2

else 

# cd linux-3.9.3 
 
 
Then   
 # make menuconfig
The make menuconfig, will launch a text-based user interface with default configuration options as shown in the figure. You should have installed “libncurses and libncurses-devel” packages for this command to work.

We will use the default config provided by the kernel. So select “Save” and save the config in the file name “.config”.
The following is a sample of the “.config” file:
CONFIG_MMU=y  CONFIG_NEED_DMA_MAP_STATE=y  CONFIG_NEED_SG_DMA_LENGTH=y  CONFIG_GENERIC_ISA_DMA=y  CONFIG_GENERIC_BUG=y  CONFIG_GENERIC_HWEIGHT=y

4. Compile the Linux Kernel


  • Compile the main kernel:
# make
  • Compile the kernel modules:
# make modules
  • Install the kernel modules:
# make modules_install
  • At this point, you should see a directory named /lib/modules/3.9.3/ in your system.

5. Install the New Kernel

Install the new kernel on the system:

# make install
 
The make install command will create the following files in the /boot directory.
  • vmlinuz-3.9.3 – The actual kernel
  • System.map-3.9.3 – The symbols exported by the kernel
  • initrd.img-3.9.3 – initrd image is temporary root file system used during boot process
  • config-3.9.3 – The kernel configuration file
The command “make install” will also update the grub.cfg by default. So we don’t need to manually edit the grub.cfg file.

6. Boot Linux to the new Kernel

To use the new kernel that you just compiled, reboot the system.

# reboot
 
Since, in grub.cfg, the new kernel is added as default boot, the system will boot from the new kernel. Just in case if you have problems with the new kernel, you can select the old kernel from the grub menu during boot and you can use your system as usual.
Once the system is up, use uname command to verify that the new version of Linux kernel is installed.

Fix this error on Debian / Ubuntu Linux

Open terminal and type the following apt-get command to install ncurses:
$ sudo apt-get install libncurses5-dev libncursesw5-dev
Now you should able to compile any program under Linux using ncurses. Here is a sample command to compile and use ncurses:
$ gcc -o output input.c -lncurses
$ ./output

No comments:

Post a Comment

How to install google-chrome in redhat without redhat subscription

Install google-chrome in redhat  Download the .rpm file of chrome https://www.google.com/chrome/thank-you.html?installdataindex=empty&st...