Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (4.34 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
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/TARGET \
57
                    /VM/DEPLOY_ID \
58
                    '%m%/VM/TEMPLATE/DISK/TARGET')
59

    
60
DISK_SRC="${XPATH_ELEMENTS[j++]}"
61
CLONE="${XPATH_ELEMENTS[j++]}"
62
TARGET="${XPATH_ELEMENTS[j++]}"
63
DEPLOY_ID="${XPATH_ELEMENTS[j++]}"
64
ALL_DISKS="${XPATH_ELEMENTS[j++]}"
65

    
66
SYSTEM_DS_PATH=$(dirname ${SRC_PATH})
67

    
68
SYSTEM_DS_DISK_PATH="${SYSTEM_DS_PATH}/disk.${DISK_ID}"
69

    
70
if [ "$CLONE" = "YES" ]; then
71
    DISK_PATH="${SYSTEM_DS_DISK_PATH}"
72
else
73
    DISK_PATH=$DISK_SRC
74
fi
75

    
76
NEXT_SNAP_ID=$(($SNAP_ID+1))
77

    
78
SNAP_DIR="${DISK_PATH}.snap"
79
SNAP_PATH="${SNAP_DIR}/${NEXT_SNAP_ID}"
80

    
81
# virsh snapshot-create by default creates snapshots for all the disks that
82
# are not read only. To be able to make a snapshot in only one of the disks
83
# an xml must be created describing all of the disks and setting the value
84
# snapshot='no' to the disks that are not going to be snapshotted.
85

    
86
# <domainsnapshot>
87
#   <name>1</name>
88
#   <description>snap 1</description>
89
#   <disks>
90
#     <disk name='vda' snapshot='no'/>
91
#     <disk name='vdb'>
92
#       <source file='/var/lib/one/datastores/1/09eec196c67b38dea41048a628978600.snap/1'/>
93
#     </disk>
94
#     <disk name='vdc' snapshot='no'/>
95
#   </disks>
96
# </domainsnapshot>
97

    
98
DOC="
99
<domainsnapshot>
100
  <name>${DISK_ID}-${SNAP_ID}</name>
101
  <description>snap ${DISK_ID}-${SNAP_ID}</description>
102
  <disks>"
103

    
104
for disk in $ALL_DISKS; do
105
    if [ "$disk" = "$TARGET" ]; then
106
        DOC="$DOC
107
    <disk name='$disk' snapshot='external'>
108
      <source file='$SNAP_PATH'/>
109
    </disk>"
110
    else
111
        DOC="$DOC
112
    <disk name='$disk' snapshot='no'/>"
113
    fi
114
done
115

    
116
DOC="$DOC
117
  </disks>
118
</domainsnapshot>"
119

    
120
CMD=$(cat <<EOT
121
set -ex
122

    
123
mkdir -p "${SNAP_DIR}"
124

    
125
PREVIOUS_SNAP=$(readlink $SYSTEM_DS_DISK_PATH)
126

    
127
# The file must be created beforehand or libvirt complains with
128
# "permission denied"
129
touch $SNAP_PATH
130

    
131
# Temporary xml file
132
FILENAME="/tmp/snapshot-$VMID-$DISK_ID-$SNAP_ID"
133
echo -e "$DOC" > \$FILENAME
134

    
135
virsh -c qemu:///system snapshot-create $DEPLOY_ID --disk-only --atomic \
136
    --xmlfile \$FILENAME
137

    
138
rm \${FILENAME}
139

    
140
qemu-img rebase -u -b "\${PREVIOUS_SNAP}" "${SNAP_PATH}"
141
ln -sf $SNAP_PATH $SYSTEM_DS_DISK_PATH
142
EOT
143
)
144

    
145
ssh_exec_and_log "${SRC_HOST}" "${CMD}" \
146
                 "Error creating snapshot ${SNAP_PATH}"
147