There are many reasons you might want to patch your kernel. You might need extra security patches. Maybe you need the ACS Override patch to use VFIO, or you have an AMD board with a broken UEFI that you can’t downgrade.
Consider supporting us if you value independent tech news and research. This site will always be free of ads and sponsored content.
No matter your use case, here’s a tutorial for patching the Linux kernel in Fedora.
Install Build Dependencies
The First thing we need to do is install dependencies for the build process which can be handled by the following command.
sudo dnf install fedpkg fedora-packager rpmdevtools ncurses-devel pesign
Next, we want to create a directory tree in your home directory. This will create a directory at /home/YourName/rpmbuild.
We want to download our kernel source after the directory tree is created. For this tutorial, we’re going to use Fedora’s kernel source. In this example, we’ve used the 5.1.16 kernel source. You can use tab completion to find the latest kernel source.
Downloading the Fedora Kernel
koji download-build --arch=src kernel-5.1.16-300.fc30.src.rpm
Next, We will unpack our rpm into our rpmbuild directory we created earlier. Do not worry about the warning for mockbuild. Change the kernel version to the one that you’ve downloaded.
rpm -Uvh kernel-5.1.16-300.fc30.src.rpm
Next, we want to put the kernel patch into the sources directory. In this tutorial, we will use the AGESA patch, but whatever patch you want could go here.
cd ~/rpmbuild/SOURCES/ wget -O agesa.patch https://clbin.com/VCiYJ
We need to add a few things to the beginning of the patch as Fedora’s tools are a little particular. Make sure it looks like the picture below. (<YOUR EDITOR> should be changed to vim, nano, gedit, etc.)
<YOUR EDITOR> agesa.patch From: Aiber Who <email@example.com> Subject: [PATCH] agesa patch
Now after we have the patch in place we want to change directory into our kernel spec folder. Once there we want to install some more dependencies.
cd ~/rpmbuild/SPECS/ sudo dnf builddep kernel.spec
Now we have our patch in the correct directory and all of our dependencies installed. We have to edit our kernel.spec file to make sure our patch is added, along with a custom kernel name.
Once we’re in the kernel.spec file, the first thing we need to do is edit the build id. Change
# define buildid .local
To look like
%define buildid .agesa
Note that the extra space after the hash is removed, along with a percent added.
Patching the Fedora Kernel
Before saving the file we need to go down until we reach the “# END OF PATCH DEFINITIONS” and add right before it.
Save and exit your text editor.
Building Your Kernel
Now you can build your kernel. All you have to do is simply type.
rpmbuild -bb --without debug --target=x86_64 kernel.spec
After your kernel has compiled you can install it by doing a couple of commands.
cd ~/rpmbuild/RPMS/x86_64/ sudo dnf install kernel*
It’s worth noting that the debuginfo rpms are not necessary and can be removed before installing.
After everything is installed you need to update grub.
sudo grub2-mkconfig -o /etc/grub2-efi.cfg
Now reboot and select your new kernel with the AGESA breakage workaround!
This process can be used with other patches, too, and you can apply multiple patches in one go, if you need them.
Consider Supporting us on Patreon if you like our work and want a say in what we cover and access to early content. We provide RSS feeds as well as regular updates on Twitter if you want to be the first to know about the next part in this series or other projects we’re working on. If you need help or have questions about any of our articles, you can find us on our Discord.
Images via Pixabay & Blake Lee. Special thanks to VFIO community member, Aiber, for putting together the AGESA 0.0.7.2 reset patch.