How to compile and install its own Linux kernel

The Linux Kernel is the operating system kernel. It is the core of the system and provide an interface between the hardware and the software layers.

Most of Linux distributions are coming with a pre-compiled and ready-to-use kernel for most of standard usage. However, some functionalities are sometimes missing in the kernel to allow the execution of specific functions or the support of some hardware (cards, …). In these cases, you will need to use the source code of the needed kernel, recompile it and reinstall it to replace the existing one. All the official soruces of the different kernels are available for free on the website kernel.org.

I will show you here how to modify the source code, compile and install your own kernel for your system (command-lines provided are working under Debian/Ubuntu based system, you will probably need to adapt them depending on the system you are running).

Pre-requisites

In a first time, check that you got the different packages necessary for the operation:

sudo apt-get install build-essential make libncurses5-dev lzma initramfs-tools bc

Once these packages are correctly installed, you can go on the official website to retrieve the source and the configuration files for the kernel you want to use: http://www.kernel.org. In our example, we will use the kernel on version 3.13.5 (latest stable version available today).

Let’s move to the /usr/src directory and then download the kernel archive that we will uncompress immediately:

cd /usr/src/
sudo wget https://www.kernel.org/pub/linux/kernel/v3.x/linux-3.13.5.tar.gz
sudo tar xzvf linux-3.13.5.tar.gz

Kernel sources are now uncompressed in the /usr/src/linux-3.13.5 folder and we can go ahead with its configuration.

Configuration

Let’s move in the directory you just created and start with the use of command make menuconfig to launch the configuration tool:

cd linux-3.13.5
sudo make menuconfig

At this time, you can activate any feature you want on the kernel. For example:

  • To activate the virtualization and its specific functions, move on Virtualization and then press the Space bar to get access to the different available functionalitiesuis  (that you can activate/deactivate as you want)
  • To activate the modules support, move on Enable loadable module support and choose the modules you want to enable by pressing the Space bar (activated items will be checked)

kernel_01

Once you’re done with this configuration, choose Exit and confirm the save of your configuration. This one will be saved in the current directory under the .config file (that you can manually edit with a simple text editor if necessary).

Compilation

Now that the kernel is configured, you will have to compile it so that it can be used on your system. For that, you will need to execute several commands for the different compilation steps:

  • Dependencies compilation (now useless with latest versions)
    sudo make dep
  • Kernel image compilation
    sudo make bzImage
  • Modules compilation
    sudo make modules
  • Modules installation
    sudo make modules_install

At this time, your kernel has been compiled and is ready to be set up on your system.

Installation

To install your own kernel, you just need to execute the command-line below:

sudo make install

This command will create the following files in the /boot start directory:

  • vmlinuz-3.13.5 : the current kernel
  • config-3.13.5 : the kernel configuration file
  • System.map-3.13.5 : the symbol table exported by the kernel
  • initrd.img-3.13.5 : the root file used temporarily during the boot process

This command should update automatically the Grub configuration file (grub.cfg). You won’t need to perform the update manually.

If it’s not done automatically, you can execute it manually with the command-line below:

sudo update-grub2

Checking

In order to check that your kernel has correctly been loaded and is used, you need to reboot your system. Once restarted, you can check which kernel is used with the uname command:

sudo reboot
uname -r
> 3.13.5

You are now using your own kernel with the functionalities you wanted.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *