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 <serge@raspberrypi.com>
This commit is contained in:
Tom Dewey 2024-05-15 15:50:40 +01:00 committed by GitHub
parent d790fed832
commit fb48183f0d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 4 deletions

View File

@ -158,7 +158,17 @@ do
done done
term() { 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 trap term EXIT INT TERM

View File

@ -58,9 +58,12 @@ unmount(){
export -f unmount export -f unmount
unmount_image(){ unmount_image(){
sync if command -v udevadm >/dev/null 2>&1; then
udevadm settle 10
else
sleep 1 sleep 1
LOOP_DEVICE=$(losetup --list | grep "$1" | cut -f1 -d' ') fi
LOOP_DEVICE=$(losetup -n -O NAME -j "$1")
if [ -n "$LOOP_DEVICE" ]; then if [ -n "$LOOP_DEVICE" ]; then
for part in "$LOOP_DEVICE"p*; do for part in "$LOOP_DEVICE"p*; do
if DIR=$(findmnt -n -o target -S "$part"); then if DIR=$(findmnt -n -o target -S "$part"); then