From 8ba8418ca1d1a76a7e24c34045ca7702b0cd969d Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Fri, 1 Jun 2018 16:37:39 +0200 Subject: [PATCH] API/create: move locking inside worker Move the locking inside worker, so that the process doing the actual work (create or restore) holds the lock, and can call functions which do locking without deadlocking. This mirrors the behaviour we use for containers, and allows to add an 'autostart' parameter which starts the VM after successful creation. vm_start needs the lock and as not the worker but it's parents held it, it couldn't know that it was actually save to continue... Signed-off-by: Thomas Lamprecht --- PVE/API2/Qemu.pm | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 8d4b10d3..c6572458 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -462,6 +462,7 @@ __PACKAGE__->register_method({ my $vmid = extract_param($param, 'vmid'); my $archive = extract_param($param, 'archive'); + my $is_restore = !!$archive; my $storage = extract_param($param, 'storage'); @@ -569,7 +570,7 @@ __PACKAGE__->register_method({ # ensure no old replication state are exists PVE::ReplicationState::delete_guest_states($vmid); - return $rpcenv->fork_worker('qmrestore', $vmid, $authuser, $realcmd); + return PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd); }; my $createfn = sub { @@ -616,10 +617,13 @@ __PACKAGE__->register_method({ PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool; }; - return $rpcenv->fork_worker('qmcreate', $vmid, $authuser, $realcmd); + return PVE::QemuConfig->lock_config_full($vmid, 1, $realcmd); }; - return PVE::QemuConfig->lock_config_full($vmid, 1, $archive ? $restorefn : $createfn); + my $worker_name = $is_restore ? 'qmrestore' : 'qmcreate'; + my $code = $is_restore ? $restorefn : $createfn; + + return $rpcenv->fork_worker($worker_name, $vmid, $authuser, $code); }}); __PACKAGE__->register_method({