From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.tschlatscher@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 206E3C71C
 for <pve-devel@lists.proxmox.com>; Tue, 29 Nov 2022 15:01:43 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id DD7B76B12
 for <pve-devel@lists.proxmox.com>; Tue, 29 Nov 2022 15:01:12 +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 <pve-devel@lists.proxmox.com>; Tue, 29 Nov 2022 15:01:08 +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 6E77644B5A
 for <pve-devel@lists.proxmox.com>; Tue, 29 Nov 2022 15:01:08 +0100 (CET)
From: Daniel Tschlatscher <d.tschlatscher@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Tue, 29 Nov 2022 15:00:18 +0100
Message-Id: <20221129140019.203126-1-d.tschlatscher@proxmox.com>
X-Mailer: git-send-email 2.30.2
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.123 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. [lxc.pm]
Subject: [pve-devel] [PATCH container v2 1/2] restore: clean up config when
 invalid source archive is given
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: Tue, 29 Nov 2022 14:01:43 -0000

Before, if a non-existent source archive parameter was passed when
restoring a container, the task would fail but leave an empty config
file behind. The same with invalid mount point configurations.
In both cases, the empty config will now be removed.

Signed-off-by: Daniel Tschlatscher <d.tschlatscher@proxmox.com>
---
Changes from v1:
* According to Thomas' suggestion I revised the code so that all
  errors are now handled centrally in the lower clean up handler which
  before was only used for the case when the config was unrecoverable
  and merged the two consecutive eval blocks into one.
  For this, I repurposed the $remove_lock variable and renamed it to
  better reflect its new usage.

 src/PVE/API2/LXC.pm | 19 +++++++++----------
 1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 03d7ea0..f2113de 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -372,12 +372,13 @@ __PACKAGE__->register_method({
 	eval { PVE::LXC::Config->create_and_lock_config($vmid, $force) };
 	die "$emsg $@" if $@;
 
-	my $remove_lock = 1;
+	my $destroy_config_on_error = !$same_container_exists;
 
 	my $code = sub {
 	    my $old_conf = PVE::LXC::Config->load_config($vmid);
 	    my $was_template;
 
+	    my $vollist = [];
 	    eval {
 		my $orig_mp_param; # only used if $restore
 		if ($restore) {
@@ -444,14 +445,10 @@ __PACKAGE__->register_method({
 			$mp_param->{rootfs} = "$storage:4"; # defaults to 4GB
 		    }
 		}
-	    };
-	    die "$emsg $@" if $@;
 
-	    # up until here we did not modify the container, besides the lock
-	    $remove_lock = 0;
+		# up until here we did not modify the container, besides the lock
+		$destroy_config_on_error = 1;
 
-	    my $vollist = [];
-	    eval {
 		$vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
 
 		# we always have the 'create' lock so check for more than 1 entry
@@ -499,8 +496,10 @@ __PACKAGE__->register_method({
 	    };
 	    if (my $err = $@) {
 		PVE::LXC::destroy_disks($storage_cfg, $vollist);
-		eval { PVE::LXC::Config->destroy_config($vmid) };
-		warn $@ if $@;
+		if ($destroy_config_on_error) {
+		    eval { PVE::LXC::Config->destroy_config($vmid) };
+		    warn $@ if $@;
+		}
 		die "$emsg $err";
 	    }
 	    PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
@@ -516,7 +515,7 @@ __PACKAGE__->register_method({
 	    };
 	    if (my $err = $@) {
 		# if we aborted before changing the container, we must remove the create lock
-		if ($remove_lock) {
+		if (!$destroy_config_on_error) {
 		    PVE::LXC::Config->remove_lock($vmid, 'create');
 		}
 		die $err;
-- 
2.30.2