[pve-devel] [RFC v10 qemu-server 7/7] api: create disks: factor out common part from if/else

Fabian Ebner f.ebner at proxmox.com
Thu Jan 13 11:08:31 CET 2022


Signed-off-by: Fabian Ebner <f.ebner at proxmox.com>
---

New in v10.

 PVE/API2/Qemu.pm | 16 ++++++----------
 1 file changed, 6 insertions(+), 10 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 8c74ecc..fa6aa9c 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -204,7 +204,7 @@ my $create_disks = sub {
 		my $src_size = PVE::Storage::file_size_info($source);
 		die "Could not get file size of $source" if !defined($src_size);
 
-		my (undef, $dst_volid) = PVE::QemuServer::ImportDisk::do_import(
+		(undef, $volid) = PVE::QemuServer::ImportDisk::do_import(
 		    $source,
 		    $vmid,
 		    $storeid,
@@ -215,18 +215,13 @@ my $create_disks = sub {
 		    },
 		);
 
-		push @$vollist, $dst_volid;
-		$disk->{file} = $dst_volid;
 		$disk->{size} = $src_size;
-		delete $disk->{format}; # no longer needed
-		$res->{$ds} = PVE::QemuServer::print_drive($disk);
 	    } else {
 		my $defformat = PVE::Storage::storage_default_format($storecfg, $storeid);
 		my $fmt = $disk->{format} || $defformat;
 
 		$size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb
 
-		my $volid;
 		if ($ds eq 'efidisk0') {
 		    my $smm = PVE::QemuServer::Machine::machine_type_is_q35($conf);
 		    ($volid, $size) = PVE::QemuServer::create_efidisk(
@@ -238,12 +233,13 @@ my $create_disks = sub {
 		} else {
 		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size);
 		}
-		push @$vollist, $volid;
-		$disk->{file} = $volid;
 		$disk->{size} = PVE::Tools::convert_size($size, 'kb' => 'b');
-		delete $disk->{format}; # no longer needed
-		$res->{$ds} = PVE::QemuServer::print_drive($disk);
 	    }
+
+	    push @$vollist, $volid;
+	    $disk->{file} = $volid;
+	    delete $disk->{format}; # no longer needed
+	    $res->{$ds} = PVE::QemuServer::print_drive($disk);
 	} else {
 
 	    PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
-- 
2.30.2






More information about the pve-devel mailing list