From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 08BF160876 for ; Thu, 13 Jan 2022 11:09:09 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4949B1C26C for ; Thu, 13 Jan 2022 11:08:38 +0100 (CET) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id 7506A1C204 for ; Thu, 13 Jan 2022 11:08:35 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 4D11546AC0 for ; Thu, 13 Jan 2022 11:08:35 +0100 (CET) From: Fabian Ebner To: pve-devel@lists.proxmox.com Date: Thu, 13 Jan 2022 11:08:26 +0100 Message-Id: <20220113100831.34113-4-f.ebner@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220113100831.34113-1-f.ebner@proxmox.com> References: <20220113100831.34113-1-f.ebner@proxmox.com> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.138 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [agent.pm, qemuserver.pm, cpu.pm, ovf.pm, machine.pm] Subject: [pve-devel] [PATCH v10 qemu-server 3/7] api: add endpoint for parsing .ovf files X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 13 Jan 2022 10:09:09 -0000 Co-developed-by: Fabian Grünbichler Signed-off-by: Dominic Jäger [split into its own patch + minor improvements/style fixes] Signed-off-by: Fabian Ebner --- Changes from v9: * Include $! in the error for the file check. * Have json_ovf_properties return all of them rather than just the disk-related ones, and add description for cpu/name/memory. * Tiny style fixes foreach -> for, etc. The file check can also fail because of permission problems, since the API endpoint is not protected => 1, when used via the web UI and when the manifest is at a location not accessible to www-data (e.g. in /root/ on a default installation). Including $! in the error message helps of course, but I'm sure there'll be users wondering why they get permission errors while being logged in as root in the web UI. Not sure what to do about it though. PVE/API2/Qemu/Makefile | 2 +- PVE/API2/Qemu/OVF.pm | 55 ++++++++++++++++++++++++++++++++++++++++++ PVE/QemuServer.pm | 32 ++++++++++++++++++++++++ 3 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 PVE/API2/Qemu/OVF.pm diff --git a/PVE/API2/Qemu/Makefile b/PVE/API2/Qemu/Makefile index 5d4abda..bdd4762 100644 --- a/PVE/API2/Qemu/Makefile +++ b/PVE/API2/Qemu/Makefile @@ -1,4 +1,4 @@ -SOURCES=Agent.pm CPU.pm Machine.pm +SOURCES=Agent.pm CPU.pm Machine.pm OVF.pm .PHONY: install install: diff --git a/PVE/API2/Qemu/OVF.pm b/PVE/API2/Qemu/OVF.pm new file mode 100644 index 0000000..b1d79d2 --- /dev/null +++ b/PVE/API2/Qemu/OVF.pm @@ -0,0 +1,55 @@ +package PVE::API2::Qemu::OVF; + +use strict; +use warnings; + +use PVE::JSONSchema qw(get_standard_option); +use PVE::QemuServer::OVF; +use PVE::RESTHandler; + +use base qw(PVE::RESTHandler); + +__PACKAGE__->register_method ({ + name => 'index', + path => '', + method => 'GET', + proxyto => 'node', + description => "Read an .ovf manifest.", + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + manifest => { + description => ".ovf manifest", + type => 'string', + }, + }, + }, + returns => { + description => "VM config according to .ovf manifest.", + type => "object", + }, + returns => { + type => 'object', + additionalProperties => 1, + properties => PVE::QemuServer::json_ovf_properties({}), + }, + code => sub { + my ($param) = @_; + + my $manifest = $param->{manifest}; + die "check for file $manifest failed - $!\n" if !-f $manifest; + + my $parsed = PVE::QemuServer::OVF::parse_ovf($manifest); + my $result; + $result->{cores} = $parsed->{qm}->{cores}; + $result->{name} = $parsed->{qm}->{name}; + $result->{memory} = $parsed->{qm}->{memory}; + my $disks = $parsed->{disks}; + for my $disk (@$disks) { + $result->{$disk->{disk_address}} = $disk->{backing_file}; + } + return $result; + }}); + +1; diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index 819eb5f..98eb6b3 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -2221,6 +2221,38 @@ sub json_config_properties { return $prop; } +# Properties that we can read from an OVF file +sub json_ovf_properties { + my $prop = shift; + + for my $device (PVE::QemuServer::Drive::valid_drive_names()) { + $prop->{$device} = { + type => 'string', + format => 'pve-volume-id-or-absolute-path', + description => "Disk image that gets imported to $device", + optional => 1, + }; + } + + $prop->{cores} = { + type => 'integer', + description => "The number of CPU cores.", + optional => 1, + }; + $prop->{memory} = { + type => 'integer', + description => "Amount of RAM for the VM in MB.", + optional => 1, + }; + $prop->{name} = { + type => 'string', + description => "Name of the VM.", + optional => 1, + }; + + return $prop; +} + # return copy of $confdesc_cloudinit to generate documentation sub cloudinit_config_properties { -- 2.30.2