Whether it’s curiosity or testing a patch, there are lots of reasons for compiling a kernel. This is a brief tutorial for compiling a kernel for Fedora.

Getting started with the kernel

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

This installs basic tools like fedpkg. fedpkg is used to get the kernel source code. We use fedpkg to make a clone of the Fedora kernel pkg-git in the directory called ‘kernel’. By default, the source that’s checked out will be for Rawhide. Each Fedora release has its own branch. You can use git commands to change branches. For example, to check out the source for Fedora 24, we would run the following commands.

$ fedpkg co -a kernel
$ cd kernel
$ git checkout -b my_kernel origin/f24

This creates a git branch named my_kernel containing the source for Fedora 24.

Building the kernel

The kernel has a number of build dependencies. These can be installed with

$ dnf builddep kernel.spec

When building a kernel for testing, it’s best to give it an identifiable name. This makes it easier to differentiate it from officially released Fedora kernels and others that have been built. There is a %define in kernel.spec that can be enabled for this purpose.

-# define buildid .local
+%define buildid .my_kernel

This will give the kernel the name my_kernel.

As mentioned previously, the kernel is just another package. Standard fedpkg commands can be used to build.

$ fedpkg local

This will build the kernel. RPMs will be generated in the pkg-git directory under x86_64 (or the name of the architecture you are building for). This will take a long time and require up to 12GB of disk space. If you want a slightly faster build experience, you can use the fast-build.sh script.

$ fedpkg srpm
Wrote /current/path/kernel-4.8.0-0.rc1.git1.1.my_kernel.fc26.src.rpm
$ ./scripts/fast-build.sh x86_64 kernel-4.8.0-0.rc1.git1.1.my_kernel.fc26.src.rpm

This will generate only the kernel and modules.

Why rebuild the kernel?

Two common reasons to rebuild the kernel are to change configuration options and to add patches. Changes to kernel configuration options can be added to the file config-local. The easiest way to add patches is to use the script newpatch.sh.

$ ./scripts/newpatch.sh my-patch.patch

Questions about the steps for building the kernel can be sent to the Fedora kernel mailing list. The Fedora kernel wiki also has reference instructions for building a kernel.