#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
#--------------------------------------------------------------------------- #

SRC=$1
DST=$2
SNAP_ID=$3
VM_ID=$4
DS_ID=$5

if [ -z "${ONE_LOCATION}" ]; then
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
else
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
fi

DRIVER_PATH=$(dirname $0)

. $TMCOMMON
. ${DRIVER_PATH}/../../etc/vmm/kvm/kvmrc

#-------------------------------------------------------------------------------
# Set dst path and dir
#-------------------------------------------------------------------------------
SRC_PATH=`arg_path $SRC`
SRC_HOST=`arg_host $SRC`

DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`

DISK_ID=$(echo $SRC_PATH|$AWK -F. '{print $NF}')
DS_SYS_ID=$(echo $SRC_PATH|$AWK -F/ '{print $(NF-2)}')

VG_IMAGE_NAME="vg-one-${DS_ID}"
LV_IMAGE_NAME=`basename $DST`

XPATH="${DRIVER_PATH}/../../datastore/xpath.rb --stdin"

unset i j XPATH_ELEMENTS

while IFS= read -r -d '' element; do
    XPATH_ELEMENTS[i++]="$element"
done < <(onevm show -x $VM_ID| $XPATH \
                    /VM/DEPLOY_ID \
                    /VM/LCM_STATE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/TARGET \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE \
                    /VM/HISTORY_RECORDS/HISTORY[last\(\)]/VM_MAD \
                    '%m%/VM/TEMPLATE/DISK/TARGET')

DEPLOY_ID="${XPATH_ELEMENTS[j++]}"
LCM_STATE="${XPATH_ELEMENTS[j++]}"
DISK_TARGET="${XPATH_ELEMENTS[j++]}"
DISK_SRC="${XPATH_ELEMENTS[j++]}"
CLONE="${XPATH_ELEMENTS[j++]}"
VM_MAD="${XPATH_ELEMENTS[j++]}"
ALL_DISKS="${XPATH_ELEMENTS[j++]}"

IFS= read -r -d '' BRIDGE_LIST < <(onedatastore show -x "$DS_SYS_ID" \
    | $XPATH /DATASTORE/TEMPLATE/BRIDGE_LIST )

if [ -n "$BRIDGE_LIST" ]; then
    SRC_HOST=$(get_destination_host)
fi

while [ ! -e "$DST" ]; do
    sleep 5
done


if [ "${LCM_STATE}" = '26' -a "${VM_MAD}" != "kvm" ]; then
    error_message "cpds: Live operation not supported on ${VM_MAD}"
    exit 1
fi

if [ "${LCM_STATE}" = '26' ]; then

    DUMP_CMD=$(cat <<EOF

	${SUDO} ${LVCHANGE} -ay "${DST}"

    if ! virsh -c ${LIBVIRT_URI} blockcopy ${DEPLOY_ID} ${DISK_TARGET} ${DST} --blockdev --wait --finish; then
	if virsh -c ${LIBVIRT_URI} domfsfreeze ${DEPLOY_ID}; then
    	    trap "virsh -c ${LIBVIRT_URI} domfsthaw ${DEPLOY_ID}" EXIT
	elif virsh -c ${LIBVIRT_URI} suspend ${DEPLOY_ID}; then
    	    trap "virsh -c ${LIBVIRT_URI} resume ${DEPLOY_ID}" EXIT
	else
    	    echo "Could not domfsfreeze or suspend domain" >&2
    	    exit 1
	fi
        $SUDO ${DD} if=\${DEV} of=${DST} bs=${DD_BLOCK_SIZE:-64k}

fi
EOF
)

else

    DUMP_CMD=$(cat <<EOF
	set -e -o pipefail
	$SUDO $SYNC
	$SUDO $LVSCAN
	DEV=\$(readlink $SRC_PATH)
	${SUDO} ${LVCHANGE} -ay "${DST}"
	$SUDO ${DD} if=\${DEV} of=${DST} bs=${DD_BLOCK_SIZE:-64k}
EOF
)
fi

#-------------------------------------------------------------------------------
# Move the image back to the datastore
#-------------------------------------------------------------------------------
log "Dumping $SRC to $DST"

ssh_exec_and_log "$SRC_HOST" "$DUMP_CMD" \
    "Error dumping $SRC to $DST"

exit 0