From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 8C5D31FF0C2 for ; Thu, 03 Dec 2026 10:58:52 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 378812174D; Thu, 03 Dec 2026 10:58:48 +0100 (CET) From: POYEN Alexis To: pve-devel@lists.proxmox.com Subject: [RFC PATCH manager 1/3] close #4186: vzdump: select guests by tag Date: Thu, 3 Dec 2026 10:27:08 +0100 Message-ID: <20261203092717.1863218-3-alexis.poyen@groupe-cyllene.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203092717.1863218-1-alexis.poyen@groupe-cyllene.com> References: <20261203092717.1863218-1-alexis.poyen@groupe-cyllene.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 DMARC_PASS -0.1 DMARC pass policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RDNS_NONE 1.274 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record X-MailFrom: alexis.poyen@groupe-cyllene.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation Message-ID-Hash: U6HDI2BUHNPOHSIRY6N23OX6GTV6QPEF X-Message-ID-Hash: U6HDI2BUHNPOHSIRY6N23OX6GTV6QPEF X-Mailman-Approved-At: Thu, 03 Dec 2026 10:58:46 +0100 CC: POYEN Alexis X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Allow vzdump backup jobs to select the guests to back up by tag: all guests having at least one of the given tags are included. The new 'tags' option is mutually exclusive with the existing 'vmid', 'all', 'exclude' and 'pool' selection modes. Add unit tests covering tag selection, tag selection combined with a node limit, multiple tags, and a tag without any match. Signed-off-by: POYEN Alexis --- PVE/VZDump.pm | 31 ++++++++++++- test/vzdump_guest_included_test.pl | 71 +++++++++++++++++++++++++++++- 2 files changed, 100 insertions(+), 2 deletions(-) diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm index 5b9397cd..c4675449 100644 --- a/PVE/VZDump.pm +++ b/PVE/VZDump.pm @@ -1533,6 +1533,18 @@ sub verify_vzdump_parameters { raise_param_exc({ pool => "option conflicts with option 'vmid'" }) if $param->{pool} && $param->{vmid}; + raise_param_exc({ tags => "option conflicts with option 'vmid'" }) + if $param->{tags} && $param->{vmid}; + + raise_param_exc({ tags => "option conflicts with option 'all'" }) + if $param->{tags} && $param->{all}; + + raise_param_exc({ tags => "option conflicts with option 'exclude'" }) + if $param->{tags} && $param->{exclude}; + + raise_param_exc({ tags => "option conflicts with option 'pool'" }) + if $param->{tags} && $param->{pool}; + $parse_prune_backups->($param, 'CLI parameters'); parse_fleecing($param); parse_performance($param); @@ -1547,7 +1559,8 @@ sub verify_vzdump_parameters { return if !$check_missing; raise_param_exc({ vmid => "property is missing" }) - if !($param->{all} || $param->{stop} || $param->{pool}) && !$param->{vmid}; + if !($param->{all} || $param->{stop} || $param->{pool} || $param->{tags}) + && !$param->{vmid}; } @@ -1584,6 +1597,22 @@ sub get_included_guests { if ($job->{pool}) { $vmids = PVE::API2Tools::get_resource_pool_guest_members($job->{pool}); + } elsif ($job->{tags}) { + # select guests by tags + my $tags = { map { $_ => 1 } split_list($job->{tags}) }; + my $props = PVE::Cluster::get_guest_config_properties(['tags']); + + for my $id (keys %{ $vmlist->{ids} }) { + my $guest_tags = $props->{$id}->{tags} // ''; + my $has_tag = 0; + for my $tag (split_list($guest_tags)) { + if ($tags->{$tag}) { + $has_tag = 1; + last; + } + } + push @$vmids, $id if $has_tag; + } } elsif ($job->{vmid}) { $vmids = [split_list($job->{vmid})]; } elsif ($job->{all}) { diff --git a/test/vzdump_guest_included_test.pl b/test/vzdump_guest_included_test.pl index be42e9ab..de9db6e5 100755 --- a/test/vzdump_guest_included_test.pl +++ b/test/vzdump_guest_included_test.pl @@ -5,7 +5,7 @@ use warnings; use lib '..'; -use Test::More tests => 10; +use Test::More tests => 14; use Test::MockModule; use PVE::VZDump; @@ -53,12 +53,32 @@ my $pools = { testpool => [100, 101, 200, 201], }; +my $guest_tags = { + 100 => 'prod,web', + 101 => 'prod', + 112 => 'dev', + 200 => 'prod,db', + 201 => 'dev', + 212 => 'db', + 213 => 'prod', +}; + my $pve_cluster_module = Test::MockModule->new('PVE::Cluster'); $pve_cluster_module->mock( get_vmlist => sub { return $vmlist; }, ); +$pve_cluster_module->mock( + get_guest_config_properties => sub { + my ($prop_list) = @_; + my $res = {}; + for my $id (keys %{ $vmlist->{ids} }) { + $res->{$id}->{tags} = $guest_tags->{$id} // ''; + } + return $res; + }, +); my $pve_inotify = Test::MockModule->new('PVE::INotify'); $pve_inotify->mock( @@ -215,6 +235,55 @@ $addtest->( }, ); +$addtest->( + 'Test guests by tag', + { + expected => { + node1 => [100, 101], + node2 => [200, 213], + }, + param => { + tags => 'prod', + }, + }, +); + +$addtest->( + 'Test guests by tag with node limit', + { + expected => { + node2 => [200, 213], + }, + param => { + tags => 'prod', + node => 'node2', + }, + }, +); + +$addtest->( + 'Test guests by multiple tags', + { + expected => { + node1 => [100, 101, 112], + node2 => [200, 201, 213], + }, + param => { + tags => 'prod,dev', + }, + }, +); + +$addtest->( + 'Test guests by tag with no matches', + { + expected => {}, + param => { + tags => 'nonexistent', + }, + }, +); + for my $test (@{$tests}) { my $testname = $test->{name}; my $testdata = $test->{test}; -- 2.53.0