Statistics
| Branch: | Tag: | Revision:

one / src / tm_mad / qcow2 / snap_create @ 40086e5c

History | View | Annotate | Download (2.77 KB)

1
#!/bin/bash
2

    
3
# -------------------------------------------------------------------------- #
4
# Copyright 2002-2015, OpenNebula Project (OpenNebula.org), C12G Labs        #
5
#                                                                            #
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
7
# not use this file except in compliance with the License. You may obtain    #
8
# a copy of the License at                                                   #
9
#                                                                            #
10
# http://www.apache.org/licenses/LICENSE-2.0                                 #
11
#                                                                            #
12
# Unless required by applicable law or agreed to in writing, software        #
13
# distributed under the License is distributed on an "AS IS" BASIS,          #
14
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
15
# See the License for the specific language governing permissions and        #
16
# limitations under the License.                                             #
17
#--------------------------------------------------------------------------- #
18

    
19
# snap_create host:parent_image snap_id vmid ds_id
20

    
21
SRC=$1
22
SNAP_ID=$2
23
VMID=$3
24
DSID=$4
25

    
26
if [ -z "${ONE_LOCATION}" ]; then
27
    TMCOMMON=/var/lib/one/remotes/tm/tm_common.sh
28
    DATASTORES=/var/lib/one/datastores
29
else
30
    TMCOMMON=$ONE_LOCATION/var/remotes/tm/tm_common.sh
31
    DATASTORES=$ONE_LOCATION/var/datastores
32
fi
33

    
34
DRIVER_PATH=$(dirname $0)
35

    
36
. $TMCOMMON
37

    
38
SRC_PATH=$(arg_path $SRC)
39
SRC_HOST=$(arg_host $SRC)
40

    
41
#-------------------------------------------------------------------------------
42
# Get Image information
43
#-------------------------------------------------------------------------------
44

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

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

    
49
unset i j XPATH_ELEMENTS
50

    
51
while IFS= read -r -d '' element; do
52
    XPATH_ELEMENTS[i++]="$element"
53
done < <(onevm show -x $VMID| $XPATH \
54
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/SOURCE \
55
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE )
56

    
57
DISK_SRC="${XPATH_ELEMENTS[j++]}"
58
CLONE="${XPATH_ELEMENTS[j++]}"
59

    
60
SYSTEM_DS_PATH=$(dirname ${SRC_PATH})
61
SYSTEM_DS_DISK_PATH="${SYSTEM_DS_PATH}/disk.${DISK_ID}"
62

    
63
if [ "$CLONE" = "YES" ]; then
64
    DISK_PATH="${SYSTEM_DS_DISK_PATH}"
65
else
66
    DISK_PATH=$DISK_SRC
67
fi
68

    
69
NEXT_SNAP_ID=$(($SNAP_ID+1))
70

    
71
SNAP_DIR="${DISK_PATH}.snap"
72
SNAP_PATH="${SNAP_DIR}/${NEXT_SNAP_ID}"
73

    
74
SNAP_CMD=$(cat <<EOT
75
set -ex
76

    
77
mkdir -p "${SNAP_DIR}"
78

    
79
PREVIOUS_SNAP=$(readlink $SYSTEM_DS_DISK_PATH)
80

    
81
qemu-img create -f qcow2 -b "\${PREVIOUS_SNAP}" "${SNAP_PATH}"
82
ln -sf $SNAP_PATH $SYSTEM_DS_DISK_PATH
83
EOT
84
)
85

    
86
ssh_exec_and_log "${SRC_HOST}" "${SNAP_CMD}" \
87
                 "Error creating snapshot ${SNAP_PATH}"
88