You are not logged in.

#1 2023-09-26 20:18:32

Frontear
Member
Registered: 2023-05-22
Posts: 29

[solved] Trying to create a hook for any kernel upgrades

I've wrote a scrappy script that automates systemd-boot entries by parsing the compressed kernel files in /boot, then creating an entry for each one (it skips fallback since I can just manually edit an entry to boot into fallback if necessary). For clarity, the script is attached:

#!/bin/sh

if [ $EUID -ne 0 ]; then
    echo "Requires root permissions, exiting..."
    exit 1
fi

DEFAULT_PARAMETERS="quiet loglevel=3 systemd.show_status=auto rd.udev.log_level=3 zswap.enabled=0"

OS_NAME="$(hostnamectl --json=short | jq -r ".OperatingSystemPrettyName")"
MICROCODE="$(basename $(find /boot -type f -name "*-ucode.img"))"
OPTIONS="root=\"PARTUUID=$(findmnt -no PARTUUID /)\" rw"

basename -a $(find /boot -type f -name "vmlinuz-linux-*") | while read KERNEL; do
    TYPE=$(awk -F"-" "{print \$3}" <<< "$KERNEL")
    DEFAULT_IMAGE="initramfs-linux-$TYPE.img"
    ENTRY_FILE="/boot/loader/entries/$(awk -F" " "{print tolower(\$1)}" <<< "$OS_NAME")-$TYPE.conf"

    FILE_CONTENT=("title   $OS_NAME (linux $TYPE)"
                  "linux   /$KERNEL"
                  "initrd  /$MICROCODE"
                  "initrd  /$DEFAULT_IMAGE"
                  "options $OPTIONS $DEFAULT_PARAMETERS")

    echo "" > $ENTRY_FILE
    for line in "${FILE_CONTENT[@]}"; do
        echo "$line" >> $ENTRY_FILE
    done
done

In order to further automate this, I decided to try and make a Pacman Hook. My first attempt involved something like this:

[Trigger]
Operation = Install
Operation = Upgrade
Operation = Remove
Type = Path
Target = boot

[Action]
Description = Re-creating systemd-boot entries...
When = PostTransaction
Exec = /home/frontear/.local/bin/update-bootloader

I tried to test this by running pacman -S linux-hardened, but unfortunately it does not work.
I figure it's possible that since the target isn't actually being changed by the packages, but by another hook, pacman doesn't know to run it, so I modified the target to check for kernel module directories, which are (usually), only made when a kernel is installed

Target = usr/lib/modules

Unfortunately this did not fire either. I'm not sure how I should go about making this hook, I originally wanted to do Type = Package, Target = linux-*, but this would run on off packages like util-linux-libs, or archlinux-keyring, which I didn't want.

Last edited by Frontear (2023-09-26 21:29:41)

Offline

#2 2023-09-26 20:56:42

Scimmia
Fellow
Registered: 2012-09-01
Posts: 12,182

Re: [solved] Trying to create a hook for any kernel upgrades

See how the mkinitcpio hooks do it, that's a better way.

Why, though? Arch kernel filenames don't change, so you set this up once and it's done, redoing the config every time the package is updated seems pointless.

Offline

#3 2023-09-26 21:03:20

V1del
Forum Moderator
Registered: 2012-10-16
Posts: 23,447

Re: [solved] Trying to create a hook for any kernel upgrades

The mkinitcpio hook uses

[Trigger]
Type = Path
Operation = Install
Operation = Upgrade
Target = usr/lib/modules/*/vmlinuz
Target = usr/lib/initcpio/*

which will cover the cases you'd want.

Edit: F5, F5

Last edited by V1del (2023-09-26 21:03:51)

Offline

#4 2023-09-26 21:23:45

Frontear
Member
Registered: 2023-05-22
Posts: 29

Re: [solved] Trying to create a hook for any kernel upgrades

Scimmia wrote:

See how the mkinitcpio hooks do it, that's a better way.

Why, though? Arch kernel filenames don't change, so you set this up once and it's done, redoing the config every time the package is updated seems pointless.

Upgrade is probably not needed yeah, I just wanted it for situations when I remove/install new kernels and it does it all automatically. I'll peek at the mkini hook, thanks

Offline

#5 2023-09-26 21:24:31

Frontear
Member
Registered: 2023-05-22
Posts: 29

Re: [solved] Trying to create a hook for any kernel upgrades

V1del wrote:

The mkinitcpio hook uses

[Trigger]
Type = Path
Operation = Install
Operation = Upgrade
Target = usr/lib/modules/*/vmlinuz
Target = usr/lib/initcpio/*

which will cover the cases you'd want.

Edit: F5, F5

Thanks for this, it didn't occur to me to check the mkinitcpio hook

Offline

#6 2023-09-26 21:29:17

Frontear
Member
Registered: 2023-05-22
Posts: 29

Re: [solved] Trying to create a hook for any kernel upgrades

Well this is embarassing. I named the file .conf, and not .hook, so it never actually ever ran because pacman could never read it. Good to know i'm an idiot

Last edited by Frontear (2023-09-26 21:38:17)

Offline

Board footer

Powered by FluxBB