From 3e8e214d73949c766ba33bbf85a8b69c32d22759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominic=20J=C3=A4ger?= Date: Tue, 15 Oct 2019 12:17:41 +0200 Subject: [PATCH] Fix #2412: Missing VMs in pools MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Between calling vm_destroy and removing the ID from user.cfg (remove_vm_access) creating a new VM with this ID was possible. VMs could go missing from pools as a consequence. Adding a lock solves this for clones from the same node. Additionally, unlinking must happen at the very end of the deletion process to avoid that other nodes use the ID in the meanwhile. Co-developed-by: Fabian Grünbichler Signed-off-by: Dominic Jäger --- PVE/API2/Qemu.pm | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 267a08e8..7e1d3147 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -1492,9 +1492,15 @@ __PACKAGE__->register_method({ my $upid = shift; syslog('info', "destroy VM $vmid: $upid\n"); - PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); - PVE::AccessControl::remove_vm_access($vmid); - PVE::Firewall::remove_vmfw_conf($vmid); + PVE::QemuConfig->lock_config($vmid, sub { + die "VM $vmid is running - destroy failed\n" + if (PVE::QemuServer::check_running($vmid)); + PVE::QemuServer::destroy_vm($storecfg, $vmid, 1, $skiplock); + PVE::AccessControl::remove_vm_access($vmid); + PVE::Firewall::remove_vmfw_conf($vmid); + unlink PVE::QemuConfig->config_file($vmid) + or die "Removal of VM $vmid config file failed: $!\n"; + }); }; return $rpcenv->fork_worker('qmdestroy', $vmid, $authuser, $realcmd);