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
9 changes: 9 additions & 0 deletions bin/omarchy-hibernation-remove
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ fi
echo "Removing resume hook"
sudo rm "$MKINITCPIO_CONF"

# The drop-caches hook is hibernation's too: it does nothing outside a
# hibernate, so leaving it would be inert rather than harmful, but hibernation
# is what it belongs to. The keyboard-backlight hook stays - it is published for
# hardware that needs it, not for hibernation.
if [[ -e /usr/lib/systemd/system-sleep/drop-caches ]]; then
echo "Removing hibernate drop-caches hook"
sudo rm -f /usr/lib/systemd/system-sleep/drop-caches
fi

echo "Regenerating initramfs..."
sudo limine-mkinitcpio

Expand Down
11 changes: 8 additions & 3 deletions bin/omarchy-hibernation-setup
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,19 @@ if ! swapon --show | grep -q "$SWAP_FILE"; then
sudo swapon -p 0 "$SWAP_FILE"
fi

# Ensure keyboard backlight doesn't prevent sleep
# Install this before writing the resume marker so a failed install remains
# retryable through the normal setup command.
# Ensure keyboard backlight doesn't prevent sleep and speed up hibernation
# entry by dropping clean caches before the snapshot allocation. Install these
# before writing the resume marker so a failed install remains retryable.
if ! install_root_file "$OMARCHY_PATH/default/systemd/system-sleep/keyboard-backlight" \
/usr/lib/systemd/system-sleep/keyboard-backlight 0755; then
echo "Could not install the keyboard-backlight system-sleep hook" >&2
exit 1
fi
if ! install_root_file "$OMARCHY_PATH/default/systemd/system-sleep/drop-caches" \
/usr/lib/systemd/system-sleep/drop-caches 0755; then
echo "Could not install the drop-caches system-sleep hook" >&2
exit 1
fi

# Add resume hook to mkinitcpio
sudo mkdir -p /etc/mkinitcpio.conf.d
Expand Down
37 changes: 37 additions & 0 deletions default/systemd/system-sleep/drop-caches
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

# Omarchy: speed up hibernate entry by dropping clean caches before the
# snapshot allocation, and by letting image compression use more than the
# default three threads.
#
# Dropping the cache first leaves the allocator less to reclaim, and less
# clean cache to compress on the way into the image. Reading mm/vmscan.c
# suggests the compress half is what matters here: Omarchy runs
# vm.swappiness=150, where reclaim favours anonymous pages over file cache, so
# "the kernel would discard clean caches anyway" is not what the gain comes
# from. That is reasoning from the kernel source, not a measurement; the
# measured effect is in the PR discussion, along with the remaining entry time,
# which is anonymous pages being evicted into zram.
#
# Dropping level 3 also frees reclaimable dentries and inodes, which cost
# something to rebuild after resume - see the vfs_cache_pressure note in
# etc/sysctl.d. Kept at 3 because that is the measured configuration.

sleep_action=${SYSTEMD_SLEEP_ACTION:-$2}

if [[ $1 == "pre" && $sleep_action == "hibernate" ]]; then
# Writes nproc, which the kernel clamps to num_online_cpus()-1, so this asks
# for one thread fewer than nproc and replaces any lower value a
# hibernate_compression_threads= boot parameter set. The knob exists from
# Linux 6.19; older kernels skip it here and keep the default. It does not
# persist across boots, so it is reapplied whenever hibernation runs.
if [[ -w /sys/power/hibernate_compression_threads ]]; then
nproc=$(nproc 2>/dev/null || echo 0)
if [[ $nproc -gt 0 ]]; then
echo "$nproc" > /sys/power/hibernate_compression_threads 2>/dev/null || true
fi
fi

