Fast Learning the Linux Kernel

In Linux Kernel, there are multiple layers, modules, functionalities, calls and functions โ€“ and to make everything clear, MakeLinux has a cool interactive map of the complete kernel. Here, we are sharing this interactive map of Linux Kernel that has all the components and each item on the map is a hyperlink to the source code and documentation.

This Linux kernel map helps you to understand complex interconnections between subsystems of the kernel while you navigate through its source code. Letโ€™s suppose you are trying to hack down a bug and getting irritated trying to figure out what invokes what โ€“ so here the kernel map comes to your rescue.

This is also very helpful if you just want to understand the subsystems. On the map, there are more than 400 prominent functions and functions are divided into major subsystems.

The relationships are shown by the lines and by clicking on any function, youโ€™ll reach to its Linux source code and documentation.

Browse the entire interactive map here on MakeLinux.

The majority of users that are interested in building their own kernel are doing so because they have installed Linux on their system and they wish to make a small change to the kernel for that system. In many cases the user just wants to make a kernel configuration change.

The purpose of this page is to give that user a minimum amount of information for them to meet the goal of making a simple change to the kernel, building it and installing their kernel. It is not intended to be the definitive guide to doing Linux kernel development.

The procedure to build (compile) and install the latest Linux kernel from source is as follows:

Once upon a time the idea of upgrading the Linux kernel sent fear through the hearts of many a user. Back then, the process of upgrading the kernel involved a lot of steps and even more time. Now, installing a new kernel can be easily handled with package managers like apt. With the addition of certain repositories, you can even easily install experimental or specific kernels (such as real-time kernels for audio production) without breaking a sweat.

Considering how easy it is to upgrade your kernel, why would you bother compiling one yourself? Here are a few possible reasons:

Regardless of why, knowing how to compile a Linux kernel is very useful and can even be seen as a right of passage. When I first compiled a new Linux kernel (a long, long time ago) and managed to boot from said kernel, I felt a certain thrill coursing through my system (which was quickly crushed the next time I attempted and failed).

With that said, letโ€™s walk through the process of compiling a Linux kernel. After running through a standard sudo apt upgrade, the installed kernel is 5.1.1 I want to upgrade to kernel 6.0.3. Letโ€™s take care of that.

A word of warning: I highly recommend you practice this procedure on a virtual machine. By working with a VM, you can always create a snapshot and back out of any problems with ease. DO NOT upgrade the kernel this way on a production machineโ€ฆ not until you know what youโ€™re doing.

Downloading the kernel

The first thing to do is download the kernel source file. This can be done by finding the URL of the kernel you want to download (from Kernel.org). Once you have the URL, download the source file with the following command (Iโ€™ll demonstrate with kernel 6.4.9):

wget https://cdn.kernel.org/pub/linux/kernel/v6.x/linux-6.4.9.tar.xz

While that file is downloading, there are a few bits to take care of.

Installing requirements

In order to compile the kernel, weโ€™ll need to first install a few requirements. This can be done with a single command:

sudo apt-get install git fakeroot build-essential ncurses-dev xz-utils libssl-dev bc flex libelf-dev bison

Do note: You will need at least 12GB of free space on your local drive to get through the kernel compilation process. So make sure you have enough space.

Extracting the source

From within the directory housing our newly downloaded kernel, extract the kernel source with the command:

tar xvzf linux-6.4.9.tar.gz

Change into the newly created directory with the command cd linux-6.4.9.

Configuring the kernel

Before we actually compile the kernel, we must first configure which modules to include. There is actually a really easy way to do this. With a single command, you can copy the current kernelโ€™s config file and then use the tried and true menuconfig command to make any necessary changes. To do this, issue the command:

cp /boot/config-$(uname -r) .config

Now that you have a configuration file, issue the command make menuconfig. This command will open up a configuration tool (Figure 1) that allows you to go through every module available and enable or disable what you need or donโ€™t need.

Figure 1

