--- shared_tm_clone.sh.v3_0	2011-12-14 18:55:57.360569732 -0600
+++ shared_tm_clone.sh.v3_0-modified	2011-12-14 21:15:47.000000000 -0600
@@ -18,6 +18,7 @@
 
 SRC=$1
 DST=$2
+COW=true # Replace later by script parameters. This dictates whether we create a copy-on-write disk for an existing QCOW disk.
 
 if [ -z "${ONE_LOCATION}" ]; then
     TMCOMMON=/usr/lib/one/mads/tm_common.sh
@@ -32,6 +33,9 @@
 SRC_PATH=`arg_path $SRC`
 DST_PATH=`arg_path $DST`
 
+SRC_HOST=`arg_host $SRC`
+DST_HOST=`arg_host $DST`
+
 fix_paths
 
 log_debug "$1 $2"
@@ -40,7 +44,8 @@
 DST_DIR=`dirname $DST_PATH`
 
 log "Creating directory $DST_DIR"
-exec_and_log "mkdir -p $DST_DIR"
+exec_and_log "mkdir -p $DST_DIR" \
+    "Error creating directory $DST_DIR"
 exec_and_log "chmod a+w $DST_DIR"
 
 case $SRC in
@@ -51,9 +56,17 @@
     ;;
 
 *)
-    log "Cloning $SRC_PATH"
-    exec_and_log "cp -r $SRC_PATH $DST_PATH" \
+    log "Cloning $SRC"
+
+	# Support QEMU copy-on-write disks when given a disk of QCOW type.
+	fileType=$(file -b $SRC_PATH | cut -f 2 -d ' ')
+	if [[ "$fileType" == "QCOW" && $COW == true ]]; then
+		exec_and_log "$SSH $DST_HOST qemu-img create -f qcow2 -o backing_file=$SRC_PATH $DST_PATH" \
+        "Error copying $SRC to $DST"
+	else
+		exec_and_log "cp -r $SRC_PATH $DST_PATH" \
         "Error copying $SRC to $DST"
+	fi
     ;;
 esac
 
