Revision 40086e5c src/tm_mad/qcow2/snap_create

View differences:

src/tm_mad/qcow2/snap_create
52 52
    XPATH_ELEMENTS[i++]="$element"
53 53
done < <(onevm show -x $VMID| $XPATH \
54 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/HISTORY_RECORDS/HISTORY/VMMMAD \
58
                    /VM/STATE \
59
                    /VM/LCM_STATE \
60
                    /VM/DEPLOY_ID \
61
                    '%m%/VM/TEMPLATE/DISK/TARGET')
55
                    /VM/TEMPLATE/DISK[DISK_ID=$DISK_ID]/CLONE )
62 56

  
63 57
DISK_SRC="${XPATH_ELEMENTS[j++]}"
64 58
CLONE="${XPATH_ELEMENTS[j++]}"
65
TARGET="${XPATH_ELEMENTS[j++]}"
66
VMMMAD="${XPATH_ELEMENTS[j++]}"
67
STATE="${XPATH_ELEMENTS[j++]}"
68
LCM_STATE="${XPATH_ELEMENTS[j++]}"
69
DEPLOY_ID="${XPATH_ELEMENTS[j++]}"
70
ALL_DISKS="${XPATH_ELEMENTS[j++]}"
71 59

  
72 60
SYSTEM_DS_PATH=$(dirname ${SRC_PATH})
73
IMAGE_DS_PATH=$(dirname ${DISK_SRC})
74

  
75 61
SYSTEM_DS_DISK_PATH="${SYSTEM_DS_PATH}/disk.${DISK_ID}"
76 62

  
77 63
if [ "$CLONE" = "YES" ]; then
78 64
    DISK_PATH="${SYSTEM_DS_DISK_PATH}"
79
    DISK_PATH_ORIG="${DISK_PATH}.orig"
80 65
else
81 66
    DISK_PATH=$DISK_SRC
82
    DISK_PATH_ORIG=$DISK_PATH
83 67
fi
84 68

  
85 69
NEXT_SNAP_ID=$(($SNAP_ID+1))
......
87 71
SNAP_DIR="${DISK_PATH}.snap"
88 72
SNAP_PATH="${SNAP_DIR}/${NEXT_SNAP_ID}"
89 73

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

  
95
# <domainsnapshot>
96
#   <name>1</name>
97
#   <description>snap 1</description>
98
#   <disks>
99
#     <disk name='vda' snapshot='no'/>
100
#     <disk name='vdb'>
101
#       <source file='/var/lib/one/datastores/1/09eec196c67b38dea41048a628978600.snap/1'/>
102
#     </disk>
103
#     <disk name='vdc' snapshot='no'/>
104
#   </disks>
105
# </domainsnapshot>
106

  
107
DOC="
108
<domainsnapshot>
109
  <name>${DISK_ID}-${SNAP_ID}</name>
110
  <description>snap ${DISK_ID}-${SNAP_ID}</description>
111
  <disks>"
112

  
113
for disk in $ALL_DISKS; do
114
    if [ "$disk" = "$TARGET" ]; then
115
        DOC="$DOC
116
    <disk name='$disk' snapshot='external'>
117
      <source file='$SNAP_PATH'/>
118
    </disk>"
119
    else
120
        DOC="$DOC
121
    <disk name='$disk' snapshot='no'/>"
122
    fi
123
done
124

  
125
DOC="$DOC
126
  </disks>
127
</domainsnapshot>"
128

  
129
CMD=$(cat <<EOT
74
SNAP_CMD=$(cat <<EOT
130 75
set -ex
131 76

  
132
if [ ! -d "${SNAP_DIR}" ]; then
133
    mkdir "${SNAP_DIR}"
134
fi
77
mkdir -p "${SNAP_DIR}"
135 78

  
136 79
PREVIOUS_SNAP=$(readlink $SYSTEM_DS_DISK_PATH)
137 80

  
138
# 57 is DISK_SNAPSHOT. When the snapshot is done with the VM in running state
139
# TODO: LCM_STATE will *always* be 57. Need to check virsh status.
140
if [ "$LCM_STATE" = "57" ] && [ "$VMMMAD" = "kvm" ] ; then
141
    # The file must be created beforehand or libvirt complains with
142
    # "permission denied"
143
    touch $SNAP_PATH
144

  
145
    # Temporary xml file
146
    FILENAME="/tmp/snapshot-$VMID-$DISK_ID-$SNAP_ID"
147
    echo -e "$DOC" > \$FILENAME
148

  
149
    virsh -c qemu:///system snapshot-create $DEPLOY_ID --disk-only --atomic \
150
        --xmlfile \$FILENAME
151

  
152
    rm \${FILENAME}
153

  
154
    qemu-img rebase -u -b "\${PREVIOUS_SNAP}" "${SNAP_PATH}"
155
    ln -sf $SNAP_PATH $SYSTEM_DS_DISK_PATH
156
else
157
    qemu-img create -f qcow2 -b "\${PREVIOUS_SNAP}" "${SNAP_PATH}"
158
    ln -sf $SNAP_PATH $SYSTEM_DS_DISK_PATH
159
fi
81
qemu-img create -f qcow2 -b "\${PREVIOUS_SNAP}" "${SNAP_PATH}"
82
ln -sf $SNAP_PATH $SYSTEM_DS_DISK_PATH
160 83
EOT
161 84
)
162 85

  
163
ssh_exec_and_log "${SRC_HOST}" "${CMD}" \
86
ssh_exec_and_log "${SRC_HOST}" "${SNAP_CMD}" \
164 87
                 "Error creating snapshot ${SNAP_PATH}"
165 88

  

Also available in: Unified diff