Skip to content

Commit 81b7e6e

Browse files
GutoVeroneziGutoVeronezi
andauthored
Fix extract snapshot from vm snapshot on kvm (apache#6422)
* Fix extract snapshot from VM snapshot on KVM * Fix validation expression - does not need to escape the slash Co-authored-by: GutoVeronezi <[email protected]>
1 parent 45ea764 commit 81b7e6e

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ public Answer backupSnapshot(final CopyCommand cmd) {
930930
final String secondaryStoragePoolUrl = nfsImageStore.getUrl();
931931
// NOTE: snapshot name is encoded in snapshot path
932932
final int index = snapshot.getPath().lastIndexOf("/");
933-
final boolean isCreatedFromVmSnapshot = (index == -1) ? true: false; // -1 means the snapshot is created from existing vm snapshot
933+
final boolean isCreatedFromVmSnapshot = index == -1; // -1 means the snapshot is created from existing vm snapshot
934934

935935
final String snapshotName = snapshot.getPath().substring(index + 1);
936936
String descName = snapshotName;
@@ -1002,7 +1002,7 @@ public Answer backupSnapshot(final CopyCommand cmd) {
10021002
}
10031003
} else {
10041004
final Script command = new Script(_manageSnapshotPath, cmd.getWaitInMillSeconds(), s_logger);
1005-
command.add("-b", snapshot.getPath());
1005+
command.add("-b", isCreatedFromVmSnapshot ? snapshotDisk.getPath() : snapshot.getPath());
10061006
command.add(NAME_OPTION, snapshotName);
10071007
command.add("-p", snapshotDestPath);
10081008
if (isCreatedFromVmSnapshot) {

scripts/storage/qcow2/managesnapshot.sh

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -226,14 +226,48 @@ backup_snapshot() {
226226
return 2
227227
fi
228228
elif [ -f ${disk} ]; then
229+
if [[ $disk == *"/snapshots/"* ]]; then
230+
#Backup volume snapshot
231+
cp "$disk" "${destPath}/${destName}"
232+
ret_code=$?
229233

230-
cp "$disk" "${destPath}/${destName}"
231-
ret_code=$?
234+
if [ $ret_code -gt 0 ]
235+
then
236+
printf "Failed to backup $snapshotname for disk $disk to $destPath\n" >&2
237+
return 2
238+
fi
239+
else
240+
# Backup VM snapshot
241+
qemuimg_ret=$($qemu_img snapshot $forceShareFlag -l $disk 2>&1)
242+
ret_code=$?
243+
if [ $ret_code -gt 0 ] && [[ $qemuimg_ret == *"snapshot: invalid option -- 'U'"* ]]; then
244+
forceShareFlag=""
245+
qemuimg_ret=$($qemu_img snapshot $forceShareFlag -l $disk)
246+
ret_code=$?
247+
fi
232248

233-
if [ $ret_code -gt 0 ]
234-
then
235-
printf "Failed to backup $snapshotname for disk $disk to $destPath\n" >&2
236-
return 2
249+
if [ $ret_code -gt 0 ] || [[ ! $qemuimg_ret == *"$snapshotname"* ]]; then
250+
printf "there is no $snapshotname on disk $disk\n" >&2
251+
return 1
252+
fi
253+
254+
qemuimg_ret=$($qemu_img convert $forceShareFlag -f qcow2 -O qcow2 -l snapshot.name=$snapshotname $disk $destPath/$destName 2>&1 > /dev/null)
255+
ret_code=$?
256+
if [ $ret_code -gt 0 ] && [[ $qemuimg_ret == *"convert: invalid option -- 'U'"* ]]; then
257+
forceShareFlag=""
258+
qemuimg_ret=$($qemu_img convert $forceShareFlag -f qcow2 -O qcow2 -l snapshot.name=$snapshotname $disk $destPath/$destName 2>&1 > /dev/null)
259+
ret_code=$?
260+
fi
261+
262+
if [ $ret_code -gt 0 ] && [[ $qemuimg_ret == *"convert: invalid option -- 'l'"* ]]; then
263+
$qemu_img convert $forceShareFlag -f qcow2 -O qcow2 -s $snapshotname $disk $destPath/$destName >& /dev/null
264+
ret_code=$?
265+
fi
266+
267+
if [ $ret_code -gt 0 ]; then
268+
printf "Failed to backup $snapshotname for disk $disk to $destPath\n" >&2
269+
return 2
270+
fi
237271
fi
238272
else
239273
printf "***Failed to backup snapshot $snapshotname, undefined type $disk\n" >&2

0 commit comments

Comments
 (0)