diff --git a/os/mini/prepare-stick.sh b/os/mini/prepare-stick.sh index 37b42c5d3..c3dafb5f3 100755 --- a/os/mini/prepare-stick.sh +++ b/os/mini/prepare-stick.sh @@ -227,10 +227,29 @@ printf 'node %s\n' "$PART" # ext4 grows in place; the filesystem is not mounted yet, so this is an offline resize - the quick path. if [ "$GREW" = 1 ]; then command -v resize2fs >/dev/null 2>&1 || die "resize2fs is missing; the data partition grew but its filesystem did not" - resize2fs "$PART" >/dev/null 2>&1 || die "resize2fs failed on $PART; the partition grew but the filesystem did not" - printf 'grown %s now holds %.1f GiB of ext4 (was %.1f GiB)\n' "$PART" \ - "$(awk -v s="$SIZE" 'BEGIN { printf "%.1f", s * 512 / 1073741824 }')" \ - "$(awk -v s="$SIZE0" 'BEGIN { printf "%.1f", s * 512 / 1073741824 }')" + # engine#1374: an ext4 that needs journal recovery makes resize2fs refuse, and the operator's workaround on c32 was exactly + # `e2fsck -f` then resize2fs - the tool should do that itself rather than hand it back as a failed stage. The partition is not + # mounted here, so the fsck is offline and safe. + if ! resize2fs "$PART" >/dev/null 2>&1; then + printf 'grow resize2fs refused %s; fscking it offline and retrying once (engine#1374)\n' "$PART" + command -v e2fsck >/dev/null 2>&1 || die "resize2fs failed on $PART and e2fsck is missing; the partition grew but the filesystem did not" + e2fsck -f -y "$PART" >/dev/null 2>&1 || die "e2fsck failed on $PART; the partition grew but the filesystem did not" + resize2fs "$PART" >/dev/null 2>&1 || die "resize2fs failed on $PART even after an offline fsck; the partition grew but the filesystem did not" + fi + # the SIZE in the partition table is the PARTITION's; what a person needs to know is what the FILESYSTEM can hold, which is + # the number whose absence made the c32 failure confusing (58.1 GiB partition, 7.8 GiB fs, and a message that claimed both) + fsblocks=$(dumpe2fs -h "$PART" 2>/dev/null | sed -n 's/^Block count:[[:space:]]*//p' | head -1) + fsbytes=$(dumpe2fs -h "$PART" 2>/dev/null | sed -n 's/^Block size:[[:space:]]*//p' | head -1) + if [ -n "${fsblocks:-}" ] && [ -n "${fsbytes:-}" ]; then + printf 'grown %s filesystem now holds %.1f GiB of ext4 (was %.1f GiB); partition %.1f GiB\n' "$PART" \ + "$(awk -v b="$fsblocks" -v s="$fsbytes" 'BEGIN { print b * s / 1073741824 }')" \ + "$(awk -v s="$SIZE0" 'BEGIN { print s * 512 / 1073741824 }')" \ + "$(awk -v s="$SIZE" 'BEGIN { print s * 512 / 1073741824 }')" + else + printf 'grown %s partition now holds %.1f GiB (was %.1f GiB); the filesystem size could not be read\n' "$PART" \ + "$(awk -v s="$SIZE" 'BEGIN { print s * 512 / 1073741824 }')" \ + "$(awk -v s="$SIZE0" 'BEGIN { print s * 512 / 1073741824 }')" + fi fi # ---- mount --------------------------------------------------------------------------------------- @@ -311,12 +330,41 @@ if [ "$CHECK" = 0 ]; then esac write_key "$KEY" fi + # engine#1374: refuse BEFORE writing if the staging will not fit. Without this the first sign of a data partition that did not + # grow (or whose filesystem did not resize) is a `cp` that fails part way - and the failure leaves a TRUNCATED model file + # behind, which `du -sb` on a partially merged tree does not reliably show. Measured on c32: 6.6 of 7.9 GB landed and + # sd_turbo.safetensors was truncated; only a byte comparison against the source found it. 5 % headroom covers ext4's + # metadata for many small files; the number is a refusal threshold, not a reservation. + want=0 + for m in $MODELS; do + [ -s "$m" ] || die "model $m is empty or missing" + sz=$(stat -Lc %s "$m" 2>/dev/null || echo 0) + want=$((want + sz)) + done + for d in $NPUMODELS; do + sz=$(du -sb "$d" 2>/dev/null | cut -f1) + want=$((want + ${sz:-0})) + done + if [ "$want" -gt 0 ]; then + avail=$(df -B1 --output=avail "$MNT" 2>/dev/null | awk 'NR==2 {print $1}') + need=$((want + want / 20)) + if [ -z "${avail:-}" ] || [ "$avail" -lt "$need" ]; then + die "the staging needs $((need / 1048576)) MiB (models + 5 % headroom) but $PART has $(( ${avail:-0} / 1048576 )) MiB free: the data partition or its filesystem did not grow. Refusing rather than truncating a model file (engine#1374)" + fi + printf 'fit staging needs %s MiB, %s MiB free on %s\n' "$((need / 1048576))" "$((avail / 1048576))" "$PART" + fi for m in $MODELS; do [ -s "$m" ] || die "model $m is empty or missing" [ -d "$MNT/models" ] || mkdir -p "$MNT/models" cp -f "$m" "$MNT/models/$(basename "$m")" || die "could not copy $m into $MNT/models" chown 0:0 "$MNT/models" "$MNT/models/$(basename "$m")" 2>/dev/null - printf 'model models/%s (%s bytes)\n' "$(basename "$m")" "$(stat -Lc %s "$m")" + # engine#1374: prove the staged bytes, do not assume them. `cp` returning 0 is not evidence that what is on the medium is + # what was asked for - measured on main: a 20 GiB model was "prepared and proved" onto an 8 GiB partition with zero blocks + # written, and neither the exit code, `df`, nor the directory entry could tell. cmp is byte-exact and reads neither twice + # more than the copy did. + cmp -s "$m" "$MNT/models/$(basename "$m")" || + die "the staged models/$(basename "$m") is not byte-identical to $m (a short write, a truncated copy, or the source changed while staging)" + printf 'model models/%s (%s bytes, byte-identical to the source)\n' "$(basename "$m")" "$(stat -Lc %s "$m")" done for d in $NPUMODELS; do name=${d%/}; name=${name##*/} @@ -326,6 +374,10 @@ if [ "$CHECK" = 0 ]; then # -L: on a build box the model directory is symlinks into the FLM install; the stick needs the bytes cp -fRL "$d"/. "$MNT/models/$name"/ || die "could not copy the NPU model $d into $MNT/models/$name" chown -R 0:0 "$MNT/models/$name" 2>/dev/null + # the same proof for a directory of files: every staged file byte-identical to its source (diff -r compares contents and + # reports a missing or extra file as a difference too) + diff -r -q "$d" "$MNT/models/$name" > /dev/null 2>&1 || + die "the staged models/$name is not byte-identical to $d (run: diff -r $d $MNT/models/$name)" printf 'npu models/%s (%s files, %s bytes)\n' "$name" \ "$(find "$MNT/models/$name" -type f | wc -l)" "$(du -sb "$MNT/models/$name" | cut -f1)" done diff --git a/os/v3/puppy/1bit-update-selftest.sh b/os/v3/puppy/1bit-update-selftest.sh new file mode 100755 index 000000000..a391c779a --- /dev/null +++ b/os/v3/puppy/1bit-update-selftest.sh @@ -0,0 +1,165 @@ +#!/bin/sh +# Copyright 2026 bong-water-water-bong +# SPDX-License-Identifier: Apache-2.0 +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# +# 1bit-update-selftest.sh - the second-update logic of 1bit-update, on fake /data trees, with no VM and no disk. +# +# engine#1376: `1bit-update` let an installed machine take ONE update. The second was refused on the machine's own save +# layer, because after update 1 the machine runs the alt slot while the layer still lives in /1bit - so the second update's +# target IS the slot holding the real layer, and the "a slot that already owns a save layer is another machine's state" +# guard walked into it. `update-gate.sh` cannot see this: its boots are update -> boot alt -> rollback -> boot original, so it +# never runs a second update. +# +# The four cases, each on a tree built to be the shape the real machine has at that moment: +# +# A one layer, linked into the other slot (what update 1 leaves behind) -> must PROCEED: the layer found in the target +# slot is the same file as this machine's, so it is kept, the layer is not moved, and NO self-referencing link is created +# (the latent defect #1334's review found: linking a layer into the slot it really lives in) +# B a genuinely different layer in the target slot -> must REFUSE, naming which is the machine's own +# C a foreign real layer in the OTHER slot, the machine's own in the active -> the scan must pick the ACTIVE slot's layer, +# not the last real directory it happens to see +# D no layer at all -> must proceed (nothing to keep, nothing to link) +# +# The three blocks under test are lifted out of the script itself, between the section markers it already carries, so this +# tests the shipped code rather than a copy of it. If the markers ever move, the extraction is empty and the selftest fails +# loudly (case 0) instead of silently passing nothing. +# +# Usage: 1bit-update-selftest.sh [path to 1bit-update] Exit: 0 all cases pass, 1 a case failed, 2 could not run. +set -u + +here=$(cd "$(dirname "$0")" && pwd) +script=${1:-$here/embed/usr/local/bin/1bit-update} +[ -f "$script" ] || { echo "1bit-update-selftest: no such script: $script" >&2; exit 2; } +command -v mktemp > /dev/null 2>&1 || { echo "1bit-update-selftest: mktemp is missing" >&2; exit 2; } + +work=$(mktemp -d "${TMPDIR:-/tmp}/1bu-selftest.XXXXXX") || exit 2 +trap 'chmod -R u+w "$work" 2> /dev/null; rm -rf "$work"' EXIT + +# ── lift the three blocks out of the script under test ──────────────────────────────────────────────────────────────── +blocks=$work/blocks.sh +awk ' + # two contiguous blocks, each ending at the same marker: the scan, then the guard + write + link. Fragment-based + # extraction (stopping at a line that appears twice) cut a for-loop mid-body and produced a syntax error - the failure + # was loud, but it wasted a run; contiguous ranges cannot do that. + /^save_slot=$/ { scan = 1 } + scan && /^# ── GRUB/ { scan = 0 } + scan { print } + + /^# a slot that already owns a save layer/ { act = 1 } + act && /^# ── GRUB/ { act = 0; print "" } + act { print } +' "$script" > "$blocks" + +fail=0 ok=0 +check() { # [grep for a line that must appear] + want=$1 name=$2 data=$3 active=$4 target=$5 say_grep=${6:-} + out=$(env -i PATH=/usr/bin:/bin DATA="$data" active="$active" target="$target" ALT=1bit-alt \ + LOG="$work/log" BLOCKS="$blocks" SELFTEST_SRC="$SELFTEST_SRC" sh "$work/harness.sh" 2>&1) + rc=$? + case $want in + ok) + if [ "$rc" = 0 ] && { [ -z "$say_grep" ] || printf '%s' "$out" | grep -q "$say_grep"; }; then + printf 'PASS %s\n' "$name"; ok=$((ok + 1)) + else + printf 'FAIL %s: expected success%s, got rc=%s\n%s\n' "$name" "${say_grep:+ and '$say_grep'}" "$rc" "$out" + fail=$((fail + 1)) + fi + ;; + die) + if [ "$rc" = 2 ] && printf '%s' "$out" | grep -q 'DIE(rc 2)'; then + printf 'PASS %s\n' "$name"; ok=$((ok + 1)) + else + printf 'FAIL %s: expected the refusal (rc 2), got rc=%s\n%s\n' "$name" "$rc" "$out" + fail=$((fail + 1)) + fi + ;; + esac + [ -f "$work/log" ] && : > "$work/log" +} + +# the harness the blocks run inside: the two commands they call, and a source tree with one file to copy +cat > "$work/harness.sh" <<'H' +say() { printf 'say: %s\n' "$*"; } +die() { printf 'DIE(rc 2): %s\n' "$*" >&2; exit 2; } +fail() { printf 'FAIL(rc 3): %s\n' "$*" >&2; exit 3; } +src=$SELFTEST_SRC +need="vmlinuz" +. "$BLOCKS" +H +SELFTEST_SRC=$work/src +export SELFTEST_SRC +mkdir -p "$SELFTEST_SRC" +# NOT empty: the script's own pre-check is `[ -s "$src/$f" ]` (size > 0), so an empty fixture fails a check that has +# nothing to do with the case under test - measured, that is how this line was found +printf 'vmlinuz fixture\n' > "$SELFTEST_SRC/vmlinuz" + +mk() { # + d=$1 + rm -rf "$d" + mkdir -p "$d/1bit" "$d/1bit-alt" + case $2 in + linked) mkdir -p "$d/1bit/upup64rsave"; ln -s ../1bit/upup64rsave "$d/1bit-alt/upup64rsave" ;; + foreign) mkdir -p "$d/1bit/foreignsave"; ln -s ../1bit/foreignsave "$d/1bit-alt/foreignsave" ;; + two) mkdir -p "$d/1bit/foreignsave" "$d/1bit-alt/mysave" ;; + none) : ;; + esac + mkdir -p "$d/1bit" +} + +# ── case 0: the extraction itself ─────────────────────────────────────────────────────────────────────────────────── +if [ -s "$blocks" ] && grep -q 'savename=' "$blocks" && grep -q 'ln -sfn' "$blocks"; then + printf 'PASS %s\n' 'the three blocks were found in the script under test'; ok=$((ok + 1)) +else + printf 'FAIL %s: the marker-based extraction found nothing in %s - the selftest would pass nothing\n' 'extraction' "$script" + fail=$((fail + 1)) +fi + +# ── A: the shape update 1 leaves behind - must proceed, keep the layer, create no self-link ───────────────────────── +mk "$work/a" linked +check ok 'A: second update proceeds on one linked layer' "$work/a" 1bit-alt 1bit 'own layer' +if [ -e "$work/a/1bit/upup64rsave/upup64rsave" ]; then + printf 'FAIL %s: a self-referencing upup64rsave/upup64rsave was created inside the layer\n' 'A: no self-link' + fail=$((fail + 1)) +else + printf 'PASS %s\n' 'A: no self-referencing link inside the layer'; ok=$((ok + 1)) +fi +if [ -f "$work/a/1bit/upup64rsave/.keep" ] || [ -d "$work/a/1bit/upup64rsave" ]; then + printf 'PASS %s\n' 'A: the layer is still where it lives'; ok=$((ok + 1)) +else + printf 'FAIL %s\n' 'A: the layer directory is gone'; fail=$((fail + 1)) +fi + +# ── B: a genuinely different layer in the target slot - must refuse ────────────────────────────────────────────── +mk "$work/b" two +check die 'B: a foreign layer in the target slot is refused' "$work/b" 1bit-alt 1bit + +# ── C: a foreign real layer in the other slot - the scan must pick the ACTIVE slot's layer ──────────────────────── +mk "$work/c" two +out=$(env -i PATH=/usr/bin:/bin DATA="$work/c" active=1bit-alt target=1bit ALT=1bit-alt LOG="$work/log" \ + BLOCKS="$blocks" SELFTEST_SRC="$SELFTEST_SRC" sh "$work/harness.sh" 2>&1) +if printf '%s' "$out" | grep -q 'not this machine.s (1bit-alt/mysave)'; then + printf 'PASS %s\n' "C: the scan picked the active slot's layer, not the foreign one"; ok=$((ok + 1)) +else + printf 'FAIL %s\n%s\n' 'C: the scan did not name 1bit-alt/mysave as the machine layer' "$out" + fail=$((fail + 1)) +fi + +# ── D: no layer at all - must proceed ──────────────────────────────────────────────────────────────────────────── +mk "$work/d" none +check ok 'D: an update with no layer anywhere proceeds' "$work/d" 1bit 1bit-alt + +printf '\n1bit-update-selftest: %s passed, %s failed\n' "$ok" "$fail" +[ "$fail" = 0 ] || exit 1 +echo "RESULT: PASS" diff --git a/os/v3/puppy/embed/usr/local/bin/1bit-update b/os/v3/puppy/embed/usr/local/bin/1bit-update index 837c4fa9b..bda7a33b0 100755 --- a/os/v3/puppy/embed/usr/local/bin/1bit-update +++ b/os/v3/puppy/embed/usr/local/bin/1bit-update @@ -95,13 +95,16 @@ done [ -n "$esp" ] || die "no 1BIT-OS partition on $bootdisk: this machine did not boot from an installed disk" save_slot= savename= -for d in "$DATA"/1bit "$DATA"/"$ALT"; do +# engine#1376: the ACTIVE slot's layer is the one this machine is running, so it is found first and wins. Scanning both slots +# and letting the last real directory win meant a foreign layer in the other slot could be adopted as "this machine's layer". +for d in "$DATA/$active" "$DATA/1bit" "$DATA/$ALT"; do [ -d "$d" ] || continue for s in "$d"/*save*; do [ -e "$s" ] || continue # -L: a symlink to the layer counts, but only a REAL directory is where the layer lives - if [ -d "$s" ] && [ ! -L "$s" ]; then save_slot=$(basename "$d"); savename=$(basename "$s"); fi + if [ -d "$s" ] && [ ! -L "$s" ]; then save_slot=$(basename "$d"); savename=$(basename "$s"); break; fi done + [ -n "$savename" ] && break done # ── GRUB: read the config, find a slot's entries, write the default ────────────────────────────────────────── @@ -235,7 +238,11 @@ freek=$(df -Pk "$DATA" | awk 'NR == 2 { print $4 }') for s in "$DATA/$target"/*save*; do [ -e "$s" ] || continue if [ -d "$s" ] && [ ! -L "$s" ]; then - die "/$target already holds a save layer ($(basename "$s")): two layers cannot be merged by an update - pick the slot by hand if that is really what you want" + if [ -n "$savename" ] && [ "$s" -ef "$DATA/$save_slot/$savename" ]; then + say " $(basename "$s") in /$target is this machine's own layer (same file as /$save_slot/$savename): kept" + continue + fi + die "/$target already holds a save layer ($(basename "$s")) that is not this machine's ($save_slot/$savename): two layers cannot be merged by an update - pick the slot by hand if that is really what you want" fi rm -f "$s" done @@ -246,7 +253,18 @@ for f in $need; do cp -f "$src/$f" "$DATA/$target/$f" >> "$LOG" 2>&1 || fail "copying $f into /$target failed" done [ -s "$src/SHA256SUMS" ] && cp -f "$src/SHA256SUMS" "$DATA/$target/SHA256SUMS" 2> /dev/null -ln -sfn "../$save_slot/$savename" "$DATA/$target/$savename" || fail "cannot link the save layer into /$target" +if [ -z "$savename" ]; then + # engine#1376: a machine with no save layer at all (a fresh installed root, or a --check tree) has nothing to link, and + # the old line would have run `ln -sfn "../$save_slot/$savename"` with both empty - i.e. linked the target slot to its own + # parent. Found by this selftest's no-layer case. + say " no save layer on this machine: nothing to link into /$target" +elif [ "$DATA/$target/$savename" -ef "$DATA/$save_slot/$savename" ] 2> /dev/null; then + # already this machine's layer where it belongs (target is the slot the layer really lives in): nothing to link, and + # linking here would point the layer at itself + say " save layer stays where it is (/$save_slot/$savename); /$target needs no link" +else + ln -sfn "../$save_slot/$savename" "$DATA/$target/$savename" || fail "cannot link the save layer into /$target" +fi for f in $need; do cmp -s "$src/$f" "$DATA/$target/$f" || fail "/$target/$f does not match the source after copying" done