[HowTo] Build your very first custom kernel

Difficulty: ★★★★☆
:warning:
If you compile your own custom kernel, you’re basically creating your own distro, so before you post any support questions here on the forum, please revert back to the Manjaro kernel first!

  1. To start, grab the Linux kernel source. Go ahead and head to kernel.org, and choose the kernel you want to compile. I’ll use the mainline kernel as an example, which is 5.11-rc2 at the time of writing this post. You can grab it with git clone or wget the download link to it if you want.

  2. Extract the archive:

    bsdtar -xvf [kernel-archive-file]
    

    Where, obviously you have to replace [kernel-archive-file] with the name of the compressed archive of the kernel.

  3. cd into the directory it was extracted in. And run the following if you want to use the config that came with your distro. (RECOMMENDED)

     zcat /proc/config.gz >> .config
    
  4. If you want to configure some extra features with the stock config, run the following:

    make menuconfig
    
    • At this point, you’ll be greeted with a menu showing a bunch of options. If you don’t know what you’re doing, exit out and leave it.
  5. Now, it’s time to compile it!

    make -j$(nproc)
    

    Where $(nproc) will use all the CPU pipelines available so your build should go faster.

  6. After the compilation finises, it’s time to install the modules:

    sudo make modules_install
    
  7. Now, you’re ready to install the kernel file and the System.map. Run the following command to install them:

    sudo make install
    
  8. The following commands need a root shell for what we’re about to do, so:

    sudo su
    
  9. As we’re in the root shell now, we want to copy vmlinuz to vmlinuz-[kernel-version]-[arch]

    cp /boot/vmlinuz /boot/vmlinuz-[kernel-version]-[arch]
    
    • Replace [kernel-version]-[arch] with the kernel version (E.g: linux-5.11-rc2-x86_64)
    • If you do not perform the above command you won’t be able to reboot!
  10. Now, we’re going to generate initramfs.
    Arch Based distros use mkinitcpio, which is a bash script to automate the generation of the initramfs.
    Therefore, we want to make custom presets for the kernel. These are usually located in /etc/mkinitcpio.d, so we’ll use a template from another version of the kernel for our newly compiled version:

    # mkinitcpio preset file for the 'linux' package
    
    ALL_config="/etc/mkinitcpio.conf"
    ALL_kver="/boot/vmlinuz-[kernel-version]-[arch]"
    
    PRESETS=('default' 'fallback')
    
    #default_config="/etc/mkinitcpio.conf"
    default_image="/boot/initramfs-[kernel-version]-[arch].img"
    #default_options=""
    
    #fallback_config="/etc/mkinitcpio.conf"
    fallback_image="/boot/initramfs-[kernel-version]-[arch]-fallback.img"
    fallback_options="-S autodetect"
    
  11. Edit the [kernel-version]-[arch] with the kernel version you compiled and the arch of your cpu. E.G: x86_64 and save this to a preset in /etc/mkinitcpio.d/[kernel-version].preset:

    sudo mkinitcpio -p linux[kernel-version].preset
    
    • If you get “no modules were added, this is probably not what you wanted”, please use the config that came with the distro, and recompile your kernel. Then follow the steps above again.

    • If everything goes well, you can reboot and try out your new kernel! If you boot successfully.

    • To verify the kernel version, execute::

      uname -a 
      
    • If you see the kernel version you compiled, congrats! You just successfully compiled your first Linux kernel!

:+1:

12 Likes

Nice guide :+1::+1:. If you are using nvidia drivers can you use mhwd/manjaro settings gui to add the drivers to a custom kernel?

2 Likes

As far as I know you can.
And thank you!! :smiley:

2 posts were split to a new topic: I get an error compiling my own custom kernel

A post was split to a new topic: Converted to personal Message

The step to build the modules missing in this guide:

make modules

before the make modules_install step. This solves the modules.order problem.

I would recommend to follow the Arch Linux guide wiki.archlinux.org/title/Kernel/Traditional_compilation.

2 Likes

2 posts were split to a new topic: Converted to Personal Message

yeah! the arch guide is cool! the tip on using that modeprobed-db tool is well cool, trying to compile the full set of modules with march=native shifts difficulty to 6 star I reckon, but make localmodconfig worked a treat on the atom I compiled using the native tweak for the selected cpu in nconfig…

with amd and trying to compile kernel 4.19 having loads of problems… obviously do not need the i945 driver and it failed to compile for example this time… not even trying any cpu optimisation…

funny the 15 to 20% improvement I noticed from previously having compiled a 4.19 kernel which using pkgbuild native mod probably did not even compile the kernel with optimisation is just that the 4 series kernel is 15% faster compared to 5 series LOL

and adding preempt made audio work cool!

