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

Thomas Lamprecht t.lamprecht at proxmox.com
Mon Aug 28 10:37:44 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:
* moved "non-hardcoded size" part to own patch
* use new convert_size method and return size in kb from create_efidisk to
  match the common case better

 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 5fbb998..4bf51a1 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 2aafc42..b716491 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6308,6 +6308,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', '-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