Skip to content
Closed
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
46 changes: 29 additions & 17 deletions bin/omarchy-hibernation-setup
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ fi

if [[ ! -f /sys/power/image_size ]]; then
echo -e "Hibernation is not supported on your system" >&2
exit 0
exit 1
fi

# When --no-rebuild is set, the caller is responsible for the UKI rebuild
# (e.g. running before limine-mkinitcpio-hook is installed during initial
# install), so we only require limine-mkinitcpio when we'd invoke it ourselves.
if ! $NO_REBUILD && omarchy-cmd-missing limine-mkinitcpio; then
echo "Skipping hibernation setup (requires Limine bootloader)"
exit 0
echo "Skipping hibernation setup (requires Limine bootloader)" >&2
exit 1
fi

MKINITCPIO_CONF="/etc/mkinitcpio.conf.d/omarchy_resume.conf"
Expand Down Expand Up @@ -93,15 +93,25 @@ resume_device() {
# the drop-in changes stays until a rebuild succeeds. A failed or interrupted
# rebuild then leaves the marker, and the next run rebuilds again.
rename_resume_device() {
local previous device offset staged
local previous previous_device device="" offset staged

[[ -f $RESUME_DROP_IN && -f $SWAP_FILE ]] || return 0
[[ -f $RESUME_DROP_IN ]] || return 0
previous=$(<"$RESUME_DROP_IN")
[[ $previous =~ ^KERNEL_CMDLINE\[default\]\+=\"\ ?resume=(/dev/[^[:space:]\"]+)\ resume_offset=([0-9]+)\"$ ]] || return 0
[[ ${BASH_REMATCH[1]} != /dev/mapper/* ]] || return 0
[[ $previous =~ ^KERNEL_CMDLINE\[default\]\+=\"\ ?resume=(/dev/[^[:space:]\"]+)?\ resume_offset=([0-9]+)\"$ ]] || return 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Empty offsets skip device repair

If a legacy drop-in contains resume= resume_offset= and map-swapfile still fails, this regex returns success without repairing either value. The new migration matches that line, but setup exits successfully and the migration is marked complete. Fixing the mount later will not trigger another repair.

Detect the empty device independently of the offset and return non-zero until both values can be repaired. This also satisfies the migration rule that unfinished repairs must remain pending.

File Used: agents/skills/migrations.md (source)

previous_device=${BASH_REMATCH[1]}
offset=${BASH_REMATCH[2]}
device=$(resume_device)
[[ $device == UUID=* ]] || return 0
[[ $previous_device != /dev/mapper/* ]] || return 0
if [[ -f $SWAP_FILE ]]; then
device=$(resume_device)
fi
if [[ -z $previous_device ]]; then
if [[ $device != UUID=?* && $device != /dev/mapper/?* ]]; then
echo "Could not determine the resume device for $SWAP_FILE. Fix the swapfile mount, then re-run omarchy-hibernation-setup." >&2
return 1
fi
else
[[ $device == UUID=?* ]] || return 0
fi

staged=$(mktemp) || return 1
if printf 'KERNEL_CMDLINE[default]+=" resume=%s resume_offset=%s"\n' "$device" "$offset" >"$staged" &&
Expand Down Expand Up @@ -138,7 +148,7 @@ rebuild_renamed_resume_device() {
}

# Check if hibernation is already configured
if [[ -f $MKINITCPIO_CONF ]] && grep -q "^HOOKS+=(resume)$" "$MKINITCPIO_CONF"; then
if [[ -f $MKINITCPIO_CONF && -f $RESUME_DROP_IN ]] && grep -q "^HOOKS+=(resume)$" "$MKINITCPIO_CONF"; then
# Fix empty resume_offset if btrfs map-swapfile failed during initial setup
if [[ -f $RESUME_DROP_IN ]] && grep -q 'resume_offset="$' "$RESUME_DROP_IN" && [[ -f $SWAP_FILE ]]; then
RESUME_OFFSET=$(sudo btrfs inspect-internal map-swapfile -r "$SWAP_FILE" 2>/dev/null)
Expand Down Expand Up @@ -203,26 +213,28 @@ if ! install_root_file "$OMARCHY_PATH/default/systemd/system-sleep/keyboard-back
exit 1
fi

# Add resume hook to mkinitcpio
sudo mkdir -p /etc/mkinitcpio.conf.d
echo "Adding resume hook to $MKINITCPIO_CONF"
echo "HOOKS+=(resume)" | sudo tee "$MKINITCPIO_CONF" >/dev/null

# Add resume= kernel parameters so the initramfs resume hook knows where to find the
# hibernation image. Without these, resume happens late (after GPU drivers load) and fails.
if [[ ! -f $RESUME_DROP_IN ]]; then
echo "Adding resume kernel parameters"
sudo swapon -p 0 "$SWAP_FILE" 2>/dev/null
RESUME_DEVICE=$(resume_device)
RESUME_OFFSET=$(sudo btrfs inspect-internal map-swapfile -r "$SWAP_FILE")
if [[ -n $RESUME_OFFSET ]]; then
# An empty device writes `resume= resume_offset=N`. The factory reset's chroot finds
# no device, so leave the drop-in out without failing; a rerun adds it.
if [[ -n $RESUME_DEVICE && -n $RESUME_OFFSET ]]; then
sudo mkdir -p /etc/limine-entry-tool.d
echo "KERNEL_CMDLINE[default]+=\" resume=$RESUME_DEVICE resume_offset=$RESUME_OFFSET\"" | sudo tee "$RESUME_DROP_IN" >/dev/null
else
echo "Warning: Could not determine resume offset for $SWAP_FILE" >&2
echo "Warning: Could not determine resume device and offset for $SWAP_FILE (device='$RESUME_DEVICE' offset='$RESUME_OFFSET'); re-run omarchy-hibernation-setup to add them" >&2
Comment on lines +223 to +229

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Reset leaves resume settings missing

When findmnt finds no device in the factory-reset chroot, setup now succeeds without writing resume.conf. The reset accepts that success and rebuilds the boot files, but no later step reruns setup to add the missing resume parameters.

Setup still writes omarchy_resume.conf, so omarchy-hibernation-available can offer Hibernate even though the boot files lack the parameters needed to restore the session. Determine the device before completing the reset, or arrange a required repair before hibernation becomes available.

fi
fi

# Add resume hook to mkinitcpio
sudo mkdir -p /etc/mkinitcpio.conf.d
echo "Adding resume hook to $MKINITCPIO_CONF"
echo "HOOKS+=(resume)" | sudo tee "$MKINITCPIO_CONF" >/dev/null

# Use ACPI alarm for RTC wakeup on s2idle systems (needed for suspend-then-hibernate)
if grep -q "\[s2idle\]" /sys/power/mem_sleep 2>/dev/null; then
LIMINE_DROP_IN="/etc/limine-entry-tool.d/rtc-alarm.conf"
Expand Down
17 changes: 17 additions & 0 deletions migrations/1789972000.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
echo "Repair hibernation drop-ins with an empty resume= device"

drop_in=/etc/limine-entry-tool.d/resume.conf
hook=/etc/mkinitcpio.conf.d/omarchy_resume.conf
pending=/var/lib/omarchy/hibernation-resume-rebuild

if [[ ! -e $pending ]]; then
[[ -f $drop_in ]] && grep -Eq 'resume=[[:space:]]+resume_offset=' "$drop_in" || exit 0
fi

# omarchy-hibernation-remove leaves the drop-in behind; without the hook, setup
# would create the swapfile again.
[[ -f $hook ]] && grep -q '^HOOKS+=(resume)$' "$hook" || exit 0

# Setup repairs the drop-in atomically and keeps this migration pending if the
# device is unavailable or the boot image could not be rebuilt.
omarchy-hibernation-setup --force
24 changes: 23 additions & 1 deletion test/shell.d/hibernation-setup-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ machine() {
}

setup() {
env PATH="$rtmp/bin:$PATH" SWAP_FILE_PATH="$rtmp/swapfile" SWAP_SOURCE="$SWAP_SOURCE" SWAP_UUID="$uuid" \
env PATH="$rtmp/bin:$PATH" SWAP_FILE_PATH="$rtmp/swapfile" SWAP_SOURCE="$SWAP_SOURCE" SWAP_UUID="${SWAP_UUID-$uuid}" \
DROP_IN="$drop_in" REBUILD_LOG="$rtmp/rebuilds" REBUILD_RESULT="${REBUILD_RESULT:-ok}" \
bash "$rtmp/setup" "$@" >"$rtmp/out" 2>&1
}
Expand Down Expand Up @@ -106,6 +106,28 @@ for line in 'KERNEL_CMDLINE[default]+=" resume=/dev/nvme1n1p2 resume_offset=4242
done
pass "a rerun renames a kernel-name resume device once"

empty_device_line='KERNEL_CMDLINE[default]+=" resume= resume_offset=4242"'
machine "$empty_device_line"
SWAP_SOURCE="/dev/sda2[/@]" setup || fail "setup repairs an empty resume device" "$(<"$rtmp/out")"
[[ $(<"$drop_in") == "$uuid_line" && $(<"$rtmp/rebuilds") == "$uuid_line" ]] ||
fail "setup rebuilds from the repaired resume device"
machine "$empty_device_line"
if SWAP_SOURCE="" SWAP_UUID="" setup; then
fail "setup reports success without a resume device"
fi
[[ $(<"$drop_in") == "$empty_device_line" && ! -s $rtmp/rebuilds ]] ||
fail "setup leaves the drop-in untouched when the resume device is unknown"
pass "setup repairs an empty device only when it can find a stable replacement"

# The factory reset runs setup in a chroot where findmnt finds no device.
machine
SWAP_SOURCE="" SWAP_UUID="" setup --force --no-rebuild || fail "setup without a resume device" "$(<"$rtmp/out")"
[[ ! -e $drop_in && -f $rtmp/mkinitcpio.conf.d/omarchy_resume.conf ]] ||
fail "setup writes no empty resume device" "$(cat "$drop_in" 2>&1)"
SWAP_SOURCE="/dev/sda2[/@]" setup --force --no-rebuild || fail "a rerun once the device is found" "$(<"$rtmp/out")"
[[ $(<"$drop_in") == "$uuid_line" ]] || fail "a rerun adds the resume parameters" "$(cat "$drop_in" 2>&1)"
pass "setup without a resume device finishes, and a rerun adds the parameters"

# If a kernel's image is not rebuilt, or the rebuild is interrupted, the drop-in
# is already renamed; the next run sees the pending rebuild and does it, once.
for result in skip interrupt; do
Expand Down
Loading