diff --git a/scripts/common.sh b/scripts/common.sh index fdb9b947c5..6e19bbf83d 100755 --- a/scripts/common.sh +++ b/scripts/common.sh @@ -143,3 +143,15 @@ unload_modules() { return 0 } + +unused_loop_device() { + for DEVICE in `ls -1 /dev/loop*`; do + losetup $DEVICE &>/dev/null + if [ $? -ne 0 ]; then + echo $DEVICE + return + fi + done + + die "Error: Unable to find unused loopback device" +} diff --git a/scripts/zpool-config/lo-raid0.sh b/scripts/zpool-config/lo-raid0.sh index 84c355266c..36b3c4d62f 100644 --- a/scripts/zpool-config/lo-raid0.sh +++ b/scripts/zpool-config/lo-raid0.sh @@ -11,7 +11,7 @@ DEVICES="" zpool_create() { for FILE in ${FILES}; do - DEVICE=`/sbin/losetup -f` + DEVICE=`unused_loop_device` msg "Creating ${FILE} using loopback device ${DEVICE}" rm -f ${FILE} || exit 1 dd if=/dev/zero of=${FILE} bs=1024k count=256 &>/dev/null || diff --git a/scripts/zpool-config/lo-raid10.sh b/scripts/zpool-config/lo-raid10.sh index 78004cc545..ce528004c7 100644 --- a/scripts/zpool-config/lo-raid10.sh +++ b/scripts/zpool-config/lo-raid10.sh @@ -13,7 +13,7 @@ DEVICES_M2="" zpool_create() { for FILE in ${FILES_M1}; do - DEVICE=`/sbin/losetup -f` + DEVICE=`unused_loop_device` msg "Creating ${FILE} using loopback device ${DEVICE}" rm -f ${FILE} || exit 1 dd if=/dev/zero of=${FILE} bs=1024k count=256 &>/dev/null || diff --git a/scripts/zpool-config/lo-raidz.sh b/scripts/zpool-config/lo-raidz.sh index cac9036464..616c364bed 100644 --- a/scripts/zpool-config/lo-raidz.sh +++ b/scripts/zpool-config/lo-raidz.sh @@ -11,7 +11,7 @@ DEVICES="" zpool_create() { for FILE in ${FILES}; do - DEVICE=`/sbin/losetup -f` + DEVICE=`unused_loop_device` msg "Creating ${FILE} using loopback device ${DEVICE}" rm -f ${FILE} || exit 1 dd if=/dev/zero of=${FILE} bs=1024k count=256 &>/dev/null || diff --git a/scripts/zpool-config/lo-raidz2.sh b/scripts/zpool-config/lo-raidz2.sh index bd9a40ce58..dbd7d815eb 100644 --- a/scripts/zpool-config/lo-raidz2.sh +++ b/scripts/zpool-config/lo-raidz2.sh @@ -11,7 +11,7 @@ DEVICES="" zpool_create() { for FILE in ${FILES}; do - DEVICE=`/sbin/losetup -f` + DEVICE=`unused_loop_device` msg "Creating ${FILE} using loopback device ${DEVICE}" rm -f ${FILE} || exit 1 dd if=/dev/zero of=${FILE} bs=1024k count=256 &>/dev/null ||