Files
w3cjandClaude Opus 5.5 9f2dda910d feat(assets): base's build fetches only the package lists apt installs from
apt.sh begin's update fetched 35.8 MB of lists on arm64, of which the
translations (Translation-en, 7.9 MB), AppStream's components (4.8 MB)
and command-not-found's metadata are nothing apt installs from, and the
image keeps no lists. The build's apt settings now leave them out
(Acquire::Languages "none", and DefaultEnabled "false" for the DEP-11
and CNF index targets): 22.8 MB, and apt.sh begin 3.7 s against 6.4 s on
the mini (measured). The record still gives back every InRelease and
Packages list it used. A layer's apt.sh lists runs after apt.sh end has
removed these settings, so its apt is Ubuntu's as it is.

Base's and every layer's key change (apt.sh).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-29 22:57:15 -07:00

139 lines
6.4 KiB
Bash

#!/bin/bash
# apt.sh begin|end, as root: base's packages, around base.sh and its features; apt.sh lists, before
# a layer's script, fetches package lists base keeps none of. What the build gives
# it in /var/cache/devvm/packages/in says how they come:
# Packages and *.deb replay: the packages this image's key recorded at its first build, from
# devvm's cache on the host, and nothing else (no network)
# mirrors record: from Ubuntu's archive, the mirrors listed tried in order, apt
# checking their signatures with Ubuntu's archive key; at the end, each
# package downloaded goes to out/ with what apt's lists say of it, and so do
# the InRelease files and package lists apt used, which devvm checks again
# neither a --from image's own sources, as they are
# Both end with the image's own sources, no package lists and an empty apt cache, so a first build
# and a replay make the same image. A source that doesn't answer exits 75 at once: no retries. Once
# a source has answered, the packages' downloads are tried again after a failure, three times.
set -euo pipefail
dir=/var/cache/devvm/packages archives=/var/cache/apt/archives lists=/var/lib/apt/lists
command -v apt-get >/dev/null || exit 0 # Alpine (--from alpine): apk, not pinned
export DEBIAN_FRONTEND=noninteractive
aside=/etc/apt/devvm-sources
# build_conf RETRIES: the build's apt settings, with RETRIES tries again of a failed download, each
# after a longer wait (apt's own backoff). Package lists kept as they're signed, uncompressed, for
# record to give back. Only the lists apt installs from: no translations, AppStream components or
# command-not-found metadata, a third of what an update fetches, which the image keeps none of.
build_conf() {
printf 'Acquire::Retries "%s";\nAcquire::http::Timeout "30";\nAcquire::https::Timeout "30";\nAcquire::GzipIndexes "false";\n' \
"$1" > /etc/apt/apt.conf.d/50devvm-build
printf 'Acquire::Languages "none";\nAcquire::IndexTargets::deb::DEP-11::DefaultEnabled "false";\nAcquire::IndexTargets::deb::CNF::DefaultEnabled "false";\n' \
>> /etc/apt/apt.conf.d/50devvm-build
}
begin() {
build_conf 0
if [[ -f $dir/in/Packages ]]; then
# The host checked each file against the SHA-256 recorded for it, and Packages lists those.
sources_aside
echo "deb [trusted=yes] file:$dir/in ./" > /etc/apt/sources.list.d/devvm-replay.list
apt-get update
return
elif [[ -f $dir/in/mirrors ]]; then
# Phased updates too: devvm refuses a package older than a list it gives back has.
printf 'APT::Keep-Downloaded-Packages "true";\nBinary::apt::APT::Keep-Downloaded-Packages "true";\nAPT::Get::Always-Include-Phased-Updates "true";\n' \
> /etc/apt/apt.conf.d/50devvm-keep
apt-get clean
# Ubuntu's sources (deb822, as 26.04's are) with each archive's URI the mirrors listed, then
# that archive. A one-line sources.list, which 26.04's holds only comments in, stays aside.
sources_aside
local f
for f in "$aside"/sources.list.d/*.sources; do
[[ -e $f ]] || continue
mirrored "$f" "/etc/apt/sources.list.d/${f##*/}"
done
fi
if ! apt-get update --error-on=any; then
echo "devvm: no package source answered (above)" >&2
exit 75
fi
build_conf 3
}
# mirrored FROM TO writes FROM, a deb822 sources file, to TO with each archive's URI the list
# $dir/mirrors-<n> of in/mirrors' mirror, then that archive.
mirrored() {
local u n
cp "$1" "$2"
while read -r u; do
n=$(( $(find "$dir" -maxdepth 1 -name 'mirrors-*' | wc -l) + 1 ))
{ cat "$dir/in/mirrors"; echo "$u"; } > "$dir/mirrors-$n"
sed -i.bak "s|^URIs: *$u *\$|URIs: mirror+file:$dir/mirrors-$n|" "$2" && rm -f "$2.bak"
done < <(sed -n 's/^URIs: *\([^ ]*\) *$/\1/p' "$1" | sort -u)
}
# sources_aside moves the image's sources out of apt's way, kept to put back.
sources_aside() {
mkdir -p "$aside/sources.list.d"
[[ ! -f /etc/apt/sources.list ]] || mv /etc/apt/sources.list "$aside/"
find /etc/apt/sources.list.d -mindepth 1 -maxdepth 1 -exec mv {} "$aside/sources.list.d/" \;
}
# record moves each package the build downloaded to out/<sha256>.deb, and its entry in apt's lists,
# which apt checked against the archive's signature, to out/Packages. A package apt's lists don't
# have with that SHA-256 fails the build. Each InRelease and package list apt used goes to
# out/<n>.InRelease or out/<n>.Packages, so the host checks them with the archive's key itself.
record() {
local deb pkg ver arch sum entry n=0 f
mkdir -p "$dir/out"
: > "$dir/out/Packages"
for f in "$lists"/*_InRelease "$lists"/*_Packages; do
[[ -e $f ]] || continue
n=$((n + 1))
cp "$f" "$dir/out/$n.${f##*_}"
done
for deb in "$archives"/*.deb; do
[[ -e $deb ]] || continue
pkg=$(dpkg-deb -f "$deb" Package) ver=$(dpkg-deb -f "$deb" Version) arch=$(dpkg-deb -f "$deb" Architecture)
sum=$(sha256sum "$deb" | cut -d' ' -f1)
# By name and version alone ("all" isn't an architecture apt names packages by): each
# architecture's entry has its own SHA-256, which picks the one.
entry=$(apt-cache show "$pkg=$ver" 2>/dev/null |
awk -v s="$sum" 'BEGIN { RS = ""; ORS = "\n\n" } $0 ~ "(^|\n)SHA256: " s "(\n|$)" { print; exit }')
if [[ -z $entry ]]; then
echo "devvm: apt's lists have no $pkg $ver ($arch) with the SHA-256 of $deb" >&2
exit 1
fi
printf '%s\n\n' "$entry" >> "$dir/out/Packages"
mv "$deb" "$dir/out/$sum.deb"
done
}
end() {
[[ ! -f $dir/in/mirrors ]] || record
if [[ -d $aside ]]; then
rm -rf /etc/apt/sources.list.d
mkdir -p /etc/apt/sources.list.d
[[ ! -f $aside/sources.list ]] || mv "$aside/sources.list" /etc/apt/
find "$aside/sources.list.d" -mindepth 1 -maxdepth 1 -exec mv {} /etc/apt/sources.list.d/ \;
rm -rf "$aside"
fi
rm -f /etc/apt/apt.conf.d/50devvm-build /etc/apt/apt.conf.d/50devvm-keep
apt-get clean
rm -rf "${lists:?}"/*
}
# lists fetches the package lists from the image's own sources when it has none, as base's image
# doesn't: a layer's script then installs as on any Ubuntu, from the archive as it is.
lists() {
if ! compgen -G "$lists/*_Packages" >/dev/null &&! apt-get update --error-on=any; then
echo "devvm: no package source answered (above)" >&2
exit 75
fi
}
case ${1:-} in
begin) begin ;;
end) end ;;
lists) lists ;;
*) echo "usage: apt.sh begin|end|lists" >&2; exit 2 ;;
esac