#!/bin/bash

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, 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.                                             #
#--------------------------------------------------------------------------- #

# cpds host:remote_system_ds/disk.i fe:SOURCE|SOURCE snapid vmid dsid
#   - fe is the front-end hostname
#   - SOURCE is the path of the disk image in the form DS_BASE_PATH/disk
#   - host is the target host to deploy the VM
#   - remote_system_ds is the path for the system datastore in the host
#   - snapid is the snapshot id. "-1" for none
#   - dsid is the target datastore

SRC=$1
DST=$2
SNAP_ID=$3
VMID=$4
DSID=$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

function set_src_dst_vars
{
    SRC_PATH="$(arg_path "$SRC")"
    DST_PATH="$(arg_path "$DST")"

    SRC_HOST="$(arg_host "$SRC")"
    DST_HOST="$(arg_host "$DST")"

    SRC_FILE="$(basename "$SRC_PATH")"
    DST_FILE="$(basename "$DST_PATH")"

    SRC_DIR="$(dirname "$SRC_PATH")"
    DST_DIR="$(dirname "$DST_PATH")"
}

#-------------------------------------------------------------------------------
# Get Image information
#-------------------------------------------------------------------------------

DISK_ID=$(basename ${SRC} | cut -d. -f2)

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 $VMID| $XPATH \
                    /VM/DEPLOY_ID \
                    /VM/LCM_STATE \
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/TARGET \
                    /VM/HISTORY_RECORDS/HISTORY[last\(\)]/VM_MAD)

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

#-------------------------------------------------------------------------------
# Set src path
#-------------------------------------------------------------------------------

if [ "${SNAP_ID}" != "-1" ]; then
    SRC="${SRC}.snap/${SNAP_ID}"
fi

set_src_dst_vars

SRC_TEMP_PATH="$(mktemp -u "${SRC_PATH}.XXXXXXXX")"

#-------------------------------------------------------------------------------
# Move the image back to the datastore
#-------------------------------------------------------------------------------
# For current image of the running VMs, don't touch the image directly,
# but export the content via blockcopy. If that's not possible (old QEMU),
# domfsfreeze or suspend the domain before.
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' -a "${SNAP_ID}" = '-1' ]; then
    log "VM is running, trying blockcopy, fsfreeze, suspend"

    CPDS_CMD_EXPORT=$(cat <<EOF
$(type retry_if_no_error| grep -v 'is a function')

touch ${SRC_TEMP_PATH}
if ! retry_if_no_error "active block job" 3 5 virsh -c ${LIBVIRT_URI} blockcopy ${DEPLOY_ID} ${DISK_TARGET} ${SRC_TEMP_PATH} --wait --finish; then
    set -e -o pipefail

    if timeout ${VIRSH_TIMEOUT:-60} virsh -c ${LIBVIRT_URI} domfsfreeze ${DEPLOY_ID}; then
        trap "virsh -c ${LIBVIRT_URI} domfsthaw ${DEPLOY_ID}" EXIT
        $CP $SRC_PATH $SRC_TEMP_PATH
    elif virsh -c ${LIBVIRT_URI} suspend ${DEPLOY_ID}; then
        trap "virsh -c ${LIBVIRT_URI} resume ${DEPLOY_ID}" EXIT
        $CP $SRC_PATH $SRC_TEMP_PATH
    else
        echo "Could not domfsfreeze or suspend domain" >&2
        exit 1
    fi
fi

EOF
)

    CPDS_CMD_ERR="Error creating export for domain ${DEPLOY_ID} of disk ${DISK_TARGET} at ${SRC_TEMP_PATH}"

    # Always try delete temp export
    trap "ssh_exec_and_log $SRC_HOST \"$RM ${SRC_TEMP_PATH}\"" EXIT

    if ssh_exec_and_log_no_error $SRC_HOST "$CPDS_CMD_EXPORT" "$CPDS_CMD_ERR"; then
        # Export creation succeeded, point SRC there
        SRC="${SRC_HOST}:${SRC_TEMP_PATH}"
        set_src_dst_vars
    else
        log_error "Exporting failed"
        exit 1
    fi
else
    # if SRC is qcow2 with backing chain, convert it to tmp path
    CPDS_CMD_EXPORT=$(cat <<EOF
        set -e -o pipefail
        if [ -L $SRC_PATH ] && file -L $SRC_PATH | grep -q 'has backing file'; then
            $QEMU_IMG convert -O qcow2 $SRC_PATH $SRC_TEMP_PATH;
            exit 42;
        fi
EOF
)

    ssh_exec_and_log_no_error $SRC_HOST "$CPDS_CMD_EXPORT" "$CPDS_CMD_ERR" "100"
    RC=$?
    if [ $RC = 0 ]; then
        # no export needed
        true
    elif [ $RC = 42 ]; then
        # qcow2 detected and exported, re-point SRC and other vars
        SRC="${SRC_HOST}:${SRC_TEMP_PATH}"
        set_src_dst_vars

        # Always try delete temp export
        trap "ssh_exec_and_log $SRC_HOST \"$RM ${SRC_TEMP_PATH}\"" EXIT
    else
        error_message "cpds: Can not convert qcow2 chain $SRC_PATH"
        exit 1
    fi
fi

log "Moving $SRC to datastore as $DST_PATH"

# Copy from possibly remote host to possible remote host
if [ "$(fix_dir_slashes $DST_HOST)" != "$DST_PATH" ]; then
    COPY_CMD=$(cat <<EOF
        set -e -o pipefail
        $TAR -C $SRC_DIR --transform="flags=r;s|$SRC_FILE|$DST_FILE|" -cSf - $SRC_FILE | \
            $SSH $DST_HOST "$TAR -xSf - -C $DST_DIR"
EOF
)

    ssh_forward ssh_exec_and_log "$SRC_HOST" "$COPY_CMD" \
                                 "Error copying $SRC to $DST_HOST:$DST_PATH"
else # Copy from possibly remote host to local
    REMOTE_CMD=$(cat <<EOF
        set -e -o pipefail
        $SSH $SRC_HOST "$TAR -C $SRC_DIR --transform=\"flags=r;s|$SRC_FILE|$DST_FILE|\" -cSf - $SRC_FILE" | \
            $TAR -xSf - -C $DST_DIR
EOF
)

    multiline_exec_and_log "$REMOTE_CMD" "Error copying $SRC to $DST_PATH"
fi

exit 0
