From 6862f21598b361084075ea029851818c3fcd47fe Mon Sep 17 00:00:00 2001 From: Roland Date: Fri, 22 May 2026 22:17:09 -0600 Subject: [PATCH] Identify new persistence partition by start sector, not lsblk's tail. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The fallback after `partx -a` (which is wrapped in `|| true` and can silently no-op) was `lsblk -lnpo NAME "$BOOT_DRIVE" | tail -1`. If the new partition node has not appeared in the kernel's partition table yet, that fallback returns the previous last partition — typically sdb2 (the ESP) or sdb1 (the live ISO data partition). Both are valid block devices, so the `[ -b ... ]` guard passes; the next line is `mkfs.ext4 -L persistence -F`, which wipes whichever wrong partition the fallback returned. Narrow conditions, but catastrophic when triggered. Switching to matching on the start sector we just assigned via sfdisk: no existing partition can share it, so the result is unambiguous. Retry loop allows up to 5s for udev to register the new node. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../local/sbin/wrolpi-portable-bootstrap.sh | 24 ++++++++++++++----- 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/wrolpi-portable/config/includes.chroot/usr/local/sbin/wrolpi-portable-bootstrap.sh b/wrolpi-portable/config/includes.chroot/usr/local/sbin/wrolpi-portable-bootstrap.sh index 619af80b..79052f27 100644 --- a/wrolpi-portable/config/includes.chroot/usr/local/sbin/wrolpi-portable-bootstrap.sh +++ b/wrolpi-portable/config/includes.chroot/usr/local/sbin/wrolpi-portable-bootstrap.sh @@ -74,12 +74,24 @@ if [ -z "$PERSIST_PART" ]; then # a re-read of the whole partition table. echo "start=$START, type=83" | sfdisk --append --no-reread --force "$BOOT_DRIVE" partx -a "$BOOT_DRIVE" 2>/dev/null || true - udevadm settle || true - PERSIST_PART=$(blkid -L persistence 2>/dev/null || true) - if [ -z "$PERSIST_PART" ]; then - PERSIST_PART=$(lsblk -lnpo NAME "$BOOT_DRIVE" | tail -1) - fi - if ! [ -b "$PERSIST_PART" ]; then + + # Identify the new partition by the start sector we just assigned to it. + # We deliberately avoid `lsblk | tail -1` here: if partx -a or udev have + # not registered the new node yet, that fallback would return the + # existing last partition (typically the ESP or the live data partition) + # — and the mkfs.ext4 -F below would wipe it. Matching on start sector + # is unambiguous: no existing partition can share it. Retry up to 5s + # for udev to settle and the partition node to appear. + PERSIST_PART="" + for _i in 1 2 3 4 5; do + udevadm settle || true + PERSIST_PART=$(lsblk -lnpo NAME,START "$BOOT_DRIVE" 2>/dev/null \ + | awk -v s="$START" '$2 == s {print $1; exit}') + [ -n "$PERSIST_PART" ] && [ -b "$PERSIST_PART" ] && break + PERSIST_PART="" + sleep 1 + done + if [ -z "$PERSIST_PART" ] || ! [ -b "$PERSIST_PART" ]; then log "ERROR: failed to locate the newly created persistence partition" exit 1 fi