From 0d56e70688c4d04009d471b23c33a28269a728da Mon Sep 17 00:00:00 2001 From: BodgeMaster <> Date: Wed, 21 Jun 2023 07:07:13 +0200 Subject: [PATCH] add kernel hooks --- install-stage1.sh | 11 +++++- kernel.d/post-install/99-void-usb | 59 +++++++++++++++++++++++++++++++ kernel.d/post-remove/99-void-usb | 10 ++++++ kernel.d/pre-install/99-void-usb | 24 +++++++++++++ 4 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 kernel.d/post-install/99-void-usb create mode 100644 kernel.d/post-remove/99-void-usb create mode 100644 kernel.d/pre-install/99-void-usb diff --git a/install-stage1.sh b/install-stage1.sh index bdbf1d2..e832fcd 100644 --- a/install-stage1.sh +++ b/install-stage1.sh @@ -283,7 +283,16 @@ chmod 755 /mnt/target/opt/void-usb wget --output-document=/mnt/target/opt/void-usb/grub-config https://lostcave.ddnss.de/git/BodgeMaster/void-minecraft-usb/raw/branch/master/opt/grub-config chmod 744 /mnt/target/opt/void-usb/grub-config -run_in_target /opt/void-usb/grub-config +#TODO: ensure that this is being run by xbps-reconfigure -fa, if not add it here +# run_in_target /opt/void-usb/grub-config +wget --output-document=/mnt/target/etc/kernel.d/pre-install/99-void-usb https://lostcave.ddnss.de/git/BodgeMaster/void-minecraft-usb/raw/branch/master/kernel.d/pre-install/99-void-usb +chmod 744 /mnt/target/kernel.d/pre-install/99-void-usb +wget --output-document=/mnt/target/etc/kernel.d/post-install/99-void-usb https://lostcave.ddnss.de/git/BodgeMaster/void-minecraft-usb/raw/branch/master/kernel.d/post-install/99-void-usb +chmod 744 /mnt/target/kernel.d/post-install/99-void-usb +wget --output-document=/mnt/target/etc/kernel.d/post-remove/99-void-usb https://lostcave.ddnss.de/git/BodgeMaster/void-minecraft-usb/raw/branch/master/kernel.d/post-remove/99-void-usb +chmod 744 /mnt/target/kernel.d/post-remove/99-void-usb + +run_in_target xbps-reconfigure -fa # diff --git a/kernel.d/post-install/99-void-usb b/kernel.d/post-install/99-void-usb new file mode 100644 index 0000000..9d14895 --- /dev/null +++ b/kernel.d/post-install/99-void-usb @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +# +# Kernel post-install hook for Void-USB +# This script generates the symlinks for default /boot/{vmlinuz,initramfs}. +# It also re-generates the GRUB config. +# +# Arguments: $1 package name, $2 kernel version + +PKGNAME="$1" +VERSION="$2" + +echo "Updating default kernel and initramfs symlinks..." + +##################### +# Delete old symlinks +##################### +OLD_LINUX=/boot/vmlinu? +if [ ! -f "$OLD_LINUX" ]; then + # in cases where it’s a .gz or .xz or whatever + OLD_LINUX=$(find /boot -type l -name "vmlinu*" 2>/dev/null | sort | head -n1) +fi +[ -f "$OLD_LINUX" ] && rm "$OLD_LINUX" + +OLD_INITRAMFS=/boot/initramfs.img +if [ ! -f "$OLD_INITRAMFS" ]; then + # hope to find anything named initramfs or initrd + OLD_INITRAMFS=$(find /boot -type l -name "init*" 2>/dev/null | sort | head -n1) +fi +[ -f "$OLD_INITRAMFS" ] && rm "$OLD_INITRAMFS" + +##################### +# Create new symlinks +##################### +NEW_LINUX_FILE="$(find /boot -name "*$VERSION*" 2>/dev/null | sed 's|^/boot/||' | grep -e '^vmlinu' -e '^linux' -e '^linuz' -e '^kernel')" +NEW_LINUX_SYMLINK="vmlinuz" +if grep -e '^vmlinux' -e '^linux' -e '^kernel' >/dev/null 2>&1 <<< "$NEW_LINUX_FILE"; then + NEW_LINUX_SYMLINK="vmlinux" +fi +if grep -e ".gz$" -e ".xz$" -e ".bz$" -e ".bz2$" -e ".lzma$" -e ".lz$" -e ".img$" >/dev/null 2>&1 <<< "$NEW_LINUX_FILE"; then + NEW_LINUX_SYMLINK="$NEW_LINUX_SYMLINK$(grep -o -e ".gz$" -e ".xz$" -e ".bz$" -e ".bz2$" -e ".lzma$" -e ".lz$" -e ".img$" 2>/dev/null <<< "$NEW_LINUX_FILE")" +fi +ln -s "$NEW_LINUX_FILE" "/boot/$NEW_LINUX_SYMLINK" + +NEW_INITRAMFS_FILE="$(find /boot -name "init*$VERSION*" 2>/dev/null | sed 's|^/boot/||')" +NEW_INITRAMFS_SYMLINK="initramfs" +if grep '^initrd' >/dev/null 2>&1 <<< "$NEW_INITRAMFS_FILE"; then + NEW_INITRAMFS_SYMLINK="initrd" +fi +if grep -e ".gz$" -e ".xz$" -e ".bz$" -e ".bz2$" -e ".lzma$" -e ".lz$" -e ".img$" >/dev/null 2>&1 <<< "$NEW_INITRAMFS_FILE"; then + NEW_INITRAMFS_SYMLINK="$NEW_INITRAMFS_SYMLINK$(grep -o -e ".gz$" -e ".xz$" -e ".bz$" -e ".bz2$" -e ".lzma$" -e ".lz$" -e ".img$" 2>/dev/null <<< "$NEW_INITRAMFS_FILE")" +fi +ln -s "$NEW_INITRAMFS_FILE" "/boot/$NEW_INITRAMFS_SYMLINK" + +echo "Default kernel and initramfs are now: $NEW_LINUX_FILE, $NEW_INITRAMFS_FILE" + +#################### +# Update GRUB config +#################### +/opt/void-usb/grub-config diff --git a/kernel.d/post-remove/99-void-usb b/kernel.d/post-remove/99-void-usb new file mode 100644 index 0000000..a8fea0a --- /dev/null +++ b/kernel.d/post-remove/99-void-usb @@ -0,0 +1,10 @@ +#!/usr/bin/env bash +# +# Kernel post-remove hook for Void-USB +# This script regenerates the GRUB config. +# +# Arguments: $1 package name, $2 kernel version +PKGNAME="$1" +VERSION="$2" + +/opt/void-usb/grub-config diff --git a/kernel.d/pre-install/99-void-usb b/kernel.d/pre-install/99-void-usb new file mode 100644 index 0000000..e3481e0 --- /dev/null +++ b/kernel.d/pre-install/99-void-usb @@ -0,0 +1,24 @@ +#!/usr/bin/env bash +# +# Kernel pre-install hook for Void-USB +# This script removes old kernels when disk space is low. +# +# Arguments: $1 package name, $2 kernel version +PKGNAME="$1" +VERSION="$2" + +MB_LOW=250 +MB_FREE="$(df --block-size=1M --output=avail /boot | tail -n1)" + +if [ "$MB_FREE" -lt "$MB_LOW" ]; then + echo + OLDEST_KERNEL="$(vkpurge list | sort | head -n1)" + if [ -z "$OLDEST_KERNEL" ]; then + echo -e "\033[31m================================================\n\033[33mWARNING:\033[0m Failed to find an old kernel to remove.\nThe /boot partition is running out of space.\nThis will become an issue if left unaddressed.\n\033[31m================================================\033[0m" + else + echo "Removing old kernel $OLDEST_KERNEL using vkpurge..." + vkpurge rm "$OLDEST_KERNEL" + fi +else + echo "Not doing anything, /boot has plenty of space." +fi