Home >> Linux >> Compile your own kernel – Tutorial

Compile your own kernel – Tutorial

Follow @LinuxUserMag

Back in the mid-1990s, recompiling the kernel was something of a necessity, and it was also a good test that a user had mastered the basics of administering Linux. These days, the stock kernel that comes with most distros has much improved, removing the necessity of kernel recompilation for basic use. However, there are cases where it’s well worth becoming familiar with this area of tweaking your system. For one thing, it’s a must if you want to access the latest and greatest kernel improvements, hot off the press, so to speak. It’s also a good way of understanding how the kernel and other fundamental parts of a Linux system actually work. It can also be useful when troubleshooting: the newest kernel might bug-fix the problem you’re having. On the other hand, an older kernel might be the workaround that you need.

We’ll start you off with a simple example that begins with fetching the source archive for the kernel that you are currently running, proceeding through to configuration, compilation and installation. Following this, we’ll go through some examples that are a bit more specialist. Most of the examples are for Debian-derived distros, but we’ve deliberately kept things as neutral as possible and added some notes for how to handle things on Red Hat-based distros such as Fedora.

Compile your own kernel - Tutorial
Once the kernel is complete, you can add it to the system using the standard packaging tools

Step by Step

Step 01

Install prerequisites

Begin by fetching the tools needed to create a suitable build environment. Enter ‘sudo apt-get install fakeroot crash kexec- tools makedumpfile kernel-wedge git-core libncurses5 python libncurses5-dev kernel- package libelf-dev binutils-dev’ followed by ‘sudo apt-get build-dep linux-image’.

Step 02

Fetch the kernel (source archive)

If you need the latest kernel, use Git to fetch it (see later step), but we are going to use the standard package tools in the first example. Use ‘apt-get source linux-image-$(uname -r)’ to install the source for the currently running kernel.

Step 03

Examine the source directory

You should now have a source directory in the current directory. Move into it using the cd command. Note that there is an archived (tar.gz) copy as well. In addition, there is a diff file that contains the Ubuntu-specific additions to the standard kernel source tree.

Step 04

Generate a .config file

The (hidden) file ‘.config’, located in the source code directory, tells the compiler what to build. The configuration file for each installed kernel is stored in the /boot directory, but you can capture the configuration of the current kernel (a good starting point) by typing ‘make oldconfig’.

Step 05

Edit .config

Open up .config in a text editor. Note that there are thousands of options, and this approach is best if you know exactly what settings you would like to edit. It’s a good idea to search for ‘CONFIG_LOCALVERSION’ to add a small identifying string for your custom kernel.

Step 06

Turn off debugging

One way to speed things up and produce smaller files is to turn off debugging.
It’s a specialist feature and mainly used by developers. You can use xconfig for this. Set ‘CONFIG_DEBUG_INFO:’ to ‘n’.

Step 07

Invoke xconfig

Type ‘make xconfig’ to launch the GUI config file editor. It’s a good way to gain an overview, and it offers information for most of the options. Run ‘sudo apt-get install libqt4-core libqt4-dev libqt4-gui’ if it complains about not being able to find Qt.

Step 08

Prepare Debian scripts

Some required scripts lose their execution privileges due to how apt-get works. Rectify this by typing ‘chmod -R u+x debian/ scripts/*’ and then ‘chmod u+x debian/rules’.

Step 09

Recreating the Source Tree

If you mess things up and want to start from scratch, delete the source directory. Backup your .config file first, if needed. Then run “tar xzf” on the source archive to unpack it. Move into the directory and type “zcat ../[name of diff archive] | patch -p1” to add the Ubuntu patches into the source tree.

Step 10

Compile the kernel

Tell the build environment how many cores you want to use with ‘export CONCURRENCY_LEVEL=[number of cores]’. Begin the build process with ‘fakeroot make- kpkg –initrd –append-to-version=-luad kernel- image kernel-headers’, which takes about an hour on a Core Duo 2.7GHz system.

Step 11

Install kernel

Warning: This is stage where you actually make some changes to your machine. Using our method means that we can install our custom kernel just like any other package. Type ‘sudo dpkg -i linux-image[version].deb’ and then, ‘sudo dpkg -i linux-headers-[version].deb’.

Follow @LinuxUserMag

Leave a Reply

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