From 7cba17357bbd427040303883ab42823ed41a21d9 Mon Sep 17 00:00:00 2001 From: Adrian Wannenmacher Date: Wed, 10 Feb 2021 21:32:05 +0100 Subject: [PATCH] refactor: :recycle: start rewrite BREAKING CHANGE: everything --- README.md | 8 -- hooks/01_snap-pac-uki_pre.hook | 12 --- hooks/zzy_snap-pac-uki_post.hook | 11 --- hooks/zzz_snap-pac-uki_cleanup.hook | 11 --- install.fish | 4 - scripts/snap-pac-uki_cleanup.fish | 17 ---- scripts/snap-pac-uki_create.fish | 123 ++++++++++++++++++++-------- uninstall.fish | 9 -- 8 files changed, 90 insertions(+), 105 deletions(-) delete mode 100644 hooks/01_snap-pac-uki_pre.hook delete mode 100644 hooks/zzy_snap-pac-uki_post.hook delete mode 100644 hooks/zzz_snap-pac-uki_cleanup.hook delete mode 100755 install.fish delete mode 100755 scripts/snap-pac-uki_cleanup.fish delete mode 100755 uninstall.fish diff --git a/README.md b/README.md index c2343b5..14c4a12 100644 --- a/README.md +++ b/README.md @@ -1,9 +1 @@ # snap-pac-uki -Pacman hooks creating unified kernel images for snapshots created by snap-pac. - -## Limits -- only works on the "root" snap-pac config -- puts all UKIs into `/boot/EFI/Linux/` -- kernel parameters are extracted from a file called `/boot/loader/entries/00-arch.conf` -- expects that the default subvolume is mounted via kernel command line -- expects that the default subvolume is called `@` diff --git a/hooks/01_snap-pac-uki_pre.hook b/hooks/01_snap-pac-uki_pre.hook deleted file mode 100644 index ad022a9..0000000 --- a/hooks/01_snap-pac-uki_pre.hook +++ /dev/null @@ -1,12 +0,0 @@ -[Trigger] -Operation = Upgrade -Operation = Install -Operation = Remove -Type = Package -Target = * - -[Action] -Description = Performing pre UKI creation... -When = PreTransaction -Exec = /usr/share/libalpm/scripts/snap-pac-uki_create.fish -AbortOnFail diff --git a/hooks/zzy_snap-pac-uki_post.hook b/hooks/zzy_snap-pac-uki_post.hook deleted file mode 100644 index 8c7711a..0000000 --- a/hooks/zzy_snap-pac-uki_post.hook +++ /dev/null @@ -1,11 +0,0 @@ -[Trigger] -Operation = Upgrade -Operation = Install -Operation = Remove -Type = Package -Target = * - -[Action] -Description = Performing post UKI creation... -When = PostTransaction -Exec = /usr/share/libalpm/scripts/snap-pac-uki_create.fish diff --git a/hooks/zzz_snap-pac-uki_cleanup.hook b/hooks/zzz_snap-pac-uki_cleanup.hook deleted file mode 100644 index 0f6869d..0000000 --- a/hooks/zzz_snap-pac-uki_cleanup.hook +++ /dev/null @@ -1,11 +0,0 @@ -[Trigger] -Operation = Upgrade -Operation = Install -Operation = Remove -Type = Package -Target = * - -[Action] -Description = Performing UKI cleanup... -When = PostTransaction -Exec = /usr/share/libalpm/scripts/snap-pac-uki_cleanup.fish diff --git a/install.fish b/install.fish deleted file mode 100755 index 95b094d..0000000 --- a/install.fish +++ /dev/null @@ -1,4 +0,0 @@ -#!/bin/fish - -cp ./hooks/* /usr/share/libalpm/hooks/ -cp ./scripts/* /usr/share/libalpm/scripts/ diff --git a/scripts/snap-pac-uki_cleanup.fish b/scripts/snap-pac-uki_cleanup.fish deleted file mode 100755 index 0657cc8..0000000 --- a/scripts/snap-pac-uki_cleanup.fish +++ /dev/null @@ -1,17 +0,0 @@ -#!/bin/fish - -# get snapshots -set -l config "root" -set -l snapshot_ids (snapper -c $config --jsonout ls | jq ".$config | .[] | .number") - - -# get current ukis -set -l ukis (ls /boot/EFI/Linux/ | grep '^snap-pac-uki-[1-9][0-9]*\.efi$' | grep -o "[1-9][0-9]*") - -for x in $ukis - if not contains $x $snapshot_ids - set -l path "/boot/EFI/Linux/snap-pac-uki-$x.efi" - rm $path - echo "==> removed UKI: $path" - end -end diff --git a/scripts/snap-pac-uki_create.fish b/scripts/snap-pac-uki_create.fish index 7174905..dfc10e6 100755 --- a/scripts/snap-pac-uki_create.fish +++ b/scripts/snap-pac-uki_create.fish @@ -1,46 +1,103 @@ #!/bin/fish -# get id and type of last snapshot -set -l config "root" -set -l snapshot (snapper -c $config --jsonout ls | jq -c ".$config | last") -set -l id (echo $snapshot | jq ".number") +function error -d "prints an error and terminates the program" -a error + echo $error -if not contains (echo $snapshot | jq -r ".type") "pre" "post" - echo "==> last snapshot type is neither 'pre' nor 'post'; no UKI creation" - exit + exit 1 end +function checkdeps -d "check for depenencies" + set -l deps "jq" "snapper" -# check if UKI already exists -set -l ukis (ls /boot/EFI/Linux/ | grep '^snap-pac-uki-[1-9][0-9]*\.efi$' | grep -o "[1-9][0-9]*") - -if contains $id $ukis - echo "==> UKI already exists; no UKI creation" - exit + for dep in $deps + which $dep >/dev/null 2>/dev/null + if test $status != "0" + error "didn't find depenecy: $dep" + end + end end +function load_config -d "loads the config from a config file" -a cfg + # set default values + set -g BUILD_PATH "/tmp/snap-pac-uki" + set -g EFI_DIR "/efi/EFI/Linux/" + set -g SNAPPER_CONFIG "root" + set -g SNAPSHOT_SUBVOL "@snapshots" -# prepare uki creation -set -l buildpath "/tmp/snap-pac-uki/$id" -mkdir -p $buildpath -rm -rf "$buildpath/*" -cd $buildpath + # load file + set -l cfg (jq -c <$cfg) -sed "s/BUILD_ID=.*/BUILD_ID=\"Snapshot $id\"/" ./os-release + # parse config + set -l cfgs "build_path" "efi_dir" "kernel_cmd" "fallback_cmd" "snapper_config" "snapshot_subvol" + for c in $cfgs + echo $cfg | jq -e ".$c" >/dev/null -grep options /boot/loader/entries/00-arch.conf | \ - string sub -s 9 | \ - sed ':a;N;$!ba;s/\n/ /g' | \ - sed "s/subvol=@/subvol=@snapshots\\/$id\\/snapshot/" >./kernel-parameters + switch $status + case "0" + set -g (echo $c | sed "s/./\U&/g") (echo $cfg | jq -r ".$c") + case "4" + error "failed to parse option: $c" + end + end + # check for mandatory options + set -l mandatory_cfgs "kernel_cmd" "fallback_cmd" + for mc in $mandatory_cfgs + set -gq (echo $mc | sed "s/./\U&/g") -# create uki -set -l ukipath "/boot/EFI/Linux/snap-pac-uki-$id.efi" -objcopy \ - --add-section .osrel="./os-release" --change-section-vma .osrel=0x20000 \ - --add-section .cmdline="./kernel-parameters" --change-section-vma .cmdline=0x30000 \ - --add-section .splash="/usr/share/systemd/bootctl/splash-arch.bmp" --change-section-vma .splash=0x40000 \ - --add-section .linux="/boot/vmlinuz-linux" --change-section-vma .linux=0x2000000 \ - --add-section .initrd="/boot/initramfs-linux.img" --change-section-vma .initrd=0x3000000 \ - "/usr/lib/systemd/boot/efi/linuxx64.efi.stub" $ukipath -echo "==> created UKI: $ukipath" + if test $status != 0 + error "missing config parameter: $mc" + end + end +end + +function get_last_snapshot -d "reads the last snapper snapshot id" + set -g SNAPSHOT_ID (snapper -c $SNAPPER_CONFIG --jsonout ls | jq ".$SNAPPER_CONFIG | last | .number") + echo $SNAPSHOT_ID +end + +function create_uki -d "creates a new uki" -a fallback + # prepare directory + set -l bp "$BUILD_PATH/$SNAPSHOT_ID" + mkdir -p $bp + rm -rf "$bp/*" + + # os-release + sed "s/BUILD_ID=.*/BUILD_ID=\"Snapshot $SNAPSHOT_ID\"/" "$bp/os-release" + + # kernel cmd + echo $KERNEL_CMD | sed "s/SNAPSHOT/$SNAPSHOT_SUBVOL\\/$SNAPSHOT_ID\\/snapshot/" >"$bp/kernel-cmd" + + # create uki + objcopy \ + --add-section .osrel="$bp/os-release" --change-section-vma .osrel=0x20000 \ + --add-section .cmdline="$bp/kernel-cmd" --change-section-vma .cmdline=0x30000 \ + --add-section .splash="/usr/share/systemd/bootctl/splash-arch.bmp" --change-section-vma .splash=0x40000 \ + --add-section .linux="/boot/vmlinuz-linux" --change-section-vma .linux=0x2000000 \ + --add-section .initrd="/boot/initramfs-linux$fallback.img" --change-section-vma .initrd=0x3000000 \ + "/usr/lib/systemd/boot/efi/linuxx64.efi.stub" "$bp/arch-linux-$SNAPSHOT_ID$fallback.efi" +end + +function copy_current -d "copies new ukis to efi" + # copy to "current" + cp "$BUILD_PATH/$SNAPSHOT_ID/arch-linux-$SNAPSHOT_ID.efi" "$EFI_DIR/arch-linux-current.efi" + cp "$BUILD_PATH/$SNAPSHOT_ID/arch-linux-$SNAPSHOT_ID-fallback.efi" "$EFI_DIR/arch-linux-fallback.efi" + + # copy to "snapshots" + cp "$BUILD_PATH/$SNAPSHOT_ID/arch-linux-$SNAPSHOT_ID.efi" $EFI_DIR +end + +function main -d "the main function" + checkdeps + load_config "/etc/snap-pac-uki.json" + + get_last_snapshot + create_uki + create_uki "-fallback" + + copy_current + + exit 0 +end + +main diff --git a/uninstall.fish b/uninstall.fish deleted file mode 100755 index ab80864..0000000 --- a/uninstall.fish +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/fish - -for x in (ls ./hooks) - rm "/usr/share/libalpm/hooks/$x" -end - -for x in (ls ./scripts) - rm "/usr/share/libalpm/scripts/$x" -end