Files
glacier-ng/tests/bootstrap_alpine.sh
T
2026-10-29 09:15:34 -04:00

349 lines
16 KiB
Bash

#!/bin/sh
# glacier-bootstrap, local mode (ROADMAP 1.3, GLACIER_BOOTSTRAP.txt):
# - a template + PKGDIR become a chrootable system in T, as root and as
# a user through doas; the host's /usr, /glacier and /etc stay
# byte-identical; the private copies are removed;
# - every template, verification and resolution problem is reported
# (all of them, each with its own exit status) before T is touched;
# - a pin is enforced, and a tampered file never reaches syspkg;
# - T must pass syspkg -R's trust check and be fresh, or initialised
# with nothing installed (a declined prompt can be re-run);
# - one transaction: a syspkg refusal leaves T with nothing installed;
# - -n never touches T; -t fails (6) when T has no working /bin/sh;
# - repositories: T's indexes are fetched with syspkg -R T -y, from
# T's OWN repos.conf and keys (a key the host doesn't trust), into
# T only; a failed fetch exits 7 without "Bootstrapped"; no url is a
# warning; a T keyring file resolving outside T is refused; -R -f
# uses T's repositories too.
# Runs INSIDE the Dockerfile.alpine_root image:
#
# docker run --rm -v "$PWD/tests/bootstrap_alpine.sh:/t.sh:ro" glacier-alpine_root sh /t.sh
FAILS=0
check() { d=$1; shift; if "$@"; then echo "PASS: $d"; else echo "FAIL: $d"; FAILS=$((FAILS+1)); fi; }
nocolor() { sed 's/\x1b\[[0-9;]*m//g'; }
export PATH=/app:$PATH
# B ARGS...: run glacier-bootstrap as root, answering the prompt with $ANS
# (default y). Sets $OUT and $RC.
B() {
OUT=$( (echo "${ANS:-y}" | glacier-bootstrap "$@"; echo "rc=$?") 2>&1 | nocolor)
RC=$(echo "$OUT" | sed -n 's/^rc=//p' | tail -1)
echo "$OUT" | sed 's/^/ /'
}
has() { echo "$OUT" | grep -qF -- "$1"; }
lacks() { ! has "$1"; }
rc() { [ "$RC" = "$1" ]; }
untouched() { [ -z "$(ls -A "$1")" ]; }
P=/srv/pkgs
mkdir -p $P /srv/src
# pkg NAME VER REPO [DEPS] [CONFS] -> $P/NAME-VER.gpkg + sidecar; files
# come from /srv/src/NAME (laid out as files/...), if it exists.
pkg() {
w=$(mktemp -d); d=$w/$1-$2
mkdir -p $d/files
[ -d /srv/src/$1 ] && cp -a /srv/src/$1/. $d/files/
list() { l=""; sep=""; for it in $1; do l="$l$sep{ PKG_NAME = \"${it%%:*}\"; PKG_VER = \"${it#*:}\"; }"; sep=", "; done; echo "$l"; }
printf 'PKG_NAME = "%s";\nPKG_VER = "%s";\nPKG_DESC = "t";\nPKG_MAINT = "t@e";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "%s";\nPKG_DEPS = (%s);\nPKG_CONFS = (%s);\n' \
"$1" "$2" "$3" "$(list "$4")" "$(list "$5")" > $d/manifest.gpm.cfg
tar czf $P/$1-$2.gpkg -C $w $1-$2
rm -rf $w
sidecar $P/$1-$2.gpkg
}
sidecar() { printf 'sha256:%s\n' "$(sha256sum "$1" | cut -d' ' -f1)" > "$1.sha256"; gsign -S -s /root/glacier-test.sec -m "$1.sha256" >/dev/null; }
hash() { sha256sum "$1" | cut -d' ' -f1; }
# A minimal system out of Alpine's own loader and busybox: alpmusl puts
# the loader where PT_INTERP looks (/lib -> usr/lib), abox gives /bin/sh.
mkdir -p /srv/src/alpmusl/usr/lib
cp /lib/ld-musl-x86_64.so.1 /srv/src/alpmusl/usr/lib/
ln -s ld-musl-x86_64.so.1 /srv/src/alpmusl/usr/lib/libc.musl-x86_64.so.1
mkdir -p /srv/src/abox/usr/bin
cp /bin/busybox /srv/src/abox/usr/bin/busybox
ln -s busybox /srv/src/abox/usr/bin/sh
mkdir -p /srv/src/abox/etc; echo "from abox" > /srv/src/abox/etc/motd
pkg alpmusl 1.2.5 base
pkg abox 1.37.0 base "alpmusl:1.2.5"
mkdir -p /srv/src/tool/usr/bin; printf '#!/bin/sh\necho tool-ok\n' > /srv/src/tool/usr/bin/tool; chmod 755 /srv/src/tool/usr/bin/tool
pkg tool 2.0.0 extra "abox:1.0.0"
# Resolution fixtures
pkg lib 1.0.0 extra
pkg app 1.0.0 extra "lib:2.0.0"
pkg needy 1.0.0 extra "ghost:1.0.0"
pkg rival 1.0.0 extra "" "abox:0.0.0"
# Collision: both ship /glacier/sys/links/bin/same
mkdir -p /srv/src/col1/usr/bin /srv/src/col2/usr/bin
echo 1 > /srv/src/col1/usr/bin/same; echo 2 > /srv/src/col2/usr/bin/same
pkg col1 1.0.0 extra; pkg col2 1.0.0 extra
# No shell at all
pkg data 1.0.0 extra
T=/mnt/img
mkdir -p $T
cat > /srv/min.template <<EOF
# a minimal system
NAME=minimal
PKG=alpmusl 1.2.5 sha256:$(hash $P/alpmusl-1.2.5.gpkg)
PKG=abox 1.37.0
PKG=tool 2.0.0
EOF
snap() {
find /usr /glacier /etc -xdev -printf '%p %y %m %U %G %s %T@ %l\n' 2>/dev/null | sort
find /usr /glacier /etc -xdev -type f -exec sha256sum {} + 2>/dev/null | sort
}
snap > /tmp/before
ls -A /tmp > /tmp/tmp-before
echo "== 1. bootstrap as root, with the smoke test"
B -t -d $P /srv/min.template $T
check "exits 0" rc 0
check "syspkg's summary was shown" has "Proceed? (y/N)"
check "ends with Bootstrapped" has "[✓] Bootstrapped minimal into $T."
check "smoke test reported" has "[✓] chroot $T /bin/sh -c true succeeded."
check "no repos.conf in T: a warning" has "[!] No remote repositories are configured in $T."
check "no repos.conf in T: no refresh" lacks "Syncing"
check "the pin is shown" has " * base::alpmusl 1.2.5 (pinned)"
check "syspkg read the verified copies, not PKGDIR" lacks "staged from $P/"
check "T's index lists all three" sh -c "grep -c :: $T/glacier/sys/index/current | grep -qx 3"
check "chroot T runs a base program" sh -c "chroot $T /bin/sh -c 'echo ok' | grep -qx ok"
check "chroot T runs an extra program (sys links)" sh -c "chroot $T /glacier/sys/links/bin/tool | grep -qx tool-ok"
check "/etc was copied into T" sh -c "grep -qx 'from abox' $T/etc/motd && ! [ -L $T/etc/motd ]"
check "T's links say runtime paths" sh -c "readlink $T/usr/bin/busybox | grep -q '^/glacier/sys/store/'"
snap > /tmp/after
check "host /usr, /glacier and /etc are byte-identical" cmp -s /tmp/before /tmp/after
ls -A /tmp | grep -v '^after$' | grep -v '^tmp-after$' > /tmp/tmp-after
check "the private copies were removed" cmp -s /tmp/tmp-before /tmp/tmp-after
echo "== 2. T already has packages: refused"
B -d $P /srv/min.template $T
check "exits 1" rc 1
check "says so" has "[x] $T already has packages installed."
echo "== 3. as a user, through doas"
echo "permit nopass glacier as root cmd /app/syspkg" >> /etc/doas.conf
echo "permit nopass glacier as root cmd chroot" >> /etc/doas.conf
mkdir -p /mnt/u
OUT=$(su glacier -c "PATH=/app:\$PATH; echo y | glacier-bootstrap -t -d $P /srv/min.template /mnt/u; echo rc=\$?" 2>&1 | nocolor)
echo "$OUT" | sed 's/^/ /'
check "exits 0" has "rc=0"
check "bootstrapped" has "[✓] Bootstrapped minimal into /mnt/u."
check "smoke test passed" has "succeeded."
check "T's store is root's" sh -c "[ \"\$(stat -c %u /mnt/u/glacier/sys/store)\" = 0 ]"
check "the user's private copies were removed" sh -c "! find /tmp -maxdepth 1 -user glacier | grep -q ."
echo "== 4. template errors: all reported, exit 2, T untouched"
mkdir -p /mnt/e
cat > /srv/bad.template <<EOF
NAME=bad
SYS_PROFILE=x86_64-musl
PKG=abox 1.37.0
PKG=abox 1.37.0
PKG=lib 1.0
PKG=lib2 1.0.0 sha256:abc
PKG=lib3
just words
NAME=again
EOF
B -d $P /srv/bad.template /mnt/e
check "exits 2" rc 2
check "unknown key" has "bad.template:2: Unknown key: SYS_PROFILE"
check "duplicate package" has "bad.template:4: abox is listed twice (first on line 3)."
check "two-part version" has "bad.template:5: Invalid version for lib: 1.0"
check "malformed pin" has "bad.template:6: Malformed pin for lib2: sha256:abc"
check "PKG with no version" has "bad.template:7: Expected PKG=NAME VERSION [sha256:HEX]."
check "not KEY=value" has "bad.template:8: Expected KEY=value."
check "second NAME" has "bad.template:9: NAME given more than once."
check "T untouched" untouched /mnt/e
printf 'PKG=abox 1.37.0\n' > /srv/noname.template
B -d $P /srv/noname.template /mnt/e
check "no NAME: exit 2" rc 2
check "no NAME: says so" has "noname.template: No NAME."
printf 'NAME=empty\n# nothing\n' > /srv/empty.template
B -d $P /srv/empty.template /mnt/e
check "no packages: says so" has "empty.template: No packages."
B -d $P /srv/missing.template /mnt/e
check "missing template: exit 2" rc 2
echo "== 5. verification: all reported, exit 3, T untouched"
mkdir -p /srv/v
for f in alpmusl-1.2.5 abox-1.37.0 tool-2.0.0 lib-1.0.0; do cp $P/$f.gpkg $P/$f.gpkg.sha256 /srv/v/; done
rm /srv/v/abox-1.37.0.gpkg.sha256
echo "sha256:nothex" > /srv/v/lib-1.0.0.gpkg.sha256
echo junk >> /srv/v/tool-2.0.0.gpkg
cat > /srv/v.template <<EOF
NAME=v
PKG=alpmusl 1.2.5 sha256:$(printf '0%.0s' $(seq 64))
PKG=abox 1.37.0
PKG=tool 2.0.0
PKG=lib 1.0.0
PKG=gone 1.0.0
EOF
B -d /srv/v /srv/v.template /mnt/e
check "exits 3" rc 3
check "pin mismatch" has "[x] Refusing /srv/v/alpmusl-1.2.5.gpkg: the template pins 0000"
check "no sidecar" has "[x] Refusing /srv/v/abox-1.37.0.gpkg: no sidecar /srv/v/abox-1.37.0.gpkg.sha256."
check "tampered file" has "[x] Refusing /srv/v/tool-2.0.0.gpkg: hash mismatch"
check "malformed sidecar" has "[x] Refusing /srv/v/lib-1.0.0.gpkg: malformed sidecar"
check "missing file" has "[x] Refusing /srv/v/gone-1.0.0.gpkg: no such file."
check "T untouched" untouched /mnt/e
echo "== 6. a pin binds even when the sidecar is rewritten to match"
mkdir -p /srv/w; cp $P/alpmusl-1.2.5.gpkg /srv/w/; echo x >> /srv/w/alpmusl-1.2.5.gpkg; sidecar /srv/w/alpmusl-1.2.5.gpkg
printf 'NAME=w\nPKG=alpmusl 1.2.5 sha256:%s\n' "$(hash $P/alpmusl-1.2.5.gpkg)" > /srv/w.template
B -d /srv/w /srv/w.template /mnt/e
check "exits 3" rc 3
check "pin vs sidecar" has "the template pins $(hash $P/alpmusl-1.2.5.gpkg), the sidecar says"
check "T untouched" untouched /mnt/e
echo "== 7. resolution: all reported, exit 4, T untouched"
mkdir -p /srv/r; cp $P/* /srv/r/
cp $P/lib-1.0.0.gpkg /srv/r/libx-1.0.0.gpkg; sidecar /srv/r/libx-1.0.0.gpkg
cat > /srv/r.template <<EOF
NAME=r
PKG=alpmusl 1.2.5
PKG=abox 1.37.0
PKG=libx 1.0.0
PKG=lib 1.0.0
PKG=app 1.0.0
PKG=needy 1.0.0
PKG=rival 1.0.0
EOF
B -d /srv/r /srv/r.template /mnt/e
check "exits 4" rc 4
check "file holds another package" has "[x] /srv/r/libx-1.0.0.gpkg is lib 1.0.0, not libx 1.0.0."
sed -i '/libx/d' /srv/r.template
B -d /srv/r /srv/r.template /mnt/e
check "exits 4" rc 4
check "dependency too old" has "[x] extra::app requires lib >= 2.0.0, but the template lists 1.0.0."
check "dependency not listed" has "[x] extra::needy requires ghost >= 1.0.0, which the template does not list."
check "conflict" has "[x] base::abox conflicts with extra::rival."
check "T untouched" untouched /mnt/e
echo "== 8. -n: plan only"
B -n -d $P /srv/min.template /mnt/e
check "exits 0" rc 0
check "says dry run" has "[✓] Dry run: every package verified and resolved."
check "never asks" lacks "Proceed"
check "T untouched" untouched /mnt/e
echo "== 9. target refusals (exit 1)"
B -d $P /srv/min.template mnt/e
check "relative: exit 1" rc 1
check "relative: says so" has "[x] TARGET must be an absolute path: mnt/e"
B -d $P /srv/min.template /
check "/: refused" has "[x] TARGET must not be /."
mkdir -p /tmp/tt
B -d $P /srv/min.template /tmp/tt
check "under /tmp: exit 1" rc 1
check "under /tmp: the library says why" has "[x] Refusing install root /tmp/tt: /tmp is not root-owned, or is group/world-writable."
B -d $P /srv/min.template /mnt/nonexistent
check "missing: exit 1" rc 1
B -x -d $P /srv/min.template /mnt/e
check "unknown option: exit 1" rc 1
check "unknown option: says so" has "[x] Unknown option: -x"
echo "== 10. declined prompt: exit 5, then re-runnable"
mkdir -p /mnt/d
ANS=n B -d $P /srv/min.template /mnt/d
check "exits 5" rc 5
check "says nothing was installed" has "[x] Nothing was installed into /mnt/d."
check "T was initialised" test -e /mnt/d/glacier/sys/index/current
B -d $P /srv/min.template /mnt/d
check "re-run exits 0" rc 0
check "re-run did not run -n again" lacks "Created system glacier directories"
check "re-run installed" has "[✓] Bootstrapped minimal into /mnt/d."
echo "== 11. one transaction: a syspkg refusal installs nothing (exit 5)"
mkdir -p /mnt/c
printf 'NAME=c\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=col1 1.0.0\nPKG=col2 1.0.0\n' > /srv/c.template
B -d $P /srv/c.template /mnt/c
check "exits 5" rc 5
check "syspkg reported the collision" has "both ship /glacier/sys/links/bin/same."
check "T has nothing installed" sh -c "! grep -q :: /mnt/c/glacier/sys/index/current"
check "T's /usr has no links" sh -c "[ -z \"\$(ls -A /mnt/c/usr/bin)\" ]"
echo "== 12. -t with no shell: exit 6"
mkdir -p /mnt/s
printf 'NAME=s\nPKG=data 1.0.0\n' > /srv/s.template
B -t -d $P /srv/s.template /mnt/s
check "exits 6" rc 6
check "says the smoke test failed" has "[x] chroot /mnt/s /bin/sh -c true failed."
echo "== 13. syspkg not on PATH: exit 5"
mkdir -p /mnt/p
OUT=$( (echo y | PATH=/usr/bin:/bin /app/glacier-bootstrap -d $P /srv/min.template /mnt/p; echo "rc=$?") 2>&1 | nocolor)
check "exits 5" has "rc=5"
check "says so" has "[x] syspkg is not on PATH."
check "T untouched" untouched /mnt/p
echo "== 14. repositories: T's own repos.conf and keys"
# tkey is trusted by T's repos.conf only; the host's lists glacier-test.
gsign -G -p /srv/tkey.pub -s /root/tkey.sec -c tkey >/dev/null
R=/srv/repo/base
mkdir -p $R && chmod 755 /srv/repo $R
# repos NAME URL -> package NAME 1.0.0 (base), shipping T's keyring
repos() {
rm -rf /srv/src/$1; mkdir -p /srv/src/$1/etc/glacier/keys
cp /srv/tkey.pub /srv/src/$1/etc/glacier/keys/
printf 'repos = (\n\t{ name = "base";%s keys = [ "tkey" ]; }\n);\n' "${2:+ url = \"$2\";}" \
> /srv/src/$1/etc/glacier/repos.conf
pkg $1 1.0.0 base
}
repos trepos file://$R
repos badrepos file:///srv/nowhere
repos keyrepos ""
cp $P/alpmusl-1.2.5.gpkg* $P/abox-1.37.0.gpkg* $P/trepos-1.0.0.gpkg* $R/
pkg lib 1.0.0 base; cp $P/lib-1.0.0.gpkg* $R/
grepo -s /root/tkey.sec -r base $R >/dev/null
check "grepo signed the test repo" test -f $R/repo.idx.sig
printf 'NAME=r\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=trepos 1.0.0\n' > /srv/repo.template
mkdir -p /mnt/r
B -d $P /srv/repo.template /mnt/r
check "exits 0" rc 0
check "synced" has "[✓] All repositories synced successfully."
check "then Bootstrapped" has "[✓] Bootstrapped r into /mnt/r."
check "T holds the index" cmp -s $R/repo.idx /mnt/r/glacier/sys/repos/base.idx
check "and its signature" cmp -s $R/repo.idx.sig /mnt/r/glacier/sys/repos/base.idx.sig
check "the index is world-readable" sh -c "[ \"\$(stat -c %a /mnt/r/glacier/sys/repos/base.idx)\" = 644 ]"
check "the host got no index" test ! -e /glacier/sys/repos/base.idx
OUT=$( (echo y | syspkg -R /mnt/r -f lib; echo "rc=$?") 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
check "syspkg -R T -f uses T's repositories" has "rc=0"
check "and installed into T" sh -c "grep -q '^base::lib::1.0.0::' /mnt/r/glacier/sys/index/current"
echo "== 15. a failed fetch: exit 7, T stays installed"
mkdir -p /mnt/f
printf 'NAME=f\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=badrepos 1.0.0\n' > /srv/f.template
B -d $P /srv/f.template /mnt/f
check "exits 7" rc 7
check "says why" has "[x] Cannot fetch the index for base."
check "no Bootstrapped" lacks "Bootstrapped"
check "T is installed" sh -c "grep -q '^base::badrepos::1.0.0' /mnt/f/glacier/sys/index/current"
echo "== 15b. T's repos.conf has no url: a warning, exit 0"
mkdir -p /mnt/k
printf 'NAME=k\nPKG=alpmusl 1.2.5\nPKG=abox 1.37.0\nPKG=keyrepos 1.0.0\n' > /srv/k.template
B -d $P /srv/k.template /mnt/k
check "exits 0" rc 0
check "warns" has "[!] No remote repositories are configured in /mnt/k."
check "no refresh" lacks "Syncing"
check "Bootstrapped" has "[✓] Bootstrapped k into /mnt/k."
echo "== 16. T's keyring must resolve inside T"
mkdir -p /srv/hostconf && cp /mnt/r/etc/glacier/repos.conf /srv/hostconf/
rm /mnt/r/etc/glacier/repos.conf; ln -s /srv/hostconf/repos.conf /mnt/r/etc/glacier/repos.conf
OUT=$(syspkg -R /mnt/r -y 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
check "repos.conf outside T refused" has "[x] Refusing /mnt/r/etc/glacier/repos.conf: it resolves outside /mnt/r."
rm /mnt/r/etc/glacier/repos.conf; cp /srv/hostconf/repos.conf /mnt/r/etc/glacier/
rm /mnt/r/etc/glacier/keys/tkey.pub; ln -s /srv/tkey.pub /mnt/r/etc/glacier/keys/tkey.pub
OUT=$(syspkg -R /mnt/r -y 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
check "a key outside T refused" has "[x] Refusing /mnt/r/etc/glacier/keys/tkey.pub: it resolves outside /mnt/r."
rm /mnt/r/etc/glacier/keys/tkey.pub; cp /srv/tkey.pub /mnt/r/etc/glacier/keys/
OUT=$(syspkg -R /mnt/r -y 2>&1 | nocolor); echo "$OUT" | sed 's/^/ /'
check "restored: syncs again" has "[✓] All repositories synced successfully."
echo
echo "FAILURES: $FAILS"
[ $FAILS -eq 0 ]