Author

Alejandro Alcalde

Graduado en Ingeniería Informática en la ETSIIT, Granada.

More Alejandro Alcalde's posts

Have you seen a Typo?: Please, help me fix it by contacting me.

Table Of Contents

I’ve been using Gentoo for a few years now. And every time a new kernel version went out, I ended visiting Gentoo’s wiki page Kernel/Upgrade and following its steps to upgrade the kernel.

But I thought it would be better to automate the process, so I wrote a simple bash script that from now on will do the work for me. It may be useful for other Gentoo users:

#!/usr/bin/env bash

## Upgrade gentoo kernel

# The first and only argument must be the folder name of the new kernel.

# Stop script when an error occurs
set -o errexit
set -o pipefail
set -o nounset
# For debugging purposes
set -o xtrace

readonly kernel_path='/usr/src/'

_usage () {
    local script_name="$0"
    echo "Usage: $0 <newkernelfolder>"
}

_main () {
    echo "Backing up old kernel..."
    cd "${kernel_path}/linux/"
    cp .config ~/kernel-config-"$(uname -r)"
    echo "Copying old configuration..."
    cp /usr/src/linux/.config /tmp/.config
    echo "Setting new kernel as default..."
    #ln -sf /usr/src/"$1" /usr/src/linux
    cp /tmp/.config /usr/src/linux/
    eselect kernel set 2
    cd /usr/src/linux/
    echo "Building..."
    make -j4 olddefconfig
    make -j4 modules_prepare
    emerge --ask @module-rebuild
    make -j4
    make install
    echo "Please, update your EFI entry: cp /boot/vmlinuz-*-gentoo /boot/efi/boot/bootx64.efi"
}


if [[ $# -eq 1 ]]
then
    _main $1
else
    _usage
fi

To use it, simply write:

$ sudo ./updatekernel.sh linux-4.12.12-gentoo

Where linux-4.12.12-gentoo is the name of the new kernel.

Contributions are welcome, because there is still room for improvement, you can check the script in my Dotfiles repository.



After writing the script, I saw portage already have hooks for execute actions when new ebuilds are downloaded, but when I tried it I realized that the .config from my previous kernel was not being picked up. I am sure there is a way to tell portage hooks to use the previous config, but since I’ve already wrote the script, I decided to use it.

Hope you find the script useful!

Update

I posted this script on the Gentoo subreddit /r/Gentoo and it has received a lot of comments. Some redditors had posted its own ways to update a kernel automatically, here are some of them:

eselect kernel list
eselect kernel set #

# Use current kernel config and store copy
gunzip /proc/config.gz -c > /root/config; cp /root/config /root/config-$(uname -r)

#  Optionally change module settings in the config file, or use --menuconfig
#  with genkernel.
genkernel --makeopts=-j9 --splash --kernel-config=/root/config all

# Update packages with kernel modules
emerge -1 @module-rebuild

# Cleanup old files
for name in $(ls /lib/modules/ | sort -V | head -n -3); do
  rm -rf /boot/{initramfs,kernel,System.map}-genkernel-x86_64-${name:?}
  rm -rf /lib/modules/${name:?}
done

# Update boot config file
grub-mkconfig -o /boot/grub/grub.cfg

All of them are far more elaborate than mine, so I will try to use them and improve mine.

Categories:Tags:

Maybe this posts are also worth reading