Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/dev/appliance-wizard.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,16 @@ forks first thing and does two steps:
appliance-mode setup the Both role runs, from the same tree on `/data`, against the same
prebuilt XMRig the image baked. Nothing on this path clones or compiles; a Tor-only box could
not. A native rebuild is the operator's option, cached on `/data`, never a requirement.

When the baked tree carries a RigForge Rust binary seed (`prebuilt/rust/rigforge` plus its
`rigforge.sha256` record), `pithead-sync` installs it on every boot, but only after
the hash of the seed file equals the one recorded for `rigforge` in `rigforge.sha256`. It installs mode 0755 root:root into `/data/rigforge-bin/`
(`PITHEAD_SYNC_RIGFORGE_BIN` overrides the path), outside the synced tree, through a temporary
file, re-hashed before a rename. A symlinked seed, record or install directory, or a directory at the install path, is refused. The binary is reinstalled when its mode or owner drifts. A seed whose checksum fails, or whose record is missing, prints one
`REFUSED rigforge binary` line and leaves the installed binary alone; the rest of the sync
still completes. A seed with a different valid checksum replaces the installed binary, which is
how a pin move delivers its binary on the new slot's first boot. Without a seed the sync is
unchanged (pithead#4212, rigforge#709).
2. **The same health-gated slot commit**, on the miner running. The gate is deliberately
*pool-independent*: a rig whose coordinator is late still starts, still retries, and still
commits. Rolling a slot back cannot fix a switch nobody plugged in, and a gate that punished
Expand Down
37 changes: 30 additions & 7 deletions os/overlay/pithead-sync
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SRC="${PITHEAD_SYNC_SRC:-/opt/pithead}"
DST="${PITHEAD_SYNC_DST:-/data/pithead}"
RF_SRC="${PITHEAD_SYNC_RIGFORGE_SRC:-/opt/rigforge}"
RF_DST="${PITHEAD_SYNC_RIGFORGE_DST:-/data/rigforge}"
RF_BIN="${PITHEAD_SYNC_RIGFORGE_BIN:-/data/rigforge-bin/rigforge}"

mkdir -p "$DST"
for f in pithead pithead-completion.bash VERSION docker-compose.yml \
Expand Down Expand Up @@ -34,22 +35,15 @@ fi
# An images/ copy from an older layout is dead weight on the data partition — reclaim it.
rm -rf "${DST:?}/images"

# The RigForge leg: the image bakes a pinned RigForge tree, and the miner must run from /data —
# RigForge roots its workspace under its own script dir (DYNAMIC_HOME), so tree placement alone
# puts the XMRig build on writable storage. Program files are replaced wholesale like pithead's;
# state the baked tree never carries (config.json — pithead-rendered — and the data/ workspace)
# is untouched by construction, because only names present in the source are replaced.
if [ -d "$RF_SRC" ]; then
mkdir -p "$RF_DST"
for f in "$RF_SRC"/* "$RF_SRC"/.[!.]*; do
[ -e "$f" ] || continue
base=$(basename "$f")
# prebuilt/ is a seed, not a program file — handled below, never synced as a tree.
[ "$base" = "prebuilt" ] && continue
rm -rf "${RF_DST:?}/$base"
cp -a "$f" "$RF_DST/$base"
done
# Reseed when the commit or donate-min marker differs, since a Tor-only box cannot recompile.
if [ -d "$RF_SRC/prebuilt/xmrig" ]; then
baked_commit=$(cat "$RF_SRC/prebuilt/xmrig/.rigforge-commit" 2>/dev/null || true)
have_commit=$(cat "$RF_DST/data/worker/xmrig/.rigforge-commit" 2>/dev/null || true)
Expand All @@ -61,6 +55,35 @@ if [ -d "$RF_SRC" ]; then
cp -a "$RF_SRC/prebuilt/xmrig" "$RF_DST/data/worker/xmrig"
fi
fi
rf_seed="$RF_SRC/prebuilt/rust"
if [ -e "$rf_seed/rigforge" ] || [ -L "$rf_seed/rigforge" ]; then
rf_want=""
if [ -f "$rf_seed/rigforge" ] && [ ! -L "$rf_seed/rigforge" ] && [ -f "$rf_seed/rigforge.sha256" ] && [ ! -L "$rf_seed/rigforge.sha256" ]; then
rf_want=$(awk 'NF == 2 && $2 == "rigforge" && $1 ~ /^[0-9a-f]{64}$/ { print $1 }' "$rf_seed/rigforge.sha256")
fi
rf_have=$(sha256sum <"$rf_seed/rigforge" 2>/dev/null | cut -d' ' -f1 || true)
if [ -n "$rf_want" ] && [ "$rf_want" = "$rf_have" ]; then
rf_bin_dir=$(dirname "$RF_BIN")
if [ -d "$RF_BIN" ] || [ -L "$rf_bin_dir" ]; then
echo "pithead-sync: REFUSED rigforge binary: install location is not a plain path; keeping the installed version" >&2
elif ! cmp -s "$rf_seed/rigforge" "$RF_BIN" || [ "$(stat -c %a:%u "$RF_BIN" 2>/dev/null)" != "755:$(id -u)" ]; then
rf_owner=()
[ "$(id -u)" = 0 ] && rf_owner=(-o root -g root)
rf_tmp="$RF_BIN.new.$$"
if mkdir -p "$rf_bin_dir" && chmod 755 "$rf_bin_dir" &&
{ [ "$(id -u)" != 0 ] || chown root:root "$rf_bin_dir"; } &&
install -m 755 "${rf_owner[@]}" "$rf_seed/rigforge" "$rf_tmp" &&
[ "$(sha256sum <"$rf_tmp" | cut -d' ' -f1)" = "$rf_want" ] && mv -fT "$rf_tmp" "$RF_BIN"; then
echo "pithead-sync: rigforge binary installed at $RF_BIN"
else
rm -f "$rf_tmp"
echo "pithead-sync: REFUSED rigforge binary: install failed; keeping the installed version" >&2
fi
fi
else
echo "pithead-sync: REFUSED rigforge binary: checksum mismatch or missing record; keeping the installed version" >&2
fi
fi
echo "pithead-sync: rigforge tree synced to $RF_DST (state preserved)"
fi
# repart formats /data with ext4's default 5% root reserve — ~50 GB lost on a 1 TB disk that
Expand Down
90 changes: 90 additions & 0 deletions tests/stack/appliance/test-appliance-baked-images.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,93 @@ done
rm -rf "$SLB"
unset SLB appliance
unset -f slb_probe

echo "== unit: pithead-sync installs the RigForge Rust binary only on checksum pass (#4532) =="
RBSCRIPT="$ROOT/os/overlay/pithead-sync"
mk_tmpdir RBS
mkdir -p "$RBS/opt-pithead" "$RBS/opt-rigforge/prebuilt/rust"
rb_seed() {
printf '%s' "$1" >"$RBS/opt-rigforge/prebuilt/rust/rigforge"
(cd "$RBS/opt-rigforge/prebuilt/rust" && sha256sum rigforge >rigforge.sha256)
}
rb_sync() {
PITHEAD_SYNC_SRC="$RBS/opt-pithead" PITHEAD_SYNC_DST="$RBS/data/pithead" \
PITHEAD_SYNC_RIGFORGE_SRC="$RBS/opt-rigforge" PITHEAD_SYNC_RIGFORGE_DST="$RBS/data/rigforge" \
PITHEAD_SYNC_RIGFORGE_BIN="$RBS/data/rigforge-bin/rigforge" \
bash "$RBSCRIPT" 2>&1
}
RB_BIN="$RBS/data/rigforge-bin/rigforge"
printf 'program' >"$RBS/opt-rigforge/rigforge.sh"

out=$(rb_sync)
assert_rc "seed absent: sync runs clean" "$?" "0"
if [ -e "$RB_BIN" ]; then bad "seed absent: nothing installed" "present"; else ok "seed absent: nothing installed"; fi
assert_not_contains "seed absent: no refusal line" "$out" "REFUSED"

rb_seed 'rust-v1'
out=$(rb_sync)
assert_rc "good seed: sync runs clean" "$?" "0"
assert_eq "good seed: binary installed" "$(cat "$RB_BIN" 2>/dev/null)" "rust-v1"
assert_eq "good seed: mode 0755" "$(stat -c %a "$RB_BIN" 2>/dev/null)" "755"
assert_eq "good seed: directory not writable by others" "$(stat -c %a "$(dirname "$RB_BIN")")" "755"
if [ -e "$RBS/data/rigforge/prebuilt" ]; then bad "seed is never synced into the tree" "synced"; else ok "seed is never synced into the tree"; fi
assert_contains "good seed: install is announced" "$out" "rigforge binary installed"

printf 'tampered' >"$RBS/opt-rigforge/prebuilt/rust/rigforge"
out=$(rb_sync)
assert_rc "tampered seed: sync still completes" "$?" "0"
assert_eq "tampered seed: previous binary kept" "$(cat "$RB_BIN")" "rust-v1"
assert_contains "tampered seed: one loud refusal line" "$out" "REFUSED rigforge binary: checksum mismatch"
assert_eq "tampered seed: exactly one refusal line" "$(printf '%s\n' "$out" | grep -c REFUSED)" "1"
assert_eq "tampered seed: the tree sync still ran" "$(cat "$RBS/data/rigforge/rigforge.sh")" "program"
assert_contains "tampered seed: sync reports completion" "$out" "program tree synced"

rm -f "$RBS/opt-rigforge/prebuilt/rust/rigforge.sha256"
out=$(rb_sync)
assert_contains "seed without a checksum record is refused" "$out" "REFUSED rigforge binary"
assert_eq "seed without a checksum record: previous binary kept" "$(cat "$RB_BIN")" "rust-v1"

rb_seed 'rust-v2'
out=$(rb_sync)
assert_eq "replaced seed with a valid checksum installs the new version" "$(cat "$RB_BIN")" "rust-v2"
assert_not_contains "replaced seed: no refusal" "$out" "REFUSED"

out=$(rb_sync)
assert_not_contains "unchanged seed: no reinstall" "$out" "installed at"
assert_eq "unchanged seed: binary intact" "$(cat "$RB_BIN")" "rust-v2"
chmod 777 "$RB_BIN"
out=$(rb_sync)
assert_eq "mode drift on an unchanged binary is repaired" "$(stat -c %a "$RB_BIN")" "755"
assert_contains "mode drift: reinstall announced" "$out" "installed at"

printf '%s other\n' "$(sha256sum <"$RBS/opt-rigforge/prebuilt/rust/rigforge" | cut -d' ' -f1)" >"$RBS/opt-rigforge/prebuilt/rust/rigforge.sha256"
printf 'evil' >"$RBS/opt-rigforge/prebuilt/rust/rigforge"
out=$(rb_sync)
assert_contains "a record naming another file is refused" "$out" "REFUSED rigforge binary: checksum mismatch"
assert_eq "a record naming another file: previous binary kept" "$(cat "$RB_BIN")" "rust-v2"

rb_seed 'rust-v3'
mv "$RBS/opt-rigforge/prebuilt/rust/rigforge" "$RBS/real-seed"
ln -s "$RBS/real-seed" "$RBS/opt-rigforge/prebuilt/rust/rigforge"
out=$(rb_sync)
assert_contains "a symlinked seed is refused" "$out" "REFUSED rigforge binary"
assert_eq "a symlinked seed: previous binary kept" "$(cat "$RB_BIN")" "rust-v2"
rm -f "$RBS/opt-rigforge/prebuilt/rust/rigforge"

rb_seed 'rust-v4'
mv "$RBS/data/rigforge-bin" "$RBS/real-bin"
ln -s "$RBS/real-bin" "$RBS/data/rigforge-bin"
out=$(rb_sync)
assert_contains "a symlinked install directory is refused" "$out" "REFUSED rigforge binary"
assert_eq "a symlinked install directory: nothing written through it" "$(cat "$RBS/real-bin/rigforge")" "rust-v2"
rm -f "$RBS/data/rigforge-bin"
mv "$RBS/real-bin" "$RBS/data/rigforge-bin"

rm -f "$RB_BIN"
mkdir "$RB_BIN"
out=$(rb_sync)
assert_contains "a directory at the install path is refused" "$out" "REFUSED rigforge binary"
if [ -z "$(ls -A "$RB_BIN")" ]; then ok "a directory at the install path stays empty"; else bad "a directory at the install path stays empty" "written into"; fi
rm -rf "$RBS"
unset -f rb_seed rb_sync
unset RBS RBSCRIPT RB_BIN out
Loading