summaryrefslogtreecommitdiff
path: root/functions/losetup.sh
diff options
context:
space:
mode:
Diffstat (limited to 'functions/losetup.sh')
-rwxr-xr-xfunctions/losetup.sh27
1 files changed, 25 insertions, 2 deletions
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})"