From 8b2715ff12b1d2a594d0aca02b3c42466ac06bed Mon Sep 17 00:00:00 2001 From: Recolic K <bensl@microsoft.com> Date: Mon, 27 Sep 2021 16:55:05 +0800 Subject: [PATCH] add --- 55-check-boot-mounted.hook | 11 +++++++ 85-call-kernel-rename.hook | 12 ++++++++ README.md | 4 +++ hook-check-bootdir-mounted.sh | 8 ++++++ hook-kernel-rename.sh | 54 +++++++++++++++++++++++++++++++++++ 5 files changed, 89 insertions(+) create mode 100644 55-check-boot-mounted.hook create mode 100644 85-call-kernel-rename.hook create mode 100755 hook-check-bootdir-mounted.sh create mode 100755 hook-kernel-rename.sh diff --git a/55-check-boot-mounted.hook b/55-check-boot-mounted.hook new file mode 100644 index 0000000..386ea18 --- /dev/null +++ b/55-check-boot-mounted.hook @@ -0,0 +1,11 @@ +[Trigger] +Type = Path +Operation = Install +Operation = Upgrade +Target = usr/lib/modules/*/vmlinuz + +[Action] +Description = Checking if /boot is mounted... +When = PreTransaction +Exec = /usr/share/libalpm/scripts/hook-check-bootdir-mounted.sh + diff --git a/85-call-kernel-rename.hook b/85-call-kernel-rename.hook new file mode 100644 index 0000000..3b26613 --- /dev/null +++ b/85-call-kernel-rename.hook @@ -0,0 +1,12 @@ +[Trigger] +Type = Path +Operation = Install +Operation = Upgrade +Target = usr/lib/modules/*/vmlinuz + +[Action] +Description = Invoking hook-kernel-rename.sh to allow multiboot with different kparam... +When = PostTransaction +Exec = /usr/share/libalpm/scripts/hook-kernel-rename.sh +NeedsTargets + diff --git a/README.md b/README.md index 47f5b47..3daa82c 100644 --- a/README.md +++ b/README.md @@ -28,3 +28,7 @@ only supports grub only supports arch-based distributions. Tested on Arch Linux and Manjaro Linux. +## notes + +depends on: sed, bash, + diff --git a/hook-check-bootdir-mounted.sh b/hook-check-bootdir-mounted.sh new file mode 100755 index 0000000..452e894 --- /dev/null +++ b/hook-check-bootdir-mounted.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# This is the pre-transaction hook, to force user to check if /boot is mounted. +# If there's no device mounted at /boot, generates an error. + +mount | cut -d ' ' -f 3 | grep '^/boot$' > /dev/null +exit $? + + diff --git a/hook-kernel-rename.sh b/hook-kernel-rename.sh new file mode 100755 index 0000000..3543b4d --- /dev/null +++ b/hook-kernel-rename.sh @@ -0,0 +1,54 @@ +#!/bin/bash +# post-transaction hook, to rename the installed kernel basing on hostname. +# This hook must run before mkinitcpio hook. + +echo "Executing $0..." 1>&2 + +function generate_mkinitcpio_preset () { + template=' +# mkinitcpio preset file for the "__PKGBASE__" package + +ALL_config="/etc/mkinitcpio.conf" +ALL_kver="/boot/vmlinuz-__PKGBASE__" + +PRESETS=("default" "fallback") + +#default_config="/etc/mkinitcpio.conf" +default_image="/boot/initramfs-__PKGBASE__.img" +#default_options="" + +#fallback_config="/etc/mkinitcpio.conf" +fallback_image="/boot/initramfs-__PKGBASE__-fallback.img" +fallback_options="-S autodetect" +' + new_pkgbase="$1" + echo -n "$template" | sed "s/__PKGBASE__/$new_pkgbase/g" > "/etc/mkinitcpio.d/${new_pkgbase}.preset" + # Maybe we should delete old_pkgbase.preset. I'm not sure if it would crash something on `mkinitcpio -P` (may called by mkinitcpio-install) + return $? +} + +# Learned from /usr/share/libalpm/scripts/mkinitcpio-install +while read -r line; do + [[ $line != */vmlinuz ]] && continue + if ! read -r pkgbase > /dev/null 2>&1 < "${line%/vmlinuz}/pkgbase"; then + # if the kernel has no pkgbase, we skip it + continue + fi + + # Generates a filename for the kernel, and limit the length + new_pkgbase="${pkgbase}-$(hostname)" + new_pkgbase="${new_pkgbase:0:63}" + + # Generate mkinitcpio presets + generate_mkinitcpio_preset "${new_pkgbase}" && + + # Rename vmlinuz + mv "/boot/vmlinuz-${pkgbase}" "/boot/vmlinuz-${new_pkgbase}" && + + # Overwrite /usr/lib/modules/.../pkgbase to make mkinitcpio-install use the new filename + echo "${new_pkgbase}" > "${line%/vmlinuz}/pkgbase" && + + echo "Renamed pkgbase from $pkgbase to $new_pkgbase successfully. " 1>&2 || + exit $? # Crash on error +done + -- GitLab