Add directory for code documentation
[yardstick.git] / tools / yardstick-img-modify
index ee9a457..5e9b450 100755 (executable)
@@ -25,6 +25,7 @@
 #
 
 set -e
+set -x
 
 die() {
     echo "error: $1" >&2
@@ -103,6 +104,10 @@ modify() {
     umount $mountdir
 
     qemu-img convert -c -o compat=0.10 -O qcow2 $raw_imgfile $imgfile
+
+    if dmsetup table | grep $loopdevice; then
+       dmsetup clear $loopdevice || true
+    fi
 }
 
 # cleanup (umount) the image
@@ -111,19 +116,40 @@ cleanup() {
     mount | grep $mountdir/proc && umount $mountdir/proc
     mount | grep $mountdir && umount $mountdir
     if [ -f $raw_imgfile ]; then
-        kpartx -d $raw_imgfile || true
+        kpartx -dv $raw_imgfile || true
     fi
     rm -f $raw_imgfile
     rm -rf $mountdir
 }
 
-set -x
+exitcode=""
+error_trap()
+{
+    local rc=$?
+
+    set +e
+
+    if [ -z "$exitcode" ]; then
+        exitcode=$rc
+    fi
+
+    cleanup
+
+    echo "Image build failed with $exitcode"
+
+    exit $exitcode
+}
 
 main() {
     cleanup
+
+    trap "error_trap" EXIT SIGTERM
+
     download
     setup
     modify
+
+    trap - EXIT SIGTERM
     cleanup
 
     echo "the modified image is found here: $imgfile"