Compiling A Custom Kernel On Gentoo Linux

Posted on

Hey everyone! Archie here. Today we're gonna compile the Linux kernel from the official Gentoo sources.

We're gonna start by becoming root:

sudo -i

Or if you use doas:

doas -s

Then, we're gonna unmask the latest version of the official sources and setting the symlink use flag to control the kernel source directory symbolic link:

echo sys-kernel/gentoo-sources ~amd64 > /etc/portage/package.accept_keywords/gentoo-sources
echo sys-kernel/gentoo-sources symlink > /etc/portage/package.use/gentoo-sources

If instead it is a file, run:

echo sys-kernel/gentoo-sources ~amd64 > /etc/portage/package.accept_keywords

Then, we're gonna emerge it:

emerge sys-kernel/gentoo-sources

Then, we need to go to /usr/src and run:

eselect kernel list

to list the kernel symlink options. Typically, the first option will be your currently running kernel, and the second will be the sources that you installed. So, we run:

eselect kernel set 2

to switch the "linux" directory symlink to the corresponding kernel version.

We're gonna enter the "linux" directory, which if you did the eselect part, it should now point to the kernel sources that we installed.

Creating A Custom Kernel Config

Now that we have the kernel sources and we're in the kernel source directory, we're gonna need a file called ".config", that will be our config. I used to copy the running kernel config using zcat /proc/config.gz > .config, but that somehow messed up my config and i had to do it all over again, so now i just do this:

make localmodconfig

to create a configuration based on the modules i currently have.

Customizing & Installing The Kernel

Then, you might want to customize the kernel's version string. Open "Makefile" with an editor of your choice, and in the "EXTRAVERSION" part, add your custom kernel name, e.g.

EXTRAVERSION = -archie

then enter the configuration menu with make menuconfig, and go into "General setup", then into "Local version - append to kernel release", and delete all the chars in there. This will leave you with only your custom "EXTRAVERSION" string.

It's always a good practice to, after copying/creating your .config, run make menuconfig, and make sure you have this, either as a module (M) or built-in (Y):

and misc/other hardware support that you might eventually need.

Then, to compile and install the kernel, run:

make -j$(nproc); make modules_install; make install

Then, wait a bit and it should be done. It all depends on your current processing power. You don't need to worry about the initramfs here though, the system will take care of generating the initramfs and updating your boot loader configuration file if you have the "dracut" and "grub" use flags on the sys-kernel/installkernel package. With that being said, in case that stuff go sideways, you can always run dracut --force /boot/initramfs-<version>.img to generate one if needed.

Then we can reboot:

reboot

If everything went well, you will see GNU GRUB (or your boot loader's menu). Select the "advanced options" and hit return (enter) on your custom kernel entry. This should boot into your new kernel. You can confirm your kernel version by running:

uname -sr

or

cat /proc/version

Either of those two commands will tell you the version of your new kernel.

That's about it for this post, hope it has been useful, and see ya next time! <3

Go back to the blog's main index.