Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

Harness OS: build the Orange Pi 4 Pro image in two phases

bubu-dudu601d9d047eef
diff --git a/os/platforms/orangepi-4pro/README.md b/os/platforms/orangepi-4pro/README.md
index 1dc383b0c..2e50776f0 100644
--- a/os/platforms/orangepi-4pro/README.md
+++ b/os/platforms/orangepi-4pro/README.md
@@ -26,6 +26,13 @@ installer, system updates, PC drivers and boot splash.
## Build
+The build has two phases, so a code change does not repeat the slow, rarely changing part:
+
+| Phase | Script | Contents | Rebuilt when |
+|---|---|---|---|
+| 1. Base | `build-compositor.sh`, `build-base.sh` | Orange Pi's image grown by 3 GB, Debian's session packages, Node 22, the compositor in `/opt/harness-wl` | its key changes (`base-key.sh`: the base image's checksum and the phase-1 scripts, labwc source and patch) |
+| 2. Harness | `build-runtime.sh`, `build-image.sh` | the session tree, runtime and agents on a copy of the base | every build |
+
With Docker, on any host:
```sh
@@ -33,10 +40,13 @@ bash os/platforms/orangepi-4pro/build-docker.sh \
Orangepi4pro_1.0.6_debian_bookworm_server_linux5.15.147.img out/
```
-It runs the three steps below in a privileged arm64 Debian 12 container from the committed
-HEAD, and writes `harness-orangepi4pro-debian12-<commit>.img.xz` with its `.sha256`. Named
-volumes keep cargo downloads and the compositor build between runs. An x86-64 host emulates
-arm64 with QEMU, so a first build there takes hours; an arm64 host builds natively.
+`--base` runs only phase 1 (forcing a rebuild); `--harness` runs only phase 2 and needs the
+base for the current key. It runs in a privileged arm64 Debian 12 container from the committed HEAD, and
+writes `harness-orangepi4pro-debian12-<commit>.img.xz` with its `.sha256`. Named volumes
+(`harness-orangepi4pro-*`) keep the base image under its key, the compositor build under its
+own key, and cargo's target directory and npm's cache for incremental phase-2 builds. An
+x86-64 host emulates arm64 with QEMU, so a first build there takes hours; an arm64 host
+builds natively.
Or natively on the board, running Orange Pi's Debian 12 server image, with a clean checkout:
@@ -49,8 +59,10 @@ sudo apt install build-essential musl-tools cmake ninja-build pkg-config python3
HARNESS_OS_RUNTIME_DIR=os/work/runtime-arm bash os/tools/build-runtime.sh
sudo install -d -o "$USER" /opt/harness-wl
bash os/platforms/orangepi-4pro/build-compositor.sh
-sudo bash os/platforms/orangepi-4pro/build-image.sh \
- Orangepi4pro_1.0.6_debian_bookworm_server_linux5.15.147.img os/work/runtime-arm \
+# Phase 1 once (again only when base-key.sh prints a new key), phase 2 per code change:
+sudo bash os/platforms/orangepi-4pro/build-base.sh \
+ Orangepi4pro_1.0.6_debian_bookworm_server_linux5.15.147.img harness-base.img
+sudo bash os/platforms/orangepi-4pro/build-image.sh harness-base.img os/work/runtime-arm \
harness-orangepi4pro.img
```
diff --git a/os/platforms/orangepi-4pro/base-key.sh b/os/platforms/orangepi-4pro/base-key.sh
new file mode 100755
index 000000000..c4904313a
--- /dev/null
+++ b/os/platforms/orangepi-4pro/base-key.sh
@@ -0,0 +1,25 @@
+#!/usr/bin/env bash
+# Print the key of the phase-1 base image (build-base.sh): it changes exactly when the base
+# image would. Inputs: Orange Pi's image and the files that decide what phase 1 installs.
+# Usage: base-key.sh BASE.img (uses BASE.img.sha when present instead of hashing 4 GB)
+set -euo pipefail
+BASE=${1:?official Orangepi4pro_*_debian_bookworm_server_*.img}
+HERE=$(cd -- "$(dirname -- "$0")" && pwd)
+SOURCE=$(cd -- "$HERE/../../.." && pwd)
+python3 - "$BASE" "$HERE" "$SOURCE" <<'PY'
+import hashlib, sys
+from pathlib import Path
+base, here, source = map(Path, sys.argv[1:])
+recorded = Path(str(base) + '.sha')
+if recorded.is_file():
+ base_digest = recorded.read_text().split()[0].lower()
+else:
+ with base.open('rb') as handle:
+ base_digest = hashlib.file_digest(handle, 'sha256').hexdigest()
+key = hashlib.sha256(b'harness-orangepi4pro-base\n' + base_digest.encode() + b'\n')
+for path in [here / 'build-base.sh', here / 'image-lib.sh', here / 'build-compositor.sh',
+ source / 'os/packaging/labwc/source.json',
+ source / 'os/packaging/labwc/session-lock-presentation.patch']:
+ key.update(path.relative_to(source).as_posix().encode() + b'\0' + path.read_bytes() + b'\0')
+print(key.hexdigest()[:16])
+PY
diff --git a/os/platforms/orangepi-4pro/build-base.sh b/os/platforms/orangepi-4pro/build-base.sh
new file mode 100755
index 000000000..7999cc7b9
--- /dev/null
+++ b/os/platforms/orangepi-4pro/build-base.sh
@@ -0,0 +1,45 @@
+#!/usr/bin/env bash
+# Phase 1: the base image, which changes rarely. Orange Pi's official Debian 12 server image,
+# grown by 3 GB, with Debian's session packages, Node 22 and the private compositor build.
+# Phase 2 (build-image.sh) adds Harness itself to a copy of it.
+# Run as root on an aarch64 Debian 12 host, after build-compositor.sh.
+# Usage: build-base.sh BASE.img OUTPUT_BASE.img [KEY] (KEY from base-key.sh, recorded)
+set -euo pipefail
+BASE=${1:?official Orangepi4pro_*_debian_bookworm_server_*.img}
+OUT=${2:?output base image}
+HERE=$(cd -- "$(dirname -- "$0")" && pwd)
+KEY=${3:-$("$HERE/base-key.sh" "$BASE")}
+PREFIX=${HARNESS_WL_PREFIX:-/opt/harness-wl}
+[[ $EUID == 0 && $(uname -m) == aarch64 ]] || { echo 'Run as root on aarch64.' >&2; exit 1; }
+[[ -x $PREFIX/bin/labwc ]] || { echo "Build the compositor first: $HERE/build-compositor.sh" >&2; exit 1; }
+source "$HERE/image-lib.sh"
+
+PARTIAL=$OUT.partial
+cp --sparse=always "$BASE" "$PARTIAL"
+attach_image "$PARTIAL" +3G
+curl -fsSL https://deb.nodesource.com/setup_22.x -o "$MNT/tmp/nodesource.sh"
+chroot "$MNT" /bin/bash -euo pipefail <<'EOF'
+export DEBIAN_FRONTEND=noninteractive LC_ALL=C
+bash /tmp/nodesource.sh >/dev/null 2>&1
+rm -f /tmp/nodesource.sh
+# Session tools from Debian 12, plus the runtime libraries of the private compositor build.
+apt-get install -y -qq nodejs jq curl tmux python3 foot chromium dbus-user-session swayidle grim slurp \
+ wl-clipboard xdg-desktop-portal-wlr xdg-utils fonts-dejavu fonts-noto-color-emoji fonts-noto-cjk \
+ libcairo2 libpango-1.0-0 libpangocairo-1.0-0 librsvg2-2 libxml2 libglib2.0-0 libpng16-16 \
+ libmtdev1 libevdev2 libudev1 libseat1 libgbm1 libegl1 libgles2 hwdata >/dev/null
+apt-get clean
+rm -rf /root/.npm
+EOF
+mkdir -p "$MNT$PREFIX"
+cp -a "$PREFIX/." "$MNT$PREFIX/"
+rm -rf "$MNT$PREFIX/include" "$MNT$PREFIX/share/doc"
+if chroot "$MNT" ldd "$PREFIX/bin/labwc" | grep 'not found'; then
+ echo 'The compositor is missing a library in the image.' >&2
+ exit 1
+fi
+chroot "$MNT" "$PREFIX/bin/labwc" --version
+printf '{\n "board": "orangepi4pro",\n "base": "%s",\n "base_key": "%s"\n}\n' "$(basename -- "$BASE")" "$KEY" \
+ > "$MNT/etc/harness-base.json"
+release_image
+mv "$PARTIAL" "$OUT"
+echo "$OUT ($KEY)"
diff --git a/os/platforms/orangepi-4pro/build-docker.sh b/os/platforms/orangepi-4pro/build-docker.sh
index c79fe8643..a0f83556f 100755
--- a/os/platforms/orangepi-4pro/build-docker.sh
+++ b/os/platforms/orangepi-4pro/build-docker.sh
@@ -1,22 +1,46 @@
#!/usr/bin/env bash
# Build the Orange Pi 4 Pro image in an arm64 Debian 12 container instead of on the board.
-# Runs build-runtime.sh, build-compositor.sh and build-image.sh unchanged inside it.
+# Two phases, as on the board:
+# 1. build-compositor.sh + build-base.sh: Debian packages and labwc. Kept in a volume under
+# the key from base-key.sh and rebuilt only when that key changes.
+# 2. build-runtime.sh + build-image.sh: Harness from the committed HEAD, on a copy of the base.
+# Cargo's target directory and npm's cache persist, so a code change compiles incrementally.
# An x86-64 host emulates arm64 through Docker's QEMU (slow: a first build takes hours);
# an arm64 host (Apple Silicon, an arm64 Linux runner) builds natively.
-# Usage: build-docker.sh BASE.img OUTPUT_DIR
-# BASE.img Orange Pi's official Orangepi4pro_*_debian_bookworm_server_*.img (checked
-# against its .sha first)
+# Usage: build-docker.sh [--base | --harness] BASE.img OUTPUT_DIR
+# (no flag) phase 1 when its key is new, then phase 2
+# --base phase 1 only (rebuilt even when its key exists); OUTPUT_DIR is unused
+# --harness phase 2 only; the base for the current key must already exist
+# BASE.img Orange Pi's official Orangepi4pro_*_debian_bookworm_server_*.img; its .sha
+# beside it is checked and used for the key
# OUTPUT_DIR receives harness-orangepi4pro-debian12-<commit>.img.xz and its .sha256
-# The build uses the committed HEAD of this checkout; uncommitted changes are not included.
+# Uncommitted changes are not built. Remove the cache: docker volume rm $(docker volume ls -q
+# --filter name=harness-orangepi4pro-)
set -euo pipefail
+PHASES=all
+case ${1:-} in
+ --base) PHASES=base; shift ;;
+ --harness) PHASES=harness; shift ;;
+esac
BASE=$(cd -- "$(dirname -- "${1:?base image}")" && pwd)/$(basename -- "$1")
OUT=$(mkdir -p -- "${2:?output directory}" && cd -- "$2" && pwd)
-SOURCE=$(cd -- "$(dirname -- "$0")/../../.." && pwd)
+HERE=$(cd -- "$(dirname -- "$0")" && pwd)
+SOURCE=$(cd -- "$HERE/../../.." && pwd)
BUILDER=harness-orangepi4pro-builder
[[ -f $BASE ]] || { echo "No base image at $BASE" >&2; exit 1; }
+if [[ -f $BASE.sha ]]; then
+ (cd -- "$(dirname -- "$BASE")" && shasum -a 256 -c "$(basename -- "$BASE").sha" >/dev/null) ||
+ { echo "$BASE does not match its .sha." >&2; exit 1; }
+fi
if [[ -n $(git -C "$SOURCE" status --porcelain --untracked-files=no) ]]; then
echo 'Uncommitted changes are not part of the build; it uses HEAD.' >&2
fi
+# The key is computed from the committed scripts, as the container sees them.
+KEY_SOURCE=$(mktemp -d)
+trap 'rm -rf "$KEY_SOURCE"' EXIT
+git -C "$SOURCE" archive HEAD os/platforms/orangepi-4pro os/packaging/labwc | tar -x -C "$KEY_SOURCE"
+KEY=$(bash "$KEY_SOURCE/os/platforms/orangepi-4pro/base-key.sh" "$BASE")
+echo "Base key: $KEY"
docker build --platform linux/arm64 -t "$BUILDER" - <<'EOF'
FROM debian:bookworm
@@ -35,23 +59,51 @@ RUN curl -fsSL https://sh.rustup.rs | sh -s -- -y --profile minimal \
&& rustup target add aarch64-unknown-linux-musl
EOF
-# Named volumes keep the slow parts between builds: cargo downloads, the compositor's
-# sources and done-markers, and its install prefix (whose path the image keeps).
+# Volumes: phase 1 (compositor sources and prefix, base images) and phase 2 caches.
docker run --rm --privileged --platform linux/arm64 \
-v "$SOURCE":/src:ro -v "$BASE":/base.img:ro -v "$OUT":/out \
- -v harness-orangepi4pro-cargo:/usr/local/cargo/registry \
-v harness-orangepi4pro-wl-work:/cache/wl \
-v harness-orangepi4pro-wl:/opt/harness-wl \
- -e HARNESS_WL_WORK=/cache/wl \
+ -v harness-orangepi4pro-base:/cache/base \
+ -v harness-orangepi4pro-cargo:/usr/local/cargo/registry \
+ -v harness-orangepi4pro-target:/cache/tui-target \
+ -v harness-orangepi4pro-npm:/root/.npm \
+ -e HARNESS_WL_WORK=/cache/wl -e BASE_KEY="$KEY" -e PHASES="$PHASES" \
"$BUILDER" bash -euo pipefail -c '
git config --global --add safe.directory /src
git clone -q /src /build
cd /build
COMMIT=$(git rev-parse --short HEAD)
+HERE=os/platforms/orangepi-4pro
+
+# Phase 1, only when its key is new (or asked for with --base).
+HARNESS_BASE=/cache/base/base-$BASE_KEY.img
+if [[ $PHASES == harness && ! -f $HARNESS_BASE ]]; then
+ echo "No base for key $BASE_KEY yet: run with --base first." >&2
+ exit 1
+elif [[ -f $HARNESS_BASE && $PHASES != base ]]; then
+ echo "Phase 1: reusing base $BASE_KEY"
+else
+ echo "Phase 1: building base $BASE_KEY"
+ # The compositor has its own key: its sources and done-markers are reused only for the
+ # same script, labwc source and patch.
+ COMPOSITOR_KEY=$(cat $HERE/build-compositor.sh os/packaging/labwc/source.json \
+ os/packaging/labwc/session-lock-presentation.patch | sha256sum | cut -c1-16)
+ if [[ $(cat /opt/harness-wl/.harness-key 2>/dev/null) != "$COMPOSITOR_KEY" ]]; then
+ find /opt/harness-wl /cache/wl -mindepth 1 -delete
+ fi
+ bash $HERE/build-compositor.sh
+ echo "$COMPOSITOR_KEY" > /opt/harness-wl/.harness-key
+ bash $HERE/build-base.sh /base.img "$HARNESS_BASE" "$BASE_KEY"
+ find /cache/base -name "base-*.img" ! -name "base-$BASE_KEY.img" -delete
+fi
+[[ $PHASES != base ]] || { ls -la /cache/base; exit 0; }
+
+# Phase 2: Harness from this commit. The clone is fresh; cargo output is not.
+ln -s /cache/tui-target tui/target
HARNESS_OS_RUNTIME_DIR=/build/os/work/runtime-arm bash os/tools/build-runtime.sh
-bash os/platforms/orangepi-4pro/build-compositor.sh
IMG=/build/harness-orangepi4pro-debian12-$COMMIT.img
-bash os/platforms/orangepi-4pro/build-image.sh /base.img /build/os/work/runtime-arm "$IMG"
+bash $HERE/build-image.sh "$HARNESS_BASE" /build/os/work/runtime-arm "$IMG"
xz -T0 -6 -c "$IMG" > "/out/${IMG##*/}.xz"
cd /out && sha256sum "${IMG##*/}.xz" > "${IMG##*/}.xz.sha256"
ls -la /out
diff --git a/os/platforms/orangepi-4pro/build-image.sh b/os/platforms/orangepi-4pro/build-image.sh
index 8497b32d1..15babe3c7 100755
--- a/os/platforms/orangepi-4pro/build-image.sh
+++ b/os/platforms/orangepi-4pro/build-image.sh
@@ -1,74 +1,24 @@
#!/usr/bin/env bash
-# Build a Harness SD image for Orange Pi 4 Pro from Orange Pi's official Debian 12 server image.
-# Run as root on an aarch64 Debian 12 host (the board itself), after build-runtime.sh and
-# build-compositor.sh, so the chroot runs natively and links against the same libraries.
-# Usage: build-image.sh BASE.img RUNTIME_DIR OUTPUT.img
+# Phase 2: Harness on a copy of the phase-1 base image (build-base.sh). This is the part that
+# changes with the code: the session tree from this source, its runtime and the agents.
+# Run as root on an aarch64 Debian 12 host, after os/tools/build-runtime.sh.
+# Usage: build-image.sh HARNESS_BASE.img RUNTIME_DIR OUTPUT.img
# Nothing from the build host's accounts (sign-ins, SSH keys, sources) enters the image.
set -euo pipefail
-BASE=${1:?official Orangepi4pro_*_debian_bookworm_server_*.img}
+BASE=${1:?base image from build-base.sh}
RUNTIME=${2:?runtime directory from os/tools/build-runtime.sh}
OUT=${3:?output image}
-SOURCE=$(cd -- "$(dirname -- "$0")/../../.." && pwd)
-HERE=$SOURCE/os/platforms/orangepi-4pro
+HERE=$(cd -- "$(dirname -- "$0")" && pwd)
+SOURCE=$(cd -- "$HERE/../../.." && pwd)
PREFIX=${HARNESS_WL_PREFIX:-/opt/harness-wl}
ACCOUNT=orangepi
[[ $EUID == 0 && $(uname -m) == aarch64 ]] || { echo 'Run as root on aarch64.' >&2; exit 1; }
-[[ -x $PREFIX/bin/labwc ]] || { echo "Build the compositor first: $HERE/build-compositor.sh" >&2; exit 1; }
OPENCODE=$(python3 -c "import json,sys;print(json.load(open(sys.argv[1]))['version'])" "$SOURCE/os/packaging/fedora/opencode.lock.json")
-MNT=$(mktemp -d)
-LOOP=
-
-cleanup() {
- set +e
- for m in dev/pts dev proc sys; do mountpoint -q "$MNT/$m" && umount "$MNT/$m"; done
- mountpoint -q "$MNT" && umount "$MNT"
- [[ -n $LOOP ]] && losetup -d "$LOOP"
- [[ -n $MADE_NODE ]] && rm -f "$MADE_NODE"
- rmdir "$MNT"
-}
-MADE_NODE=
-trap cleanup EXIT
+source "$HERE/image-lib.sh"
cp --sparse=always "$BASE" "$OUT"
-truncate -s +3G "$OUT"
-LOOP=$(losetup -fP --show "$OUT")
-parted -s "$LOOP" resizepart 1 100%
-partprobe "$LOOP" || true
-PART=${LOOP}p1
-if [[ ! -b $PART ]]; then
- # A container (build-docker.sh) has no udev to create partition nodes.
- IFS=: read -r major minor < "/sys/class/block/${LOOP#/dev/}p1/dev"
- mknod "$PART" b "$major" "$minor"
- MADE_NODE=$PART
-fi
-e2fsck -fy "$PART" >/dev/null || true
-resize2fs "$PART" >/dev/null
-mount "$PART" "$MNT"
-for m in dev dev/pts proc sys; do mount --bind "/$m" "$MNT/$m"; done
-# Network for the chroot; the image's own resolv.conf is restored afterwards.
-mv "$MNT/etc/resolv.conf" "$MNT/etc/resolv.conf.harness" 2>/dev/null || true
-cp -L /etc/resolv.conf "$MNT/etc/resolv.conf"
-printf '#!/bin/sh\nexit 101\n' > "$MNT/usr/sbin/policy-rc.d"
-chmod 755 "$MNT/usr/sbin/policy-rc.d"
-curl -fsSL https://deb.nodesource.com/setup_22.x -o "$MNT/tmp/nodesource.sh"
-
-chroot "$MNT" /bin/bash -euo pipefail <<'EOF'
-export DEBIAN_FRONTEND=noninteractive LC_ALL=C
-bash /tmp/nodesource.sh >/dev/null 2>&1
-# Session tools from Debian 12, plus the runtime libraries of the private compositor build.
-apt-get install -y -qq nodejs jq curl tmux python3 foot chromium dbus-user-session swayidle grim slurp \
- wl-clipboard xdg-desktop-portal-wlr xdg-utils fonts-dejavu fonts-noto-color-emoji fonts-noto-cjk \
- libcairo2 libpango-1.0-0 libpangocairo-1.0-0 librsvg2-2 libxml2 libglib2.0-0 libpng16-16 \
- libmtdev1 libevdev2 libudev1 libseat1 libgbm1 libegl1 libgles2 hwdata >/dev/null
-apt-get clean
-EOF
-mkdir -p "$MNT$PREFIX"
-cp -a "$PREFIX/." "$MNT$PREFIX/"
-rm -rf "$MNT$PREFIX/include" "$MNT$PREFIX/share/doc"
-if chroot "$MNT" ldd "$PREFIX/bin/labwc" | grep 'not found'; then
- echo 'The compositor is missing a library in the image.' >&2
- exit 1
-fi
+attach_image "$OUT"
+[[ -f $MNT/etc/harness-base.json && -x $MNT$PREFIX/bin/labwc ]] || { echo "$BASE is not a build-base.sh image." >&2; exit 1; }
HARNESS_WL_PREFIX=$PREFIX "$HERE/install-session.sh" "$MNT" "$RUNTIME" "$ACCOUNT"
# OpenCode, the version the ARM package pins. Claude Code too, by hn's own install recipe
@@ -76,23 +26,22 @@ HARNESS_WL_PREFIX=$PREFIX "$HERE/install-session.sh" "$MNT" "$RUNTIME" "$ACCOUNT
# other agents install from hn when chosen.
chroot "$MNT" su - "$ACCOUNT" -c "npm_config_prefix=\$HOME/.local npm install -g --no-audit --no-fund opencode-ai@$OPENCODE @anthropic-ai/claude-code >/dev/null 2>&1"
CLAUDE=$(chroot "$MNT" su - "$ACCOUNT" -c '$HOME/.local/bin/claude --version' | cut -d' ' -f1)
-python3 - "$MNT/etc/harness-image.json" "$BASE" "$SOURCE" "$OPENCODE" "$CLAUDE" <<'PY'
+python3 - "$MNT/etc/harness-image.json" "$MNT/etc/harness-base.json" "$SOURCE" "$OPENCODE" "$CLAUDE" <<'PY'
import json, subprocess, sys
from pathlib import Path
path, base, source, opencode, claude = sys.argv[1:]
commit = subprocess.check_output(['git', '-c', f'safe.directory={source}', '-C', source, 'rev-parse', 'HEAD'], text=True).strip()
-Path(path).write_text(json.dumps({'board': 'orangepi4pro', 'base': Path(base).name, 'source_commit': commit,
- 'opencode': opencode, 'claude_code': claude}, indent=2) + '\n')
+info = json.loads(Path(base).read_text())
+info.update(source_commit=commit, opencode=opencode, claude_code=claude)
+Path(path).write_text(json.dumps(info, indent=2) + '\n')
PY
# Leave no build-host state behind.
-rm -f "$MNT/usr/sbin/policy-rc.d" "$MNT/etc/resolv.conf" "$MNT/tmp/nodesource.sh"
-mv "$MNT/etc/resolv.conf.harness" "$MNT/etc/resolv.conf" 2>/dev/null || true
-rm -rf "$MNT/root/.npm" "$MNT/home/$ACCOUNT/.npm"
+rm -rf "$MNT/root/.npm" "$MNT/home/$ACCOUNT/.npm" "$MNT/tmp/"*
for leftover in "home/$ACCOUNT/.ssh/authorized_keys" "home/$ACCOUNT/.harness" "root/.ssh/authorized_keys"; do
[[ ! -e $MNT/$leftover ]] || { echo "Unexpected build-host state: $leftover" >&2; exit 1; }
done
chroot "$MNT" /usr/lib/harness/hn --version
chroot "$MNT" su - "$ACCOUNT" -c '$HOME/.local/bin/opencode --version'
-chroot "$MNT" "$PREFIX/bin/labwc" --version
df -h "$MNT" | tail -1
+release_image
diff --git a/os/platforms/orangepi-4pro/image-lib.sh b/os/platforms/orangepi-4pro/image-lib.sh
new file mode 100644
index 000000000..49288ca96
--- /dev/null
+++ b/os/platforms/orangepi-4pro/image-lib.sh
@@ -0,0 +1,62 @@
+# Shared by build-base.sh and build-image.sh: attach an SD image, mount its root partition
+# and prepare a chroot with network. Source it; it sets MNT and an EXIT trap that undoes it.
+
+MNT=$(mktemp -d)
+LOOP=
+MADE_NODE=
+
+release_image() {
+ set +e
+ if [[ -e $MNT/usr/sbin/policy-rc.d.harness ]]; then
+ rm -f "$MNT/usr/sbin/policy-rc.d" "$MNT/usr/sbin/policy-rc.d.harness"
+ fi
+ if [[ -n $RESOLV ]]; then
+ rm -f "$MNT/etc/resolv.conf"
+ if [[ -e $MNT/etc/resolv.conf.harness || -L $MNT/etc/resolv.conf.harness ]]; then
+ mv "$MNT/etc/resolv.conf.harness" "$MNT/etc/resolv.conf"
+ fi
+ fi
+ for m in dev/pts dev proc sys; do mountpoint -q "$MNT/$m" && umount "$MNT/$m"; done
+ mountpoint -q "$MNT" && umount "$MNT"
+ [[ -n $LOOP ]] && losetup -d "$LOOP"
+ [[ -n $MADE_NODE ]] && rm -f "$MADE_NODE"
+ rmdir "$MNT" 2>/dev/null
+ LOOP=
+ MADE_NODE=
+ RESOLV=
+}
+RESOLV=
+trap release_image EXIT
+
+# attach_image IMAGE [GROW]: GROW (e.g. +3G) enlarges the file and its root filesystem first.
+attach_image() {
+ local image=$1 grow=${2:-} part
+ [[ -z $grow ]] || truncate -s "$grow" "$image"
+ LOOP=$(losetup -fP --show "$image")
+ [[ -z $grow ]] || parted -s "$LOOP" resizepart 1 100%
+ partprobe "$LOOP" || true
+ part=${LOOP}p1
+ if [[ ! -b $part ]]; then
+ # A container (build-docker.sh) has no udev to create partition nodes.
+ local major minor
+ IFS=: read -r major minor < "/sys/class/block/${LOOP#/dev/}p1/dev"
+ mknod "$part" b "$major" "$minor"
+ MADE_NODE=$part
+ fi
+ if [[ -n $grow ]]; then
+ e2fsck -fy "$part" >/dev/null || true
+ resize2fs "$part" >/dev/null
+ fi
+ mount "$part" "$MNT"
+ for m in dev dev/pts proc sys; do mount --bind "/$m" "$MNT/$m"; done
+ # Network for the chroot, and no service starts from package scripts; both undone on release.
+ if [[ -e $MNT/etc/resolv.conf || -L $MNT/etc/resolv.conf ]]; then
+ mv "$MNT/etc/resolv.conf" "$MNT/etc/resolv.conf.harness"
+ fi
+ RESOLV=1
+ cp -L /etc/resolv.conf "$MNT/etc/resolv.conf"
+ [[ ! -e $MNT/usr/sbin/policy-rc.d ]] || { echo 'The image already has a policy-rc.d.' >&2; return 1; }
+ printf '#!/bin/sh\nexit 101\n' > "$MNT/usr/sbin/policy-rc.d"
+ chmod 755 "$MNT/usr/sbin/policy-rc.d"
+ touch "$MNT/usr/sbin/policy-rc.d.harness"
+}