Compile Fedora Kernel - The Fedora Way

So, you want to build a custom kernel in Fedora? I love Fedora. They have a wiki:

https://fedoraproject.org/wiki/Building_a_custom_kernel

This page is in need of some updates, though.

If you follow the guide and issue

git checkout origin/f30

then update your kernel.spec (%define .local etc)
then do

fedpkg local

as indicated. IT WILL FAIL. Every time. With this:

Could not execute local: Repo in inconsistent state: HEAD is a detached symbolic reference as it points to '<some commit id>'

How obtuse is that? I think this section could be written a bit better.

First, you should make a branch for your local dev:

git checkout -b level1_rocks

which will make a new branch called level1_rocks. You should also git commit your changes to kernel.spec (plus whatever else you’ve changed) on this new branch. However, this isn’t enough because fedpkg wants to track the upstream branch as well (even though you aren’t going to push there, because you don’t work for redhat. Why is this a default? The error messages from fedpkg are especially obtuse. Someone want to submit a patch with help text suggesting these commands to resolve the situation? Sheesh.)

git branch -u origin/f30 

As an example, if you want to patch the machine check exception handler for AMD cpus, edit arch/x86/kernel/cpu/mce/amd.c


                smca_set_misc_banks_map(bank, cpu);
 

                /* Return early if this bank was already initialized. */

-              if (smca_banks[bank].hwid)                                                                                                           
+             if (smca_banks[bank].hwid && smca_banks[bank].hwid->hwid_mcatype != 0)                
                                return;

                if (rdmsr_safe_on_cpu(cpu, MSR_AMD64_SMCA_MCx_IPID(bank), &low, &high)) {

This one line update is important for removing a boot-time machine check exception on newer kernels, for example. Of course adding mce=off to your kernel boot line in grub works as a temporary–workaround for booting your system.

(Note I was doing some hacking on f30 bu you might want a different branch like origin/f31 since that dropped today, or literally anything…)

With that,

fedpkg local

will work correctl. I also had to

  dnf install elfutils-devel gcc-plugin-devel perl-devel perl-generators python3-devel

… to get some dependencies I missed during the builddep sep.

From here resuming the Fedora guide on the wiki should be possible. I hope the error comes up in search now, and is useful to people. What came up for me was utterly useless. Now this isn’t. Hopefully.

If you want to change the kernel config, there are some “fun” landmines there, too.

Normally you’d just edit kernel-x86_64.config (or whatever is appropriate for your machine) and change the config. For things you want to say no to, usually you’d just comment it out. In fact if you have config_some_opt=n it will actually error out!

So you comment out those lines. However, that will also error:

Found unset config items, please set them to an appropriate value

Uhh so literally every option has to be Y or M? Good job fedpkg (facepalm).

Edit kernel.spec and change

%define listnewconfig_fail 0
%define configmismatch_fail 0

(is no one testing things anymore?)

And now you should be able to compile the custom kernel options.

The compile will produce .rpm packages that you can install normally on Fedora.

18 Likes

Ahh Thank you very much wendell -your advice still works in June 2022 on version F36 … to help compile locally compiled functioning kernels (compiles, installs and works!). After a long vacation from compiling kernels obtained direct from kerneldotorg I returned to the task on Fedora and found that no other option of the multitudinous things written for compiling installable working kernels from source on fedora (36) systems else did. Thank you good friend thank you!

3 Likes

It’s amazing the user experience here is still pretty bad. Glad this worked for you. I certainly expunged it from permanent storage.

Something something the dullest pencil is better than the sharpest mind.

Inb4 I use the forum as my zettlekasten haha