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 …

1
2
3
4
make
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 …

1
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 to clean up old Kernel installations.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash

selected_kernel=""
kernel_base_directory="/usr/src/"
kernel_directory=""


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
        -k=*|--kernel=*)
            selected_kernel="${i#*=}"
            shift
            ;;

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

        -*|--*)
            echo "Unknown option $i"
            exit 1
            ;;
        *)
            usage
            ;;
    esac
done


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


#set the new kernel directory
kernel_directory="${kernel_base_directory}${selected_kernel}"


# 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!";
        exit;
    else
        compile_new_kernel
    fi
else
    echo "Error: Directory ${kernel_directory} does not exist."
    echo "Exiting!";
    exit;
fi

Save this as /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):

1
kernel-update --latest

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

1
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

Your email address will not be published.