diff --git a/install/hardware/apple/fix-suspend-nvme.sh b/install/hardware/apple/fix-suspend-nvme.sh index 27e0270e3279..ca3d5cea3af2 100644 --- a/install/hardware/apple/fix-suspend-nvme.sh +++ b/install/hardware/apple/fix-suspend-nvme.sh @@ -1,13 +1,13 @@ # Fix NVMe suspend issues on MacBook models -# This prevents NVMe drives from failing to wake from sleep properly +# Apple's own NVMe controller fails to wake from D3cold, so keep it out of that state where it sits at 01:00.0. +# The MacBook8,1 (Wi-Fi there, its controller at 03:00.0) and the 15-inch 13,3 and 14,3 (the AMD GPU there) must not match. MACBOOK_MODEL=$(cat /sys/class/dmi/id/product_name 2>/dev/null || true) +NVME_PCI="/sys/bus/pci/devices/0000:01:00.0" -if [[ $MACBOOK_MODEL =~ MacBook(8,1|9,1|10,1)|MacBookPro13,[123]|MacBookPro14,[123] ]]; then +if [[ $MACBOOK_MODEL =~ MacBook(9,1|10,1)|MacBookPro13,[12]|MacBookPro14,[12] ]]; then echo "Detected MacBook model: $MACBOOK_MODEL" - NVME_DEVICE="/sys/bus/pci/devices/0000:01:00.0/d3cold_allowed" - - if [[ -f $NVME_DEVICE ]]; then + if [[ $(cat "$NVME_PCI/vendor" 2>/dev/null || true) == "0x106b" ]]; then echo "Applying NVMe suspend fix..." sudo mkdir -p /etc/systemd/system @@ -16,7 +16,7 @@ if [[ $MACBOOK_MODEL =~ MacBook(8,1|9,1|10,1)|MacBookPro13,[123]|MacBookPro14,[1 Description=Omarchy NVMe Suspend Fix for MacBook [Service] -ExecStart=/bin/bash -c 'echo 0 > /sys/bus/pci/devices/0000\:01\:00.0/d3cold_allowed' +ExecStart=/bin/bash -c 'echo 0 > /sys/bus/pci/devices/0000:01:00.0/d3cold_allowed' [Install] WantedBy=multi-user.target @@ -24,7 +24,7 @@ EOF sudo systemctl enable omarchy-nvme-suspend-fix.service else - echo "Warning: NVMe device not found at expected PCI address (0000:01:00.0)" + echo "Warning: Apple NVMe controller not found at expected PCI address (0000:01:00.0)" echo "This fix may not be needed for this MacBook model" fi fi diff --git a/migrations/1789274148.sh b/migrations/1789274148.sh new file mode 100644 index 000000000000..c782f4f7214b --- /dev/null +++ b/migrations/1789274148.sh @@ -0,0 +1,28 @@ +echo "Remove the MacBook NVMe suspend fix from Macs where it disabled D3cold on the GPU or Wi-Fi instead of the drive" + +# The leaf used to match the MacBook8,1 and the 15-inch MacBookPro13,3 and 14,3, whose 01:00.0 is Wi-Fi or the GPU. +# It now needs Apple's controller there, so remove the service everywhere that is not. +unit="${OMARCHY_NVME_SUSPEND_UNIT:-/etc/systemd/system/omarchy-nvme-suspend-fix.service}" +pci_vendor="${OMARCHY_NVME_SUSPEND_PCI_VENDOR:-/sys/bus/pci/devices/0000:01:00.0/vendor}" + +[[ -f $unit && ! -L $unit ]] || exit 0 +[[ $(cat "$pci_vendor" 2>/dev/null || true) != "0x106b" ]] || exit 0 + +# Remove only the unit the leaf wrote, with or without its escaped colons. An edited one is the administrator's. +generated="[Unit] +Description=Omarchy NVMe Suspend Fix for MacBook + +[Service] +ExecStart=/bin/bash -c 'echo 0 > /sys/bus/pci/devices/0000:01:00.0/d3cold_allowed' + +[Install] +WantedBy=multi-user.target" +contents=$(cat "$unit" 2>/dev/null || true) + +if [[ ${contents//'\:'/:} == "$generated" ]]; then + sudo systemctl disable omarchy-nvme-suspend-fix.service + sudo rm -f "$unit" + sudo systemctl daemon-reload +else + echo "Leaving $unit in place: it was changed after Omarchy wrote it" +fi diff --git a/test/shell.d/apple-suspend-nvme-test.sh b/test/shell.d/apple-suspend-nvme-test.sh new file mode 100755 index 000000000000..6d37efb46a15 --- /dev/null +++ b/test/shell.d/apple-suspend-nvme-test.sh @@ -0,0 +1,171 @@ +#!/bin/bash + +set -euo pipefail + +source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh" + +leaf="$ROOT/install/hardware/apple/fix-suspend-nvme.sh" +migration="$ROOT/migrations/1789274148.sh" + +test_tmp=$(mktemp -d) +trap 'rm -rf "$test_tmp"' EXIT + +stub_bin="$test_tmp/bin" +calls="$test_tmp/calls.log" +unit="$test_tmp/etc/systemd/system/omarchy-nvme-suspend-fix.service" +mkdir -p "$stub_bin" "$test_tmp/dmi" "$test_tmp/pci" + +cat >"$stub_bin/sudo" <<'SH' +#!/bin/bash + +printf 'sudo' >>"$TEST_LOG" +printf '\t%s' "$@" >>"$TEST_LOG" +printf '\n' >>"$TEST_LOG" +if [[ $1 != "systemctl" ]]; then + "$@" +fi +SH + +chmod +x "$stub_bin"/* + +# The leaf reads the model and the PCI vendor from absolute paths and writes the +# unit to /etc, so run a copy with all three redirected into the sandbox. +run_leaf() { + local model="$1" vendor="$2" + rm -rf "${test_tmp:?}/etc" + : >"$calls" + printf '%s\n' "$model" >"$test_tmp/dmi/product_name" + printf '%s\n' "$vendor" >"$test_tmp/pci/vendor" + + local script="$test_tmp/leaf.sh" + sed -e "s|/sys/class/dmi/id/product_name|$test_tmp/dmi/product_name|g" \ + -e "s|\"/sys/bus/pci/devices/0000:01:00.0\"|\"$test_tmp/pci\"|g" \ + -e "s|/etc/systemd/system|$test_tmp/etc/systemd/system|g" \ + "$leaf" >"$script" + + PATH="$stub_bin:$PATH" TEST_LOG="$calls" \ + bash -eE -o pipefail -c 'source "$1"' bash "$script" /dev/null + grep -q 'd3cold_allowed' "$unit" 2>/dev/null || fail "a Mac with Apple's NVMe controller gets the fix" "$model" + grep -Fq $'sudo\tsystemctl\tenable\tomarchy-nvme-suspend-fix.service' "$calls" || + fail "the fix is enabled" "$model: $(cat "$calls")" +done +pass "every Mac with Apple's NVMe controller gets the fix" + +# The unit used to escape the colons, which systemd warns about on every boot. +! grep -q '\\:' "$unit" || fail "the unit has no escape sequences systemd ignores" "$(cat "$unit")" +pass "the unit has no escape sequences systemd ignores" + +# 15-inch models: 01:00.0 exists, but it is the AMD GPU. +for model in MacBookPro13,3 MacBookPro14,3; do + run_leaf "$model" 0x1002 >/dev/null + [[ ! -e $unit ]] || fail "a 15-inch MacBook Pro is left alone" "$model" + [[ ! -s $calls ]] || fail "a 15-inch MacBook Pro escalates nothing" "$model: $(cat "$calls")" +done +pass "15-inch MacBook Pros, whose 01:00.0 is the GPU, are left alone" + +# MacBook8,1: 01:00.0 is the Broadcom Wi-Fi, and Apple's controller is at 03:00.0. +run_leaf MacBook8,1 0x14e4 >/dev/null +[[ ! -e $unit ]] || fail "a MacBook8,1 is left alone" +[[ ! -s $calls ]] || fail "a MacBook8,1 escalates nothing" "$(cat "$calls")" +run_leaf MacBook8,1 0x106b >/dev/null +[[ ! -e $unit ]] || fail "a MacBook8,1 is not matched by model" +pass "the MacBook8,1, whose 01:00.0 is the Wi-Fi card, is left alone" + +# A 13-inch board whose 01:00.0 is not Apple's controller does not get the fix blind. +run_leaf MacBookPro14,2 0x144d >/dev/null +[[ ! -e $unit ]] || fail "a replaced or unexpected controller is left alone" +pass "a device at 01:00.0 that is not Apple's controller is left alone" + +run_migration() { + local vendor="$1" + : >"$calls" + printf '%s\n' "$vendor" >"$test_tmp/pci/vendor" + + PATH="$stub_bin:$PATH" TEST_LOG="$calls" \ + OMARCHY_NVME_SUSPEND_UNIT="$unit" \ + OMARCHY_NVME_SUSPEND_PCI_VENDOR="$test_tmp/pci/vendor" \ + bash -euo pipefail "$migration" >/dev/null +} + +# Exactly what the leaf wrote before this change. +write_old_unit() { + mkdir -p "$(dirname "$unit")" + cat >"$unit" <<'EOF' +[Unit] +Description=Omarchy NVMe Suspend Fix for MacBook + +[Service] +ExecStart=/bin/bash -c 'echo 0 > /sys/bus/pci/devices/0000\:01\:00.0/d3cold_allowed' + +[Install] +WantedBy=multi-user.target +EOF +} + +# A 15-inch install from before the fix carries the service aimed at its GPU. +write_old_unit +run_migration 0x1002 +[[ ! -e $unit ]] || fail "the migration removes the service from a 15-inch MacBook Pro" +grep -Fq $'sudo\tsystemctl\tdisable\tomarchy-nvme-suspend-fix.service' "$calls" || + fail "the migration disables the service" "$(cat "$calls")" +pass "the migration removes the service where 01:00.0 is not Apple's controller" + +run_migration 0x1002 +[[ ! -s $calls ]] || fail "the migration is idempotent" "$(cat "$calls")" +pass "the migration is idempotent" + +# A MacBook8,1 install from before the fix carries the service aimed at its Wi-Fi card. +write_old_unit +run_migration 0x14e4 +[[ ! -e $unit ]] || fail "the migration removes the service from a MacBook8,1" +pass "the migration removes the service from a MacBook8,1" + +# The machines the fix was written for keep it. +write_old_unit +run_migration 0x106b +[[ -f $unit ]] || fail "the migration keeps the service on a Mac with Apple's NVMe controller" +[[ ! -s $calls ]] || fail "the migration escalates nothing on those Macs" "$(cat "$calls")" +pass "the migration keeps the service on Macs with Apple's NVMe controller" + +rm -rf "${test_tmp:?}/etc" +run_migration 0x8086 +[[ ! -s $calls ]] || fail "the migration skips machines that never had the service" "$(cat "$calls")" +pass "the migration skips machines that never had the service" + +# The unit the leaf writes now, without the escaped colons, is still Omarchy's to remove. +run_leaf MacBook9,1 0x106b >/dev/null +run_migration 0x1002 +[[ ! -e $unit ]] || fail "the migration removes the unit the current leaf writes" +pass "the migration removes the unit the current leaf writes" + +# An administrator who edited the unit, say to point it at the drive on 02:00.0, keeps it. +write_old_unit +sed -i 's/01\\:00.0/02\\:00.0/' "$unit" +cp "$unit" "$test_tmp/edited-unit" +run_migration 0x1002 +cmp -s "$unit" "$test_tmp/edited-unit" || fail "the migration leaves an edited unit alone" +[[ ! -s $calls ]] || fail "the migration does not disable an edited unit" "$(cat "$calls")" +pass "the migration leaves an edited unit alone" + +rm -f "$unit" +write_old_unit +# Root reads it regardless, so only a user can see the unreadable case. +if (( EUID != 0 )); then + chmod 000 "$unit" + run_migration 0x1002 + [[ -f $unit && ! -s $calls ]] || fail "the migration leaves a unit it cannot read alone" "$(cat "$calls")" + pass "the migration leaves a unit it cannot read alone" +fi + +rm -f "$unit" +write_old_unit +mv "$unit" "$test_tmp/linked-unit" +ln -s "$test_tmp/linked-unit" "$unit" +run_migration 0x1002 +[[ -L $unit && ! -s $calls ]] || fail "the migration leaves a linked unit alone" "$(cat "$calls")" +pass "the migration leaves a linked unit alone"