It is quite possible you might disable a critical portion of the kernel, so step through menuconfig with care. If youโ€™re not sure about an option, leave it alone. Or, better yet, stick with the configuration we just copied from the running kernel (as we know it works). Once youโ€™ve gone through the entire list (itโ€™s quite long), youโ€™re ready to compile!

Compiling and installing

Now itโ€™s time to actually compile the kernel. The first step is to compile using the make command. Trust me when I say give yourself plenty of time here.

After answering the litany of questions, you can then install the modules youโ€™ve enabled with the command:

make modules_install

Once again, this command will take some time, so either sit back and watch the output, or go do something else (as it will not require your input). Chances are, youโ€™ll want to undertake another task (unless you really enjoy watching output fly by in a terminal).

Now we install the kernel with the command:

sudo make install

Again, another command thatโ€™s going to take a significant amount of time. In fact, the make install command will take even longer than the make modules_install command. Go have lunch, configure a router, install Linux on a few servers, or take a nap.

Enable the kernel for boot

Once the make install command completes, itโ€™s time to enable the kernel for boot. To do this, issue the command:

sudo update-initramfs -c -k 6.4.9

Of course, you would substitute the kernel number above for the kernel youโ€™ve compiled. When that command completes, update grub with the command:

sudo update-grub

You should now be able to restart your system and select the newly installed kernel.

Congratulations!

Youโ€™ve compiled a Linux kernel! Itโ€™s a process that may take some time; but, in the end, youโ€™ll have a custom kernel for your Linux distribution, as well as an important skill that many Linux admins tend to overlook.

Learn more about Linux through the free โ€œIntroduction to Linuxโ€ course from The Linux Foundation and edX.

The Fedora Way

Each distribution has some specific tools to build a custom kernel from the sources. Is about compiling a kernel on Fedora systems. It describes how to build a custom kernel using the latest unmodified kernel sources from www.kernel.org (vanilla kernel) so that you are independent from the kernels supplied by your distribution. It also shows how to patch the kernel sources if you need features that are not in there.

The Fedora kernel is just another package in Fedora. This means that if you know how to compile other Fedora packages, you have a good start on compiling the kernel. The easiest way to compile the kernel is to use the packager tools.

$ dnf install packager-tools

Get the required packages

sudo dnf install fedpkg fedora-packager rpmdevtools ncurses-devel pesign

Set up rpmdev environment if you don't have one

rpmdev-setuptree

Make sure you're in your home dir (or wherever, just know where you're working)

cd

Grab the source

fedpkg clone -a kernel

cd kernel

Change to rawhide branch

fedpkg switch-branch master

Uncomment the buildid line in the kernel.spec and change it to something e.g.:

%define buildid .bzzzap

vim kernel.spec

Optional, configure your kernel how you want (below is how I do it, YMMV, do what works for you...I don't think the default fedora kernel even has /proc/config.gz available now that I think about it).

cd /usr/src/kernels/$(uname -r)

zcat /proc/config.gz >.config

make oldconfig

make menuconfig

Put it in the kernel-local file. The available reading material implies you only want to make the minimal amount of changes to add to kernel-local. I think that's a crock. I've spent a lot of time grokking make menuconfig and run a heavily modified, very opinionated configuration with many (MANY) options and drivers removed compared to the default. For the curious, my personal kernel-local is here.

cat .config >~/kernel/kernel-local

cd ~/kernel

This tells it to not create a debugging kernel (not actually sure if this is necessary now that I think about it)

make release

Create a .src.rpm

fedpkg srpm

Use the fast-build.sh script on the .src.rpm (if this fails you may need to go a few rounds of fixing config options in kernel-local, re-running the make release ; fedpkg srpm each time you make a change.

scripts/fast-build.sh x86_64 kernel-xxxxx.src.rpm

Find your fresh kernel RPMs (yum! (not the command))

cd ~/rpmbuild/RPMS/x86_64

Install 'em. You probably don't want to "upgrade" unless you're actually sure your new kernel is going to boot

sudo rpm -ivh kernel*rpm

Update grub config

sudo grub2-mkconfig -o /boot/efi/EFI/fedora/grub.cfg

Respect your elders and the shoulders of the giant's we stand on!