diff --git a/55-check-boot-mounted.hook b/55-check-boot-mounted.hook new file mode 100644 index 0000000000000000000000000000000000000000..386ea1845e76933529f7960699093b34868db54c --- /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 0000000000000000000000000000000000000000..3b266132f89a5d621cb78cb75ad6534d7c124050 --- /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 47f5b472ca1935d8312c8bd650376a8f7e4d253c..3daa82cececc9a989b3ed99aab95d8a596067a76 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 0000000000000000000000000000000000000000..452e8946fe871c7ecae4ec0410faf46f0b905b33 --- /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 0000000000000000000000000000000000000000..3543b4d6b6432a422965f4dcf7a82422211d8a41 --- /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 +