system/adapters/host.sh

108 lines
4.8 KiB
Bash

[[ $EUID == 0 ]] || { echo 'Run as root' >&2; exit 1; }
if [[ $1 == boot-sync ]]; then
set -euo pipefail
[[ ${SERVICE_RESULT:-success} == success ]] || { echo 'OSTree finalization failed; ESP unchanged' >&2; exit 1; }
[[ -e /run/ostree-booted ]]
mountpoint -q /efi || { echo '/efi is not mounted; ESP unchanged' >&2; exit 1; }
# The two-partition layout bind-mounts /boot from the root filesystem, so a
# mountpoint alone must NOT skip mirroring. Skip only when /boot is a truly
# separate filesystem (XBOOTLDR) that OSTree writes directly.
if mountpoint -q /boot && [[ "$(stat -c %d /boot)" != "$(stat -c %d /)" ]]; then exit 0; fi
src=/sysroot/boot/ostree
dst=/efi/ostree
esrc=/sysroot/boot/loader/entries
edst=/efi/loader/entries
mkdir -p "$dst" "$edst"
# Never retain artifacts of a deployment OSTree has already pruned.
for d in "$dst"/archipelago-*; do
[[ -d $d ]] || continue
b=${d##*/}
[[ -d $src/$b ]] || { echo "prune stale ESP copy $b"; rm -rf "$d"; }
done
for e in "$edst"/*.conf; do
[[ -f $e ]] || continue
b=${e##*/}
[[ -f $esrc/$b ]] || { echo "drop stale loader entry $b"; rm -f "$e"; }
done
# Check space before touching valid artifacts; publish entries only afterwards.
need=0
incomplete=()
for d in "$src"/archipelago-*; do
[[ -d $d ]] || continue
b=${d##*/}
bad=0
if [[ -d $dst/$b ]]; then
while IFS= read -r -d '' f; do
rel=${f#"$d"/}
if [[ ! -f $dst/$b/$rel || $(stat -c %s "$f") != $(stat -c %s "$dst/$b/$rel") ]]; then bad=1; break; fi
done < <(find "$d" -type f -print0)
else
bad=1
fi
if (( bad )); then
incomplete+=("$b")
need=$((need + $(du -sb "$d" | cut -f1)))
fi
done
avail=$(df --output=avail -B1 /efi | tail -1)
if (( need > avail )); then
echo "refusing to publish boot entries: need $need bytes, ESP has $avail" >&2
exit 1
fi
for b in "${incomplete[@]}"; do
d=$src/$b
rm -rf "$dst/.$b.tmp"
cp -a "$d" "$dst/.$b.tmp"
for f in "$d"/*; do
[[ -f $f ]] || continue
cmp -s "$f" "$dst/.$b.tmp/${f##*/}" || { echo "copy verification failed for $b/${f##*/}" >&2; rm -rf "$dst/.$b.tmp"; exit 1; }
done
rm -rf "$dst/$b"
mv "$dst/.$b.tmp" "$dst/$b"
echo "copied boot artifacts $b"
done
for e in "$esrc"/*.conf; do
[[ -f $e ]] || continue
linux=$(awk '$1=="linux"{print $2}' "$e")
initrd=$(awk '$1=="initrd"{print $2}' "$e")
[[ -f /efi$linux && -f /efi$initrd ]] || { echo "missing kernel/initrd for ${e##*/}; entries not updated" >&2; exit 1; }
done
for e in "$esrc"/*.conf; do [[ -f $e ]] || continue; cp -f "$e" "$edst/"; done
sync -f /efi
echo 'boot artifacts synchronized'
exit 0
fi
[[ -e /run/ostree-booted ]] || { echo 'Not an installed OSTree deployment' >&2; exit 1; }
if [[ $1 == status ]]; then
ostree admin status
printf 'Experimental Core; public release gates not yet satisfied\n'
if [[ -f /usr/lib/archipelago/overlays ]]; then cat /usr/lib/archipelago/overlays; fi
exit 0
fi
[[ -f /etc/archipelago/provisioned ]] || { echo 'Complete firstboot before preparing deployments' >&2; exit 1; }
id "$(cat /etc/archipelago/provisioned)" >/dev/null
exec 9>/run/archipelago-compose.lock
flock -n 9
repo=/sysroot/ostree/repo
[[ ! -e /run/ostree/staged-deployment ]] || { echo 'Activate the pending deployment first'; exit 1; }
booted=$(ostree admin status | sed -n 's/^\* *archipelago \([0-9a-f]*\)\..*/\1/p')
[[ $booted =~ ^[a-f0-9]{64}$ ]]
work=$(mktemp -d /var/tmp/archipelago-compose.XXXXXX)
trap 'rm -rf "$work"' EXIT
ostree --repo="$repo" checkout --force-copy "$booted" "$work/root"
if [[ $1 == overlay ]]; then
[[ ! -f /usr/lib/archipelago/host-extensions ]] || { echo 'Apply managed overlay before Host Extensions in this prototype'; exit 1; }
ostree --repo="$repo" checkout --force-copy test-marker "$work/layer"
cmp "$work/layer/metadata/snapshot" "$work/root/usr/lib/archipelago/snapshot"
target=usr/share/archipelago/test-marker
[[ ! -e $work/root/$target && ! -L $work/root/$target ]]
[[ ! -L $work/root/usr/share/archipelago ]]
install -m644 "$work/layer/payload/$target" "$work/root/$target"
printf 'test-marker\n' > "$work/root/usr/lib/archipelago/overlays"
fi
ref="local/$(date +%s)-$$"
commit=$(ostree --repo="$repo" commit -b "$ref" --tree="dir=$work/root" \
--add-metadata-string="archipelago.base=$booted" \
--add-metadata-string="archipelago.snapshot=$(cat "$work/root/usr/lib/archipelago/snapshot")")
ostree admin deploy --stage --retain --os=archipelago "$commit"
echo "Prepared $commit; activate with an explicit reboot. Current /usr unchanged."