Record image transfer and installed size in the published commit (D-113)

The builder measures the composed tree with image-size.py through a dangling commit, then records archipelago.image-size and archipelago.image-installed-size on the published branch commit. Clients receive both with commit metadata only.
This commit is contained in:
Pavel Isaev 2026-11-14 17:15:49 +03:00
parent 666cb76403
commit 853a88deb2
5 changed files with 106 additions and 3 deletions

View file

@ -76,6 +76,10 @@ if [[ -n ${ARCHIPELAGO_PLASMA_SETUP_PACKAGE:-} ]]; then
cp "$ARCHIPELAGO_PLASMA_SETUP_PACKAGE" "$work/plasma-setup.pkg.tar.zst"
sha256sum "$work/plasma-setup.pkg.tar.zst" > "$work/plasma-setup-input.sha256"
fi
cat > "$work/image-size.py" <<'ARCHIPELAGO_IMAGE_SIZE'
@@IMAGE_SIZE@@
ARCHIPELAGO_IMAGE_SIZE
chmod 0755 "$work/image-size.py"
grep -q '^DisableSandbox' "$toolroot/etc/pacman.conf" || sed -i '/\[options\]/a DisableSandbox\nNoProgressBar' "$toolroot/etc/pacman.conf"
install -m755 "$binary" "$work/archipelago"
printf '%s\n' "$profile" > "$work/build-profile"
@ -260,7 +264,13 @@ printf '%s\n' "$snapshot" > /job/layer/metadata/snapshot
ostree --repo=/job/repo init --mode=archive
layercommit=$(ostree --repo=/job/repo commit --fsync=0 -b layers/core --tree=dir=/job/layer --add-metadata-string=archipelago.snapshot="$snapshot")
ostree --repo=/job/repo checkout --force-copy --subpath=payload "$layercommit" /job/composed
ostree --repo=/job/repo commit --fsync=0 -b core --tree=dir=/job/composed --add-metadata-string=archipelago.snapshot="$snapshot" --add-metadata-string=archipelago.layer-core="$layercommit"
# Clients only receive commit metadata before they decide to update. Measure the
# composed tree once, then record both sizes in the published commit so the
# software centers can show them without downloading the image.
image=$(ostree --repo=/job/repo commit --fsync=0 --orphan --tree=dir=/job/composed)
read -r image_transfer image_installed < <(python3 /job/image-size.py /job/repo "$image")
[[ $image_transfer =~ ^[0-9]+$ && $image_installed =~ ^[0-9]+$ ]]
ostree --repo=/job/repo commit --fsync=0 -b core --tree=dir=/job/composed --add-metadata-string=archipelago.snapshot="$snapshot" --add-metadata-string=archipelago.layer-core="$layercommit" --add-metadata-string=archipelago.image-size="$image_transfer" --add-metadata-string=archipelago.image-installed-size="$image_installed"
mkdir -p /job/overlay/payload/usr/share/archipelago /job/overlay/metadata
printf 'Archipelago managed overlay test\n' > /job/overlay/payload/usr/share/archipelago/test-marker
printf '%s\n' "$snapshot" > /job/overlay/metadata/snapshot

View file

@ -163,10 +163,18 @@ find /job/core/usr/lib/archipelago/package-db/local -name install -exec sha256su
test ! -e /job/core/usr/etc
mv /job/core/etc /job/core/usr/etc
mv /job/core /job/composed
# Clients only receive commit metadata before they decide to update. Measure the
# composed tree once, then record both sizes in the published commit so the
# software centers can show them without downloading the image.
image=$(ostree --repo=/job/repo commit --fsync=0 --orphan --tree=dir=/job/composed)
read -r image_transfer image_installed < <(python3 /job/image-size.py /job/repo "$image")
[[ $image_transfer =~ ^[0-9]+$ && $image_installed =~ ^[0-9]+$ ]]
ostree --repo=/job/repo commit --fsync=0 -b core --tree=dir=/job/composed \
--add-metadata-string=archipelago.snapshot="$snapshot" \
--add-metadata-string=archipelago.layer-core="$core_layer" \
--add-metadata-string=archipelago.layer-purpose="$purpose_layer"
--add-metadata-string=archipelago.layer-purpose="$purpose_layer" \
--add-metadata-string=archipelago.image-size="$image_transfer" \
--add-metadata-string=archipelago.image-installed-size="$image_installed"
ostree --repo=/job/repo pull-local /core-input/repo test-marker
test "$(ostree --repo=/core-input/repo rev-parse "$core_layer")" = "$core_layer"
printf 'Core reused: %s\nPurpose: %s\n' "$core_layer" "$purpose_layer" > /job/evidence/composition.txt

View file

