[pve-devel] [PATCH qemu-server 1/2] create_disks: refactor out and improve uefidisk creation

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Aug 25 15:22:59 CEST 2017


factor out code in a new create_efidisk submethod, as else this is
hardly readable, the efidisk0 case is a special case to, so refer
from putting this specialised handling directly into the much shorter
code for all other cases.

Also the disk was created with a specific format and then a format
detection on the newly created disk was done, which is pretty
useless.

Also combine setting drive->size and drive->file.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 PVE/API2/Qemu.pm  | 30 ++++++------------------------
 PVE/QemuServer.pm | 17 +++++++++++++++++
 2 files changed, 23 insertions(+), 24 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index aa7c832..e8bb4a9 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -142,34 +142,16 @@ my $create_disks = sub {
 
 	    my $volid;
 	    if ($ds eq 'efidisk0') {
-		# handle efidisk
-		my $ovmfvars = '/usr/share/kvm/OVMF_VARS-pure-efi.fd';
-		die "uefi vars image not found\n" if ! -f $ovmfvars;
-		$volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
-						      $fmt, undef, 128);
-		$disk->{file} = $volid;
-		$disk->{size} = 128*1024;
-		my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid);
-		my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-		my $qemufmt = PVE::QemuServer::qemu_img_format($scfg, $volname);
-		my $path = PVE::Storage::path($storecfg, $volid);
-		my $efidiskcmd = ['/usr/bin/qemu-img', 'convert', '-n', '-f', 'raw', '-O', $qemufmt];
-		push @$efidiskcmd, $ovmfvars;
-		push @$efidiskcmd, $path;
-
-		PVE::Storage::activate_volumes($storecfg, [$volid]);
-
-		eval { PVE::Tools::run_command($efidiskcmd); };
-		my $err = $@;
-		die "Copying of EFI Vars image failed: $err" if $err;
+		($volid, $size) = PVE::QemuServer::create_uefidisk($vmid, $storeid, $fmt, $storecfg);
 	    } else {
-		$volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid,
-						      $fmt, undef, $size*1024*1024);
-		$disk->{file} = $volid;
-		$disk->{size} = $size*1024*1024*1024;
+		$volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size<<20);
+		$size <<= 30; # gigabytes to bytes
 	    }
 	    push @$vollist, $volid;
+	    $disk->{file} = $volid;
+	    $disk->{size} = $size;
 	    delete $disk->{format}; # no longer needed
+
 	    $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
 	} else {
 
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 2aafc42..7d9987e 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6308,6 +6308,23 @@ sub qemu_use_old_bios_files {
     return ($use_old_bios_files, $machine_type);
 }
 
+sub create_uefidisk {
+    my ($vmid, $storeid, $fmt, $storecfg) = @_;
+
+    die "uefi vars image not found\n" if ! -f $OVMF_VARS;
+
+    my $vars_size = -s $OVMF_VARS;
+    my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $vars_size>>10);
+    PVE::Storage::activate_volumes($storecfg, [$volid]);
+
+    my $path = PVE::Storage::path($storecfg, $volid);
+    eval {
+	run_command(['/usr/bin/qemu-img', 'convert', '-n', '-O', $fmt, $OVMF_VARS, $path]);
+    }; die "Copying of EFI Vars image failed: $@" if $@;
+
+    return ($volid, $vars_size);
+}
+
 sub lspci {
 
     my $devices = {};
-- 
2.11.0





More information about the pve-devel mailing list