[pve-devel] [PATCH qemu-server v5 1/3] Refactor the resolution of target disk format in own sub

Emmanuel Kasper e.kasper at proxmox.com
Mon May 22 10:34:45 CEST 2017


This will allow use to reuse the code in ImportDisk.pm.
---
 PVE/QemuServer.pm | 33 ++++++++++++++++++++++-----------
 1 file changed, 22 insertions(+), 11 deletions(-)

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 2fb419d..b5075d0 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6146,20 +6146,11 @@ sub clone_disk {
 	my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
 	$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);
-	}
-
-	# test if requested format is supported - else use default
-	my $supported = grep { $_ eq $format } @$validFormats;
-	$format = $defFormat if !$supported;
-
+	my $dst_format = resolve_dst_disk_format($storecfg, $storeid, $volname, $format);
 	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));
+	$newvolid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $newvmid, $dst_format, undef, ($size/1024));
 	push @$newvollist, $newvolid;
 
 	PVE::Storage::activate_volumes($storecfg, [$newvolid]);
@@ -6365,6 +6356,26 @@ sub windows_version {
     return $winversion;
 }
 
+sub resolve_dst_disk_format {
+	my ($storecfg, $storeid, $src_volname, $format) = @_;
+	my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
+
+	if (!$format) {
+	    # if no target format is specified, use the source disk format as hint
+	    if ($src_volname) {
+		my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+		$format = qemu_img_format($scfg, $src_volname);
+	    } else {
+		return $defFormat;
+	    }
+	}
+
+	# test if requested format is supported - else use default
+	my $supported = grep { $_ eq $format } @$validFormats;
+	$format = $defFormat if !$supported;
+	return $format;
+}
+
 # bash completion helper
 
 sub complete_backup_archives {
-- 
2.11.0





More information about the pve-devel mailing list