Gentoo:Kernel Cleanup

Gentoo is a source-based Linux distribution. Moreover it’s aimed at more advanced users with the intent of not forcing anything on them, as much as is possible with so many packages.

Normally, whenever you get a kernel update on a distro, say from 4.2.0 to 4.2.5, the system does several things.

  • Download the new kernel (pre-compiled)

  • Install the new kernel

  • Update the initramfs

  • Update the bootloader

  • Clean up the unused kernels

Gentoo requires that their users to each of those items. I’m fairly new to Gentoo and, coming from Arch Linux, already knew for the most part how to do all of those steps, except for the last one. That said, lets talka bout how to clean up unused kernels on your system.

TL;DR Summary

Here are the locations that need to be cleaned up, in case you want to skip to that section or already know what you’re doing but just need the list.

  • emerge -C <sys-kernel/<type>-sources-<version> (This is specific to Gentoo)

  • Files

  • /usr/src/linux-<versions>

  • /lib/modules/<versions>

  • /boot/*-<versions>

Inventory

The first step is to take an inventory. The largest part of a kernel in Gentoo is its source code, so we’ll look at that for the inventory (there are other ways and we’ll look into those as well).

Gentoo stores its kernel source at /usr/src/linux-*. In there, you can see multiple versions of the linux source code. In my case, I have linux-4.2.0 all the way up to linux 4.2.5.

Note
In my case, I’m using vanilla-sources for my kernel, so the directory names for you might be a tad different. Despite though, they should still start with linux-x.x.x*.

Clean Up the Source

Now that we have an inventory, let’s clean up the source. This is very simple. Once you know what versions you want to keep, we can issue an emerge command that will clean up the files for the source. For my example, we’ll say we want to remove anything before 4.2.4

emerge -Cp <sys-kernel/vanilla-sources-4.2.4

That command will delete any files installed by emerge if they belong to kernel versions 4.2.3 or less. However, it doesn’t clean up any files created by the compile process, so we still need to remove those.

To see where we are first, here’s an ls example of my src directory.

0 [nullspoon@null ~]$ ls /usr/src/
linux  linux-4.2.0  linux-4.2.1  linux-4.2.2  linux-4.2.3  linux-4.2.4  linux-4.2.4-gentoo  linux-4.2.5  linux-4.2.5-gentoo

Now for a simple but gratifying rm command. Since rm has no knowledge of the kernel versioning scheme, we can’t say "less than version 4.2.4" and it’ll clean up. In this case, we’ll use a simple bash sequence.

rm -rf /usr/src/linux-4.2.{0,1,2,3}
Note
If you are worried about running that command and what it’ll do, run it with echo before it to see what it will output without actually deleting anything.

Clean up Installed Modules

The Linux kernel stores its modules in /lib/modules (fun fact: /lib is usually a symlink to /lib64 if you’re running a 64 bit system, or /lib32 if you’re running a 32 bit system).

Similar to cleaning up the sources, we will clean up the moduels with a simple but gratifying rm -rf command.

rm -rf /lib/modules/4.2.{0,1,2,3}

That will clean up all the installed modules for the specified old kernel versions (in this case, 4.2.0, 4.2.1, etc).

Clean up Old Installed Kernels

The Linux Kernel installs itself to /boot. To see what you have for the given version set (again, using the one from previous examples, run the following command…​

ls /boot/*-4.2.{0,1,2,3}

Again, a simple rm command will clean up these files.

rm -f /boot/*-4.2.{0,1,2,3}

And with that, you’re done. All clean!

Last edited: October 30, 2015