Skip to content
Merged
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
2 changes: 1 addition & 1 deletion packages/omarchy-mac/boot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This subtree is built into `omarchy-mac-boot`, separately from the kernel-neutral `omarchy-mac` hardware package. `../install` intentionally does not stage this directory. The boot recipe pins a runtime revision, the same one as the desktop candidate, and stages this subtree from it; the recipe keeps only packaging metadata and the pacman scriptlet.

`install DESTDIR` stages commands in `/usr/bin`, sourced modules/setup leaves in `/usr/lib/omarchy-mac/boot`, the lifecycle dispatch entrypoints in `entrypoints/` in `/usr/lib/omarchy/mac-boot`, and the initramfs, in-place encryption and first-boot payload in `files/` at its installed paths (mkinitcpio drop-ins, initcpio hooks, units, presets, the ALPM hook and `/etc/default/update-m1n1`, which pins update-m1n1's device-tree order to the C locale). It performs no live setup. Its presets and first-boot hand-off never enable `speakersafetyd`: omarchy-mac's `80-omarchy-mac-audio.preset` (#535) is its only owner, and an upgrade leaves existing enable links alone. A boot pin must therefore not publish ahead of an omarchy-mac pin that includes #535: until then no edge package presets speakersafetyd, and the image check refuses an image without it enabled. The recipe's `backup=` covers every `files/etc` path and `files/usr/lib/omarchy/initcpio`. `test/all` runs command fixtures and the payload's offline checks; `OMARCHY_DISPOSABLE_BOOT_TESTS=1` also builds real initramfs images and converts loop-device disks in privileged containers. Shared provisioning/reset integration remains covered by the runtime suite.
`install DESTDIR` stages commands in `/usr/bin`, sourced modules/setup leaves in `/usr/lib/omarchy-mac/boot`, the lifecycle dispatch entrypoints in `entrypoints/` in `/usr/lib/omarchy/mac-boot`, and the initramfs, in-place encryption and first-boot payload in `files/` at its installed paths (mkinitcpio drop-ins, initcpio hooks, units, presets, the ALPM hooks and `/etc/default/update-m1n1`, which pins update-m1n1's device-tree order to the C locale). One ALPM hook rebuilds the initramfs when a drop-in changes; `11-omarchy-mac-saved-modules.hook` runs right after kernel-modules-hook puts the running kernel's modules back and, only after a kernel downgrade, moves them to `/usr/lib/modules/.old` as `linux-modules-cleanup` would at the next boot, so update-m1n1's `-ARCH` default gives m1n1 the installed kernel's device trees. It performs no live setup. Its presets and first-boot hand-off never enable `speakersafetyd`: omarchy-mac's `80-omarchy-mac-audio.preset` (#535) is its only owner, and an upgrade leaves existing enable links alone. A boot pin must therefore not publish ahead of an omarchy-mac pin that includes #535: until then no edge package presets speakersafetyd, and the image check refuses an image without it enabled. The recipe's `backup=` covers every `files/etc` path and `files/usr/lib/omarchy/initcpio`. `test/all` runs command fixtures and the payload's offline checks; `OMARCHY_DISPOSABLE_BOOT_TESTS=1` also builds real initramfs images and converts loop-device disks in privileged containers. Shared provisioning/reset integration remains covered by the runtime suite.

The implementation was moved from runtime integration `e82fe3c8850b184dfa22eefd571646ad85446122`, preserving Marcelo's provenance recorded in the runtime source-port ledger. The payload in `files/` comes from the boot recipe paired with #503 (omarchy-mac/omarchy-pkgs-aarch64#65 at `617a907f99a1`) with maralcbr/omarchy-pkgs#202 applied (omarchy-mac-boot 20260921-10: the owner's keyboard layout and dock keyboards at the disk prompt). The owner wizard, recovery-slot operations, journal/retry handling and shared Limine menu/hash operations remain core code. Owner provisioning, factory reset and `omarchy-drive-password` reach this package only through `omarchy-lifecycle-dispatch`, which runs the `entrypoints/` staged in `/usr/lib/omarchy/mac-boot` and fails on Apple Silicon when they are missing.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Trigger]
Type = Path
Operation = Install
Operation = Upgrade
Target = usr/lib/modules/*/vmlinuz

[Action]
Description = Checking for a kernel downgrade's saved modules...
When = PostTransaction
Exec = /usr/share/libalpm/scripts/omarchy-mac-retire-saved-modules
NeedsTargets
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#!/bin/bash
# 11-omarchy-mac-saved-modules.hook, right after kernel-modules-hook's
# 10-linux-modules-post.hook. That hook puts the running kernel's modules back
# in /usr/lib/modules, owned by no package, until linux-modules-cleanup moves
# them to /usr/lib/modules/.old at the next boot. After a downgrade (a channel
# move, a reinstall from the cache, a migration to an older kernel) that copy
# is the newest -ARCH directory, so update-m1n1's ALARM default, which
# 95-m1n1-install.hook runs later in this transaction, would build m1n1 stage
# 2 with the running kernel's device trees and the boot check would refuse the
# reboot. Exactly then, this makes the cleanup's move now; anything else is
# left for the boot check to judge. pacman holds its lock for the whole
# transaction, so nothing reinstalls that kernel in between.

set -uo pipefail

modules=/usr/lib/modules

# Fixtures give an unprivileged caller another root; as root, which pacman
# hooks always are, the live system decides.
root=""
pacman_args=()
if (( EUID != 0 )) && [[ -n ${OMARCHY_SAVED_MODULES_ROOT:-} ]]; then
root=${OMARCHY_SAVED_MODULES_ROOT%/}
pacman_args=(--root "$root" --dbpath "$root/var/lib/pacman/")
fi

# pacman writes the targets to a pipe and fails the write if the hook has gone.
installed=()
while IFS= read -r target; do
[[ ! $target =~ ^usr/lib/modules/([^/]+)/vmlinuz$ ]] || installed+=("${BASH_REMATCH[1]}")
done

running=$(uname -r) || exit 0
[[ $running =~ ^[A-Za-z0-9._+-]+$ ]] || exit 0
saved=$modules/$running
[[ -d $root$saved && ! -L $root$saved ]] || exit 0
pacman "${pacman_args[@]}" -Qq kernel-modules-hook >/dev/null 2>&1 || exit 0

# Only when update-m1n1 takes its device trees from the -ARCH default.
script=$root/usr/bin/update-m1n1
[[ -r $script ]] && grep -Eq '^[[:space:]]*:[[:space:]]+\$\{DTBS:=.*/modules/\*-ARCH' "$script" || exit 0
config=$root/etc/default/update-m1n1
if [[ -e $config ]]; then
configured=$(env -i PATH=/usr/bin:/bin bash --noprofile --norc -c '
unset DTBS M1N1_UPDATE_DISABLED
. "$1" >/dev/null 2>&1 || exit 1
printf "%s" "${DTBS-}${M1N1_UPDATE_DISABLED-}"
' _ "$config") || exit 0
[[ -z $configured ]] || exit 0
fi

# Only after a downgrade: the running kernel's tree is the newest, and this
# transaction installed a kernel below it.
mapfile -t newest < <(
for match in "$root$modules"/*-ARCH; do
[[ ! -e $match && ! -L $match ]] || printf '%s\n' "${match##*/}"
done | sort -rV
)
(( ${#newest[@]} >= 2 )) && [[ ${newest[0]} == "$running" ]] || exit 0
downgrade=0
for kver in "${installed[@]}"; do
[[ $kver == "$running" || $(printf '%s\n' "$kver" "$running" | sort -V | head -n 1) != "$kver" ]] || downgrade=1
done
(( downgrade )) || exit 0

# The tree update-m1n1 takes once the saved one is gone must be the installed
# kernel's, and the saved one must be no package's.
leave() {
echo "Leaving $saved in place, so update-m1n1 takes the running kernel's device trees until the reboot: $1"
exit 0
}
[[ " ${installed[*]} " == *" ${newest[1]} "* ]] ||
leave "$modules/${newest[1]} sits between it and the kernel this transaction installed"
owner=$(LC_ALL=C pacman "${pacman_args[@]}" -Qqo "$root$saved" 2>&1) &&
leave "$owner still owns it; downgrade or remove $owner as well"
[[ $owner == *"No package owns"* ]] || leave "pacman cannot tell whether a package owns it"

echo "Moving the running kernel's saved modules ($running) to $modules/.old, as linux-modules-cleanup does at boot, so m1n1 carries the device trees of ${newest[1]}. Reboot soon: until then the running kernel cannot load modules it has not loaded yet."
rsync -AHXal "$root$saved" "$root$modules/.old/" || {
echo "Could not copy $saved to $modules/.old; nothing was removed, and m1n1 will carry the running kernel's device trees until the reboot" >&2
exit 1
}
rm -rf "$root$saved" || {
echo "Could not remove $saved after copying it to $modules/.old" >&2
exit 1
}
202 changes: 202 additions & 0 deletions packages/omarchy-mac/boot/test/mac-saved-modules-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
#!/bin/bash

set -euo pipefail
source "$(dirname "$0")/base-test.sh"
source "$ROOT/test/fixtures/limine-mac.sh"

require_command gzip
require_command b2sum

# After a kernel downgrade, kernel-modules-hook leaves the running kernel's
# modules in /usr/lib/modules until the next boot, where update-m1n1's -ARCH
# default takes their device trees. 11-omarchy-mac-saved-modules.hook retires
# that copy in the same transaction, before m1n1 is rebuilt.
hook=$ROOT/files/usr/share/libalpm/hooks/11-omarchy-mac-saved-modules.hook
retire=$ROOT/files/usr/share/libalpm/scripts/omarchy-mac-retire-saved-modules
entrypoint=$ROOT/entrypoints/update-verify
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT

grep -Fxq 'Target = usr/lib/modules/*/vmlinuz' "$hook" && grep -Fxq 'When = PostTransaction' "$hook" &&
grep -Fxq 'NeedsTargets' "$hook" && grep -Fxq "Exec = /${retire#"$ROOT/files/"}" "$hook" && [[ -x $retire ]] ||
fail "the hook runs the packaged script after every kernel install or upgrade, with the kernels it installed"
# pacman runs the hooks of one stage in the order of their file names.
order=(10-linux-modules-post.hook "${hook##*/}" 60-depmod.hook 90-mkinitcpio-apple-install.hook 90-mkinitcpio-install.hook 95-m1n1-install.hook)
[[ $(printf '%s\n' "${order[@]}" | LC_ALL=C sort) == "$(printf '%s\n' "${order[@]}")" ]] ||
fail "the hook runs after kernel-modules-hook restores the saved modules and before depmod, mkinitcpio, Limine and m1n1"
pass "the hook runs right after kernel-modules-hook's restore, before anything reads /usr/lib/modules"

running=6.18.0-aurora2-ARCH
stubs=$tmp/stubs
state=$tmp/state
mkdir -p "$stubs"
cat >"$stubs/uname" <<'SH'
#!/bin/bash
[[ $* == -r ]] || exec /usr/bin/uname "$@"
echo "$TEST_RUNNING"
SH
# pacman on the fixture's root: installed names the packages, owned holds
# "directory package" for the directories packages own.
cat >"$stubs/pacman" <<'SH'
#!/bin/bash
[[ $1 == --root && $2 == "$TEST_ROOT" && $3 == --dbpath && $4 == "$TEST_ROOT/var/lib/pacman/" ]] || { echo "pacman: not the fixture root: $*" >&2; exit 2; }
shift 4
case $1 in
-Qq) grep -Fxq -- "$2" "$TEST_STATE/installed" ;;
-Qqo)
owner=$(awk -v path="${2#"$TEST_ROOT"}" '$1 == path { print $2; exit }' "$TEST_STATE/owned")
if [[ -n $owner ]]; then
echo "$owner"
else
echo "error: No package owns $2" >&2
exit 1
fi
;;
*) exit 1 ;;
esac
SH
cat >"$stubs/rsync" <<'SH'
#!/bin/bash
printf 'rsync %s\n' "$*" >>"$TEST_STATE/calls"
[[ ${TEST_RSYNC_FAILS:-0} != 1 && $1 == -AHXal && $# == 3 ]] || exit 1
mkdir -p "$3"
cp -a "$2" "$3"
SH
chmod +x "$stubs"/*

limine_mac_init "$tmp/mac"
modules=$mac_root/usr/lib/modules

# The Mac just downgraded to linux-aurora $mac_kver while $running runs: the
# transaction's hooks left the running kernel's modules, device trees and all,
# where kernel-modules-hook put them back.
downgraded() {
local dtb
limine_mac
mkdir -p "$modules/$running/kernel" "$modules/$running/dtbs" "$state"
printf 'linux-aurora kernel %s\n' "$running" >"$modules/$running/vmlinuz"
printf 'module\n' >"$modules/$running/kernel/apple-dcp.ko.zst"
for dtb in "${mac_dtbs[@]}"; do
limine_mac_dtb "$modules/$running/dtbs/${dtb##*/}" "from $running"
done
printf '%s\n' linux-aurora m1n1-aurora uboot-asahi limine kernel-modules-hook >"$state/installed"
printf '/usr/lib/modules/%s linux-aurora\n' "$mac_kver" >"$state/owned"
: >"$state/calls"
}

# The transaction's own hook for the kernels it installed, as pacman runs it.
retire_hook() {
set +e
printf 'usr/lib/modules/%s/vmlinuz\n' "${1:-$mac_kver}" |
env TEST_RUNNING="$running" TEST_ROOT="$mac_root" TEST_STATE="$state" OMARCHY_SAVED_MODULES_ROOT="$mac_root" \
PATH="$stubs:$PATH" bash "$retire" >"$tmp/retire.out" 2>"$tmp/retire.err"
retire_status=$?
set -e
}

# 95-m1n1-install.hook: update-m1n1 with ALARM's default, the device trees of
# the newest -ARCH module tree.
m1n1_hook() {
local newest dtbs=() dtb
newest=$(printf '%s\n' "$modules"/*-ARCH | sort -rV | head -n 1)
for dtb in "$newest"/dtbs/*.dtb; do
dtbs+=("${dtb#"$mac_root"}")
done
limine_mac_boot_bin "$mac_root/usr/lib/asahi-boot/m1n1.bin" "${dtbs[@]}"
}

verify() {
set +e
(
eval "$(limine_mac_env "$ROOT/bin" "$running")"
bash "$entrypoint"
) >"$tmp/out" 2>"$tmp/err"
status=$?
set -e
}

left_with_reason() {
(( retire_status == 0 )) && [[ ! -s $state/calls && -d $modules/$running && ! -e $modules/.old ]] &&
grep -Fq "Leaving /usr/lib/modules/$running in place, so update-m1n1 takes the running kernel's device trees until the reboot: $2" "$tmp/retire.out" ||
fail "$1 is left in place, and the hook says why" "status $retire_status: $(cat "$tmp/retire.out" "$tmp/retire.err" "$state/calls")"
}

left_alone() {
(( retire_status == 0 )) && [[ ! -s $tmp/retire.out && ! -s $state/calls && -d $modules/$running && ! -e $modules/.old ]] ||
fail "$1 is left alone" "status $retire_status: $(cat "$tmp/retire.out" "$tmp/retire.err" "$state/calls")"
}

downgraded
m1n1_hook
verify
(( status == 1 )) && grep -Fq "device tree /lib/modules/$running/dtbs/t6000-j314s.dtb is not one of linux-aurora $mac_kver's" "$tmp/err" ||
fail "without the hook, m1n1 takes the running kernel's device trees and update-verify refuses the reboot" "status $status: $(cat "$tmp/err")"
pass "a downgrade's saved modules give m1n1 the wrong device trees, which update-verify refuses"

downgraded
retire_hook
m1n1_hook
(( retire_status == 0 )) || fail "the hook retires the saved modules" "status $retire_status: $(cat "$tmp/retire.err")"
[[ ! -e $modules/$running && -d $modules/$mac_kver ]] || fail "the running kernel's saved modules leave /usr/lib/modules" "$(ls -A "$modules")"
cmp -s "$modules/.old/$running/kernel/apple-dcp.ko.zst" <(printf 'module\n') && [[ -f $modules/.old/$running/dtbs/t6000-j314s.dtb ]] ||
fail "they are kept in /usr/lib/modules/.old, as linux-modules-cleanup keeps them" "$(find "$modules/.old")"
grep -Fxq "rsync -AHXal $modules/$running $modules/.old/" "$state/calls" ||
fail "they are copied the way linux-modules-cleanup copies them" "$(cat "$state/calls")"
grep -Fq "Moving the running kernel's saved modules ($running) to /usr/lib/modules/.old" "$tmp/retire.out" &&
grep -Fq "Reboot soon" "$tmp/retire.out" || fail "the move and its cost are explained" "$(cat "$tmp/retire.out")"
verify
(( status == 0 )) && grep -Fq "installed linux-aurora $mac_kver boot files match; running $running, reboot pending" "$tmp/out" ||
fail "update-verify passes the downgrade, with its reboot pending" "status $status: $(cat "$tmp/err")"
retire_hook
(( retire_status == 0 )) && [[ ! -s $tmp/retire.out ]] || fail "a later kernel transaction before the reboot finds nothing to do" "$(cat "$tmp/retire.out")"
pass "the hook moves a downgrade's saved modules to .old before m1n1 is rebuilt, leaving no stale module tree, and update-verify passes"

downgraded
retire_hook "$running"
left_alone "a reinstall of the running kernel itself"
printf '/usr/lib/modules/%s linux-aurora-headers\n' "$running" >>"$state/owned"
retire_hook
left_with_reason "a downgrade whose running tree the headers still own" "linux-aurora-headers still owns it; downgrade or remove linux-aurora-headers as well"
downgraded
mv "$modules/$running" "$modules/6.16.0-aurora1-ARCH"
running=6.16.0-aurora1-ARCH retire_hook
(( retire_status == 0 )) && [[ ! -s $tmp/retire.out && -d $modules/6.16.0-aurora1-ARCH && ! -e $modules/.old ]] ||
fail "an upgrade, whose saved modules sort below the installed kernel, is left alone" "$(cat "$tmp/retire.out" "$tmp/retire.err")"
downgraded
mkdir -p "$modules/6.17.5-aurora1-ARCH"
retire_hook
left_with_reason "another module tree between the running and installed kernels" "/usr/lib/modules/6.17.5-aurora1-ARCH sits between it and the kernel this transaction installed"
downgraded
sed -i '/^kernel-modules-hook$/d' "$state/installed"
retire_hook
left_alone "a Mac without kernel-modules-hook"
downgraded
printf 'DTBS="/lib/modules/%s/dtbs/*.dtb"\n' "$mac_kver" >>"$mac_root/etc/default/update-m1n1"
retire_hook
left_alone "DTBS set in /etc/default/update-m1n1"
downgraded
printf 'M1N1_UPDATE_DISABLED=1\n' >>"$mac_root/etc/default/update-m1n1"
retire_hook
left_alone "m1n1 left to its owner"
downgraded
sed -i '/DTBS:=/d' "$mac_root/usr/bin/update-m1n1"
retire_hook
left_alone "an update-m1n1 without the -ARCH default"
downgraded
mv "$modules/$running" "$tmp/elsewhere"
ln -s "$tmp/elsewhere" "$modules/$running"
retire_hook
(( retire_status == 0 )) && [[ -L $modules/$running && ! -e $modules/.old && -d $tmp/elsewhere ]] || fail "a symlinked module tree is left alone"
rm -rf "$tmp/elsewhere"
pass "an upgrade, a reinstall, an owned or intervening tree (said why), no kernel-modules-hook, or DTBS not from the -ARCH default moves nothing"

downgraded
TEST_RSYNC_FAILS=1 retire_hook
(( retire_status == 1 )) && grep -Fq "nothing was removed" "$tmp/retire.err" && [[ -d $modules/$running && -f $modules/$running/vmlinuz ]] ||
fail "a failed copy fails the hook and removes nothing" "status $retire_status: $(cat "$tmp/retire.err")"
pass "a failed copy removes nothing"

# As root, which pacman hooks always are, a fixture root is never honoured.
grep -Fq '(( EUID != 0 )) && [[ -n ${OMARCHY_SAVED_MODULES_ROOT:-} ]]' "$retire" ||
fail "only an unprivileged caller can point the script at another root"
pass "only an unprivileged caller can point the script at another root"
Loading