From 20661e8d8070c034bbc962feeb15b72d710dee5d Mon Sep 17 00:00:00 2001 From: Clint Byrum Date: Thu, 9 May 2013 09:34:53 -0700 Subject: [PATCH] Retry losetup -d for up to 10 seconds. When partition tables are added to loopback devices, this can set off a chain of udev hooks that may still be holding the loopback open. Failing to detach loopback devices was the reason we were seeing leaked tmpfs volumes. Fixes bug #1178091 Change-Id: I836d6e2bbce824951dd4786e3ef28273ea18ee73 --- bin/disk-image-create | 2 +- lib/common-functions | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/bin/disk-image-create b/bin/disk-image-create index 3eb406b2..4cffd924 100755 --- a/bin/disk-image-create +++ b/bin/disk-image-create @@ -112,7 +112,7 @@ mv $TMP_BUILD_DIR/mnt $TMP_BUILD_DIR/built _NEEDED_SIZE=$(sudo du --block-size=800 -x -s ${TMP_BUILD_DIR}/built | awk ' { print $1 } ') truncate -s${_NEEDED_SIZE}K $TMP_IMAGE_PATH LOOPDEV=$(sudo losetup --show -f $TMP_IMAGE_PATH) -export EXTRA_UNMOUNT="sudo losetup -d $LOOPDEV" +export EXTRA_UNMOUNT="detach_loopback $LOOPDEV" export IMAGE_BLOCK_DEVICE=$LOOPDEV eval_run_d block-device "IMAGE_BLOCK_DEVICE=" OPTS="" diff --git a/lib/common-functions b/lib/common-functions index ead477a3..7cf6df02 100644 --- a/lib/common-functions +++ b/lib/common-functions @@ -1,3 +1,4 @@ +# vim: syntax=sh ts=4 sts=4 sw=2:et # Copyright 2012 Hewlett-Packard Development Company, L.P. # All Rights Reserved. # @@ -159,3 +160,16 @@ function run_d() { check_break after-$1 bash } +function detach_loopback() { + local loopdev=$1 + # loopback dev may be tied up a bit by udev events triggered by partition events + for try in $(seq 10 -1 1) ; do + if sudo losetup -d $loopdev ; then + return 0 + fi + echo $loopdev may be busy, sleeping up to $try more seconds... + sleep 1 + done + echo Gave up trying to detach $loopdev + return 1 +}