224 lines
12 KiB
Bash
224 lines
12 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"
|
|
archive_hash=$(sha256sum "$archive" | cut -d' ' -f1)
|
|
role=$(awk -v hash="$archive_hash" '$2 == hash {print $1}' "$generation/dkms-policy.tsv")
|
|
package=$(bsdtar -xOf "$archive" .PKGINFO | sed -n 's/^pkgname = //p')
|
|
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/linux)
|
|
[[ $package == linux-headers ]] || { echo "Unexpected kernel source link: $package" >&2; exit 1; } ;;
|
|
usr/src/*)
|
|
[[ $role == source ]] || { echo "Unreviewed DKMS source package: $package ($archive_hash)" >&2; exit 1; } ;;
|
|
usr/lib/modules/*/build/*|usr/lib/modules/*/vdso/*.so)
|
|
[[ $package == *-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)
|
|
[[ $role == framework ]] || 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)
|
|
[[ $role == framework ]] || 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
|
|
# Exact reviewed 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"
|
|
|
|
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" \
|
|
/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
|
|
python /run/package-transaction.py install /run/package-request.json
|
|
else
|
|
pacman -S --needed --noconfirm --cachedir /inputs -- "${requested[@]}"
|
|
fi
|
|
for target in "${requested[@]}"; do
|
|
name=${target%%=*}
|
|
if ! grep -Fxq "$name" /explicit-requested; then
|
|
pacman -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
|
|
[[ $(pacman -Q -- "$name") == "$name $version" ]] || {
|
|
echo "Base package replacement requires a separate override: $name" >&2; exit 1;
|
|
}
|
|
done < /base-packages
|
|
fi
|
|
pacman -Dk
|
|
if command -v dkms >/dev/null; then
|
|
/bin/bash /dkms-finalize.sh
|
|
fi
|
|
pacman -Q > /usr/lib/archipelago/packages.txt
|
|
ldconfig
|
|
systemd-hwdb update --usr
|
|
if command -v glib-compile-schemas >/dev/null; then glib-compile-schemas /usr/share/glib-2.0/schemas; fi
|
|
if command -v gio-querymodules >/dev/null; then gio-querymodules /usr/lib/gio/modules; fi
|
|
if command -v update-desktop-database >/dev/null; then update-desktop-database /usr/share/applications; fi
|
|
if command -v update-mime-database >/dev/null; then update-mime-database /usr/share/mime; fi
|
|
if command -v gtk-update-icon-cache >/dev/null; then
|
|
for theme in /usr/share/icons/*; do
|
|
[[ ! -f $theme/index.theme ]] || gtk-update-icon-cache -q -t -f "$theme"
|
|
done
|
|
fi
|
|
update-ca-trust
|
|
for modules in /usr/lib/modules/*; do
|
|
[[ -f $modules/vmlinuz ]] || continue
|
|
kernel=${modules##*/}
|
|
depmod "$kernel"
|
|
config=/usr/lib/archipelago/mkinitcpio-core.conf
|
|
if [[ -f /usr/lib/archipelago/mkinitcpio-gnome.conf ]]; then
|
|
config=/usr/lib/archipelago/mkinitcpio-gnome.conf
|
|
elif [[ -f /usr/lib/archipelago/mkinitcpio-kde.conf ]]; then
|
|
config=/usr/lib/archipelago/mkinitcpio-kde.conf
|
|
fi
|
|
mkinitcpio --nocolor -c "$config" -k "$kernel" -g "$modules/initramfs.img"
|
|
done
|
|
' 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"
|