diff options
-rw-r--r-- | prepare-lastboot | 36 | ||||
-rw-r--r-- | prepare-lastboot@.service | 6 | ||||
-rw-r--r-- | prepare-lastboot@.timer | 8 |
3 files changed, 50 insertions, 0 deletions
diff --git a/prepare-lastboot b/prepare-lastboot new file mode 100644 index 0000000..c8139aa --- /dev/null +++ b/prepare-lastboot @@ -0,0 +1,36 @@ +#!/bin/bash + +DEFAULT="$1" +LASTBOOT="lastboot" + +ROOT_FS=$(findmnt -no SOURCE /) +BASE="${ROOT_FS%/*}" + +EFI_DIR="/efi" + +# default UKI name used by kernel-install +UKI_NAME="$(cat /etc/machine-id)-$(uname -r).efi" + +if [ "$ROOT_FS" = "$BASE/$DEFAULT" ]; then + echo "sucessfully booted to $BASE/$DEFAULT, preparing lastboot" + + # destroy the old lastboot file system and children + zfs destroy -Rv "$BASE/$DEFAULT@lastboot" + + # make a new lastboot file system + zfs snap -r "$BASE/$DEFAULT@lastboot" + + for OLDFS in $(zfs list -rHo name -t filesystem "$BASE/$DEFAULT"); do + NEWFS=$(sed "s|^$BASE/$DEFAULT|$BASE/$LASTBOOT-$DEFAULT|" <<< $OLDFS) + echo "cloning $OLDFS to $NEWFS" + zfs clone -o canmount=noauto -o mountpoint=none "$OLDFS@lastboot" "$NEWFS" + done + + zfs set mountpoint=/ "$BASE/$LASTBOOT-$DEFAULT" + + # backup the current UKI + echo "copying $UKI_NAME to lastboot.efi" + cp "$EFI_DIR/EFI/Linux/$UKI_NAME" "$EFI_DIR/EFI/Linux/lastboot.efi" +else + echo "not booted to $BASE/$DEFAULT, taking no action" +fi diff --git a/prepare-lastboot@.service b/prepare-lastboot@.service new file mode 100644 index 0000000..f437452 --- /dev/null +++ b/prepare-lastboot@.service @@ -0,0 +1,6 @@ +[Unit] +Description=Prepares lastboot filesystem of %i for fallback + +[Service] +Type = oneshot +ExecStart = /usr/bin/prepare-lastboot "%i" diff --git a/prepare-lastboot@.timer b/prepare-lastboot@.timer new file mode 100644 index 0000000..a99ea82 --- /dev/null +++ b/prepare-lastboot@.timer @@ -0,0 +1,8 @@ +[Unit] +Description=Run prepare_lastboot after a successful boot of %i + +[Timer] +OnBootSec=30 + +[Install] +WantedBy=timers.target |