2018-03-02 16:24:36 +00:00
[[ch-build-custom-kernel]]
= Building a Custom Kernel
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[NOTE]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
====
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
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.
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
====
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[[sect-why-build-a-custom-kernel]]
== Why Build a Custom Kernel?
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
Some common reasons to build a custom kernel are to:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
.. Change configuration options
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
.. Troubleshoot issues
2018-01-26 18:29:28 +00:00
2018-03-02 16:24:36 +00:00
.. Add patches
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[[sect-preparing-to-build-a-custom-kernel]]
== Preparing to build a custom kernel
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
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.
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
To install `fedora-packager` from the command-line, enter:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
sudo dnf install fedora-packager
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
`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:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
fedpkg co -a kernel
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
cd kernel
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
git checkout -b my_kernel origin/f27
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
This creates a git branch named 'my_kernel' containing the kernel source for Fedora 27.
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[NOTE]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
====
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
Be sure to change the version number to whatever version of Fedora you are using.
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
====
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[[sect-building-the-kernel]]
== Building the kernel
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
The kernel has a number of build dependencies that can be installed with:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
sudo dnf builddep kernel.spec
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
Changes to kernel configuration options can be added to the file `config-local`.
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
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:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
`# define buildid .local`
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
and change it to:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
`%define buildid .my_kernel`
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
This will add _my_kernel_ to the the RPM package file names and will look something like this:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
`kernel-4.15.3-300.my_kernel.fc27.src.rpm`
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
To build the packages enter:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
fedpkg local
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
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.
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
If you want to create only the kernel and modules packages, you can use the `fast-build.sh` script.
First enter:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
fedpkg srpm
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
The result should look something like this:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
`Wrote /current/path/kernel-4.15.3-300.my_kernel.fc27.src.rpm`
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
Now enter:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
./scripts/fast-build.sh x86_64 kernel-4.15.3-300.my_kernel.fc27.src.rpm
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
If you need to add patches use the script `newpatch.sh` like this:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
./scripts/newpatch.sh my-patch.patch
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
(where `my-patch.patch` is the file name of the patch.)
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
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:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[source,bash]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
sudo dnf update kernel*.rpm
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
----
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
It will update any kernel packages you currently have installed on your computer.
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[[sect-additional-resources]]
== Additional Resources:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
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].
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
[[sect-credit]]
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
== Credits:
2017-10-26 21:20:01 +00:00
2018-03-02 16:24:36 +00:00
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].