[[ch-build-custom-kernel]]
= Building a Custom Kernel

[NOTE]

====

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.

====

[[sect-why-build-a-custom-kernel]]
== Why Build a Custom Kernel?

Some common reasons to build a custom kernel are to:

.. Change configuration options

.. Troubleshoot issues

.. Add patches

[[sect-preparing-to-build-a-custom-kernel]]
== 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:

[source,bash]

----

sudo dnf install fedora-packager

----

`fedpkg` will make a clone of the Fedora kernel from link:++http://pkgs.fedoraproject.org/cgit/rpms/kernel.git/++[pkg-git] into a directory called ‘kernel’.
By default, the source that is "checked out" will be the link:++https://fedoraproject.org/wiki/Releases/Rawhide++[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:

[source,bash]

----

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.

[NOTE]

====

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

====

[[sect-building-the-kernel]]
== Building the kernel

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

[source,bash]

----

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:

[source,bash]

----

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:

[source,bash]

----

fedpkg srpm

----

The result should look something like this:

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

Now enter:

[source,bash]

----

./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:

[source,bash]

----

./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:

[source,bash]

----

sudo dnf update kernel*.rpm

----

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

[[sect-additional-resources]]
== Additional Resources:

For more information on building a custom kernel refer to the link:++https://fedoraproject.org/wiki/Building_a_custom_kernel++[Fedora Wiki: Building a Custom Kernel].

[[sect-credit]]

== Credits:

This document is an updated and edited version of the article link:++https://fedoramagazine.org/building-fedora-kernel/++[Building the Fedora Kernel] from the link:++https://fedoramagazine.org++[Fedora Magazine].
