Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions install/config/all.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
run_logged "$OMARCHY_INSTALL/config/pacman-keyring.sh"
run_logged "$OMARCHY_INSTALL/config/theme-system.sh"
run_logged "$OMARCHY_INSTALL/config/browser-policy.sh"
run_logged "$OMARCHY_INSTALL/config/increase-lockout-limit.sh"
Expand Down
4 changes: 4 additions & 0 deletions install/config/pacman-keyring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Start making this machine's pacman keyring, which post-install/pacman.sh
# moves into place (install/helpers/pacman-keyring.sh).
source "$OMARCHY_INSTALL/helpers/pacman-keyring.sh"
omarchy_pacman_keyring_start
84 changes: 84 additions & 0 deletions install/helpers/pacman-keyring.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This machine's pacman keyring, made in the background during a first install.
# Sourced by install/config/pacman-keyring.sh and install/post-install/pacman.sh;
# no shebang.
#
# Making the keyring takes seconds: pacman-key generates a master key, then
# imports, signs and disables keys and rebuilds the trust database. Nothing in
# system setup needs it until post-install/pacman.sh switches pacman to its real
# repositories, so config/pacman-keyring.sh starts it first thing, in a
# directory of its own while setup keeps using the keyring the image has, and
# post-install/pacman.sh waits for it and moves it into place. A run that never
# started or didn't finish leaves that step to make the keyring itself, as it
# always did.
#
# Only a first install starts one: the keyring it replaces holds nothing yet,
# where an upgrade's may hold keys the user added.
#
# Root always uses the real /etc. Only a non-root test may move it under
# OMARCHY_IMAGE_ROOT, as install/helpers/image-target.sh does.

omarchy_pacman_keyring_init() {
local root=""
(( EUID == 0 )) || root=${OMARCHY_IMAGE_ROOT:-}
omarchy_pacman_keyring=$root/etc/pacman.d/gnupg
omarchy_pacman_keyring_next=$root/etc/pacman.d/gnupg.omarchy-next
}

# Every aarch64 platform but a Mac, whose image brings its keyrings, makes its
# keyring at install, except in an image build, which leaves it to each
# machine's first boot (install/helpers/image-target.sh).
omarchy_pacman_keyring_wanted() {
local platform
platform=$(omarchy-hw-platform)
[[ $platform == aarch64* && $platform != "aarch64-apple" ]] || return 1
source "$OMARCHY_PATH/install/helpers/image-target.sh"
! { omarchy_image_init && omarchy_image_manifest_present; }
}

omarchy_pacman_keyring_start() {
local next lock
[[ ${OMARCHY_FIRST_INSTALL:-0} == "1" ]] || return 0
omarchy_pacman_keyring_wanted || return 0
omarchy_pacman_keyring_init
next=$omarchy_pacman_keyring_next

# --populate reads the keyrings this package installs.
omarchy-pkg-add archlinuxarm-keyring

rm -rf "$next" "$next.done" "$next.log"
mkdir -p "$(dirname "$next")"
# The lock is taken here and handed to the job, so a finish that comes before
# the job is under way still waits for it.
exec {lock}>"$next.lock"
flock "$lock"
(
if pacman-key --gpgdir "$next" --init && pacman-key --gpgdir "$next" --populate; then
touch "$next.done"
fi
# The keyring moves once it's done, so no agent may hold it open.
gpgconf --homedir "$next" --kill all
) </dev/null >"$next.log" 2>&1 &
exec {lock}>&-
}

