Addressing Greptile P1s on wrolpi-usb.sh.
Some checks failed
CodeQL / Analyze (push) Has been cancelled
Controller Tests / controller-tests (push) Has been cancelled
Jest Tests / jest-tests (push) Has been cancelled

numfmt with a negative argument:
  Under `set -euo pipefail` the upgrade subcommand would die at
  `numfmt --to=iec ... $headroom_bytes` whenever the new ISO was larger
  than the headroom — numfmt treats the leading `-` as an option flag,
  exits non-zero, and the script aborts before printing the descriptive
  back-up-and-reflash error message just below.  Splitting positive
  and negative cases and feeding numfmt the absolute value fixes it;
  the eventual error message now actually shows up.

Silent umount during sanity check:
  `umount "$child" 2>/dev/null || true` was swallowing both stderr and
  the exit code.  A busy partition (open file handle from a daemon
  that auto-opened the USB) would survive the loop, and `cmd_upgrade`
  would proceed straight to `dd if=<iso> of=<dev>` — writing to a
  block device whose partition is still mounted, corrupting the
  filesystem on that partition.  Adds a post-loop assertion: re-read
  MOUNTPOINTS and die with an `lsof` hint if anything is still mounted.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Roland 2026-05-25 20:15:12 -06:00 • committed by lrnselfreliance
parent 662306a458
commit 9edfb0de25

View file

@ -81,6 +81,17 @@ sanity_check_device() {
for child in $(lsblk -lnpo NAME "$dev" | tail -n +2); do
umount "$child" 2>/dev/null || true
done
# Re-check: a busy partition (open file handle, daemon, etc.) will
# have swallowed our umount silently. dd-ing a device whose
# partition is still mounted corrupts the live filesystem. Bail
# loudly before any destructive operation.
mounted=$(lsblk -no MOUNTPOINTS "$dev" 2>/dev/null | grep -v '^$' || true)
if [ -n "$mounted" ]; then
info "Still mounted after umount attempt:"
echo "$mounted" | sed 's/^/ /'
die "could not unmount $dev (a partition is in use). Close any " \
"open file handles or run `sudo lsof $dev*` to find them, then retry."
fi
fi
}
@ -130,11 +141,19 @@ cmd_upgrade() {
local iso_size; iso_size=$(stat -c %s "$iso")
local headroom_bytes=$((persist_start * 512 - iso_size))
info "ISO: $iso ($(numfmt --to=iec --suffix=B $iso_size))"
info "ISO: $iso ($(numfmt --to=iec --suffix=B "$iso_size"))"
info "Target device: $dev ($(lsblk -no MODEL,SIZE "$dev" | head -1))"
info "Persistence: $persist_part starts at sector $persist_start ($(numfmt --to=iec --suffix=B $((persist_start * 512))))"
info "Headroom for ISO: $(numfmt --to=iec --suffix=B $((persist_start * 512)))"
info "Headroom vs ISO: $(numfmt --to=iec --suffix=B $headroom_bytes) free"
info "Persistence: $persist_part starts at sector $persist_start ($(numfmt --to=iec --suffix=B "$((persist_start * 512))"))"
info "Headroom for ISO: $(numfmt --to=iec --suffix=B "$((persist_start * 512))")"
# numfmt treats a leading dash as an option flag. Format the absolute
# value and prefix the sign manually so a negative headroom doesn't
# crash the script under `set -euo pipefail` before the descriptive
# error message below has a chance to print.
if [ "$headroom_bytes" -ge 0 ]; then
info "Headroom vs ISO: $(numfmt --to=iec --suffix=B "$headroom_bytes") free"
else
info "Headroom vs ISO: -$(numfmt --to=iec --suffix=B "$(( -headroom_bytes ))") (ISO is too large)"
fi
if [ "$headroom_bytes" -lt 0 ]; then
cat >&2 <<EOF