From d790fed832188d41c1fb5d8506d833d053e4a81c Mon Sep 17 00:00:00 2001 From: Pev Date: Wed, 15 May 2024 15:45:43 +0100 Subject: [PATCH 1/2] Compare mount output using base of dirname (#771) * Compare mount output using base of dirname If you're building from a bind-mounted directory, the build will fail as it will find two entries to unmount, but a single unmount will remove them both causing an error. Adding a space means that the mountpoint will only match with a single mount entry ; the expected path, rather than the pre bind-mount. * Switch to awk instead of mount, grep, cut pipes Retry unmount 5 times and give up, letting the user know that they need to resolve the issue manually --------- Co-authored-by: David Peverley Co-authored-by: Serge Schneider --- export-image/05-finalise/01-run.sh | 2 +- scripts/common | 15 +++++++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/export-image/05-finalise/01-run.sh b/export-image/05-finalise/01-run.sh index 8310e87..771aa7a 100755 --- a/export-image/05-finalise/01-run.sh +++ b/export-image/05-finalise/01-run.sh @@ -83,7 +83,7 @@ cp "$ROOTFS_DIR/etc/rpi-issue" "$INFO_FILE" dpkg -l --root "$ROOTFS_DIR" } >> "$INFO_FILE" -ROOT_DEV="$(mount | grep "${ROOTFS_DIR} " | cut -f1 -d' ')" +ROOT_DEV="$(awk "\$2 == \"${ROOTFS_DIR}\" {print \$1}" /etc/mtab)" unmount "${ROOTFS_DIR}" zerofree "${ROOT_DEV}" diff --git a/scripts/common b/scripts/common index 6150f2b..c305ee2 100644 --- a/scripts/common +++ b/scripts/common @@ -44,12 +44,15 @@ unmount(){ DIR=$1 fi - while mount | grep -q "$DIR"; do - local LOCS - LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r) - for loc in $LOCS; do - umount "$loc" - done + for i in {1..6}; do + if awk "\$2 ~ /^${DIR//\//\\/}/ {print \$2}" /etc/mtab | sort -r | xargs -r umount; then + break + elif [ "$i" -eq 6 ]; then + log "Failed to unmount ${DIR}. Do not try to delete this directory while it contains mountpoints!" + return 1 + fi + log "Retrying ($i/5)..." + sleep 1 done } export -f unmount From fb48183f0d49df3d069f0086263d829efa899c70 Mon Sep 17 00:00:00 2001 From: Tom Dewey <146750643+tdewey-rpi@users.noreply.github.com> Date: Wed, 15 May 2024 15:50:40 +0100 Subject: [PATCH 2/2] Expand trap cleanup function (#773) * build.sh: Unmount intermediates on trap Iterate through image files which might be in use and detach them Avoid silent failures - let the user know whether the build failed * common: update unmount_image Use udevadm settle instead of sleep if possible Use losetup's -j option to find the loop device associated with a give image file * build.sh: update clean-up trap term --------- Co-authored-by: Serge Schneider --- build.sh | 12 +++++++++++- scripts/common | 9 ++++++--- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/build.sh b/build.sh index d094eee..a96f4e4 100755 --- a/build.sh +++ b/build.sh @@ -158,7 +158,17 @@ do done term() { - true; #TODO: Cleanup + if [ "$?" -ne 0 ]; then + log "Build failed" + else + log "Build finished" + fi + unmount "${STAGE_WORK_DIR}" + if [ "$STAGE" = "export-image" ]; then + for img in "${STAGE_WORK_DIR}/"*.img; do + unmount_image "$img" + done + fi } trap term EXIT INT TERM diff --git a/scripts/common b/scripts/common index c305ee2..702576f 100644 --- a/scripts/common +++ b/scripts/common @@ -58,9 +58,12 @@ unmount(){ export -f unmount unmount_image(){ - sync - sleep 1 - LOOP_DEVICE=$(losetup --list | grep "$1" | cut -f1 -d' ') + if command -v udevadm >/dev/null 2>&1; then + udevadm settle 10 + else + sleep 1 + fi + LOOP_DEVICE=$(losetup -n -O NAME -j "$1") if [ -n "$LOOP_DEVICE" ]; then for part in "$LOOP_DEVICE"p*; do if DIR=$(findmnt -n -o target -S "$part"); then