omarchy_pacman_keyring_finish() {
local next
omarchy_pacman_keyring_init
next=$omarchy_pacman_keyring_next

if [[ -e $next.lock ]]; then
flock "$next.lock" true
cat "$next.log" 2>/dev/null || true
fi

if [[ -e $next.done ]]; then
rm -rf "$omarchy_pacman_keyring"
mv "$next" "$omarchy_pacman_keyring"
else
[[ ! -e $next ]] || rm -rf "$next"
pacman-key --init
pacman-key --populate
fi

rm -f "$next.lock" "$next.done" "$next.log"
}
8 changes: 5 additions & 3 deletions install/post-install/pacman.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# Arch Linux ARM's before its repositories replace the offline ones, and trust
# every installed keyring before the first signed sync.
source "$OMARCHY_PATH/install/helpers/pacman.sh"
source "$OMARCHY_PATH/install/helpers/pacman-keyring.sh"
source "$OMARCHY_PATH/install/helpers/image-target.sh"
platform=$(omarchy-hw-platform)
templates=$(omarchy_pacman_templates "$platform")
Expand All @@ -25,13 +26,14 @@ channel=${OMARCHY_MIRROR:-}
"$OMARCHY_PATH/bin/omarchy-apply-pacman" "$channel"

# An image build leaves the keyring to each machine's first boot, so no two
# share a master key (install/helpers/image-target.sh).
# share a master key (install/helpers/image-target.sh). An install's keyring
# was started at the beginning of system setup (config/pacman-keyring.sh) and
# lands here, or is made here when it wasn't.
if (( alarm_keyring )); then
if omarchy_image_init && omarchy_image_manifest_present; then
install -m 0644 /dev/null "$omarchy_image_keyring_request"
else
pacman-key --init
pacman-key --populate
omarchy_pacman_keyring_finish
fi
fi

