[pve-devel] [RFC qemu-server 5/9] api: create_vm: improve checks if storages for disks support vm images

Daniel Kral d.kral at proxmox.com
Mon Sep 16 18:38:35 CEST 2024


Adds checks when creating regular volume disks, the EFI disk and the TPM
state disk if the underlying storage for these disks support the content
type 'images'. This adds parameter context to the error message and
checks right before allocating the disk with `alloc_volume_disk`.

This affects the behavior when creating disks through the create vm api
(`create_disks`) and inadvertently when updating the VM configuration
(`update_vm_api`).

Signed-off-by: Daniel Kral <d.kral at proxmox.com>
---
 PVE/API2/Qemu.pm  | 18 +++++++++---------
 PVE/QemuServer.pm |  2 +-
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index a7931d98..b6bfd68e 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -29,7 +29,7 @@ use PVE::QemuServer;
 use PVE::QemuServer::Cloudinit;
 use PVE::QemuServer::CPUConfig;
 use PVE::QemuServer::Drive;
-use PVE::QemuServer::Helpers qw(check_storage_alloc check_volume_alloc);
+use PVE::QemuServer::Helpers qw(check_storage_alloc check_volume_alloc alloc_volume_disk);
 use PVE::QemuServer::ImportDisk;
 use PVE::QemuServer::Monitor qw(mon_cmd);
 use PVE::QemuServer::Machine;
@@ -147,10 +147,10 @@ my $check_storage_access = sub {
 	} elsif (!$isCDROM && ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE)) {
 	    my ($storeid, $size) = ($2 || $default_storage, $3);
 	    die "no storage ID specified (and no default storage)\n" if !$storeid;
-	    $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
-	    my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
-	    raise_param_exc({ storage => "storage '$storeid' does not support vm images"})
-		if !$scfg->{content}->{images};
+
+	    check_storage_alloc($rpcenv, $authuser, $storeid);
+	    eval { check_volume_alloc($storecfg, $storeid) };
+	    raise_param_exc({ storage => "$@" }) if $@;
 	} else {
 	    PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
 	    if ($storeid) {
@@ -402,7 +402,7 @@ my sub create_disks : prototype($$$$$$$$$$) {
 	    my ($storeid, $size) = ($2 || $default_storage, $3);
 	    die "no storage ID specified (and no default storage)\n" if !$storeid;
 
-	    $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # vdisk_alloc uses kb
+	    $size = PVE::Tools::convert_size($size, 'gb' => 'kb'); # alloc_volume_disk uses kb
 
 	    my $live_import = $is_live_import && $ds ne 'efidisk0';
 	    my $needs_creation = 1;
@@ -465,7 +465,7 @@ my sub create_disks : prototype($$$$$$$$$$) {
 		}
 
 		if ($needs_creation) {
-		    $size = PVE::Tools::convert_size($size, 'b' => 'kb'); # vdisk_alloc uses kb
+		    $size = PVE::Tools::convert_size($size, 'b' => 'kb'); # alloc_volume_disk uses kb
 		} else {
 		    $disk->{file} = $dst_volid;
 		    $disk->{size} = $size;
@@ -486,9 +486,9 @@ my sub create_disks : prototype($$$$$$$$$$) {
 		} elsif ($ds eq 'tpmstate0') {
 		    # swtpm can only use raw volumes, and uses a fixed size
 		    $size = PVE::Tools::convert_size(PVE::QemuServer::Drive::TPMSTATE_DISK_SIZE, 'b' => 'kb');
-		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, "raw", undef, $size);
+		    $volid = alloc_volume_disk($storecfg, $storeid, $vmid, "raw", undef, $size);
 		} else {
-		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $size);
+		    $volid = alloc_volume_disk($storecfg, $storeid, $vmid, $fmt, undef, $size);
 		}
 		push @$vollist, $volid;
 		$disk->{file} = $volid;
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 507932d3..b40f6f6e 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -8449,7 +8449,7 @@ sub create_efidisk($$$$$$$) {
 
     my $vars_size_b = -s $ovmf_vars;
     my $vars_size = PVE::Tools::convert_size($vars_size_b, 'b' => 'kb');
-    my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, undef, $vars_size);
+    my $volid = alloc_volume_disk($storecfg, $storeid, $vmid, $fmt, undef, $vars_size);
     PVE::Storage::activate_volumes($storecfg, [$volid]);
 
     qemu_img_convert($ovmf_vars, $volid, $vars_size_b, undef, 0);
-- 
2.39.5





More information about the pve-devel mailing list