That is to say the 4.19 kernel is as supplied by manjaro is compiled without any pre-empt (server mode), and a timer of 100Hz which is perhaps why laptop users reported better battery life from it

So… it just failed again, I was running it in console mode to get max speed and could not even see which module failed…

could be you need to compile it with an earlier gcc than the distro eh???

could be I just culled some more stuff from the atom compile, but could be that I compiled kernel 5.4…

of course the phoronix speed tests of direct 5 vs 4 series were compiled way back then at kernel 4.16.nnn series and compiled with earlier gcc but I definitely noticed 15% improvement with a 4 series kernel on manjaro 6 or so months ago… funny how changes now can effect facts in the past eh!

This thread is about how to improve the tutorial. :+1:
Could you please press the three dots below your post and press the :pencil2: to summarize your post so it’s easier for the author to incorporate your ideas into the tutorial?

:thinking:

yeah! lets say a make localmodconfig of 4.19 did just build a bzimage OK, to improve this original post is really a lot more complex, the:

zcat /proc/config.gz >> .config

is what makes it complex. I think problem is what kernel you want to compile… what gcc version you are using … (and I was definitely using the manjaro provided 4.19 kernel before I did the zcat /proc/config.gz >> .config)

the 4.19 set of included options in the manjaro kernel cause some fails using gcc11… some deprecated become gone? make localmodconfig works OK!

anyway, cool first post, made me read the arch reference…

I think also the guide is missing the make mrproper as first step

Whether it’s relevant to this article or not, the reason to build a 4 series kernel is the phoronix article showed it to be 15% faster than 5 series kernel! 15% of a quite slow cpu is a lot more than 15% of latest yuppie rig! (even though according to yuppie statistics it is less eh)

https://www.phoronix.com/scan.php?page=article&item=linux-416-54&num=7

basically i think some stuff in 4.19 kernel does not compile with GCC11

use make xconfig, that works and is a nicer UI than nconfig

One important thing to do is in general setup change the local version append to kernel release string from manjaro to a custom string for yourself, that way you will still have the manjaro kernel version to boot into if your kernel build proves to be inadequate (probably because in my case my optimisations of the kernel will result in a dropped module it turns out I actually need)

and use the manjaro kernel installer to install another manjaro kernel for fallback uses anyway.

what worked for me is

zcat /proc/config.gz >> .config

then run make localmodconfig as this option runs on the basis of disabling enabled options and does not work the other way round. Unfortunately it is a bit harsh on unselecting options and removed udf and ntfs file systems which ironically I nearly immediately had a problem with, which is why want to run modprobed-db on a stock kernel on the target machine for a few weeks and even then may still miss stuff.

then run make xconfig to further customise for your cpu and do lots of reading about the options you do change… for me on 4.19 it was basically select my CPU, set full pre-empt and timer to 1000Hz for a “pro audio” rig on an antique CPU LOL

One more thing to add here, compiling a kernel is a system stress test… for some reason with acpi on this mobo it was not driving the fans as fast as they could go, so turned off cool and quiet in the bios and set the bios to drive the fans.

Basically what I said about problems with compilation were I think quite possibly this pc overheating…

CONCLUSION: Was it worth all that effort??? Only as a learning experience.!

Useful in educating myself in how much detail you need to know about the target system and how much you need to know about all the kernel options.

It seems The current 5.4 generic kernel is better than a Custom 4.19 Kernel in many ways.

If you are building a custom product with only very limited features I think you still want to base it on kernel 5.4 or 5.10 if it is an arm multicore.

More useful tweaks have been applied in the 5 series i think and my latest tests show performance improvement is negligible between 5.4-manjaro and my custom 4.19 on this AMD Athlon XP PC anyway.

It looks like OP hasn’t been back in a few months. If he doesn’t respond within 24h, I’m going to make this post a wiki so any regular user can edit it.

@pwr33.3 you’re just a basic user so go help some people so you can improve the existing tutorials!

:+1:

Sure will if I get some time and happen to think I know the answer LOL

Just to clarify, the problems I was having was the BIOS on this old PC, I had downgraded it to the latest one that didnt have (BETA) listed against it, turns out it was the latest (BETA) I needed as that then controlled the Fans better and no more crashes, but still, compiling a kernel using all CPU’s especially on an older machine is really a system stress test as it can take several hours and really drives memory and cpu.

Make sure you can run memtest86 for a few passes first! LOL

I saw a note that you should choose memory GB/2 or number-of-cores/2 whichever is the lesser to compile…

seemed a reasonable rule of thumb, compile on economy-7!

of course as component scale has got smaller, heat generated has become less eh! I have a nice dual-use radiator here!

1 Like

Great guide. Just a heads up for anyone running into problems: make sure you have both cpio and pahole installed.