-
Notifications
You must be signed in to change notification settings - Fork 1
/
snap_create_live
executable file
·96 lines (75 loc) · 3.41 KB
/
snap_create_live
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/bin/bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2023, 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. #
#--------------------------------------------------------------------------- #
echo "ssh $(date '+%Y-%m-%d %H:%M:%S') snap_create_live" >> /var/log/one/debug.log
# snap_create host:parent_image snap_id vmid ds_id
SRC=$1
SNAP_ID=$2
VMID=$3
if [ -z "${ONE_LOCATION}" ]; then
TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
SSH_RC=/var/lib/one/remotes/etc/tm/ssh/sshrc
SSH_UTILS=/var/lib/one/remotes/tm/custom_ssh/ssh_utils.sh
else
TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
SSH_RC=$ONE_LOCATION/var/remotes/etc/tm/ssh/sshrc
SSH_UTILS=$ONE_LOCATION/var/remotes/tm/custom_ssh/ssh_utils.sh
fi
DRIVER_PATH=$(dirname $0)
source $TMCOMMON
source $SSH_RC
source $SSH_UTILS
source ${DRIVER_PATH}/../../etc/vmm/kvm/kvmrc
source ${DRIVER_PATH}/../../scripts_common.sh
SRC_PATH=$(arg_path $SRC NOFIX)
SRC_HOST=$(arg_host $SRC)
#-------------------------------------------------------------------------------
# Get VM information
#-------------------------------------------------------------------------------
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/HISTORY_RECORDS/HISTORY[last\(\)]/DS_ID)
DEPLOY_ID="${XPATH_ELEMENTS[j++]}"
DS_ID="${XPATH_ELEMENTS[j++]}"
VM_DIR="$(dirname ${SRC_PATH})"
DISK_NAME="$(basename $SRC_PATH)"
SNAP_DIR="${SRC_PATH}.snap"
SNAP_PATH="${SNAP_DIR}/${SNAP_ID}"
REPLICA_HOST=$(get_replica_host $DS_ID)
CMD=$(cat <<EOT
set -ex -o pipefail
$(type retry_if_no_error| grep -v 'is a function')
mkdir -p "${SNAP_DIR}"
touch $SNAP_PATH
REAL_DISK="\$(dirname $SRC_PATH)/\$(readlink $SRC_PATH)"
retry_if_no_error "active block job" 3 5 virsh -q -c ${LIBVIRT_URI} blockcopy ${DEPLOY_ID} \
--path ${REAL_DISK} --dest $SNAP_PATH --wait --finish
if [ -n "$REPLICA_HOST" ]; then
ssh $REPLICA_HOST "mkdir -p $REPLICA_RECOVERY_SNAPS_DIR/$VMID"
cd $VM_DIR
$TAR --transform="flags=r;s|${DISK_NAME}.snap/${SNAP_ID}|${DISK_NAME}.replica_snap|" \
-cSf - ${DISK_NAME}.snap/${SNAP_ID} | \
ssh $REPLICA_SSH_OPTS ${REPLICA_HOST} \
"$TAR -xSf - -C $REPLICA_RECOVERY_SNAPS_DIR/$VMID"
fi
EOT
)
ssh_forward ssh_exec_and_log "${SRC_HOST}" "${CMD}" \
"Error creating snapshot ${SNAP_PATH}"