Fedora Docs

Building a Custom Kernel

This document provides instructions for advanced users who want to rebuild the kernel from source. Any issues with a custom kernel build are not supported by the Fedora kernel team.

Why Build a Custom Kernel?

Some common reasons to build a custom kernel are to:

  1. Change configuration options

  2. Troubleshoot issues

  3. Add patches

Preparing to build a custom kernel

The Fedora kernel is just another package in Fedora and can be compiled like any other application. The easiest way to compile the kernel is to use the tools provided by the fedora-packager package. This will install basic tools like fedpkg which is used to download the kernel source code onto your computer.

To install fedora-packager from the command-line, enter:

sudo dnf install fedora-packager

fedpkg will make a clone of the Fedora kernel from pkg-git into a directory called ‘kernel’. By default, the source that is "checked out" will be the Rawhide kernel. Each Fedora release has its own branch and you can change branches using git commands. For example, to "check out" the source for Fedora 27, we would run the following commands:

fedpkg co -a kernel

cd kernel

git checkout -b my_kernel origin/f27

This creates a git branch named 'my_kernel' containing the kernel source for Fedora 27.

Be sure to change the version number to whatever version of Fedora you are using.

Building the kernel

The kernel has a number of build dependencies that can be installed with:

sudo dnf builddep kernel.spec

Changes to kernel configuration options can be added to the file config-local.

When building a kernel for testing, it is best to use an identifiable name. This makes it easy to distinguish your custom kernel from the official Fedora kernel builds. Find the line in the kernel.spec file that says:

# define buildid .local

and change it to:

%define buildid .my_kernel

This will add my_kernel to the the RPM package file names and will look something like this:

kernel-4.15.3-300.my_kernel.fc27.src.rpm

To build the packages enter:

fedpkg local

This will create all the kernel packages in the x86_64 directory (or the name of the architecture you are building for). This will take a long time as it is creating all the kernel, module and debug packages. Also, it will require over 12 GB of disk space.

If you want to create only the kernel and modules packages, you can use the fast-build.sh script. First enter:

fedpkg srpm

The result should look something like this:

Wrote /current/path/kernel-4.15.3-300.my_kernel.fc27.src.rpm

Now enter:

./scripts/fast-build.sh x86_64 kernel-4.15.3-300.my_kernel.fc27.src.rpm

If you need to add patches use the script newpatch.sh like this:

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

(where my-patch.patch is the file name of the patch.)

Once it is complete, you can install/update your system with your custom kernel build. cd into the directory created in the kernel build process (x86_64, x86, etc.) and type:

sudo dnf update kernel*.rpm

It will update any kernel packages you currently have installed on your computer.

Additional Resources:

For more information on building a custom kernel refer to the Fedora Wiki: Building a Custom Kernel.

Credits:

This document is an updated and edited version of the article Building the Fedora Kernel from the Fedora Magazine.