sync
echo 3 > /proc/sys/vm/drop_caches 2>/dev/null || true
fi
2 changes: 1 addition & 1 deletion docs/file-layout.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ logo.{txt,svg}, icon.{txt,png} ──► omarchy-settings /usr/share/omarchy
/etc/skel/.config/omarchy/branding/{about,screensaver}.txt
```

The hardware-conditional `force-igpu` and `keyboard-backlight` sources also live under `default/systemd/system-sleep/`, but their setup commands publish root-owned copies only on machines that need them; they are not installed by `omarchy-settings`.
The hibernation-conditional `drop-caches` source also lives there: `omarchy-hibernation-setup` publishes it for new setups, and `migrations/1790692395.sh` installs it on machines that were already configured before it existed. The hardware-conditional `force-igpu` and `keyboard-backlight` sources likewise publish root-owned copies only on machines that need them. None of them are installed by `omarchy-settings`, so a later edit to one of them reaches installed machines only through another migration.

### Why `etc-overrides/` exists

Expand Down
31 changes: 31 additions & 0 deletions migrations/1790692395.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
echo "Install the hibernate drop-caches system-sleep hook"

# Machines set up before this hook existed already have the resume marker, and
# omarchy-hibernation-setup returns early as soon as it sees that marker, so
# re-running it never installs the hook on the machines that need it. The hook
# does nothing outside a hibernate, so installing it wherever hibernation is
# configured is harmless.
resume_conf=/etc/mkinitcpio.conf.d/omarchy_resume.conf
hook=/usr/lib/systemd/system-sleep/drop-caches

as_root() {
if (( EUID == 0 )); then
"$@"
else
sudo "$@"
fi
}

# Only for machines that actually hibernate: the marker omarchy-hibernation-setup
# writes, and the same one it checks.
if [[ ! -f $resume_conf ]] || ! grep -q '^HOOKS+=(resume)$' "$resume_conf"; then
exit 0
fi

# A hook that is already there belongs to whoever put it there - a newer setup,
# or a hand edit. This migration only supplies what is missing.
if [[ -e $hook || -L $hook ]]; then
exit 0
fi

as_root /usr/bin/install -Dm0755 "$OMARCHY_PATH/default/systemd/system-sleep/drop-caches" "$hook"
137 changes: 137 additions & 0 deletions test/shell.d/hibernate-drop-caches-migration-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
#!/bin/bash

set -euo pipefail

source "$(dirname "$0")/base-test.sh"

shipped_migration="$ROOT/migrations/1790692395.sh"
[[ -f $shipped_migration ]] || fail "the hibernate drop-caches migration exists at $shipped_migration"
pass "the hibernate drop-caches migration exists"

mapfile -t drop_caches_migrations < <(grep -RIlE 'system-sleep/drop-caches' "$ROOT/migrations")
(( ${#drop_caches_migrations[@]} == 1 )) && [[ ${drop_caches_migrations[0]} == "$shipped_migration" ]] ||
fail "one migration exclusively owns the drop-caches hook path" "${drop_caches_migrations[*]}"
pass "one migration exclusively owns the drop-caches hook path"

# Installed by setup for new machines, by the migration for machines that were
# configured before it existed, and removed by hibernation removal. One place
# per direction: a fourth would be a lifecycle that drifts out of step, and a
# missing removal leaves the hook behind after hibernation is gone.
mapfile -t hook_owners < <(grep -RIl '/usr/lib/systemd/system-sleep/drop-caches' \
"$ROOT/bin" "$ROOT/migrations" | sed "s|^$ROOT/||" | sort)
[[ ${hook_owners[*]} == "bin/omarchy-hibernation-remove bin/omarchy-hibernation-setup migrations/1790692395.sh" ]] ||
fail "setup, the migration and hibernation removal are the hook's only owners" "${hook_owners[*]}"
pass "the hook has one owner per direction"

test_dir=$(mktemp -d)
trap 'rm -rf "$test_dir"' EXIT

mkdir -p "$test_dir/bin"

# sudo runs the real command, so the install lands in the redirected hook path.
cat >"$test_dir/bin/sudo" <<'STUB'
#!/bin/bash

printf 'sudo %s\n' "$*" >>"$CALLS"
exec "$@"
STUB

# The migration has to name the privileged installer by absolute path:
# resolving install through PATH would let the caller choose what runs as root.
cat >"$test_dir/bin/install" <<'STUB'
#!/bin/bash

echo "migration resolved install through PATH" >&2
exit 97
STUB

chmod +x "$test_dir/bin/"*

export CALLS="$test_dir/calls"

resume_conf="$test_dir/mkinitcpio.d/omarchy_resume.conf"
hook="$test_dir/system-sleep/drop-caches"
omarchy_path="$test_dir/omarchy"
shipped_hook="$omarchy_path/default/systemd/system-sleep/drop-caches"
migration="$test_dir/migration.sh"

# These paths become operands to a privileged command. Keep them fixed in the
# shipped migration and retarget a scratch copy for the unprivileged test; an
# environment override would let the caller choose what root writes.
grep -Fxq 'resume_conf=/etc/mkinitcpio.conf.d/omarchy_resume.conf' "$shipped_migration" ||
fail "the production resume marker path is a fixed literal"
grep -Fxq 'hook=/usr/lib/systemd/system-sleep/drop-caches' "$shipped_migration" ||
fail "the production hook path is a fixed literal"
if grep -q 'OMARCHY_DROP_CACHES' "$shipped_migration"; then
fail "the migration does not accept caller-controlled privileged paths"
fi

sed \
-e "s|^resume_conf=/etc/mkinitcpio.conf.d/omarchy_resume.conf$|resume_conf=$resume_conf|" \
-e "s|^hook=/usr/lib/systemd/system-sleep/drop-caches$|hook=$hook|" \
"$shipped_migration" >"$migration"
pass "migration keeps privileged production paths caller-independent"

reset_machine() {
rm -rf "$test_dir/mkinitcpio.d" "$test_dir/system-sleep" "$omarchy_path"
mkdir -p "$(dirname "$resume_conf")" "$(dirname "$shipped_hook")"
printf 'HOOKS+=(resume)\n' >"$resume_conf"
cat >"$shipped_hook" <<'HOOK'
#!/bin/bash

echo 3 > /proc/sys/vm/drop_caches
HOOK
}

run_migration() {
: >"$CALLS"

OMARCHY_PATH="$omarchy_path" \
PATH="$test_dir/bin:$PATH" \
bash -euo pipefail "$migration" >/dev/null
}

# The machine this migration exists for: set up when the resume marker alone
# meant "already done", so setup returns before installing the hook.
reset_machine
run_migration
[[ -f $hook ]] || fail "migration installs the hook on an already configured machine" "$(cat "$CALLS")"
cmp -s "$shipped_hook" "$hook" || fail "migration installs the shipped hook unchanged"
[[ $(stat -c '%a' "$hook") == "755" ]] || fail "migration installs the hook mode 0755"
grep -q '^sudo /usr/bin/install -Dm0755 ' "$CALLS" ||
fail "migration installs through the absolute privileged installer" "$(cat "$CALLS")"
pass "migration installs the hook on a machine that already hibernates"

# Running again changes nothing and asks for no privilege.
before=$(sha256sum "$hook")
run_migration
[[ $(sha256sum "$hook") == "$before" ]] || fail "migration rewrites an installed hook"
[[ ! -s $CALLS ]] || fail "migration installs a second time" "$(cat "$CALLS")"
pass "migration is idempotent once the hook is installed"

# A hook somebody else put there is not this migration's to replace.
reset_machine
mkdir -p "$(dirname "$hook")"
printf '#!/bin/bash\n\n# hand edited\n' >"$hook"
before=$(sha256sum "$hook")
run_migration
[[ $(sha256sum "$hook") == "$before" ]] || fail "migration overwrites an existing hook" "$(cat "$CALLS")"
[[ ! -s $CALLS ]] || fail "migration acts when a hook already exists" "$(cat "$CALLS")"
pass "migration leaves an existing hook alone"

# Without the resume marker the hook would never run, so it stays absent.
reset_machine
rm -f "$resume_conf"
run_migration
[[ ! -e $hook ]] || fail "migration installs a hibernate hook without hibernation"
[[ ! -s $CALLS ]] || fail "migration acts on a machine without hibernation" "$(cat "$CALLS")"
pass "migration leaves machines without hibernation alone"

# A resume marker that is present but empty is not a configured machine: the
# same literal check omarchy-hibernation-setup uses decides this.
reset_machine
printf '' >"$resume_conf"
run_migration
[[ ! -e $hook ]] || fail "migration treats an empty resume file as hibernation"
[[ ! -s $CALLS ]] || fail "migration acts on an empty resume file" "$(cat "$CALLS")"
pass "migration requires the resume hook line, not just the file"