91 lines
4.5 KiB
Bash
91 lines
4.5 KiB
Bash
#!/bin/bash
|
|
# Run only inside an isolated disposable Arch builder. /seed is private persistent
|
|
# build storage, /var/lib/flatpak is a dedicated installation, never the host's.
|
|
set -euo pipefail
|
|
test "$(id -u)" = 0
|
|
test -f /run/archipelago-flatpak-seed-builder
|
|
mkdir -p /seed/evidence
|
|
exec > >(tee -a /seed/evidence/prepare.log) 2>&1
|
|
pacman -S --needed --noconfirm flatpak python diffutils
|
|
command -v cmp >/dev/null
|
|
date --utc --iso-8601=seconds > /seed/evidence/started
|
|
pacman -Q > /seed/evidence/builder-packages.txt
|
|
if [[ -f /cached-seed/flathub.flatpakrepo ]]; then
|
|
cp /cached-seed/flathub.flatpakrepo /seed/flathub.flatpakrepo
|
|
else
|
|
curl --fail --location --max-time 120 https://dl.flathub.org/repo/flathub.flatpakrepo -o /seed/flathub.flatpakrepo
|
|
fi
|
|
printf '%s %s\n' 3371dd250e61d9e1633630073fefda153cd4426f72f4afa0c3373ae2e8fea03a /seed/flathub.flatpakrepo | sha256sum -c -
|
|
flatpak remote-add --system --if-not-exists flathub /seed/flathub.flatpakrepo
|
|
flatpak remote-modify --system --collection-id=org.flathub.Stable flathub
|
|
# Include all translations; the installed machine's eventual locale is unknown.
|
|
flatpak config --system --set languages '*'
|
|
python - <<'PY' > /seed/apps.txt
|
|
import json
|
|
policy=json.load(open('/policy.json'))
|
|
assert policy['version']==1 and policy['remote']=='flathub'
|
|
for ref in policy['applications']:
|
|
parts=ref.split('/')
|
|
assert len(parts)==4 and parts[0]=='app' and parts[2:] == ['x86_64','stable']
|
|
assert parts[1].startswith(('org.gnome.', 'org.kde.')) and all(c.isalnum() or c in '._-' for c in parts[1])
|
|
print(ref)
|
|
PY
|
|
mapfile -t apps < /seed/apps.txt
|
|
test "${#apps[@]}" -gt 0
|
|
if [[ -f /cached-seed/policy.json ]] && cmp -s /policy.json /cached-seed/policy.json; then
|
|
# Bootstrap from the previously verified export without redownloading apps.
|
|
unshare --net flatpak install --system --noninteractive --assumeyes \
|
|
--sideload-repo=/cached-seed/.ostree/repo flathub "${apps[@]}"
|
|
fi
|
|
flatpak install --system --noninteractive --assumeyes flathub "${apps[@]}"
|
|
flatpak update --system --noninteractive --assumeyes
|
|
flatpak list --system --columns=ref,origin,active > /seed/evidence/refs.tsv
|
|
# Ref identity plus exact commit, so an unchanged installation can reuse the
|
|
# previously verified export instead of rebuilding it. One stable format keeps
|
|
# the comparison independent of the flatpak CLI version.
|
|
ostree --repo=/var/lib/flatpak/repo refs | LC_ALL=C sort | while IFS= read -r ref; do
|
|
printf '%s %s\n' "$ref" "$(ostree --repo=/var/lib/flatpak/repo rev-parse "$ref")"
|
|
done > /seed/evidence/manifest.tsv
|
|
test -s /seed/evidence/manifest.tsv
|
|
if [[ -f /cached-seed/manifest.tsv ]] && cmp -s /seed/evidence/manifest.tsv /cached-seed/manifest.tsv; then
|
|
echo 'FLATPAK_SEED_UNCHANGED: installation commits match the cached export'
|
|
touch /seed/no-change
|
|
exit 0
|
|
fi
|
|
flatpak remotes --system --show-details > /seed/evidence/remotes.txt
|
|
test ! -e /seed/export
|
|
mkdir /seed/export
|
|
flatpak create-usb --system /seed/export "${apps[@]}"
|
|
ostree --repo=/seed/export/.ostree/repo fsck
|
|
# Content checksums/fsck alone do not prove that detached GPG metadata survived
|
|
# sideload export. Verify every exported app/runtime, including GL extensions.
|
|
python - <<'PY'
|
|
from pathlib import Path
|
|
import re
|
|
import subprocess
|
|
source = Path('/var/lib/flatpak/repo')
|
|
export = Path('/seed/export/.ostree/repo')
|
|
refs = export / 'refs/mirrors/org.flathub.Stable'
|
|
count = 0
|
|
for kind in ('app', 'runtime'):
|
|
for ref in sorted((refs / kind).rglob('*')):
|
|
if not ref.is_file():
|
|
continue
|
|
commit = ref.read_text().strip()
|
|
assert re.fullmatch('[0-9a-f]{64}', commit), ref
|
|
subprocess.run(['ostree', '--repo=' + str(source), 'show',
|
|
'--gpg-verify-remote=flathub', commit], check=True,
|
|
stdout=subprocess.DEVNULL)
|
|
meta = Path('objects') / commit[:2] / (commit[2:] + '.commitmeta')
|
|
assert (source / meta).read_bytes() == (export / meta).read_bytes(), ref
|
|
count += 1
|
|
assert count > 0, 'No signed app/runtime refs exported'
|
|
print(f'Verified detached Flathub signatures for {count} exported refs')
|
|
PY
|
|
cp /seed/flathub.flatpakrepo /seed/export/
|
|
cp /policy.json /seed/export/policy.json
|
|
cp /seed/evidence/refs.tsv /seed/export/refs.tsv
|
|
cp /seed/evidence/manifest.tsv /seed/export/manifest.tsv
|
|
(cd /seed/export; find . -type f ! -name SHA256SUMS -print0 | sort -z | xargs -0 sha256sum > SHA256SUMS)
|
|
date --utc --iso-8601=seconds > /seed/evidence/export-complete
|
|
echo 'SEED_EXPORT_COMPLETE: installed-VM delivery acceptance still required'
|