summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJaron Kent-Dobias <jaron@kent-dobias.com>2023-10-27 10:19:05 +0200
committerJaron Kent-Dobias <jaron@kent-dobias.com>2023-10-27 10:19:05 +0200
commitf7706ab5a7a4e6fb4d8d91c32ec0009e37c49b0f (patch)
tree79d5c85d74747ee6340f3c5276dbdccbd216b0b7
downloadprepare-lastboot-f7706ab5a7a4e6fb4d8d91c32ec0009e37c49b0f.tar.gz
prepare-lastboot-f7706ab5a7a4e6fb4d8d91c32ec0009e37c49b0f.tar.bz2
prepare-lastboot-f7706ab5a7a4e6fb4d8d91c32ec0009e37c49b0f.zip
Intial commit.v1.0
-rw-r--r--prepare-lastboot36
-rw-r--r--prepare-lastboot@.service6
-rw-r--r--prepare-lastboot@.timer8
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