From 9edfb0de251a2d4bdaf1bc78d738a659b6d7f06a Mon Sep 17 00:00:00 2001 From: Roland Date: Mon, 25 May 2026 20:15:12 -0600 Subject: [PATCH] Addressing Greptile P1s on wrolpi-usb.sh. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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= of=` — 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) --- scripts/wrolpi-usb.sh | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/scripts/wrolpi-usb.sh b/scripts/wrolpi-usb.sh index d265bb18..810e4f07 100755 --- a/scripts/wrolpi-usb.sh +++ b/scripts/wrolpi-usb.sh @@ -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 <