From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 72E851FF15E
	for <inbox@lore.proxmox.com>; Tue, 11 Feb 2025 17:11:14 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 3E506399F;
	Tue, 11 Feb 2025 17:09:17 +0100 (CET)
From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 11 Feb 2025 17:08:10 +0100
Message-Id: <20250211160825.254167-17-d.kral@proxmox.com>
X-Mailer: git-send-email 2.39.5
In-Reply-To: <20250211160825.254167-1-d.kral@proxmox.com>
References: <20250211160825.254167-1-d.kral@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.010 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH qemu-server v2 11/15] api: {create,
 update}_vm: use volume content type assertion helpers
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

Make use of the newly introduced content type assertion helpers in the
preconditions check paths and the call to `vdisk_alloc` in the API
handlers for creating a VM or updating a VM with newly allocated VM,
EFI, TPM and/or cloudinit images.

Since the preconditions existed before, adding a content type safeguard
at `vdisk_alloc` does not break the API.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
changes since v1:
- squashed previous rfc patch #5 and #6 as they are very related to
  another and also made explicit notice of changes to `update_vm` api

 PVE/API2/Qemu.pm            | 14 +++++++++-----
 PVE/QemuServer.pm           |  4 +++-
 PVE/QemuServer/Cloudinit.pm |  1 +
 3 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 8ce266a3..ea164eea 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -150,9 +150,8 @@ my $check_storage_access = sub {
 	    $rpcenv->check($authuser, "/", ['Sys.Console']);
 	} elsif (!$isCDROM && ($volid =~ $PVE::QemuServer::Drive::NEW_DISK_RE || $is_cloudinit)) {
 	    $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};
+	    eval { PVE::Storage::assert_content_type_supported($storecfg, $storeid, 'images') };
+	    raise_param_exc({ storage => "$@" }) if $@;
 	} else {
 	    PVE::Storage::check_volume_access($rpcenv, $authuser, $storecfg, $vmid, $volid);
 	    if ($storeid) {
@@ -416,6 +415,7 @@ my sub create_disks : prototype($$$$$$$$$$$) {
 	    my $ci_size = PVE::QemuServer::Cloudinit::CLOUDINIT_DISK_SIZE;
 	    my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $ci_size/1024, {
 		name => $name,
+		vtype => 'images',
 	    });
 	    $disk->{file} = $volid;
 	    $disk->{media} = 'cdrom';
@@ -554,9 +554,13 @@ 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', $size);
+		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, 'raw', $size, {
+			vtype => 'images',
+		    });
 		} else {
-		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $size);
+		    $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $size, {
+			vtype => 'images',
+		    });
 		}
 
 		# change created disk to a base volume in case the VM is a template
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 0cc3285f..f3178655 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -8372,7 +8372,9 @@ 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, $vars_size);
+    my $volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $fmt, $vars_size, {
+	vtype => 'images',
+    });
     PVE::Storage::activate_volumes($storecfg, [$volid]);
 
     qemu_img_convert($ovmf_vars, $volid, $vars_size_b, undef, 0);
diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index 0dc99e05..a566e811 100644
--- a/PVE/QemuServer/Cloudinit.pm
+++ b/PVE/QemuServer/Cloudinit.pm
@@ -46,6 +46,7 @@ sub commit_cloudinit_disk {
 	$size = 4 * 1024;
 	PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $size, {
 	    name => $name,
+	    vtype => 'images',
 	});
 	$size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
     }
-- 
2.39.5



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel