--- ssh_tm_clone.sh.v3_0	2011-12-14 19:02:52.376573339 -0600
+++ ssh_tm_clone.sh.v3_0-modified	2011-12-14 21:16:02.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
@@ -33,7 +34,6 @@
 SRC_HOST=`arg_host $SRC`
 DST_HOST=`arg_host $DST`
 
-
 log_debug "$1 $2"
 log_debug "DST: $DST_PATH"
 
@@ -42,6 +42,7 @@
 log "Creating directory $DST_DIR"
 exec_and_log "$SSH $DST_HOST mkdir -p $DST_DIR" \
     "Error creating directory $DST_DIR"
+exec_and_log "$SSH $DST_HOST chmod a+rw $DST_DIR"
 
 case $SRC in
 http://*)
@@ -52,8 +53,16 @@
 
 *)
     log "Cloning $SRC"
-    exec_and_log "$SCP $SRC $DST" \
+
+	# 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 "$SCP $SRC $DST" \
         "Error copying $SRC to $DST"
+	fi
     ;;
 esac
 
