[pve-devel] [PATCH qemu-server] api create: cleanup the new config log on error

Wolfgang Bumiller w.bumiller at proxmox.com
Tue Jun 12 10:50:41 CEST 2018


Otherwise cases like trying to restore a protected VM would
leave a lock in the config.

Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
---
 PVE/API2/Qemu.pm | 36 ++++++++++++++++++++++++++++++++----
 1 file changed, 32 insertions(+), 4 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index a5ab282..c15c71f 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -564,7 +564,10 @@ __PACKAGE__->register_method({
 
 		PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
 
-		PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node }) if $start_after_create;
+		if ($start_after_create) {
+		    eval { PVE::API2::Qemu->vm_start({ vmid => $vmid, node => $node }) };
+		    warn $@ if $@;
+		}
 	    };
 
 	    # ensure no old replication state are exists
@@ -617,12 +620,37 @@ __PACKAGE__->register_method({
 
 	    if ($start_after_create) {
 		print "Execute autostart\n";
-		PVE::API2::Qemu->vm_start({vmid => $vmid, node => $node});
+		eval { PVE::API2::Qemu->vm_start({vmid => $vmid, node => $node}) };
+		warn $@ if $@;
 	    }
 	};
 
-	my $worker_name = $is_restore ? 'qmrestore' : 'qmcreate';
-	my $code = $is_restore ? $restorefn : $createfn;
+	my ($code, $worker_name);
+	if ($is_restore) {
+	    $worker_name = 'qmrestore';
+	    $code = sub {
+		eval { $restorefn->() };
+		if (my $err = $@) {
+		    eval { PVE::QemuConfig->remove_lock($vmid, 'create') };
+		    warn $@ if $@;
+		    die $err;
+		}
+	    };
+	} else {
+	    $worker_name = 'qmcreate';
+	    $code = sub {
+		eval { $createfn->() };
+		if (my $err = $@) {
+		    eval {
+			my $conffile = PVE::QemuConfig->config_file($vmid);
+			unlink($conffile)
+			    or die "failed to remove config file: $@\n";
+		    };
+		    warn $@ if $@;
+		    die $err;
+		}
+	    };
+	}
 
 	return $rpcenv->fork_worker($worker_name, $vmid, $authuser, $code);
     }});
-- 
2.11.0




More information about the pve-devel mailing list