Skip to main content

How to compile kernel step by step

Recently I compiled the latest kernel by manually for just fun. so i logged my experience here. I show the step by step to the compilation process. I followed the this reference for compile the kernel.

Why Need to compile the Kernel?
  • Some time some software or modules expected that some kernel flags needed to set while compile (build) the kernel. This kind of scenario we need to rebuild the kernel with specified configuration flags are set.
  • When new kernel is released,  compile the new kernel and install in our system. But this is not recommend way to install the system. so use our distribution(Ubuntu, red-hat release )  kernel update.

Check your Linux kernel version
        In Linux system use uname -r or cat /proc/version to find the current kernel version
Linux Kernel Version
here 2.6.35.22 is current system kernel version


Step 1 : Download the latest kernel
 Goto http://kernel.org/ website and get the latest version of the kernel source. i will get 3.3.3

wget http://www.kernel.org/pub/linux/kernel/v3.0/linux-3.3.3.tar.bz2

and extract the source code.
tar xf linux-3.3.3.tar.bz2
cd linux-3.3.3
in linux-3.3.3 folder contain lots of files and folders. some folders are use following purpose

arch - this folder shows the what are the architecture port the Linux
crypto- contain cryptography implementations like AES, DES
drivers-  all device drivers modules (IDE, SCSI, Ethernet device, Wireless)
fs- all file-system implementation code (ext4, FAT, NTFS)
net - all network protocol implementations (IPv4, IPv6, tcp, 802.11)


Step 2 : make the configuration file
  While compile the kernel source code, we need configuration file. that configuration file contain lots of variable to help to understand what are the modules we need to compile.

For x86 specific kernel variable check this reference 

using make command we can build the configuration, but this command is interactive, its ask more than 1000 question about enable or disable the particular module, like which file-system r u want and IP-Tables related modules like SIP components (state-full inspection firewall).

so the best way is copy the current Linux config file. its stored in /boot/config-<version>.
cp /boot/config-2.6.35.22-generic      .config 

now we got old configuration. now we change/add new configuration settings.
for this purpose therse is lots of options are avialable

make help       ==>Provides the help

There is GUI option is also available

make   menuconfig          ==> this provides Text based GUI Configuration



here load the old config file and add/change the settings then save the config file.

make   gconfig      ==> this provides GTK 2 based GUI Configuration for GNOME



for this we need to install libgtk2.0-dev, libglad2-dev through apt-get install


make   xconfig      ==> this provides QT based GUI Configuration for KDE

for this we need qt-dev tools


Using any one of the above method modify the configuration.

Step 3 : Compile the Kernel
 
        There are two ways to compile the kernel

  • Generic way (lots of steps)
  • Debian specific way (simple, Use make-kpkg tool)

Step 3.1 : Generic Way

      Step 3.1.1 Compile the Kernel and its modules

          make  
       above code is take 1 to 2 hours depending on system performance. In this step it compile the kernel and store the kernel in binary form to arch/x86/boot/bzImage file
then based on kernel headers it will compile the kernel  modules (device driver, file-system, network,...)
and generate .ko files. ko means kernel object. These modules also called Loadable Kernel Modules (LKM).

   Step 3.1.2 Install Kernel modules

     make  modules_install
     This step copy the all kernel modules (*.ko) to /lib/modules/<version>/kernel/   folder.


       Step 3.1.3 Install Kernel 

       make  install
      This step copy the the kenel from arch/x86/boot/bzImage to /boot folder and copy the .config file to /boot/config-<latest-version> and generate the System.map file.


      Step 3.1.4 Create Initramfs file
     up to now kernel and its modules are compiled and installed. when next boot up time we need to choose latest kernel. so we need to prepare the boot-loader and its support files. When system turns on, after bios and boot loader load the kernel to main memory and mount initial dummy file system as a root file system of system. this initial file system have necessary drivers for disk hardware (SCSI or IDE) and mount the correct file system as a root file system.

so we need to create initramfs file using update-initramfs or mkinitfs tool

     update-initramfs -c -k 3.3.3