Expand Down
72 changes: 70 additions & 2 deletions test/shell.d/image-deferred-hardware-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -369,18 +369,29 @@ mkdir -p "$keyring_bin" "$fixture/install/post-install"
# the root under test, never the host's /etc.
cp "$ROOT/install/post-install/pacman.sh" "$fixture/install/post-install/pacman.sh"
cp "$ROOT/install/helpers/pacman.sh" "$fixture/install/helpers/pacman.sh"
cp "$ROOT/install/helpers/pacman-keyring.sh" "$fixture/install/helpers/pacman-keyring.sh"
target_etc='etc="${2:-/}/etc"'
grep -Fqx " $target_etc" "$ROOT/bin/omarchy-apply-pacman" || fail "omarchy-apply-pacman still names its target etc"
sed "s|^ $target_etc\$| etc=\"\${2:-\$OMARCHY_IMAGE_ROOT}/etc\"|" "$ROOT/bin/omarchy-apply-pacman" >"$fixture/bin/omarchy-apply-pacman"
chmod +x "$fixture/bin/omarchy-apply-pacman"
mkdir -p "$fixture/default"
cp -r "$ROOT/default/pacman" "$ROOT/default/regions" "$fixture/default/"
: >"$fixture/install/hardware/pacman.sh"
for command in pacman-key omarchy-pkg-add; do
# pacman-key makes the keyring directory it's given, as the real one does.
for command in pacman-key omarchy-pkg-add gpgconf; do
cat >"$keyring_bin/$command" <<'SH'
#!/bin/bash
printf '%s %s\n' "${0##*/}" "$*" >>"$KEYRING"
[[ ${0##*/} != "pacman-key" || ! -e $KEYRING_FAIL ]]
[[ ${0##*/} != "pacman-key" || ! -e $KEYRING_FAIL ]] || exit 1
[[ ${0##*/} != "pacman-key" || -z ${KEYRING_FAIL_NEXT:-} || $* != *omarchy-next* ]] || exit 1
# A test holds the background keyring's --populate until it releases it.
if [[ ${0##*/} == "pacman-key" && -n ${KEYRING_HOLD:-} && $2 == *omarchy-next && $3 == "--populate" ]]; then
read -r _ <"$KEYRING_HOLD"
fi
if [[ ${0##*/} == "pacman-key" && $1 == "--gpgdir" && $3 == "--init" ]]; then
mkdir -p "$2"
touch "$2/master-key"
fi
SH
done
printf '#!/bin/bash\necho "$PLATFORM"\n' >"$keyring_bin/omarchy-hw-platform"
Expand Down Expand Up @@ -427,6 +438,63 @@ for platform in aarch64-apple x86; do
done
pass "an image build on plain aarch64 leaves the pacman keyring to the first boot"

# A first install starts its keyring at the beginning of system setup, in a
# directory of its own, and finalization waits for it and moves it into place.
start_keyring() {
OMARCHY_IMAGE_ROOT="$1" OMARCHY_PATH="$fixture" OMARCHY_INSTALL="$fixture/install" PLATFORM=$2 OMARCHY_FIRST_INSTALL=$3 \
PATH="$keyring_bin:$base_path" bash -e -c 'source "$1"; omarchy_pacman_keyring_start' bash "$fixture/install/helpers/pacman-keyring.sh"
}
next_keyring() {
printf '%s\n' "$1/etc/pacman.d/gnupg.omarchy-next"
}

rm -f "$KEYRING"
root=$(new_root first-install-aarch64)
mkdir -p "$root/etc/pacman.d/gnupg"
touch "$root/etc/pacman.d/gnupg/image-leftover"
hold=$test_tmp/keyring-hold
mkfifo "$hold"
KEYRING_HOLD=$hold start_keyring "$root" aarch64 1 || fail "a first install starts its keyring"
finalize "$root" aarch64 &
finalizing=$!
Comment thread
dhh marked this conversation as resolved.
Resolved
# Finalization installs the keyring package just before it takes the keyring;
# once it has, it must wait for the one still being made.
for _ in {1..100}; do
(( $(grep -c '^omarchy-pkg-add' "$KEYRING") >= 2 )) && break
sleep 0.05
done
sleep 0.3
kill -0 "$finalizing" 2>/dev/null || fail "finalization waits for the keyring still being made" "$(cat "$KEYRING")"
timeout 5 bash -c 'echo >"$1"' bash "$hold" || fail "the background keyring reached --populate"
wait "$finalizing" || fail "finalization takes the keyring started for it"
next=$(next_keyring "$root")
grep -Fxq "pacman-key --gpgdir $next --init" "$KEYRING" && grep -Fxq "pacman-key --gpgdir $next --populate" "$KEYRING" ||
fail "the keyring is made in a directory of its own" "$(cat "$KEYRING")"
! grep -Fxq "pacman-key --init" "$KEYRING" || fail "finalization doesn't make a second keyring" "$(cat "$KEYRING")"
[[ -f $root/etc/pacman.d/gnupg/master-key && ! -e $root/etc/pacman.d/gnupg/image-leftover ]] ||
fail "the new keyring replaces the image's"
[[ ! -e $next && ! -e $next.lock && ! -e $next.done && ! -e $next.log ]] || fail "nothing is left of the keyring's making"
pass "a first install makes its keyring in the background, and finalization moves it into place"

rm -f "$KEYRING"
root=$(new_root first-install-fails)
mkdir -p "$root/etc/pacman.d/gnupg"
touch "$root/etc/pacman.d/gnupg/image-leftover"
KEYRING_FAIL_NEXT=1 start_keyring "$root" aarch64 1 || fail "a first install starts its keyring, even one that will fail"
finalize "$root" aarch64 || fail "finalization succeeds when the background keyring failed"
grep -Fxq "pacman-key --init" "$KEYRING" && grep -Fxq "pacman-key --populate" "$KEYRING" ||
fail "a keyring that failed in the background is made in finalization" "$(cat "$KEYRING")"
next=$(next_keyring "$root")
[[ -e $root/etc/pacman.d/gnupg/image-leftover && ! -e $next && ! -e $next.lock ]] ||
fail "a failed keyring isn't moved into place, and is cleared"
pass "a keyring that fails in the background is made in finalization, as before"

rm -f "$KEYRING"
root=$(new_root upgrade-aarch64)
start_keyring "$root" aarch64 0 || fail "an upgrade's system setup starts"
[[ ! -e $KEYRING && ! -e $(next_keyring "$root").lock ]] || fail "an upgrade starts no keyring" "$(cat "$KEYRING" 2>/dev/null)"
pass "an upgrade keeps its keyring, starting no new one"

reset_logs
rm -f "$KEYRING"
root=$(new_root image-aarch64-apple)
Expand Down
Loading