Go to

December 25, 2014

Compile and use custom Linux kernel on Ubuntu

Introduction

Everything you do on your Ubuntu operating system is controlled and managed by Linux kernel. Your system's performance, stability and hardware support all relies on the kernel being used. Ubuntu ships it's own custom Linux kernel which supports variety of hardware and has a standard performance and stability across different computers. Sometimes, you need to build your own Linux kernel for using with your Ubuntu operating system. Some of the reasons for doing so are listed below

  • Your need to use a hardware which is not supported with default kernel shipped by Ubuntu but supported with newer Linux kernel or kernel from other sources
  • You need maximum performance and stability for your specific hardware
  • You want to know how Linux kernel perform for specific kernel configuration

Preparation

Getting the Linux kernel


There are many options for choosing a Linux kernel. The first option is to use a standard Linux kernel, which can be downloaded from


https://www.kernel.org/


You can find three different kernel on the website tagged with Mainline, Stable, Longterm. As the tagging suggests, mainline kernels are the ones with latest commits and patches but needs testing and verification, stable kernels are for production use and longterm kernels come with more security patches than stable kernels.


Other, Linux Kernels such as Zen Kernel


https://github.com/zen-kernel/zen-kernel


and PF Kernel


https://pf.natalenko.name/


have more features and performance tweaking than standard (Vanilla) Linux kernel.


Now, download Linux kernel sources from one of the sites listed above.


Installation of dependencies


To compile and build packages for Linux kernel you will need to install some dependencies. Open up the terminal and put the following commands:


sudo apt-get install kernel-package libncurses5-dev build-essential


sudo apt-get build-dep linux-image-`uname -r`


Compile and Build

First move the downloaded Linux kernel file to the Home folder and open up a terminal. In the terminal put the following commands:


enter root mode:


sudo -s


change the current directory to /usr/src:


cd /usr/src


move the downloaded kernel file to /usr/src

mv /home/[username]/[kernel filename] .


extract kernel file to /usr/src:


if kernel file is tar.gz file then put:


tar xvf [kernel filename]


if kernel file is tar.bz2 file then put:


tar xjvf [kernel filename]


if kernel file is tar.xz file then put:

tar xJvf [kernel filename]


if kernel file is zip file then put:


unzip [kernel filename]


create a link to extracted folder


ln -s [extracted folder] linux


change the current directory to /usr/src/linux:


cd linux

configure kernel to your needs (Very important!). To select a feature press keyboard Spacebar key, and press Esc key twice to move back a menu. Press Esc key twice in the main menu to exit and select yes to save configuration.

make menuconfig


compile and build kernel deb packages:


make-kpkg clean


make-kpkg kernel_image kernel_headers


Or

make-kpkg clean

make deb-pkg

This will take a while depending on your CPU speed, so relax or have a break or do other work.


If there were no errors in compiling and building then you will find newly created deb files in the /usr/src folder.


Now, Install the deb files, update initramfs, update grub:

cd ..


dpkg -i *.deb


update-initramfs -c -k [kernel version, press tab to see installed linux kernels and choose the one you just installed]


update-grub

exit


All done, now you can reboot your computer and select Advance from the grub boot menu and select your kernel. After booting into the new kernel check the kernel being used from the terminal:


uname -r


If you encounter any problems during booting new kernel, do not panic. Just reboot the system and from the Advance grub menu select previous working kernel!


To remove non working kernel put the following command in the terminal:

sudo apt-get remove linux-image-[kernel version]
sudo apt-get remove linux-header-[kernel version]


If you do not know exact name in the above command for example, linux-image-3.17, press Tab key after entering linux-image. This will show installed kernels in the system.


Cheers!

No comments:

Post a Comment