here 3.3.3 is new kernel version.


   Step 3.1.5  Update GRUB bootloader
  the last step is update the boot loader here i m using GRUB boot-loader.

     update-grub 
this command automatically probe the kernels in /boot folder and add the entries in its configuration file, grub.cfg


now restart the system , we will see the new kernel is added in boot loader entries. then choose new kernel in boot loader.


now open the terminal and issue uname -r command, its shows the  current kernel version.



Step 3.2 : Debian specific way Way

              In Debian system's (Ubuntu, Linux Mint,..) there is one tool "make-kpkg" is available to automate the kernel compilation process.

to install the make-kpkg, use apt-get
sudo apt-get install    kernel-package    libncurses5-dev

this one line command is compile the kernel and its modules and make the binary form in deb file.
make-kpkg --initrd --append-to-version=-ramki    kernel_image     kernel_headers

this one line make the kernel and its headers with custom suffix -ramki
--initrd specify that make initramfs disk file
kernel_image option to make the kernel and its modules
kernel_headers option to make all header files of the kernel. (optional)

the final output is its create 2 DEB files.

Install the Deb files
      Install the deb file is simple. use dpkg tool like normal installation in Debian system
dpkg -i kernel_image-ramki-i386.deb

while executing this line install the kernel to /boot folder. install the modules in /lib/modules folder and create the initramfs file and update the grub like generic way. all the above operation is happen automatically.

then in optional case we can install the kernel_headers-ramki-i386.deb file.

why need the kernel headers are important?
       kernel_image is enough to run the system. but when we need to install drivers, for example we need to install the new driver in Linux  that time while compile the driver, that time need the kernel headers.


In my system its works fine. i tested in VM also.

Reference:

Comments Welcomed


Popular posts from this blog

Understanding Virtual Host Concept in Tomcat

Hi in this post we will see how to setup virtual host in Apache Tomcat server. Virtual Host is in-built feature that allows to deploy multiple website(domains) in single instance of tomcat server. The main benefit in this way is its cost effective. Scenario: I am going to deploy 3 website with following domain names in single tomcat http://www.ramki.com http://www.krishnan.com http://www.blog.ramki.com The following diagram is my outline. Outline structure of Virtual Host Concept in Tomcat Here my tomcat IP address 192.168.1.15. or any IP address allocated my ISP. but it should be public IP address. How all domain names are pointing to my Tomcat?                   When we purchase the domain name we need to update the our tomcat IP address to it. like or we can simulate same DNS Setup through hosts file in both Linux and Windows. In Linux tha file is located at /etc/hosts Now How Setup Virtual Host Concept? Before going to setup the virtual host. first tak

Virtual Host + Apache httpd server + Tomcat + mod_jk connector

In my last post ( Virtual Host in Tomcat ) we discussed about how setup the virtual host in Tomcat. Its cost effective technique because only one public IP is enough to host multiple domain. If we have big organization and each department want to host their website in locally in different machine. then how to achieve the virtual host concept?. In this post we will see the how we do this. Update :   I posted  Virtual Host + Nginx + Tomcat  Its easy to configure, compare to Apache httpd server Problem Scenario:         In big organization they have multiple department, each department want to host their website in different machine. so these websites are accessed locally with different local IP address. When we mapping to public address then we face the problem. We have two choice either purchase as many public address or Put one server front  and delegate these request.  We going to use 2nd option. we put Apache httpd web server in front of all department servers. so onl

Docker : Tomcat Clustering with Load Balancer (Tomcat and Nginx)

In this post i will show Tomcat Clustering in Docker Container. In  my previous post i discussed how to achieve tomcat clustering with Nginx Front end . Its almost same scenario, but this time we will achieve via docker container. Docker Docker  is an  open-source  project that automates the deployment of  applications  inside  software containers , by providing an additional layer of abstraction and automation of  operating-system-level virtualization  on  Linux . [4]  Docker uses resource isolation features of the Linux kernel  such as  cgroups  and kernel  namespaces  to allow independent "containers" to run within a single Linux instance, avoiding the overhead of starting and maintaining  virtual machine   --Wikipedia