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 055449EC72 for ; Fri, 3 Nov 2023 12:53:50 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id D94EA1C945 for ; Fri, 3 Nov 2023 12:53:49 +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 for ; Fri, 3 Nov 2023 12:53:44 +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 7D10444293 for ; Fri, 3 Nov 2023 12:53:44 +0100 (CET) From: Dominik Csapak To: pve-devel@lists.proxmox.com Date: Fri, 3 Nov 2023 12:53:35 +0100 Message-Id: <20231103115343.4133611-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20231103115343.4133611-1-d.csapak@proxmox.com> References: <20231103115343.4133611-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.017 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 T_SCC_BODY_TEXT_LINE -0.01 - Subject: [pve-devel] [RFC PATCH guest-common 1/1] add profiles section config plugin 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: Fri, 03 Nov 2023 11:53:50 -0000 this is intended to house custom profiles which can be used on guest creation instead of manually needing to specify every option. we do special things here: * we always set 'allow_unknown' to 1, because when using the guest specific parts in the cli, we cannot depend on the other one, else we'd get a cyclic dependency, and without that we need to ignore unknown sections * we'll prefix the options with the type, so that there is not a conflict, (e.g. net0 on ct vs net0 on vm has a different format) but we resolve that transparently on parse and write, so that the consumer doesn't have to do it themselves Signed-off-by: Dominik Csapak --- src/Makefile | 2 ++ src/PVE/Profiles/Plugin.pm | 72 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 src/PVE/Profiles/Plugin.pm diff --git a/src/Makefile b/src/Makefile index cbc40c1..d99645c 100644 --- a/src/Makefile +++ b/src/Makefile @@ -17,6 +17,8 @@ install: PVE install -d ${PERL5DIR}/PVE/Mapping install -m 0644 PVE/Mapping/PCI.pm ${PERL5DIR}/PVE/Mapping/ install -m 0644 PVE/Mapping/USB.pm ${PERL5DIR}/PVE/Mapping/ + install -d ${PERL5DIR}/PVE/Profiles + install -m 0644 PVE/Profiles/Plugin.pm ${PERL5DIR}/PVE/Profiles/ install -d ${PERL5DIR}/PVE/VZDump install -m 0644 PVE/VZDump/Plugin.pm ${PERL5DIR}/PVE/VZDump/ install -m 0644 PVE/VZDump/Common.pm ${PERL5DIR}/PVE/VZDump/ diff --git a/src/PVE/Profiles/Plugin.pm b/src/PVE/Profiles/Plugin.pm new file mode 100644 index 0000000..8ec5054 --- /dev/null +++ b/src/PVE/Profiles/Plugin.pm @@ -0,0 +1,72 @@ +package PVE::Profiles::Plugin; + +use strict; +use warnings; + +use PVE::Cluster qw(cfs_register_file); +use PVE::SectionConfig; + +use base qw(PVE::SectionConfig); + +cfs_register_file ('virtual-guest/profiles.cfg', + sub { __PACKAGE__->parse_config(@_); }, + sub { __PACKAGE__->write_config(@_); }); + +my $defaultData = { + propertyList => { + type => { description => 'Profile type.' }, + id => { + type => 'string', + description => "The ID of the profile.", + format => 'pve-configid', + }, + }, +}; + +sub private { + return $defaultData; +} + + +# on parse/write we map the configs from {type}_{prop} to {prop} +# so that the consumers can simply use it but we can +# have e.g. a ct_net0 and vm_net0 +sub parse_config { + my ($class, $filename, $raw, $allow_unknown) = @_; + + # always allow unknown, so that qemu-server/pct-container + # can parse the file without loading the other plugin type + my $cfg = $class->SUPER::parse_config($filename, $raw, 1); + for my $id (keys $cfg->{ids}->%*) { + my $entry = $cfg->{ids}->{$id}; + my $type = $entry->{type}; + for my $opt (keys $entry->%*) { + next if $opt eq 'type'; + if ($opt =~ m/^${type}_(.*)$/) { + my $newopt = $1; + $entry->{$newopt} = delete $entry->{$opt}; + } + } + } + + return $cfg; +} + +sub write_config { + my ($class, $filename, $cfg, $allow_unknown) = @_; + + for my $id (keys $cfg->{ids}->%*) { + my $entry = $cfg->{ids}->{$id}; + my $type = $entry->{type}; + for my $opt (keys $entry->%*) { + next if $opt eq 'type'; + next if $opt =~ m/^${type}_/; # skip already prefixed options + my $newopt = "${type}_${opt}"; + $entry->{$newopt} = delete $entry->{$opt}; + } + } + + return $class->SUPER::write_config($filename, $cfg, $allow_unknown); +} + +1; -- 2.30.2