From eb18216362636d905e951a52b1a03cd99629323e Mon Sep 17 00:00:00 2001 From: Gr-linkk <280405187+Gr-linkk@users.noreply.github.com> Date: Fri, 30 Oct 2026 12:48:04 -0700 Subject: [PATCH] tools: verify signed root images and isolate rootfs builds A signed bundle verified its boot images but trusted the adjacent root image's unsigned checksum. Bind the selected root image's name and bytes to the signed MANIFEST, and refuse an external boot-image override in bundle mode. Preserve the explicit unsigned development flow. Include the device in rootfs output names, use private temporary build directories and lock each device/version. Check the staged image before replacing a previous build. Installer and bundle selection use the root manifest's device, including images with older filenames. Verified with real GPG signatures and modified ext4 fixtures in tests/install-phone, concurrent/failing container stand-ins in tests/build-rootfs, and tests/all. No physical flash or ARM image build. Assisted-by: Codex --- CONTRIBUTING.md | 2 +- docs/RELEASES.md | 12 +++-- tests/all | 2 +- tests/build-rootfs | 109 ++++++++++++++++++++++++++++++++++++++++++++ tests/install-phone | 54 +++++++++++++++++++++- tests/rootfs-smoke | 2 +- tools/build-rootfs | 37 +++++++++------ tools/install-phone | 50 +++++++++++++++----- tools/make-bundle | 21 +++++++-- 9 files changed, 252 insertions(+), 37 deletions(-) create mode 100755 tests/build-rootfs diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index a81f474a0..026236926 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -127,7 +127,7 @@ bin/omamobile images # boot, init_boot, vendor_boot, vendor_kernel_boo # A first install: packages, root image, then the installer (phone in fastboot) tools/build-packages # [omamobile] for aarch64 into out/repo, signed -tools/build-rootfs # out/omamobile-rootfs--aarch64.img.zst +tools/build-rootfs # out/omamobile-rootfs---aarch64.img.zst tools/install-phone --dry-run # checks, questions, the exact commands; then without --dry-run # after the first boot: a read-only backup of the modem's partitions into DIR (REINSTALL-RUNBOOK 4a) tools/phone-provision --nv-backup DIR FACTORY.zip # firmware and per-phone data diff --git a/docs/RELEASES.md b/docs/RELEASES.md index 9b9d3a98b..657fa9871 100644 --- a/docs/RELEASES.md +++ b/docs/RELEASES.md @@ -232,7 +232,7 @@ For a first install, or to recover a phone that won't boot. One bundle per devic | Piece | Where | Notes | | --- | --- | --- | | aarch64 packages without a phone | `tools/build-packages` | makepkg in an Arch Linux ARM container (the official tarball, MD5 and signature checked against the pinned build key, `tools/lib/alarm.sh`) under the laptop's qemu-user; signs with `keys/` into `out/repo/aarch64`. Mesa, Hyprland and libcamera take hours this way, so they're built only when named, once per version | -| The shared root image | `tools/build-rootfs`, `rootfs/` | pacstrap in that container: `rootfs/packages.base`, then Omarchy's own `install/omarchy-base.packages` (minus `rootfs/packages.omarchy-skip` and what isn't built for aarch64), then `rootfs/packages.omamobile`, the device package and the board's `ROOTFS_PACKAGES`, the `[omamobile]` repository itself, the kernel modules of the current build, and `patches/omarchy`. Output `out/omamobile-rootfs--aarch64.img.zst`, `.sha256` and a `MANIFEST` with every package version (also inside, `/usr/share/omamobile/rootfs/MANIFEST`) | +| The device's root image | `tools/build-rootfs`, `rootfs/` | pacstrap in that container: `rootfs/packages.base`, then Omarchy's own `install/omarchy-base.packages` (minus `rootfs/packages.omarchy-skip` and what isn't built for aarch64), then `rootfs/packages.omamobile`, the device package and the board's `ROOTFS_PACKAGES`, the `[omamobile]` repository itself, the kernel modules of the current build, and `patches/omarchy`. Output `out/omamobile-rootfs---aarch64.img.zst`, `.sha256` and a `MANIFEST` with every package version (also inside, `/usr/share/omamobile/rootfs/MANIFEST`) | | First boot | `packages/omamobile-firstboot` | reads `/etc/omamobile/firstboot.conf` (data, never sourced), creates the user, SSH keys, ufw with SSH on `usb0`, runs Omarchy's own setup leaves (skipping four that don't fit a phone), SDDM autologin and the lock, Omarchy's and our user setup, then deletes the file | | Growing the root | `initramfs/init` (`grow_root`) | `resize2fs` before mounting, only for a file system labelled `omamobile-root` smaller than its partition; `omamobile.noresize` skips it | | The installer | `tools/install-phone` (the bundle's `install`) | as described below, steps 1, 3-5; the factory-image step (2) is `tools/phone-provision` after the first boot instead | @@ -241,6 +241,8 @@ For a first install, or to recover a phone that won't boot. One bundle per devic Decisions taken in building it: the device packages are **installed** in the image, not only cached (one device so far; a first boot without a package step has one less way to fail; §12 decision 6 stays open for the second device); the image's `[omarchy]` is Omarchy's **stable** channel (from 2026-10-20; before that edge was the only one with aarch64 builds of `omarchy`, `omarchy-settings` and `quickshell`, and edge then moved to 4.5.0rc1, which the phone's patches don't apply to); the image is per device (its `MANIFEST` names the device, and the installer refuses a mismatch). +Root image filenames include the device so two devices can share a version without overwriting each other. Builds use separate temporary directories and refuse a concurrent build of the same device/version in the same output directory. Compression and smoke checks finish before replacing the published files. `--local NAME` still uses `out/NAME/repo` and writes images under `out/NAME/`; use `--rootfs` to select one there. Independent package snapshots also need separate `ALARM_CACHE` directories (§10). Repository-mode installation and bundling select only images whose root `MANIFEST` matches the device, including older filenames. + The design: ``` @@ -251,11 +253,13 @@ omamobile--/ images/boot.img init_boot.img vendor_boot.img vendor_kernel_boot.img dtbo.img MANIFEST kernel release, package versions, both repositories' commits SHA256SUMS SHA256SUMS.sig checksums of everything above, signed -omamobile-rootfs--aarch64.img.zst shared by every device, downloaded once - .sha256 .sig +omamobile-rootfs---aarch64.img.zst + .sha256 .MANIFEST ``` -**The shared root image** is an ext4 Arch Linux ARM root with Omarchy, the `[omamobile]` repository configured and its keyring, `linux-omamobile`, `omamobile-shell` and every device package in the package cache but **none installed**. It is built by `pacstrap` in an aarch64 container. It contains no Google file. Its size is a few GB, flashed sparse; the first boot grows it to the whole `userdata` partition (`resize2fs` in the initramfs, before mounting). +**The device's root image** is an ext4 Arch Linux ARM root with Omarchy, the `[omamobile]` repository configured and its keyring, `omamobile-shell`, the device package installed and the matching kernel modules. It is built by `pacstrap` in an aarch64 container. It contains no Google file. Its size is a few GB, flashed sparse; the first boot grows it to the whole `userdata` partition (`resize2fs` in the initramfs, before mounting). + +The bundle's signed `SHA256SUMS` covers its `MANIFEST`, which names the exact root image and its SHA256 digest. The installer checks the selected image's name and bytes against that signed record before contacting the phone; changing the separate `.sha256` cannot authorize a replacement. Bundles use their own checked boot images; `--images` overrides are for repository mode. `--unsigned` remains available for development bundles, which use the root image's `.sha256` check. **`install`** (`tools/install-phone`), run on the laptop with the phone in fastboot: diff --git a/tests/all b/tests/all index 53ae48b40..23953e8ff 100755 --- a/tests/all +++ b/tests/all @@ -23,7 +23,7 @@ sh -n device/flame/initramfs/init sh -n device/flame/initramfs/build bash -n tools/build-flame-probe echo "ok - scripts parse" -for t in tests/pr-check tests/kernel-pr-check tests/alarm-wake-test tests/install-phone tests/phone-provision tests/initramfs-grow tests/images-layout tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/boot-method tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds; do +for t in tests/pr-check tests/kernel-pr-check tests/alarm-wake-test tests/install-phone tests/build-rootfs tests/phone-provision tests/initramfs-grow tests/images-layout tests/device-overlays tests/simplefb-trigger tests/initramfs-steps tests/storage-guard tests/usb-product tests/boot-method tests/flame-probe packages/omamobile-firstboot/test/all packages/omamobile-device-felix/test/all packages/omamobile-device-lynx/test/all packages/omamobile-device-panther/test/all tests/local-builds; do echo "# $t" "$t" done diff --git a/tests/build-rootfs b/tests/build-rootfs new file mode 100755 index 000000000..d9decc0c5 --- /dev/null +++ b/tests/build-rootfs @@ -0,0 +1,109 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# Run the real builder with a small container stand-in: device namespaces, +# concurrent builds and failed-build cleanup, without an ARM image build. +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +for tool in zstd flock; do command -v "$tool" >/dev/null || { echo "skip: no $tool"; exit 0; }; done +fail() { echo "not ok - $*" >&2; exit 1; } +t=$(mktemp -d) +pid="" +cleanup() { touch "$t/release"; [[ -z $pid ]] || wait "$pid" 2>/dev/null || true; rm -rf "$t"; } +trap cleanup EXIT +R=$t/repo +mkdir -p "$R/tools/lib" "$R/tests" "$R/out/repo/aarch64" "$t/bin" "$t/kernel/include/config" +cp tools/build-rootfs tools/make-bundle tools/install-phone "$R/tools/" +touch "$R/out/repo/aarch64/omamobile.db" "$t/kernel/modules-fixture.tar.zst" "$t/alarm.tar" +echo fixture > "$t/kernel/include/config/kernel.release" +for device in first second; do + mkdir -p "$R/device/$device" + printf 'PACKAGE=omamobile-device-%s\nCODENAME=%s\nNAME="%s"\nBOOT_IMAGES="boot"\n' "$device" "$device" "$device" > "$R/device/$device/device.conf" +done +cat > "$R/tools/lib/alarm.sh" <<'EOF' +alarm_die() { echo "$*" >&2; exit 1; } +alarm_say() { echo "$*"; } +alarm_fetch() { :; } +alarm_build_image() { echo fixture; } +EOF +cat > "$t/bin/git" <<'EOF' +#!/bin/sh +if [ "$1" = rev-parse ]; then echo fixture; fi +EOF +cat > "$t/bin/docker" <<'EOF' +#!/bin/bash +set -euo pipefail +[[ $1 == run ]] || exit 0 +work="" device="" +while (($#)); do + case $1 in + -v) [[ $2 != *:/out ]] || work=${2%:/out}; shift ;; + -e) [[ $2 != DEVICE=* ]] || device=${2#DEVICE=}; shift ;; + esac + shift +done +[[ -n $work && -n $device ]] +if [[ ${BLOCK_BUILD:-0} == 1 ]]; then + touch "$T/ready" + for ((i=0; i<100; i++)); do + [[ ! -e $T/release ]] || break + sleep 0.1 + done + [[ -e $T/release ]] +fi +[[ ${FAIL_BUILD:-0} != 1 ]] || exit 1 +printf '%s\n' "$device" > "$work/rootfs.img" +printf 'device: %s\n' "$device" > "$work/MANIFEST" +EOF +cat > "$R/tests/rootfs-smoke" <<'EOF' +#!/bin/bash +set -euo pipefail +[[ ${FAIL_SMOKE:-0} != 1 ]] || exit 1 +zstd -qt "$1" +[[ -s ${1%.img.zst}.MANIFEST ]] +EOF +chmod +x "$t/bin/"* "$R/tests/rootfs-smoke" +export T=$t PATH="$t/bin:$PATH" KERNEL_OUT=$t/kernel ALARM_CACHE=$t/cache ALARM_TARBALL=$t/alarm.tar OMARCHY_CHANNEL=stable +build() { DEVICE=$1 "$R/tools/build-rootfs" --version fixture "${@:2}"; } +BLOCK_BUILD=1 build first > "$t/first.log" 2>&1 & pid=$! +for ((i=0; i<100; i++)); do [[ ! -e $t/ready ]] || break; sleep 0.05; done +[[ -e $t/ready ]] || { cat "$t/first.log"; fail "first build reached the container"; } +if build first > "$t/duplicate.log" 2>&1; then fail "concurrent builds of one artifact are refused"; fi +grep -q 'already building' "$t/duplicate.log" || { cat "$t/duplicate.log"; fail "names the conflicting build"; } +build second > "$t/second.log" 2>&1 || { cat "$t/second.log"; fail "another device can build concurrently"; } +touch "$t/release" +wait "$pid" || { cat "$t/first.log"; fail "first build completes"; }; pid="" +for device in first second; do + image=$R/out/omamobile-rootfs-$device-fixture-aarch64.img.zst + [[ $(zstd -qdc "$image") == "$device" ]] || fail "distinct $device image" + (cd "$R/out" && sha256sum -c "${image##*/}.sha256") >/dev/null +done +echo 'ok - different devices build concurrently; duplicate device/version is locked' + +before=$(sha256sum "$R/out/omamobile-rootfs-first-fixture-aarch64.img.zst") +if FAIL_BUILD=1 build first > "$t/failed.log" 2>&1; then fail "container failure propagates"; fi +if FAIL_SMOKE=1 build first > "$t/failed.log" 2>&1; then fail "smoke failure propagates"; fi +[[ $(sha256sum "$R/out/omamobile-rootfs-first-fixture-aarch64.img.zst") == "$before" ]] || fail "failed rebuild preserves published image" +[[ -z $(find "$R/out" -maxdepth 1 -type d -name '.omamobile-rootfs-*') ]] || fail "temporary build directories are removed" +echo 'ok - failed rebuild preserves the previous image and removes only its own work' + +mkdir -p "$R/keys/first" "$R/out/first/repo/aarch64" +touch "$R/keys/first/public.asc" "$R/out/first/repo/aarch64/omamobile.db" +build first --local first > "$t/local.log" 2>&1 || { cat "$t/local.log"; fail "local build"; } +[[ $(zstd -qdc "$R/out/first/omamobile-rootfs-first-fixture-aarch64.img.zst") == first ]] || fail "local image remains under out/first" +echo 'ok - --local keeps its device repository and output directory' + +# Bundling must select this device even when another device sorts later. +mkdir -p "$t/kernel/images" "$R/packages/omamobile-keyring" +echo first > "$t/kernel/images/DEVICE" +echo boot > "$t/kernel/images/boot.img" +echo public-key > "$R/packages/omamobile-keyring/omamobile.gpg" +bundle() { DEVICE=first "$R/tools/make-bundle" --unsigned --version fixture "$@"; } +bundle > "$t/bundle.log" 2>&1 || { cat "$t/bundle.log"; fail "bundling selects this device's image"; } +image=omamobile-rootfs-first-fixture-aarch64.img.zst +digest=$(sha256sum < "$R/out/$image" | cut -d' ' -f1) +grep -Fxq "root image: $image ($digest)" "$R/out/bundle/omamobile-first-fixture/MANIFEST" || fail "bundle records the selected image and its actual digest" +if bundle --rootfs "$R/out/omamobile-rootfs-second-fixture-aarch64.img.zst" > "$t/bundle.log" 2>&1; then fail "bundling refuses another device's image"; fi +echo bad-checksum > "$R/out/$image.sha256" +if bundle > "$t/bundle.log" 2>&1; then fail "bundling refuses stale image checksum"; fi +grep -q "doesn't match its .sha256" "$t/bundle.log" || { cat "$t/bundle.log"; fail "bundling identifies the stale checksum"; } +echo 'ok - bundles select matching-device images and record verified bytes' diff --git a/tests/install-phone b/tests/install-phone index 6fc670c57..d8eb954aa 100755 --- a/tests/install-phone +++ b/tests/install-phone @@ -12,7 +12,12 @@ SRC=$PWD fail() { printf 'not ok - %s\n' "$1" >&2; exit 1; } pass() { printf 'ok - %s\n' "$1"; } for tool in debugfs mkfs.ext4 zstd openssl; do command -v $tool >/dev/null || { echo "skip: no $tool"; exit 0; }; done -t=$(mktemp -d); trap '[[ -n ${KEEP:-} ]] || rm -rf "$t"' EXIT +t=$(mktemp -d) +cleanup() { + [[ ! -d $t/gnupg ]] || gpgconf --homedir "$t/gnupg" --kill gpg-agent 2>/dev/null || true + [[ -n ${KEEP:-} ]] || rm -rf "$t" +} +trap cleanup EXIT # A repository-shaped tree: the script, a descriptor, images, a tiny root image R=$t/repo @@ -98,6 +103,14 @@ grep -qF 'correct horse' "$t/out" && fail "the Wi-Fi password is never printed" [[ -z $(ls "$t/work") ]] || fail "the image copy (with the password hash) is removed" pass "dry run: exactly the allowed commands printed, only getvar sent" +# An adjacent image for another device must not change the default selection. +other=$R/out/omamobile-rootfs-zother-2099.01.01-aarch64.img.zst +cp "$img" "$other" +printf 'device: zother\n' > "${other%.img.zst}.MANIFEST" +install --dry-run || { cat "$t/out"; fail "default root selection ignores another device"; } +rm "$other" "${other%.img.zst}.MANIFEST" +pass "default selection matches the device, including legacy filenames" + install || { cat "$t/out"; fail "a full run succeeds"; } want="-s FAKE123 flash boot_b $t/images/boot.img -s FAKE123 flash init_boot_b $t/images/init_boot.img @@ -227,6 +240,7 @@ cp tools/install-phone "$BD/install" cp "$R/device/testdev/device.conf" "$BD/device.conf" for p in boot init_boot vendor_boot vendor_kernel_boot dtbo; do cp "$t/images/$p.img" "$BD/images/"; done cp "$img" "$img.sha256" "$R/out/omamobile-rootfs-2026.10.13-aarch64.MANIFEST" "$t/bundle/" +printf 'kernel release: 7.3.0-test-1\nroot image: %s (%s)\n' "$(basename "$img")" "$(cut -d' ' -f1 "$img.sha256")" > "$BD/MANIFEST" (cd "$BD" && find . -type f ! -name SHA256SUMS -printf '%P\n' | sort | xargs sha256sum > SHA256SUMS) binstall() { : > "$t/fastboot.log" @@ -246,6 +260,44 @@ if command -v gpg >/dev/null; then gpg --homedir "$G" -q --batch --detach-sign -o "$BD/SHA256SUMS.sig" "$BD/SHA256SUMS" if binstall --dry-run; then fail "refuses a bundle signed by another key"; fi grep -q 'not signed by the Omarchy Mobile key' "$t/out" || fail "says the signature is not the release key's" + + # Trust only this ephemeral key in the copied installer. Sign and verify + # real bundle bytes with GPG; production's pinned keys stay unchanged. + signer=$(gpg --homedir "$G" --with-colons --list-keys 2>/dev/null | awk -F: '$1 == "fpr" {print $10; exit}') + sed -i "s/^SIGNING_KEYS=.*/SIGNING_KEYS='$signer'/" "$BD/install" + seal() { + (cd "$BD" && find . -type f ! -name SHA256SUMS ! -name SHA256SUMS.sig -printf '%P\n' | sort | xargs sha256sum > SHA256SUMS) + gpg --homedir "$G" -q --batch --yes --detach-sign -o "$BD/SHA256SUMS.sig" "$BD/SHA256SUMS" + } + seal + binstall --dry-run || { cat "$t/out"; fail "a bundle signed by the pinned fixture key installs"; } + if binstall --dry-run --images "$t/images"; then fail "refuses images outside the signed bundle"; fi + [[ ! -s $t/fastboot.log ]] || fail "rejects unsigned boot images before contacting the phone" + grep -q 'a bundle uses its own images' "$t/out" || { cat "$t/out"; fail "names the external images refusal"; } + # The signed name wins over a newer neighbour, and --rootfs cannot rename it. + other=$t/bundle/omamobile-rootfs-zother-2099.01.01-aarch64.img.zst + cp "$img" "$other" + binstall --dry-run || { cat "$t/out"; fail "signed bundle selects exactly its named root image"; } + if binstall --dry-run --rootfs "$other"; then fail "refuses the right bytes under an unsigned name"; fi + [[ ! -s $t/fastboot.log ]] || fail "rejects an unsigned root name before contacting the phone" + rm "$other" + # Another matching-device image with its own valid checksum must not + # replace the root image named in the signed manifest. + cp "$t/root.img" "$t/changed.img" + printf 'substituted image\n' > "$t/changed.txt" + debugfs -w -R "write $t/changed.txt /etc/substituted" "$t/changed.img" >/dev/null 2>&1 + zstd -q "$t/changed.img" -o "$t/changed.img.zst" + cp "$t/changed.img.zst" "$t/bundle/$(basename "$img")" + (cd "$t/bundle" && sha256sum "$(basename "$img")" > "$(basename "$img").sha256") + if binstall --dry-run; then fail "refuses a substituted root image with an updated checksum"; fi + [[ ! -s $t/fastboot.log ]] || fail "rejects substituted root before contacting the phone" + grep -q 'root image does not match the signed bundle' "$t/out" || { cat "$t/out"; fail "names the signed root mismatch"; } + cp "$img" "$img.sha256" "$t/bundle/" + # The manifest itself must be covered by the signed checksums. + sed -i '/ MANIFEST$/d' "$BD/SHA256SUMS" + gpg --homedir "$G" -q --batch --yes --detach-sign -o "$BD/SHA256SUMS.sig" "$BD/SHA256SUMS" + if binstall --dry-run; then fail "refuses a manifest outside the signed checksums"; fi + [[ ! -s $t/fastboot.log ]] || fail "rejects an unauthenticated manifest before contacting the phone" fi [[ -z $(flashing_calls) ]] || fail "a refused bundle flashes nothing" pass "bundle mode: signature required (pinned key), checksums checked, its own images used" diff --git a/tests/rootfs-smoke b/tests/rootfs-smoke index 09ff148c9..d817c23ac 100755 --- a/tests/rootfs-smoke +++ b/tests/rootfs-smoke @@ -2,7 +2,7 @@ # SPDX-License-Identifier: MIT # Smoke test of a root image from tools/build-rootfs, read through debugfs # (no root, no loop mount, nothing written): -# tests/rootfs-smoke out/omamobile-rootfs--aarch64.img.zst (or a raw .img) +# tests/rootfs-smoke out/omamobile-rootfs---aarch64.img.zst (or a raw .img) # Checks the label, that there is no account but a locked root, the keyrings, # the [omamobile] repository and its place in pacman.conf, the first-boot # unit, the kernel modules, the MANIFEST, and that no per-machine state, diff --git a/tools/build-rootfs b/tools/build-rootfs index d30909362..bbf7d7dfe 100755 --- a/tools/build-rootfs +++ b/tools/build-rootfs @@ -1,9 +1,9 @@ #!/bin/bash # SPDX-License-Identifier: MIT # Build Omarchy Mobile's root file system image (docs/RELEASES.md §6): -# out/omamobile-rootfs--aarch64.img.zst ext4, label omamobile-root -# out/omamobile-rootfs--aarch64.img.zst.sha256 -# out/omamobile-rootfs--aarch64.MANIFEST what is in it, with versions +# out/omamobile-rootfs---aarch64.img.zst ext4, label omamobile-root +# out/omamobile-rootfs---aarch64.img.zst.sha256 +# out/omamobile-rootfs---aarch64.MANIFEST contents and versions # # Inside an Arch Linux ARM container (tools/lib/alarm.sh) it pacstraps: # rootfs/packages.base Arch Linux ARM's base, sshd, NetworkManager, @@ -99,18 +99,25 @@ release=$(cat "$KERNEL_OUT/include/config/kernel.release") modules=$KERNEL_OUT/modules-$release.tar.zst [[ -f $modules ]] || alarm_die "no $modules: run KERNEL_OUT=$KERNEL_OUT bin/omamobile modules" -name=omamobile-rootfs-$VERSION-aarch64 +name=omamobile-rootfs-$DEVICE-$VERSION-aarch64 out=$ROOT/$outrel mkdir -p "$out" -work=$out/.rootfs-$VERSION -rm -rf "$work"; mkdir -p "$work" +# Keep the lock file: unlinking it could let two callers lock different inodes. +command -v flock >/dev/null || alarm_die "flock not found (util-linux)" +exec {lock}>"$out/.$name.lock" +flock -n "$lock" || alarm_die "already building $name in $out" +work=$(mktemp -d "$out/.$name.XXXXXX") +volume="" +cleanup() { + [[ -z $volume ]] || docker volume rm -f "$volume" >/dev/null 2>&1 || true + rm -rf "$work" +} +trap cleanup EXIT alarm_fetch image=$(alarm_build_image "$ROOT") volume=omamobile-rootfs-target-$$ docker volume create "$volume" >/dev/null -cleanup() { docker volume rm -f "$volume" >/dev/null 2>&1 || true; } -trap cleanup EXIT start=$(date +%s) alarm_say "Building $name for $DEVICE (kernel $release)" @@ -126,14 +133,16 @@ docker run --rm --privileged --platform linux/arm64 \ "$image" bash /src/rootfs/build-inside alarm_say "Compressing" -zstd -q -T0 -12 -f "$work/rootfs.img" -o "$out/$name.img.zst" -(cd "$out" && sha256sum "$name.img.zst" > "$name.img.zst.sha256") -cp "$work/MANIFEST" "$out/$name.MANIFEST" -if ((KEEP_RAW)); then mv "$work/rootfs.img" "$out/$name.img"; fi -rm -rf "$work" +zstd -q -T0 -12 "$work/rootfs.img" -o "$work/$name.img.zst" +(cd "$work" && sha256sum "$name.img.zst" > "$name.img.zst.sha256") +mv "$work/MANIFEST" "$work/$name.MANIFEST" alarm_say "Checking the image" -tests/rootfs-smoke "$out/$name.img.zst" +tests/rootfs-smoke "$work/$name.img.zst" +# Publish only a complete, checked build. A failed rebuild leaves the old one. +mv "$work/$name.img.zst" "$work/$name.MANIFEST" "$out/" +mv "$work/$name.img.zst.sha256" "$out/" +if ((KEEP_RAW)); then mv "$work/rootfs.img" "$out/$name.img"; fi echo ls -lh "$out/$name".* echo "built in $(( ($(date +%s) - start) / 60 )) min" diff --git a/tools/install-phone b/tools/install-phone index 882ecddeb..207f7d5e2 100755 --- a/tools/install-phone +++ b/tools/install-phone @@ -30,11 +30,11 @@ # instead of running it # --device CODENAME repository mode: device/CODENAME/device.conf (default # $DEVICE or cheetah); in a bundle, its device.conf -# --images DIR the slot B images (repository default: +# --images DIR repository mode: the slot B images (default: # $KERNEL_OUT/images, KERNEL_OUT=$KERNEL_BASE/out-int) # --rootfs FILE the root image (.img.zst; default: the newest -# omamobile-rootfs-*-aarch64.img.zst in out/, or -# beside a bundle) +# image for this device in out/, or the exact +# image named by a signed bundle's MANIFEST) # --work DIR where the local image copy goes (default # ~/.cache/omamobile-install; needs the image's size free) # --bootloader-slots-match VERSION @@ -80,10 +80,10 @@ done if [[ -f $here/device.conf && -d $here/images ]]; then mode=bundle conf=$here/device.conf + [[ -z $images || $(readlink -f "$images") == "$here/images" ]] || + die "a bundle uses its own images; --images is only available in repository mode" images=${images:-$here/images} - if [[ -z $rootfs ]]; then - for f in "$here"/../omamobile-rootfs-*-aarch64.img.zst "$here"/omamobile-rootfs-*-aarch64.img.zst; do rootfs=$f; done - fi + root_dirs=("$here/.." "$here") else mode=repository repo=$(cd -- "$here/.." && pwd) @@ -96,9 +96,7 @@ else [[ -d $KERNEL_BASE || ! -d $HOME/android/mainline ]] || KERNEL_BASE=$HOME/android/mainline fi images=${images:-${KERNEL_OUT:-$KERNEL_BASE/out-int}/images} - if [[ -z $rootfs ]]; then - for f in "$repo"/out/omamobile-rootfs-*-aarch64.img.zst; do rootfs=$f; done - fi + root_dirs=("$repo/out") fi [[ -f $conf ]] || die "no device descriptor $conf" @@ -136,7 +134,6 @@ if [[ $mode == repository ]]; then [[ $built_for == "$CODENAME" ]] \ || die "$images holds images for '${built_for:-an unknown device}', not $CODENAME (run: DEVICE=$CODENAME bin/omamobile images)" fi -[[ -n $rootfs && -f $rootfs ]] || die "no root image (tools/build-rootfs makes one; or --rootfs FILE)" for tool in "$FASTBOOT" debugfs dumpe2fs e2fsck zstd openssl sha256sum; do command -v "$tool" >/dev/null || die "$tool not found (fastboot: android-tools; debugfs: e2fsprogs)" done @@ -154,6 +151,7 @@ done # --- Integrity of what will be written --------------------------------------- say "Checking the files ($mode mode)" +root_digest="" root_name="" if [[ $mode == bundle ]]; then [[ -f $here/SHA256SUMS ]] || die "the bundle has no SHA256SUMS" if [[ -f $here/SHA256SUMS.sig ]]; then @@ -171,8 +169,38 @@ if [[ $mode == bundle ]]; then fi (cd "$here" && sha256sum -c --quiet SHA256SUMS) || die "a bundle file doesn't match SHA256SUMS" note "bundle checksums match" + if [[ -f $here/SHA256SUMS.sig ]]; then + # The root image sits beside the bundle, outside SHA256SUMS. Only the + # signed manifest can authorize its name and bytes; its .sha256 cannot. + [[ $(grep -Ec '^[0-9a-f]{64} [ *](\./)?MANIFEST$' "$here/SHA256SUMS") == 1 ]] || + die "the signed checksums must cover MANIFEST exactly once" + root_record=$(sed -n 's/^root image: //p' "$here/MANIFEST") + root_pattern='^([A-Za-z0-9._-]+\.img\.zst) \(([0-9a-f]{64})\)$' + [[ $root_record =~ $root_pattern ]] || die "the signed MANIFEST must name one root image and its SHA256" + root_name=${BASH_REMATCH[1]} root_digest=${BASH_REMATCH[2]} + fi +fi +if [[ -z $rootfs ]]; then + if [[ -n $root_name ]]; then + for dir in "${root_dirs[@]}"; do + [[ ! -f $dir/$root_name ]] || rootfs=$dir/$root_name + done + else + # Read the device from the manifest, including images with legacy names. + for dir in "${root_dirs[@]}"; do + for f in "$dir"/omamobile-rootfs-*-aarch64.img.zst; do + built_for=$(sed -n 's/^device: \([a-z0-9]*\).*/\1/p' "${f%.img.zst}.MANIFEST" 2>/dev/null || true) + [[ $built_for != "$CODENAME" ]] || rootfs=$f + done + done + fi fi -if [[ -f $rootfs.sha256 ]]; then +[[ -n $rootfs && -f $rootfs ]] || die "no root image for $CODENAME${root_name:+ ($root_name)} (tools/build-rootfs makes one; or --rootfs FILE)" +if [[ -n $root_digest ]]; then + [[ ${rootfs##*/} == "$root_name" ]] || die "root image must be $root_name, as named by the signed bundle" + [[ $(sha256sum < "$rootfs" | cut -d' ' -f1) == "$root_digest" ]] || die "root image does not match the signed bundle" + note "root image matches the signed bundle" +elif [[ -f $rootfs.sha256 ]]; then (cd "$(dirname "$rootfs")" && sha256sum -c --quiet "$(basename "$rootfs").sha256") || die "$rootfs doesn't match its .sha256" note "root image checksum matches" else diff --git a/tools/make-bundle b/tools/make-bundle index ab7cc7f59..d5daf2e08 100755 --- a/tools/make-bundle +++ b/tools/make-bundle @@ -11,7 +11,7 @@ # MANIFEST kernel release, root image, both repositories' commits # SHA256SUMS checksums of everything above # SHA256SUMS.sig signed with the key in keys/ (maintainer only; --unsigned skips) -# out/bundle/omamobile-rootfs--aarch64.img.zst (+ .sha256, .MANIFEST) +# out/bundle/omamobile-rootfs---aarch64.img.zst (+ .sha256, .MANIFEST) # # tools/make-bundle [--version V] [--rootfs FILE] [--unsigned] # DEVICE (default cheetah), KERNEL_OUT (default $KERNEL_BASE/out-int, @@ -50,10 +50,24 @@ boot_images=$(sed -n 's/^BOOT_IMAGES="\(.*\)"$/\1/p' "$conf") [[ -n $boot_images ]] || die "$conf has no BOOT_IMAGES" if [[ -z $rootfs ]]; then shopt -s nullglob - for f in out/omamobile-rootfs-*-aarch64.img.zst; do rootfs=$f; done + for f in out/omamobile-rootfs-*-aarch64.img.zst; do + built_for=$(sed -n 's/^device: \([a-z0-9]*\).*/\1/p' "${f%.img.zst}.MANIFEST" 2>/dev/null || true) + [[ $built_for != "$DEVICE" ]] || rootfs=$f + done fi [[ -n $rootfs && -f $rootfs && -f $rootfs.sha256 ]] || die "no root image with a .sha256 (tools/build-rootfs)" release=$(cat "$KERNEL_OUT/include/config/kernel.release" 2>/dev/null) || die "no kernel build in $KERNEL_OUT" +rootname=$(basename "$rootfs") +[[ $rootname =~ ^[A-Za-z0-9._-]+\.img\.zst$ ]] || die "root image needs a plain filename ending in .img.zst" +manifest=${rootfs%.img.zst}.MANIFEST +[[ -f $manifest ]] || die "no $manifest" +built_for=$(sed -n 's/^device: \([a-z0-9]*\).*/\1/p' "$manifest") +[[ $built_for == "$DEVICE" ]] || die "the root image was built for '${built_for:-an unknown device}', not $DEVICE" +modules_release=$(sed -n 's/^kernel modules: \([^ ]*\).*/\1/p' "$manifest") +[[ -z $modules_release || $modules_release == "$release" ]] || + die "the root image has modules for $modules_release, but the boot images are $release: rebuild one to match" +root_digest=$(sha256sum < "$rootfs" | cut -d' ' -f1) +[[ $(cut -d' ' -f1 "$rootfs.sha256") == "$root_digest" ]] || die "$rootfs doesn't match its .sha256" name=omamobile-$DEVICE-$version out=out/bundle @@ -77,7 +91,6 @@ for f in arch/arm64/boot/Image initramfs.cpio.lz4; do die "$KERNEL_OUT/images is older than $f: run bin/omamobile images" done -rootname=$(basename "$rootfs") for f in "$rootfs" "$rootfs.sha256" "${rootfs%.img.zst}.MANIFEST"; do [[ -f $f ]] && ln -f "$f" "$out/" 2>/dev/null || cp -f "$f" "$out/" done @@ -89,7 +102,7 @@ kernel_commit=$(git -C "${KERNEL_DIR:-$KERNEL_BASE/wt-int}" rev-parse --short=12 echo "kernel release: $release" echo "kernel commit (linux-omarchy-mobile): $kernel_commit" echo "omarchy-mobile commit: $(git rev-parse --short=12 HEAD)$(git diff --quiet HEAD || echo -dirty)" - echo "root image: $rootname ($(cut -d' ' -f1 "$rootfs.sha256"))" + echo "root image: $rootname ($root_digest)" echo echo "# boot images" (cd "$dir/images" && sha256sum -- *.img)