[pve-devel] [PATCH] clone_disk : add support for storagecopy for full clone

Alexandre Derumier aderumier at odiso.com
Mon Nov 14 10:08:52 CET 2016


if src storeid == dst storeid, and storage support fast copy,
use it instead using qemu-img convert

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/QemuServer.pm | 52 +++++++++++++++++++++++++++++-----------------------
 1 file changed, 29 insertions(+), 23 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 7965c05..13c99d2 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6063,38 +6063,44 @@ sub clone_disk {
     } else {
 
 	my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
+	my $src_storeid = $storeid;
 	$storeid = $storage if $storage;
 
-	my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
-	if (!$format) {
-	    my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-	    $format = qemu_img_format($scfg, $volname);
-	}
+	if ((!$running || $snapname) && $storeid eq $src_storeid && PVE::Storage::volume_has_feature($storecfg, 'storagecopy', $drive->{file})) {
+	    $newvolid = PVE::Storage::vdisk_copy($storecfg, $drive->{file}, $newvmid, $snapname);
+	} else {
+
+	    my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
+	    if (!$format) {
+		my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+		$format = qemu_img_format($scfg, $volname);
+	    }
 
-	# test if requested format is supported - else use default
-	my $supported = grep { $_ eq $format } @$validFormats;
-	$format = $defFormat if !$supported;
+	    # test if requested format is supported - else use default
+	    my $supported = grep { $_ eq $format } @$validFormats;
+	    $format = $defFormat if !$supported;
 
-	my ($size) = PVE::Storage::volume_size_info($storecfg, $drive->{file}, 3);
+	    my ($size) = PVE::Storage::volume_size_info($storecfg, $drive->{file}, 3);
 
-	print "create full clone of drive $drivename ($drive->{file})\n";
-	$newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $newvmid, $format, undef, ($size/1024));
-	push @$newvollist, $newvolid;
+	    print "create full clone of drive $drivename ($drive->{file})\n";
+	    $newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $newvmid, $format, undef, ($size/1024));
+	    push @$newvollist, $newvolid;
 
-	PVE::Storage::activate_volumes($storecfg, [$newvolid]);
+	    PVE::Storage::activate_volumes($storecfg, [$newvolid]);
 
-	my $sparseinit = PVE::Storage::volume_has_feature($storecfg, 'sparseinit', $newvolid);
-	if (!$running || $snapname) {
-	    qemu_img_convert($drive->{file}, $newvolid, $size, $snapname, $sparseinit);
-	} else {
+	    my $sparseinit = PVE::Storage::volume_has_feature($storecfg, 'sparseinit', $newvolid);
+	    if (!$running || $snapname) {
+		qemu_img_convert($drive->{file}, $newvolid, $size, $snapname, $sparseinit);
+	    } else {
 
-	    my $kvmver = get_running_qemu_version ($vmid);
-	    if (!qemu_machine_feature_enabled (undef, $kvmver, 2, 7)) {
-		die "drive-mirror with iothread requires qemu version 2.7 or higher\n"
-		    if $drive->{iothread};
-	    }
+		my $kvmver = get_running_qemu_version ($vmid);
+		if (!qemu_machine_feature_enabled (undef, $kvmver, 2, 7)) {
+		    die "drive-mirror with iothread requires qemu version 2.7 or higher\n"
+			if $drive->{iothread};
+		}
 
-	    qemu_drive_mirror($vmid, $drivename, $newvolid, $newvmid, $sparseinit, $jobs, $skipcomplete);
+		qemu_drive_mirror($vmid, $drivename, $newvolid, $newvmid, $sparseinit, $jobs, $skipcomplete);
+	    }
 	}
     }
 
-- 
2.1.4




More information about the pve-devel mailing list