Uniporn's Blog

Here I write about stuff I do, think or wish others would do

Autoloading new kernel after upgrades on Arch Linux

September 07, 2019 — Uniporn

As an Arch user you may have noticed your USB devices won't work when connected after a kernel update. If I understand correctly that is due to te new kernel storing their modules in another directory than the one still running, while during upgrade also the old location is being removed. Hence when you plug in a new device the corresponding module is not found and you won't be able to use it.


Kexec

To fix that you can switch to the updated kernel after update using kexec. You just execute the first command from "Rebooting using kexec" which goes like this: # kexec -l /boot/vmlinuz-linux --initrd=/boot/initramfs-linux.img --reuse-cmdline. Notice the # which indicates you will need root access to do that.

ALPM hook

But looking over every update list to know if the precious package "core/linux" was part of it seems tideous and so there is a way to automate it, namely a alpm-hook that pacman will execute. You can have a look at the man page in case you need hooks with pacman in another context or like for compiling your custom kernel. For the case at hand I created a file at /usr/share/libalpm/hooks/linux-kexec.hook with the following content:

[Trigger]
Type = Package
Operation = Install
Operation = Upgrade
Target = linux

[Action]
Description = switching to new kernel
When = PostTransaction
Exec = /usr/bin/kexec -l /boot/vmlinuz-linux --initrd=/boot/initramfs-linux.img --reuse-cmdline

And that's it. That is how to increase your lovely uptime. Though you may want to reboot from time to time to make sure the current kernel still boots properly.

Tags: tutorial, arch, linux, desktop