]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
tests: improve ceph-disk.sh setup/teardown
authorLoic Dachary <ldachary@redhat.com>
Wed, 1 Apr 2015 12:44:17 +0000 (14:44 +0200)
committerLoic Dachary <ldachary@redhat.com>
Sun, 5 Apr 2015 23:58:18 +0000 (01:58 +0200)
Address all possible failure cases, when ceph-disk.sh completes or when
it starts with leftover from a previous interrupted run. It is assumed
that ceph-disk.sh will crash at any point.

* umount all mount points that belong to ceph-disk.sh (check the
  absolute path of the directory)
* dmsetup remove all device mapper nodes found to hold a loop device
  that ceph-disks.sh no longer uses
* losetup --detach all loop devices that ceph-disks.sh no longer uses

Signed-off-by: Loic Dachary <ldachary@redhat.com>
src/test/ceph-disk.sh

index 8524098a9da18f26f78af62c2e56d7ff5bd0edcc..fec0e1e2287614cf0c3b6ab2b77dad5f26a210ce 100755 (executable)
@@ -65,6 +65,9 @@ function teardown() {
         rm -fr $DIR/*/*db
         teardown_btrfs $DIR
     fi
+    grep " $(pwd)/$DIR/" < /proc/mounts | while read mounted rest ; do
+        umount $mounted
+    done
     rm -fr $DIR
 }
 
@@ -367,28 +370,59 @@ function loop_sanity_check() {
     return $status
 }
 
-function create_dev() {
-    local name=$1
+function reset_dev() {
+    local dev=$1
 
-    echo create_dev $name >&2
-    dd if=/dev/zero of=$name bs=1024k count=400 > /dev/null
-    losetup --find $name
-    local dev=$(losetup --associated $name | cut -f1 -d:)
+    if test -z "$dev" ; then
+        return
+    fi
+
+    grep "^$dev" < /proc/mounts | while read mounted rest ; do
+        umount $mounted
+    done
+    local dev_base=$(basename $dev)
+    (
+        ls /sys/block/$dev_base/$dev_base*/holders 2> /dev/null
+        ls /sys/block/$dev_base/holders 2> /dev/null
+        ) | grep '^dm-' | while read dm ; do
+       dmsetup remove /dev/$dm
+    done
     ceph-disk zap $dev > /dev/null 2>&1
+}
+
+function reset_leftover_dev() {
+    local path=$1
+
+    losetup --all | sed -e 's/://' | while read dev id associated_path ; do
+        if test $associated_path = "($path)" ; then
+            reset_dev $dev
+            losetup --detach $dev
+        fi
+    done
+}
+
+function create_dev() {
+    local path=$1
+
+    echo -n "create_dev $path ... " >&2
+    reset_leftover_dev $path
+    dd if=/dev/zero of=$path bs=1024k count=400 > /dev/null 2>&1
+    losetup --find $path
+    local dev=$(losetup --associated $path | cut -f1 -d:)
+    test "$dev" || return 1
+    reset_dev $dev
+    echo $dev >&2
     echo $dev
 }
 
 function destroy_dev() {
-    local name=$1
+    local path=$1
     local dev=$2
 
-    echo destroy_dev $name $dev >&2
-    for partition in 1 2 3 4 ; do
-        umount ${dev}p${partition} > /dev/null 2>&1 || true
-    done
-    ceph-disk zap $dev > /dev/null 2>&1
+    echo destroy_dev $path $dev >&2
+    reset_dev $dev
     losetup --detach $dev
-    rm $name
+    rm -f $path
 }
 
 function activate_dev_body() {