Files
2026-10-29 09:15:34 -04:00

197 lines
10 KiB
Bash

#!/bin/sh
# syspkg -R/--root DIR and --strict (ROADMAP 1.2), and file collisions
# between packages (ROADMAP 2.6):
# - the install root must be absolute, exist, and it and every
# directory above it must be root-owned, non-symlink directories that
# aren't group/world-writable;
# - -R builds a working system in DIR (chroot runs it) and leaves the
# host's /usr, /glacier and /etc byte-identical;
# - -R implies --strict: a path the link pass would refuse fails the
# whole transaction before the prompt (-O still displaces);
# - --strict on the host does the same; without it, the old behaviour
# (committed, "[x] N paths were not linked.", exit 1);
# - two packages shipping the same path are refused, in any scope, and
# the library refuses them at commit for callers that skip the CLI;
# Runs INSIDE the Dockerfile.alpine_root image:
#
# ./run_docker.sh Dockerfile.alpine_root # builds glacier-alpine_root
# docker run --rm -v "$PWD/tests/root_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; }
# run CMD: output (colour stripped) in $OUT, exit status in $STATUS
run() {
OUT=$(sh -c "$1; echo \"exit=\$?\"" 2>&1 | sed 's/\x1b\[[0-9;]*m//g')
STATUS=$(echo "$OUT" | sed -n 's/^exit=//p' | tail -1)
OUT=$(echo "$OUT" | grep -v '^exit=')
echo "$OUT" | sed 's/^/ /'
}
has() { echo "$OUT" | grep -qxF "$1"; }
hasnt() { ! echo "$OUT" | grep -qF "$1"; }
T=/mnt/img
S=/glacier/sys/store
SP="./syspkg"
# pkg NAME REPO: /srv/src/NAME (laid out as files/...) -> /srv/NAME-1.0.0.gpkg + sidecar
pkg() {
w=$(mktemp -d); d=$w/$1-1.0.0
mkdir -p $d; cp -a /srv/src/$1 $d/files
printf 'PKG_NAME = "%s";\nPKG_VER = "1.0.0";\nPKG_DESC = "t";\nPKG_MAINT = "t@e";\nPKG_COPYRIGHT = "MIT";\nPKG_REPO = "%s";\nPKG_DEPS = ();\nPKG_CONFS = ();\n' "$1" "$2" > $d/manifest.gpm.cfg
tar czf /srv/$1-1.0.0.gpkg -C $w $1-1.0.0
printf 'sha256:%s\n' "$(sha256sum /srv/$1-1.0.0.gpkg | cut -d' ' -f1)" > /srv/$1-1.0.0.gpkg.sha256
gsign -S -s /root/glacier-test.sec -m /srv/$1-1.0.0.gpkg.sha256 >/dev/null
chmod 644 /srv/$1-1.0.0.gpkg*; rm -rf $w
}
file() { mkdir -p "/srv/src/$1/$(dirname "$2")"; echo "$1" > "/srv/src/$1/$2"; }
mkdir -p /srv/src; chmod 755 /srv
# A bootable minimum: Alpine's loader + busybox, as base packages
mkdir -p /srv/src/alpmusl/usr/lib /srv/src/bb/usr/bin
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
cp /bin/busybox /srv/src/bb/usr/bin/busybox
pkg alpmusl base; pkg bb base
file tool usr/bin/tool; pkg tool extra
file clash usr/bin/clash; pkg clash base
file hclash usr/bin/hclash; pkg hclash base
file dupa usr/bin/dup; pkg dupa extra
file dupb usr/bin/dup; pkg dupb extra
file basedup usr/bin/dup; pkg basedup base # lands in /usr: no collision
file dird usr/share/thing/inside; pkg dird extra
file filed usr/share/thing; pkg filed extra
file ua usr/bin/udup; pkg ua extra
file ub usr/bin/udup; pkg ub extra
cd /app
./syspkg -n >/dev/null 2>&1 # the host's own system
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
echo "== 1. the install root is checked before anything happens"
run "su glacier -c 'cd /app && ./syspkg -R $T -n'"
check "non-root refused" has "[x] Cannot open system index: Permission denied"
mkdir -p /tmp/img
run "$SP -R /tmp/img -n"
check "under /tmp (world-writable)" has "[x] Refusing install root /tmp/img: /tmp is not root-owned, or is group/world-writable."
mkdir -p /mnt/userimg && chown glacier /mnt/userimg
run "$SP -R /mnt/userimg -n"
check "a user-owned root" has "[x] Refusing install root /mnt/userimg: /mnt/userimg is not root-owned, or is group/world-writable."
mkdir -p /mnt/real && ln -s /mnt/real /mnt/link
run "$SP -R /mnt/link -n"
check "a symlink" has "[x] Refusing install root /mnt/link: /mnt/link is a symlink."
run "$SP -R mnt/img -n"
check "a relative path" has "[x] Refusing install root mnt/img: not an absolute path without . or .. components."
run "$SP -R /mnt/../etc -n"
check "a .. component" has "[x] Refusing install root /mnt/../etc: not an absolute path without . or .. components."
run "$SP -R /mnt/nope -n"
check "a missing root" has "[x] Refusing install root /mnt/nope: /mnt/nope is missing."
check "nothing was created anywhere" test ! -e /tmp/img/glacier -a ! -e /mnt/userimg/glacier -a ! -e /mnt/real/glacier
run "$SP -R"
check "-R without a directory" has "[x] -R needs a directory."
echo "== 2. -R DIR -n"
mkdir -p $T && chmod 755 $T
run "$SP -R $T -n"
check "created" has "[✓] Created system glacier directories:"
check "paths named under the root" has " * $T/glacier/sys/store"
check "merged-/usr tree in the root" test "$(readlink $T/bin)" = usr/bin
check "store is 755" test "$(stat -c %a $T$S)" = 755
run "$SP -R $T -n"
check "a second -n refused" has "[x] Cannot create $T/glacier/sys: already exists"
run "$SP -R /mnt/real -l /srv/tool-1.0.0.gpkg"
check "-l before -n" has "[x] No system index found. Run ./syspkg -R /mnt/real -n first."
echo "== 3. installing into the root (every flag form)"
run "echo y | $SP -R $T -l /srv/alpmusl-1.0.0.gpkg /srv/bb-1.0.0.gpkg"
check "-R DIR -l" test "$STATUS" = 0
run "echo y | $SP -lR $T /srv/tool-1.0.0.gpkg"
check "-lR DIR" test "$STATUS" = 0
run "echo y | $SP --root=$T -x tool"
check "--root=DIR -x" test "$STATUS" = 0
run "echo y | $SP --root $T -l /srv/tool-1.0.0.gpkg"
check "--root DIR -l" test "$STATUS" = 0
check "base links in T/usr say runtime paths" test "$(readlink $T/usr/bin/busybox)" = "$S/base/bb/bb-1.0.0/usr/bin/busybox"
check "extra links in T's sys links" test "$(readlink $T/glacier/sys/links/bin/tool)" = "$S/extra/tool/tool-1.0.0/usr/bin/tool"
check "chroot T runs busybox through T's loader" sh -c "chroot $T /usr/bin/busybox echo booted | grep -qx booted"
check "the image's index lists what it holds" grep -q "^extra::tool::1.0.0::explicit$" $T/glacier/sys/index/current
echo "== 4. -R is strict: a refusal fails the transaction"
echo foreign > $T/usr/bin/clash
run "echo y | $SP -R $T -l /srv/clash-1.0.0.gpkg"
check "refused with the path" has "[x] Refusing to overwrite non-glacier path: $T/usr/bin/clash"
check "before the prompt" hasnt "Proceed?"
check "exit 1" test "$STATUS" = 1
check "nothing installed" test ! -e $T$S/base/clash
check "the foreign file untouched" grep -qx foreign $T/usr/bin/clash
run "echo y | $SP -R $T -lO /srv/clash-1.0.0.gpkg"
check "-O displaces it instead" test "$STATUS" = 0
check "original kept under its runtime path" grep -qx foreign $T$S/.displaced/usr/bin/clash
echo "== 5. two packages shipping the same path"
run "echo y | $SP -R $T -l /srv/dupa-1.0.0.gpkg"
run "echo y | $SP -R $T -l /srv/dupb-1.0.0.gpkg"
check "refused, naming both and the path" has "[x] extra::dupb and extra::dupa both ship /glacier/sys/links/bin/dup."
check "before the prompt" hasnt "Proceed?"
check "dupb not installed" test ! -e $T$S/extra/dupb
run "echo y | $SP -R $T -x dupa"
run "echo y | $SP -R $T -l /srv/dupa-1.0.0.gpkg /srv/dupb-1.0.0.gpkg"
check "a colliding pair in one batch: reported once" test "$(echo "$OUT" | grep -c 'both ship')" = 1
run "echo y | $SP -R $T -l /srv/dupa-1.0.0.gpkg /srv/basedup-1.0.0.gpkg"
check "same name in /usr and in sys links: no collision" test "$STATUS" = 0
run "echo y | $SP -R $T -l /srv/dird-1.0.0.gpkg /srv/filed-1.0.0.gpkg"
check "a file where another package has a directory" has "[x] extra::dird and extra::filed both ship /glacier/sys/links/share/thing."
snap > /tmp/after
check "everything under -R left the host's /usr, /glacier and /etc byte-identical" cmp -s /tmp/before /tmp/after
cmp -s /tmp/before /tmp/after || diff /tmp/before /tmp/after | head -10
echo "== 6. the host: --strict, and collisions without it"
echo foreign > /usr/bin/hclash
run "echo y | $SP --strict -l /srv/hclash-1.0.0.gpkg"
check "--strict refuses before the prompt" has "[x] Refusing to overwrite non-glacier path: /usr/bin/hclash"
check "nothing installed" test ! -e $S/base/hclash
run "echo y | $SP -l /srv/hclash-1.0.0.gpkg"
check "without --strict: committed, but an error" has "[x] 1 path was not linked."
check "exit 1" test "$STATUS" = 1
run "echo y | $SP -l /srv/dupa-1.0.0.gpkg /srv/dupb-1.0.0.gpkg"
check "collisions refused without --strict too" has "[x] extra::dupa and extra::dupb both ship /glacier/sys/links/bin/dup."
echo "== 7. the library refuses at commit (no CLI pre-check)"
cat > /tmp/lib.lua <<'EOF'
package.path = "/app/?.lua;" .. package.path
local ffi = require("ffi"); require("lib/glacier_cdef")
local istore = ffi.load("/app/lib/glacier/libglacier_istoreutils.so")
local txn = ffi.load("/app/lib/glacier/libglacier_transaction.so")
local ctx = ffi.new("gl_context_t")
assert(txn.gl_init_stage_context(ctx, ffi.C.GL_SCOPE_SYS, 0) == 0)
ctx.strict = arg[1] == "strict" and 1 or 0
for i = 2, #arg do assert(istore.gl_install_pkg(ctx, arg[i]) == 0) end
local st = txn.gl_commit_transaction(ctx)
txn.gl_abort_transaction(ctx)
print(tonumber(st))
EOF
echo y | $SP -x hclash >/dev/null 2>&1
run "luajit /tmp/lib.lua strict /srv/hclash-1.0.0.gpkg"
check "strict context, foreign file: GL_TXN_ERR_DEPS" test "$(echo "$OUT" | tail -1)" = 9
check "nothing committed" test ! -e $S/base/hclash
run "luajit /tmp/lib.lua loose /srv/dupa-1.0.0.gpkg /srv/dupb-1.0.0.gpkg"
check "a collision: GL_TXN_ERR_DEPS, strict or not" test "$(echo "$OUT" | tail -1)" = 9
check "and said why" has "[x] extra::dupa and extra::dupb both ship /glacier/sys/links/bin/dup."
echo "== 8. user scope: collisions refused, -R doesn't exist"
su glacier -c "doas /app/gstore -n" >/dev/null 2>&1
run "su glacier -c 'cd /app && echo y | ./gpkg -l /srv/ua-1.0.0.gpkg /srv/ub-1.0.0.gpkg'"
check "two user packages shipping the same path" has "[x] extra::ua and extra::ub both ship /glacier/usr/1000/links/bin/udup."
run "su glacier -c 'cd /app && ./gpkg -R $T -l /srv/ua-1.0.0.gpkg'"
check "gpkg has no -R" has "[x] Unknown option: -R"
echo "== 9. help"
run "$SP -h"
check "-R in the help" sh -c 'echo "$1" | grep -q -- "{-R --root} DIR"' _ "$OUT"
check "--strict in the help" sh -c 'echo "$1" | grep -q -- "{--strict}"' _ "$OUT"
echo
[ $FAILS -eq 0 ] && echo "ALL PASSED" || echo "$FAILS FAILED"
exit $FAILS