system/adapters/extensions.sh

249 lines
13 KiB
Bash

generation=$1
service_packages=${2:-0}
[[ $service_packages == 0 || $service_packages == 1 ]]
root="$generation/root"
mkdir -p "$generation/inputs" "$generation/logs"
exec > >(tee "$generation/logs/compose.log") 2>&1
export LC_ALL=C
policy="$root/usr/lib/archipelago/hook-policy"
[[ -f $policy/hooks.sha256 && -f $policy/scriptlets.sha256 ]] || {
echo 'Base image lacks the reviewed hook inventory; rebuild the base with repository-backed extension support' >&2
exit 1
}
if [[ $service_packages == 0 ]]; then mv "$root/usr/etc" "$root/etc"; fi
cp -a "$root/etc/resolv.conf" "$generation/resolv.conf"
mkdir -p "$root/var/lib" "$root/run"
if [[ $service_packages == 0 && -f /etc/archipelago/extensions-mirrorlist ]]; then
cp /etc/archipelago/extensions-mirrorlist "$root/etc/pacman.d/mirrorlist"
fi
cp "$root/usr/lib/archipelago/packages.txt" "$generation/base-packages"
cp "$generation/requested" "$generation/transaction-targets"
(cd "$root/usr/lib/archipelago/package-db/sync"; sha256sum *.db) > "$generation/sync.sha256"
# The immutable base supplies sync DBs, repository order and transport URLs. No -y.
printf 'ARCHIPELAGO_PHASE\tpackages\n'
if [[ $service_packages == 0 ]]; then
systemd-nspawn --settings=no --register=no --resolv-conf=replace-host \
-D "$root" --bind="$generation/inputs:/inputs" --bind="$generation/transaction-targets:/requested" \
/bin/bash -euo pipefail -c '
export LC_ALL=C
systemd-sysusers
pacman-key --init
pacman-key --populate archlinux archipelago
mapfile -t requested < /requested
for name in "${requested[@]}"; do pacman -Si -- "$name" >/dev/null; done
pacman -Sp --needed --print-format "%n %v %r %l" -- "${requested[@]}" > /inputs/plan
if grep -q "^dkms " /inputs/plan || pacman -Qq dkms >/dev/null 2>&1; then
count=0
for modules in /usr/lib/modules/*; do
[[ -f $modules/vmlinuz ]] || continue
kernel=${modules##*/}
pkgbase=$(cat "$modules/pkgbase")
[[ $pkgbase =~ ^[a-zA-Z0-9_+-]+$ ]] || exit 1
version=$(pacman -Q "$pkgbase" | cut -d" " -f2)
# Resolve exact headers from the retained sync DB; never refresh.
pacman -Sp --print-format "%n %v %l" "$pkgbase-headers=$version" >> /inputs/headers-plan
printf "%s\n" "$pkgbase-headers=$version" >> /requested
count=$((count + 1))
done
(( count > 0 )) || { echo "No target kernel for DKMS" >&2; exit 1; }
mapfile -t requested < /requested
pacman -Sp --needed --print-format "%n %v %r %l" -- "${requested[@]}" > /inputs/plan
fi
# Keep pacman signature/checksum validation, using a bounded external
# downloader to avoid the observed HTTP/2 reset on large mirror objects.
cp /etc/pacman.conf /run/archipelago-download.conf
sed -i "/^[[:space:]]*XferCommand[[:space:]]*=/d" /run/archipelago-download.conf
sed -i "/^\\[options\\]/a XferCommand = /usr/bin/curl --http1.1 --fail --location --retry 3 --connect-timeout 30 --max-time 1200 --output %o %u" /run/archipelago-download.conf
cp /run/archipelago-download.conf /inputs/download-pacman.conf
# pacman runs XferCommand as DownloadUser=alpm. Give that identity only
# the archive cache, not the root-owned plan/provenance directory.
install -d -o alpm -g alpm -m 0700 /inputs/download
pacman --config /run/archipelago-download.conf -Sw --needed --noconfirm --cachedir /inputs/download -- "${requested[@]}"
shopt -s nullglob
archives=(/inputs/download/*.pkg.tar.*)
if (( ${#archives[@]} )); then
chown root:root "${archives[@]}"
mv -- "${archives[@]}" /inputs/
fi
' 2>&1 | tee "$generation/logs/download.log"
cp "$generation/inputs/plan" "$generation/plan"
fi
(cd "$root/usr/lib/archipelago/package-db/sync"; sha256sum -c "$generation/sync.sha256")
# Inspect exact signed/downloaded inputs before any install script can run.
# New actions and unsupported persistent/boot payloads need explicit adapters.
shopt -s nullglob
dkms_enabled=false
for archive in "$generation/inputs/"*.pkg.tar.*; do
[[ $archive != *.sig ]] || continue
listing="$generation/logs/$(basename "$archive").files"
bsdtar -tf "$archive" > "$listing"
package=$(bsdtar -xOf "$archive" .PKGINFO | sed -n 's/^pkgname = //p')
# Classify native DKMS inputs structurally, without a package/hash allowlist.
# Package configuration is evaluated only by DKMS inside offline staging.
role=''
if [[ $package == dkms ]]; then
role=framework
elif grep -qE '^(\./)?usr/src/[^/]+/dkms\.conf$' "$listing"; then
role=source
elif grep -qE '^(\./)?usr/lib/modules/[^/]+/build/include/config/kernel\.release$' "$listing"; then
# Desktop purposes replace `linux` with another kernel (linux-zen today).
# Recognize headers by the build tree they ship, not by package name or
# by the single `usr/src/linux` link of the stock kernel package.
role=headers
links="$generation/logs/$(basename "$archive").links"
bsdtar -tvf "$archive" | awk '$1 ~ /^l/ && $(NF-1) == "->" { print $(NF-2) "\t" $NF }' > "$links"
fi
if [[ $role == framework ]]; then dkms_enabled=true; fi
while IFS= read -r path; do
path=${path#./}
case "$path" in
.PKGINFO|.BUILDINFO|.MTREE|.INSTALL|''|*/) continue ;;
usr/src/*)
case $role in
source) ;;
headers)
# Headers may add only convenience links into the matching
# module build tree; a real second source tree stays an error.
target=$(awk -F'\t' -v path="$path" '$1 == path || $1 == "./" path { print $2; exit }' "$links")
[[ $target == ../lib/modules/*/build ]] || {
echo "Headers payload has an unexpected source tree: $package: $path" >&2; exit 1; }
;;
*) echo "Source payload lacks a DKMS configuration: $package" >&2; exit 1 ;;
esac ;;
usr/lib/modules/*/build/*|usr/lib/modules/*/vdso/*.so)
[[ $role == headers ]] || { echo "Unexpected kernel build payload: $package" >&2; exit 1; } ;;
usr/local/*|usr/lib/modules/*|boot/*|opt/*|var/*|home/*|root/*|srv/*|mnt/*|etc/pacman.d/hooks/*)
echo "Package needs a persistence/kernel adapter: $(basename "$archive"): $path" >&2; exit 1 ;;
usr/*|etc/*) ;;
*) echo "Unsupported package payload path: $path" >&2; exit 1 ;;
esac
done < "$listing"
if grep -qxE '(\./)?\.INSTALL' "$listing"; then
script=$(grep -xE '(\./)?\.INSTALL' "$listing")
hash=$(bsdtar -xOf "$archive" "$script" | sha256sum | cut -d' ' -f1)
[[ -n $role ]] || grep -q "^$hash " "$policy/scriptlets.sha256" || {
echo "Unclassified .INSTALL in $(basename "$archive"); hash=$hash" >&2; exit 1;
}
fi
while IFS= read -r hook; do
hash=$(bsdtar -xOf "$archive" "$hook" | sha256sum | cut -d' ' -f1)
[[ -n $role ]] || grep -Fxq "$hash ${hook#./}" "$policy/hooks.sha256" || {
echo "Unclassified hook in $(basename "$archive"): $hook; hash=$hash" >&2; exit 1;
}
done < <(grep -E '^(\./)?usr/share/libalpm/hooks/[^/]+\.hook$' "$listing" || true)
done
if $dkms_enabled; then
# Native framework hooks are replaced by the checked offline finalizer.
# Keep overrides for future staging; no live transaction is allowed on this OS.
mkdir -p "$root/etc/pacman.d/hooks"
for hook in 70-dkms-install.hook 70-dkms-upgrade.hook 71-dkms-remove.hook; do
[[ ! -e $root/etc/pacman.d/hooks/$hook && ! -L $root/etc/pacman.d/hooks/$hook ]] || {
echo "Unexpected DKMS hook override: $hook" >&2; exit 1;
}
ln -s /dev/null "$root/etc/pacman.d/hooks/$hook"
done
fi
(cd "$generation/inputs"; find . -maxdepth 1 -type f ! -name SHA256SUMS -print0 | sort -z | xargs -0 sha256sum) > "$generation/inputs/SHA256SUMS"
# Runtime-only base hooks call the target systemd bus, which does not exist in
# the isolated composition. Mask direct systemctl actions (module loading, sshd
# restart markers); the target boot applies runtime state. Hooks routed through
# systemd-hook already detect a chroot and skip themselves. Do not overwrite an
# unrelated existing hook: that would hide an image or admin override.
mkdir -p "$root/etc/pacman.d/hooks"
for hook in "$root"/usr/share/libalpm/hooks/*.hook; do
[[ -f $hook ]] || continue
grep -Eq '^[[:space:]]*Exec[[:space:]]*=.*systemctl' "$hook" || continue
name=$(basename "$hook")
existing="$root/etc/pacman.d/hooks/$name"
if [[ -e $existing && ! -L $existing ]]; then
echo "Runtime hook conflict: $name" >&2
exit 1
fi
ln -sfn /dev/null "$existing"
done
service_args=()
if [[ $service_packages == 1 ]]; then
service_args+=(--setenv=ARCHIPELAGO_SERVICE_PACKAGE=1
--bind-ro="$generation/package-transaction.py:/run/package-transaction.py"
--bind-ro="$generation/request.json:/run/package-request.json"
--bind-ro="$generation/sandbox-marker:/run/archipelago-package-sandbox")
fi
systemd-nspawn --settings=no --register=no --private-network --resolv-conf=off "${service_args[@]}" \
-D "$root" --bind-ro="$generation/inputs:/inputs" \
--bind-ro="$generation/transaction-targets:/requested" --bind-ro="$generation/base-packages:/base-packages" \
--bind-ro="$generation/requested:/explicit-requested" \
--bind="$generation/logs:/evidence" --bind-ro="$generation/dkms-finalize.sh:/dkms-finalize.sh" \
--bind-ro="$generation/finalize-managed.sh:/finalize-managed.sh" \
/bin/bash -euo pipefail -c '
export LC_ALL=C
cd /inputs; sha256sum -c SHA256SUMS; cd /
mapfile -t requested < /requested
if [[ ${ARCHIPELAGO_SERVICE_PACKAGE:-0} == 1 ]]; then
# The service checkout has factory account files, unlike the legacy
# path which already ran sysusers during download. Materialize the
# base identities before installation and ownership validation.
# Factory files are restored below before committing the image.
systemd-sysusers
python /run/package-transaction.py install /run/package-request.json
else
pacman -S --needed --noconfirm --cachedir /inputs -- "${requested[@]}"
fi
# Local DB operations must not initialize repository transport or require
# the runtime DownloadUser in this account-free checkout.
local_db=(pacman --config /dev/null --dbpath /usr/lib/archipelago/package-db)
for target in "${requested[@]}"; do
name=${target%%=*}
if ! grep -Fxq "$name" /explicit-requested; then
"${local_db[@]}" -D --asdeps -- "$name"
fi
done
# Add is not an override or base upgrade. Reject any removed/replaced base package.
if [[ ${ARCHIPELAGO_SERVICE_PACKAGE:-0} != 1 ]]; then
while read -r name version; do
[[ $("${local_db[@]}" -Q -- "$name") == "$name $version" ]] || {
echo "Base package replacement requires a separate override: $name" >&2; exit 1;
}
done < /base-packages
fi
"${local_db[@]}" -Dk
if command -v dkms >/dev/null; then
/bin/bash /dkms-finalize.sh
fi
"${local_db[@]}" -Q > /usr/lib/archipelago/packages.txt
/bin/bash -euo pipefail /finalize-managed.sh
' 2>&1 | tee "$generation/logs/transaction.log"
if grep -Ei 'error:|warning:.*(failed|failure)' "$generation/logs/transaction.log"; then
echo 'Required package action/finalization failed; no deployment prepared' >&2; exit 1
fi
(cd "$root/usr/lib/archipelago/package-db/sync"; sha256sum -c "$generation/sync.sha256")
cp "$root/usr/lib/archipelago/packages.txt" "$generation/resolved"
cp -a "$root/usr/lib/archipelago/package-db" "$generation/package-db"
# Immutable non-root ownership must refer to the same identity on the target.
# New service-owned payloads need an explicit UID/GID adapter; do not guess IDs.
while IFS= read -r -d '' file; do
uid=$(stat -c %u "$file"); gid=$(stat -c %g "$file")
if [[ $uid != 0 ]]; then
target=$(getent passwd "$uid" | cut -d: -f1)
staged=$(awk -F: -v id="$uid" '$3 == id { print $1 }' "$root/etc/passwd")
[[ -n $target && $target == "$staged" ]] || { echo "UID adapter required: $file ($uid)"; exit 1; }
fi
if [[ $gid != 0 ]]; then
target=$(getent group "$gid" | cut -d: -f1)
staged=$(awk -F: -v id="$gid" '$3 == id { print $1 }' "$root/etc/group")
[[ -n $target && $target == "$staged" ]] || { echo "GID adapter required: $file ($gid)"; exit 1; }
fi
done < <(find "$root/usr" \( ! -uid 0 -o ! -gid 0 \) -print0)
for database in passwd shadow group gshadow; do
cp -a "$root/usr/share/factory/etc/$database" "$root/etc/$database"
done
rm -rf "$root/etc/pacman.d/gnupg"
rm -f "$root/etc/resolv.conf" "$root/etc/ssh/ssh_host_"* "$root/etc/.pwd.lock"
cp -a "$generation/resolv.conf" "$root/etc/resolv.conf"
: > "$root/etc/machine-id"
mv "$root/etc" "$root/usr/etc"
rm -rf "$root/var" "$root/run" "$root/tmp"
mkdir -p "$root/var" "$root/run" "$root/tmp"
chmod 1777 "$root/tmp"