From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
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 E021261B8A
 for <pve-devel@lists.proxmox.com>; Thu, 22 Oct 2020 12:30:32 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id 6DC161F026
 for <pve-devel@lists.proxmox.com>; Thu, 22 Oct 2020 12:30:31 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (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 6003B1EFAF
 for <pve-devel@lists.proxmox.com>; Thu, 22 Oct 2020 12:30:29 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 28B2545EDE
 for <pve-devel@lists.proxmox.com>; Thu, 22 Oct 2020 12:30:29 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Thu, 22 Oct 2020 12:30:13 +0200
Message-Id: <20201022103017.19715-5-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20201022103017.19715-1-f.ebner@proxmox.com>
References: <20201022103017.19715-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.039 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 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. [vzdump.pm]
Subject: [pve-devel] [PATCH v2 manager 4/8] backup: include IDs for
 non-existent guests
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>
X-List-Received-Date: Thu, 22 Oct 2020 10:30:32 -0000

Like this, there will be a backup task (within the big worker task)
for such IDs, which will then visibly (i.e. also visible in the
notification mail) fail with, e.g.:
unable to find VM '123'

In get_included_guests, the key '' was chosen for the orphaned IDs,
because it cannot possibly denote a nodename.

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---
 PVE/API2/VZDump.pm                 |  4 ++++
 PVE/VZDump.pm                      | 19 +++++++++++++------
 test/vzdump_guest_included_test.pl | 13 ++++++++++++-
 3 files changed, 29 insertions(+), 7 deletions(-)

diff --git a/PVE/API2/VZDump.pm b/PVE/API2/VZDump.pm
index 19fa1e3b..806ac7fd 100644
--- a/PVE/API2/VZDump.pm
+++ b/PVE/API2/VZDump.pm
@@ -74,6 +74,10 @@ __PACKAGE__->register_method ({
 
 	my $local_vmids = delete $vmids_per_node->{$nodename} // [];
 
+	# include IDs for deleted guests, and visibly fail later
+	my $orphaned_vmids = delete $vmids_per_node->{''} // [];
+	push @{$local_vmids}, @{$orphaned_vmids};
+
 	my $skiplist = [ map { @$_ } values $vmids_per_node->%* ];
 
 	if($param->{stop}){
diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm
index e6082f3b..7ff32ce2 100644
--- a/PVE/VZDump.pm
+++ b/PVE/VZDump.pm
@@ -1073,9 +1073,12 @@ sub exec_backup {
 
     my $vmlist = PVE::Cluster::get_vmlist();
     foreach my $vmid (@{$opts->{vmids}}) {
-	my $guest_type = $vmlist->{ids}->{$vmid}->{type};
-	my $plugin = $vzdump_plugins->{$guest_type};
-	next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], $opts->{all});
+	my $plugin;
+	if (defined($vmlist->{ids}->{$vmid})) {
+	    my $guest_type = $vmlist->{ids}->{$vmid}->{type};
+	    $plugin = $vzdump_plugins->{$guest_type};
+	    next if !$rpcenv->check($authuser, "/vms/$vmid", [ 'VM.Backup' ], $opts->{all});
+	}
 	push @$tasklist, {
 	    mode => $opts->{mode},
 	    plugin => $plugin,
@@ -1217,10 +1220,14 @@ sub get_included_guests {
     $vmids = check_vmids(@$vmids);
 
     for my $vmid (@$vmids) {
-	my $node = $vmlist->{ids}->{$vmid}->{node};
-	next if (defined $job->{node} && $job->{node} ne $node);
+	if (defined($vmlist->{ids}->{$vmid})) {
+	    my $node = $vmlist->{ids}->{$vmid}->{node};
+	    next if (defined $job->{node} && $job->{node} ne $node);
 
-	push @{$vmids_per_node->{$node}}, $vmid;
+	    push @{$vmids_per_node->{$node}}, $vmid;
+	} else {
+	    push @{$vmids_per_node->{''}}, $vmid;
+	}
     }
 
     return $vmids_per_node;
diff --git a/test/vzdump_guest_included_test.pl b/test/vzdump_guest_included_test.pl
index eb663ad3..157644de 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 => 9;
+use Test::More tests => 10;
 use Test::MockModule;
 
 use PVE::VZDump;
@@ -174,6 +174,17 @@ $addtest->('Test selected VMIDs on other nodes', {
     }
 });
 
+$addtest->('Test VMID not present in vmlist', {
+    expected => {
+	node1 => [ 100 ],
+	node2 => [ 201, 212 ],
+	'' => [ 7654 ],
+   },
+    param => {
+	vmid => '100, 201, 212, 7654',
+    }
+});
+
 
 for my $test (@{$tests}) {
     my $testname = $test->{name};
-- 
2.20.1