Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions default/initcpio/hooks/omarchy-restore
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ omarchy_restore_get() {
sed -n "s/^$1=//p" "$OMARCHY_RESTORE_JOURNAL" | tail -1
}

omarchy_restore_children() {
# The journal separates paths with colons, not spaces within a path.
omarchy_restore_get CHILDREN | tr ':' '\n'
}

omarchy_restore_set() {
sed "/^$1=/d" "$OMARCHY_RESTORE_JOURNAL" >"$OMARCHY_RESTORE_JOURNAL.new" &&
echo "$1=$2" >>"$OMARCHY_RESTORE_JOURNAL.new" &&
Expand All @@ -168,12 +173,11 @@ omarchy_restore_is_subvol() {
}

omarchy_restore_apply() {
local top=$1 staged backup staged_uuid root_uuid child children
local top=$1 staged backup staged_uuid root_uuid child
staged=$(omarchy_restore_get STAGED)
backup=$(omarchy_restore_get BACKUP)
staged_uuid=$(omarchy_restore_get STAGED_UUID)
root_uuid=$(omarchy_restore_get ROOT_UUID)
children=$(omarchy_restore_get CHILDREN | sed 's/:/ /g')
omarchy_restore_set STATE swapping

# 1. The old root steps aside, unless an interrupted run already moved it.
Expand All @@ -193,7 +197,8 @@ omarchy_restore_apply() {
[ "$(omarchy_restore_uuid "$top/@")" = "$staged_uuid" ] || omarchy_restore_fail "@ is not the staged root" || return 1

# 3. Nested subvolumes move over, each onto an empty placeholder.
for child in $children; do
while IFS= read -r child; do
[ -n "$child" ] || continue
if omarchy_restore_is_subvol "$top/@/$child"; then
continue
fi
Expand All @@ -204,26 +209,29 @@ omarchy_restore_apply() {
mkdir -p "$(dirname "$top/@/$child")"
mv "$top/$backup/$child" "$top/@/$child" || omarchy_restore_fail "could not move $child" || return 1
sync
done
done <<EOF
$(omarchy_restore_children)
EOF
return 0
}

# Undoes whatever apply got through, in reverse.
omarchy_restore_rollback() {
local top=$1 staged backup staged_uuid root_uuid child children
local top=$1 staged backup staged_uuid root_uuid child
staged=$(omarchy_restore_get STAGED)
backup=$(omarchy_restore_get BACKUP)
staged_uuid=$(omarchy_restore_get STAGED_UUID)
root_uuid=$(omarchy_restore_get ROOT_UUID)
children=$(omarchy_restore_get CHILDREN | sed 's/:/ /g')

if [ "$(omarchy_restore_uuid "$top/@")" = "$staged_uuid" ]; then
for child in $children; do
while IFS= read -r child; do
[ -n "$child" ] || continue
if omarchy_restore_is_subvol "$top/@/$child" && [ ! -e "$top/$backup/$child" ]; then
mv "$top/@/$child" "$top/$backup/$child"
mkdir -p "$top/@/$child"
fi
done
done <<EOF
$(omarchy_restore_children)
EOF
mv "$top/@" "$top/$staged"
fi
if [ ! -e "$top/@" ] && [ "$(omarchy_restore_uuid "$top/$backup")" = "$root_uuid" ]; then
Expand Down
35 changes: 35 additions & 0 deletions test/shell.d/boot-restore-commit-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,41 @@ bash -c '
[[ ! -e $test_tmp/top/@/etc/systemd/sleep.conf.d/90-omarchy-restore-pending.conf ]] || fail "the hibernation block leaves @"
pass "cancelling a restore lifts the hibernation block in @"

# Exercise the initramfs moves on scratch directories with stand-ins for
# Btrfs UUIDs and subvolume identity.
top=$test_tmp/restore-top
mkdir -p "$top/@/vm images" "$top/@/swap" "$top/@restore-1/vm images" "$top/@restore-1/swap"
printf 'old\n' >"$top/@/uuid"
printf 'staged\n' >"$top/@restore-1/uuid"
touch "$top/@/vm images/guest.img" "$top/@/swap/swapfile"
printf 'STAGED=@restore-1\nBACKUP=@backup-1\nSTAGED_UUID=staged\nROOT_UUID=old\nCHILDREN=swap:vm images\n' >"$top/journal"

hook_shell=(sh)
if [[ -x /usr/lib/initcpio/busybox ]]; then
hook_shell=(/usr/lib/initcpio/busybox ash)
elif command -v busybox >/dev/null; then
hook_shell=(busybox ash)
fi

restore_hook() {
"${hook_shell[@]}" -c '
. "$1"
OMARCHY_RESTORE_JOURNAL=$2/journal
omarchy_restore_uuid() { cat "$1/uuid" 2>/dev/null; }
omarchy_restore_is_subvol() { [ -f "$1/guest.img" ] || [ -f "$1/swapfile" ]; }
err() { printf "%s\n" "$*" >&2; }
sync() { :; }
"$3" "$2"
' _ "$ROOT/default/initcpio/hooks/omarchy-restore" "$top" "omarchy_restore_$1"
}

restore_hook apply && restore_hook apply || fail "applying a restore preserves spaces in child subvolume names"
[[ -f "$top/@/vm images/guest.img" && -f $top/@/swap/swapfile ]] || fail "both child subvolumes move into the restored root"
restore_hook rollback && restore_hook rollback || fail "an interrupted restore can move both children back"
[[ $(<"$top/@/uuid") == old && -f "$top/@/vm images/guest.img" && -f $top/@/swap/swapfile &&
! -e "$top/@restore-1/vm images/guest.img" ]] || fail "rollback returns both children to the original root"
pass "initramfs restore and rollback keep child subvolume paths intact"

# Swap header magic lives at PAGE_SIZE - 10 (4086 on 4 KiB, 16374 on 16 KiB,
# 65526 on 64 KiB), so restore-stage must check the host's page size rather
# than hardcoding 4086.
Expand Down