-
Notifications
You must be signed in to change notification settings - Fork 498
/
Copy pathdelete
executable file
·124 lines (104 loc) · 3.97 KB
/
delete
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/bin/bash
# -------------------------------------------------------------------------- #
# Copyright 2002-2019, 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. #
#--------------------------------------------------------------------------- #
# DELETE <host:remote_system_ds/disk.i|host:remote_system_ds/>
# - host is the target host to deploy the VM
# - remote_system_ds is the path for the system datastore in the host
DST=$1
VM_ID=$2
DS_ID=$3
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)
XPATH="${DRIVER_PATH}/../../datastore/xpath.rb"
source $TMCOMMON
source ${DRIVER_PATH}/../../etc/tm/fs_lvm/fs_lvm.conf
source ${DRIVER_PATH}/../../datastore/libfs.sh
#-------------------------------------------------------------------------------
# Return if deleting a disk, we will delete them when removing the
# remote_system_ds directory for the VM (remotely)
#-------------------------------------------------------------------------------
DST_PATH=`arg_path $DST`
DST_HOST=`arg_host $DST`
if [ `is_disk $DST_PATH` -eq 1 ]; then
DS_SYS_ID=$(echo $DST_PATH | $AWK -F '/' '{print $(NF-2)}')
else
DS_SYS_ID=$(echo $DST_PATH | $AWK -F '/' '{print $(NF-1)}')
fi
# Activate device
ACTIVATE_CMD=$(cat <<EOF
set -ex -o pipefail
if [ -L "$DST_PATH" ]; then
DEV=\$(readlink $DST_PATH)
if echo "\$DEV" | grep "^/dev/" &>/dev/null; then
${SUDO} ${SYNC}
${SUDO} ${LVSCAN}
${SUDO} ${LVCHANGE} -ay "\${DEV}"
fi
fi
EOF
)
# Zero space
ZERO_CMD=$(cat <<EOF
set -ex -o pipefail
if [ -L "$DST_PATH" ]; then
DEV=\$(readlink $DST_PATH)
if echo "\$DEV" | grep "^/dev/" &>/dev/null; then
${DD} if=/dev/zero of="\${DEV}" bs=${DD_BLOCK_SIZE:-64k} || :
fi
fi
EOF
)
# Delete the device if it's a clone (LVM snapshot)
DELETE_CMD=$(cat <<EOF
set -ex -o pipefail
if [ -d "$DST_PATH" ]; then
rm -rf "$DST_PATH"
exit 0
fi
if [ -L "$DST_PATH" ]; then
DEV=\$(readlink $DST_PATH)
if echo "\$DEV" | grep "^/dev/" &>/dev/null; then
$SUDO $LVREMOVE -f "\$DEV"
fi
fi
rm -f "$DST_PATH"
EOF
)
while IFS= read -r -d '' element; do
XPATH_ELEMENTS[i++]="$element"
done < <(onedatastore show -x $DS_SYS_ID | $XPATH \
/DATASTORE/TEMPLATE/BRIDGE_LIST)
unset i
BRIDGE_LIST="${XPATH_ELEMENTS[i++]}"
# Change DST_HOST to one of the BRIDGE_LIST to prevent
# running on frontend for undeployed VMs
if [ -n "$BRIDGE_LIST" ]; then
DST_HOST=$(get_destination_host)
fi
if [ "${ZERO_LVM_ON_DELETE}" = "yes" ]; then
LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"
exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST" "$ACTIVATE_CMD" \
"Error activating disk $SRC_PATH"
ssh_exec_and_log "$DST_HOST" "$ZERO_CMD" "Error cleaning $DST_PATH"
fi
LOCK="tm-fs_lvm-${DS_SYS_ID}.lock"
exclusive "${LOCK}" 120 ssh_exec_and_log "$DST_HOST" "$DELETE_CMD" \
"Error deleting $DST_PATH"
hup_collectd $DST_HOST