@ -162,10 +162,18 @@ find /job/core/usr/lib/archipelago/package-db/local -name install -exec sha256su
test ! -e /job/core/usr/etc
mv /job/core/etc /job/core/usr/etc
mv /job/core /job/composed
# Clients only receive commit metadata before they decide to update. Measure the
# composed tree once, then record both sizes in the published commit so the
# software centers can show them without downloading the image.
image=$(ostree --repo=/job/repo commit --fsync=0 --orphan --tree=dir=/job/composed)
read -r image_transfer image_installed < <(python3 /job/image-size.py /job/repo "$image")
[[ $image_transfer =~ ^[0-9]+$ && $image_installed =~ ^[0-9]+$ ]]
ostree --repo=/job/repo commit --fsync=0 -b core --tree=dir=/job/composed \
--add-metadata-string=archipelago.snapshot="$snapshot" \
--add-metadata-string=archipelago.layer-core="$core_layer" \
--add-metadata-string=archipelago.layer-purpose="$purpose_layer"
--add-metadata-string=archipelago.layer-purpose="$purpose_layer" \
--add-metadata-string=archipelago.image-size="$image_transfer" \
--add-metadata-string=archipelago.image-installed-size="$image_installed"
ostree --repo=/job/repo pull-local /core-input/repo test-marker
test "$(ostree --repo=/core-input/repo rev-parse "$core_layer")" = "$core_layer"
printf 'Core reused: %s\nPurpose: %s\n' "$core_layer" "$purpose_layer" > /job/evidence/composition.txt

57
adapters/image-size.py Normal file
View file

@ -0,0 +1,57 @@
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-2.0-or-later
"""Report the transfer and installed size of an OSTree commit tree.
The image builder commits the composed tree twice: a dangling commit is measured
here, then the published commit records the result as commit metadata. Clients
get exact numbers from authenticated commit metadata, without downloading the
image or trusting a separate size file.
The transfer size counts every unique file object once (that is what a pull can
retrieve), so hardlinked and repeated payload does not inflate it. The installed
size sums every path of the checkout.
"""
import os
import subprocess
import sys
def object_size(objects, checksum, kinds):
for kind in kinds:
try:
return os.path.getsize(os.path.join(objects, checksum[:2], checksum[2:] + "." + kind))
except OSError:
continue
return 0
def main(repo, commit):
listing = subprocess.run(
["ostree", "--repo=" + repo, "ls", "-R", "-C", commit],
check=True, capture_output=True, text=True).stdout
objects = os.path.join(repo, "objects")
files, directories = set(), set()
transfer = 0
installed = 0
for line in listing.splitlines():
# mode uid gid size checksum path; the path may contain spaces.
fields = line.split(None, 5)
if len(fields) < 6:
raise SystemExit("Unexpected ostree ls record: " + line)
mode, size, checksum = fields[0], int(fields[3]), fields[4]
if mode.startswith("d"):
if checksum not in directories:
directories.add(checksum)
transfer += object_size(objects, checksum, ("dirtree",))
continue
installed += size
if checksum not in files:
files.add(checksum)
transfer += object_size(objects, checksum, ("filez", "file"))
print(transfer, installed)
if __name__ == "__main__":
if len(sys.argv) != 3:
raise SystemExit("usage: image-size.py REPO COMMIT")
main(sys.argv[1], sys.argv[2])

View file

@ -37,6 +37,7 @@ fn render_iso(kde: bool) -> String {
.replace("@@CONTAINER_RUNNER_LAYER_BUILD@@", ": # Container Runner layer is not used here")
.replace(if kde { "@@KDE_LAYER_BUILD@@" } else { "@@GNOME_LAYER_BUILD@@" }, &layer)
.replace("@@LAYER_DELTA@@", include_str!("../../composer/layer_delta.py"))
.replace("@@IMAGE_SIZE@@", include_str!("../adapters/image-size.py"))
.replace("@@UPDATE_PROFILE@@", include_str!("../defaults/update.json"))
.replace("@@UPDATE_PUBLIC_KEY@@", include_str!("../../system/archipelago-stable.asc"))
.replace("@@FLATPAK_IMPORT@@", include_str!("../../installer/adapters/flatpak-import.sh"))
@ -72,6 +73,7 @@ pub fn build_container_runner_script() -> Result<String, String> {
.replace("@@GNOME_LAYER_BUILD@@", ": # Container Runner does not use the GNOME layer")
.replace("@@GNOME_FINALIZE@@", ": # Container Runner finalizes inside its layer")
.replace("@@LAYER_DELTA@@", include_str!("../../composer/layer_delta.py"))
.replace("@@IMAGE_SIZE@@", include_str!("../adapters/image-size.py"))
.replace("@@UPDATE_PROFILE@@", include_str!("../defaults/update.json"))
.replace("@@UPDATE_PUBLIC_KEY@@", include_str!("../../system/archipelago-stable.asc"))
.replace("@@FLATPAK_IMPORT@@", "# Container Runner does not import a Flatpak seed")
@ -111,6 +113,24 @@ mod tests {
}
}
/// Every image path must record both sizes in the published commit, because
/// clients only receive commit metadata before they choose to update.
#[test]
fn every_image_commit_records_transfer_and_installed_size() {
for script in [build_iso_script(), build_container_runner_script().unwrap(),
build_kde_probe_script().unwrap()] {
assert!(script.contains("@@IMAGE_SIZE@@") == false);
assert!(script.contains("archipelago.image-size="));
assert!(script.contains("archipelago.image-installed-size="));
assert!(script.contains("python3 /job/image-size.py"));
}
for layer in [include_str!("../adapters/gnome-layer.sh"),
include_str!("../adapters/container-runner-layer.sh")] {
assert!(layer.contains("archipelago.image-size="));
assert!(layer.contains("archipelago.image-installed-size="));
}
}
/// D-109: only the desktop finalizers configure kmscon, because only GNOME/KDE
/// ship mesa. A silent removal here left published desktops on getty for every
/// logind VT, so the invariant is asserted against the adapters directly.