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

Emmanuel Kasper e.kasper at proxmox.com
Tue May 9 16:26:04 CEST 2017


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

diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 2fb419d..8ea411a 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 = choose_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,20 @@ sub windows_version {
     return $winversion;
 }
 
+sub choose_dst_disk_format {
+	my ($storecfg, $storeid, $src_volname, $format) = @_;
+	my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
+	if (!$format && $src_volname) {
+	    # if no target format is specified, use the source disk format as hint
+	    my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
+	    $format = qemu_img_format($scfg, $src_volname);
+	}
+	# 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