From 0d2cc22efc66e919848fe0b6559e5b59b08e0afa Mon Sep 17 00:00:00 2001 From: "Cody A.W. Somerville" Date: Thu, 21 Jul 2011 17:00:54 +0200 Subject: Adding function for more resilient detachment of loopback devices. --- functions/losetup.sh | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'functions') diff --git a/functions/losetup.sh b/functions/losetup.sh index df7e2d27f..039ff3ec1 100755 --- a/functions/losetup.sh +++ b/functions/losetup.sh @@ -7,6 +7,29 @@ ## This is free software, and you are welcome to redistribute it ## under certain conditions; see COPYING for details. +Lodetach () +{ + DEVICE="${1}" + ATTEMPT="${2:-1}" + + if [ "${ATTEMPT}" -gt 3 ] + then + Echo_error "Failed to detach loop device '${DEVICE}'." + exit 1 + fi + + # Changes to block devices result in uevents which trigger rules which in + # turn access the loop device (ex. udisks-part-id, blkid) which can cause + # a race condition. We call 'udevadm settle' to help avoid this. + ${LB_ROOT_COMMAND} udevadm settle + + # Loop back devices aren't the most reliable when it comes to writes. + # We sleep and sync for good measure - better than build failure. + sync + sleep 1 + + ${LB_ROOT_COMMAND} ${LB_LOSETUP} -d "${DEVICE}" || Lodetach "${DEVICE}" "$(expr ${ATTEMPT} + 1)" +} Losetup () { @@ -14,9 +37,9 @@ Losetup () FILE="${2}" PARTITION="${3:-1}" - ${LB_ROOT_COMMAND} ${LB_LOSETUP} "${DEVICE}" "${FILE}" + ${LB_ROOT_COMMAND} ${LB_LOSETUP} --read-only "${DEVICE}" "${FILE}" FDISK_OUT="$(${LB_FDISK} -l -u ${DEVICE} 2>&1)" - ${LB_ROOT_COMMAND} ${LB_LOSETUP} -d "${DEVICE}" + Lodetach "${DEVICE}" LOOPDEVICE="$(echo ${DEVICE}p${PARTITION})" -- cgit v1.2.3