diff --git a/bin/omarchy-hibernation-setup b/bin/omarchy-hibernation-setup index 7bc5724679b7..4889511e6292 100755 --- a/bin/omarchy-hibernation-setup +++ b/bin/omarchy-hibernation-setup @@ -89,7 +89,7 @@ echo "Adding resume hook to $MKINITCPIO_CONF" echo "HOOKS+=(resume)" | sudo tee "$MKINITCPIO_CONF" >/dev/null # Ensure keyboard backlight doesn't prevent sleep -sudo cp -p "$OMARCHY_PATH/default/systemd/system-sleep/keyboard-backlight" /usr/lib/systemd/system-sleep/ +sudo install -m755 "$OMARCHY_PATH/default/systemd/system-sleep/keyboard-backlight" /usr/lib/systemd/system-sleep/ # 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. diff --git a/bin/omarchy-toggle-hybrid-gpu b/bin/omarchy-toggle-hybrid-gpu index aa7c0582586f..d8a23c25484d 100755 --- a/bin/omarchy-toggle-hybrid-gpu +++ b/bin/omarchy-toggle-hybrid-gpu @@ -60,7 +60,7 @@ case "$gpu_mode" in # Force igpu mode after system sleep (or dgpu could get activated) sudo mkdir -p /usr/lib/systemd/system-sleep - sudo cp -p "$OMARCHY_PATH/default/systemd/system-sleep/force-igpu" /usr/lib/systemd/system-sleep/ + sudo install -m755 "$OMARCHY_PATH/default/systemd/system-sleep/force-igpu" /usr/lib/systemd/system-sleep/ # Delay supergfxd startup to avoid race condition with display manager # that can cause system freeze when booting in Integrated mode diff --git a/default/systemd/system-sleep/force-igpu b/default/systemd/system-sleep/force-igpu old mode 100644 new mode 100755 diff --git a/default/systemd/system-sleep/keyboard-backlight b/default/systemd/system-sleep/keyboard-backlight old mode 100644 new mode 100755 diff --git a/migrations/1787336732.sh b/migrations/1787336732.sh new file mode 100644 index 000000000000..5ecb408ab3fa --- /dev/null +++ b/migrations/1787336732.sh @@ -0,0 +1,43 @@ +echo "Make installed system-sleep hooks executable so systemd runs them" + +sleep_hook_dir="${OMARCHY_SYSTEM_SLEEP_DIR:-/usr/lib/systemd/system-sleep}" + +# force-igpu switches the GPU back to Integrated after every sleep, so it may +# only be live on a machine that is still in Integrated mode. The toggle removes +# the hook when it switches to Hybrid, but a mode change made directly through +# supergfxctl leaves the copy behind, and that copy has been inert and must +# stay that way. Ask supergfxd the way the toggle does; no answer means the +# hook is left as it is. +integrated_gpu_mode() { + local attempt mode + + for attempt in {1..3}; do + if mode=$(timeout --kill-after=1s 3s supergfxctl -g 2>/dev/null) && [[ -n $mode ]]; then + [[ $mode == "Integrated" ]] + return + fi + + (( attempt < 3 )) && sleep 1 + done + + return 1 +} + +# omarchy-hibernation-setup and omarchy-toggle-hybrid-gpu copied these hooks +# into place with cp -p from a 644 source, and systemd-sleep(8) silently skips +# non-executable files here. Only the mode is wrong, so fix the mode in place: +# a hook that is absent or already executable is left alone, which also makes +# the second user's run on the same machine a no-op. +for hook in keyboard-backlight force-igpu; do + path="$sleep_hook_dir/$hook" + + if [[ ! -f $path || -x $path ]]; then + continue + fi + + if [[ $hook == "force-igpu" ]] && ! integrated_gpu_mode; then + continue + fi + + sudo chmod 755 "$path" +done diff --git a/test/shell.d/sleep-hook-executable-migration-test.sh b/test/shell.d/sleep-hook-executable-migration-test.sh new file mode 100644 index 000000000000..42638ed75f40 --- /dev/null +++ b/test/shell.d/sleep-hook-executable-migration-test.sh @@ -0,0 +1,123 @@ +#!/bin/bash + +set -euo pipefail + +source "$(dirname "$0")/base-test.sh" + +migration="$ROOT/migrations/1787336732.sh" +test_dir=$(mktemp -d) +trap 'rm -rf "$test_dir"' EXIT + +mkdir -p "$test_dir/bin" "$test_dir/system-sleep" + +# sudo runs the real command, so chmod acts on the redirected hook directory. +cat >"$test_dir/bin/sudo" <<'STUB' +#!/bin/bash + +printf 'sudo %s\n' "$*" >>"$CALLS" +exec "$@" +STUB + +# supergfxd answers with whatever mode the scenario sets, and like a daemon that +# is not running, stays silent when none is set. +cat >"$test_dir/bin/supergfxctl" <<'STUB' +#!/bin/bash + +printf 'supergfxctl %s\n' "$*" >>"$CALLS" +[[ -n ${GPU_MODE:-} ]] || exit 1 +echo "$GPU_MODE" +STUB + +chmod +x "$test_dir/bin/"* + +export CALLS="$test_dir/calls" + +hook_dir="$test_dir/system-sleep" + +install_hook() { + printf '#!/bin/bash\n' >"$hook_dir/$1" + chmod "$2" "$hook_dir/$1" +} + +run_migration() { + : >"$CALLS" + + OMARCHY_SYSTEM_SLEEP_DIR="$hook_dir" \ + PATH="$test_dir/bin:$PATH" \ + bash -euo pipefail "$migration" >/dev/null +} + +# Both hooks used to be copied in with cp -p from a 644 source, so an affected +# machine has non-executable copies that systemd-sleep never runs. +rm -f "$hook_dir"/* +install_hook keyboard-backlight 644 +install_hook force-igpu 644 +GPU_MODE=Integrated run_migration + +[[ -x $hook_dir/keyboard-backlight ]] || fail "migration makes keyboard-backlight executable" +pass "migration makes keyboard-backlight executable" + +[[ -x $hook_dir/force-igpu ]] || fail "migration makes force-igpu executable in Integrated mode" +pass "migration makes force-igpu executable in Integrated mode" + +grep -qx 'supergfxctl -g' "$CALLS" || + fail "migration asks supergfxd for the GPU mode" "$(cat "$CALLS")" +pass "migration asks supergfxd for the GPU mode" + +grep -qx "sudo chmod 755 $hook_dir/keyboard-backlight" "$CALLS" || + fail "migration changes the mode through sudo" "$(cat "$CALLS")" +pass "migration changes the mode through sudo" + +# force-igpu forces Integrated mode after every sleep. The toggle removes it on +# the way to Hybrid, but a mode change made straight through supergfxctl leaves +# the inert copy behind, and waking it up would flip the GPU under the user. +rm -f "$hook_dir"/* +install_hook keyboard-backlight 644 +install_hook force-igpu 644 +GPU_MODE=Hybrid run_migration + +[[ ! -x $hook_dir/force-igpu ]] || fail "migration leaves force-igpu inert in Hybrid mode" +pass "migration leaves force-igpu inert in Hybrid mode" + +[[ -x $hook_dir/keyboard-backlight ]] || fail "migration still repairs keyboard-backlight in Hybrid mode" +pass "migration still repairs keyboard-backlight in Hybrid mode" + +# With no answer from supergfxd the mode is unknown, so the hook stays as it is. +rm -f "$hook_dir"/* +install_hook force-igpu 644 +run_migration + +[[ ! -x $hook_dir/force-igpu ]] || fail "migration leaves force-igpu inert when supergfxd does not answer" +pass "migration leaves force-igpu inert when supergfxd does not answer" + +(( $(grep -cx 'supergfxctl -g' "$CALLS") == 3 )) || + fail "migration retries the GPU mode query like the toggle does" "$(cat "$CALLS")" +pass "migration retries the GPU mode query like the toggle does" + +# Hybrid-mode machines never install force-igpu, and hibernation setup is +# optional, so an absent hook is not an error. +rm -f "$hook_dir"/* +install_hook keyboard-backlight 644 +run_migration + +[[ -x $hook_dir/keyboard-backlight ]] || fail "migration repairs the hooks that are present" +pass "migration repairs the hooks that are present" + +[[ ! -e $hook_dir/force-igpu ]] || fail "migration does not create a missing hook" +pass "migration does not create a missing hook" + +# Migration completion is recorded per user, so a second account runs it again +# on a machine that is already repaired and must not need sudo for nothing. +install_hook force-igpu 755 +GPU_MODE=Integrated run_migration + +[[ ! -s $CALLS ]] || + fail "migration touches nothing on an already-repaired machine" "$(cat "$CALLS")" +pass "migration touches nothing on an already-repaired machine" + +rm -f "$hook_dir"/* +run_migration + +[[ ! -s $CALLS ]] || + fail "migration touches nothing when no hook is installed" "$(cat "$CALLS")" +pass "migration touches nothing when no hook is installed"