]> git.proxmox.com Git - pve-container.git/commitdiff
restore: clean up config when invalid source archive is given
authorDaniel Tschlatscher <d.tschlatscher@proxmox.com>
Tue, 29 Nov 2022 14:00:18 +0000 (15:00 +0100)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Thu, 22 Dec 2022 12:47:10 +0000 (13:47 +0100)
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>
src/PVE/API2/LXC.pm

index 03d7ea01c20317d3444f48420dfd506bb88018f0..f2113deff7de611fa779530d56e4a6e1abbde738 100644 (file)
@@ -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;