[pve-devel] [PATCH qemu-server v2 3/4] create_disks: refactor out and improve efidisk creation

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Sep 11 08:40:28 CEST 2017


factor out code in a new create_efidisk submethod, as else this code
is hardly readable as the efidisk0 case is a special case. Refer from
putting all this specialised handling directly to 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, clear that up.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---

changes v1 -> v2:
 * even if qemu-img currently defaults to raw for the first image (-f
   option) still explicitly set it, this default is not documented
   AFAIS, so they could change it without notice, unlikely but better
   safe than sorry here.

 PVE/API2/Qemu.pm  | 19 +------------------
 PVE/QemuServer.pm | 18 ++++++++++++++++++
 2 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 8434018..1002c87 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -144,24 +144,7 @@ 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;
-		$size = PVE::Tools::convert_size(-s $ovmfvars, 'b' => 'kb');
-		$volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size);
-		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_efidisk($storecfg, $storeid, $vmid, $fmt);
 	    } else {
 		$volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size);
 	    }
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b52c93d..e0c0c50 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6309,6 +6309,24 @@ sub qemu_use_old_bios_files {
     return ($use_old_bios_files, $machine_type);
 }
 
+sub create_efidisk {
+    my ($storecfg, $storeid, $vmid, $fmt) = @_;
+
+    die "EFI vars default image not found\n" if ! -f $OVMF_VARS;
+
+    my $vars_size = PVE::Tools::convert_size(-s $OVMF_VARS, 'b' => 'kb');
+    my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $vars_size);
+    PVE::Storage::activate_volumes($storecfg, [$volid]);
+
+    my $path = PVE::Storage::path($storecfg, $volid);
+    eval {
+	run_command(['/usr/bin/qemu-img', 'convert', '-n', '-f', 'raw', '-O', $fmt, $OVMF_VARS, $path]);
+    };
+    die "Copying EFI vars image failed: $@" if $@;
+
+    return ($volid, $vars_size);
+}
+
 sub lspci {
 
     my $devices = {};
-- 
2.11.0





More information about the pve-devel mailing list