Gentoo Linux – kernel_update script, a Quick Helper to Update your Kernel

make menuconfig

Keeping your kernel up to date on Gentoo can be a bit daunting, especially when you are running the “keyworded” kernel, meaning not the stable version, but the “always latest” version by keyword ~amd64. There’s a new version almost every week and updating it manually can be quite annoying over time. At least it felt for me …

make modules
make modules_install
make install

Then after that, updating the grub config … And what was the command to update the kernel-dependent modules again? Oh ya …

emerge @module-rebuild

For some reason, I can never remember that one … Don’t ask me why.

So, I came up with a little helper in form of a bash script that does pretty much all the work for me, all I have to do is tell it which kernel version I want to have and it does all the magic for me. All I need to do from time to time is clean up old kernel installations.



usage() {
    printf "Usage: $0 -k=[<selected_kernel>]\n"
    printf "Usage: $0 --kernel=[<selected_kernel>]\n"
    printf "Usage: $0 --latest\n\n"
    printf "<selected_kernel> is the full kernel version string.\n"
    printf "If the '--latest' (without the quotation marks) option is used, the latest available kernel version will be compiled.\n\n"
    printf "Available kernel versions (current active version is marked with an asterisk):\n`eselect kernel list`\n" 1>&2;

    exit 1;

for i in "$@"; do
    case $i in

            selected_kernel=$(eselect kernel list | tail -1 | awk '{print $2}' | sed 's/[][]//g')

            echo "Unknown option $i"
            exit 1

# if no kernel is selected, show help text
if [ -z "${selected_kernel}" ] ; then

#set the new kernel directory

# compiling new kernel
compile_new_kernel() {
    echo "New Kernel Version: ${selected_kernel}"

    echo "Entering kernel directory: ${kernel_directory}";
    cd ${kernel_directory}

    echo "Copying kernel configuration";
    cp /usr/src/linux/.config .

    echo "Switching symlink to new kernel";
    eselect kernel set ${selected_kernel}

    echo "Applying config via 'make olddefconfig'";
    make olddefconfig

    echo "Building new kernel";
    make -j12

    echo "Building modules";
    make modules

    echo "Installing modules";
    make modules_install

    echo "Installing new kernel";
    make install

    echo "Configuring grub2";
    cp /boot/grub/grub.cfg /boot/grub/grub.cfg.bak
    GRUB_USE_LINUX_LABEL=true grub-mkconfig -o /boot/grub/grub.cfg

    echo "Re-compile kernel dependant modules";
    emerge -av @module-rebuild

    echo "Kernel update complete. You may restart your computer now."
    echo "Feel free to run 'eclean-kernel -n 2' to cleanup old kernel files but keep the latest 2."

if [ -d ${kernel_directory} ] ; then
    current_kernel_version=$(uname -r)

    if [ ${selected_kernel} == "linux-${current_kernel_version}" ] ; then
        echo "You are currently running this kernel version."
        echo "Exiting!";
    echo "Error: Directory ${kernel_directory} does not exist."
    echo "Exiting!";

Save this as e.g. /usr/bin/kernel-update, make sure it is executable via chmod +x /usr/bin/kernel-update and all you have to do to switch to your desired kernel version is (Example: latest installed kernel):

kernel-update --latest

If you want a specific version, first check which versions are available via eselect kernel list and take the version string for a kernel as the argument. For example to build linux-5.17.7-gentoo:

kernel-update -k=linux-5.17.7-gentoo

If you like this script, let me know. I am always open to improvements, so feel free to suggest them!

Leave a Reply