#!/bin/fish function error -d "prints an error and terminates the program" -a error echo $error exit 1 end function checkdeps -d "check for depenencies" set -l deps "jq" "snapper" 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" # load file set -l cfg (jq -c <$cfg) # 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 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") 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