[pve-devel] [PATCH qemu-server 2/2] api: template: invert lock and fork

Fabian Grünbichler f.gruenbichler at proxmox.com
Fri Sep 10 09:48:20 CEST 2021


like for other API calls, repeat the cheap checks done for early abort
before forking and without locks after forking and obtaining the lock,
and only hold the flock in the forked worker instead of across the fork.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
not sure whether we want to switch to the "progress bar -> task viewer"
thing in the GUI for this endpoint? it's usually instant, except when
the storage is very slow/overloaded/..

 PVE/API2/Qemu.pm | 23 ++++++++++++++---------
 1 file changed, 14 insertions(+), 9 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 0a23525..a8fbd9d 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -4353,8 +4353,7 @@ __PACKAGE__->register_method({
 
 	my $disk = extract_param($param, 'disk');
 
-	my $updatefn =  sub {
-
+	my $load_and_check = sub {
 	    my $conf = PVE::QemuConfig->load_config($vmid);
 
 	    PVE::QemuConfig->check_lock($conf);
@@ -4368,17 +4367,23 @@ __PACKAGE__->register_method({
 	    die "you can't convert a VM to template if VM is running\n"
 		if PVE::QemuServer::check_running($vmid);
 
-	    my $realcmd = sub {
-		PVE::QemuServer::template_create($vmid, $conf, $disk);
-	    };
+	    return $conf;
+	};
 
-	    $conf->{template} = 1;
-	    PVE::QemuConfig->write_config($vmid, $conf);
+	$load_and_check->();
 
-	    return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
+	my $realcmd = sub {
+	    PVE::QemuConfig->lock_config($vmid, sub {
+		my $conf = $load_and_check->();
+
+		$conf->{template} = 1;
+		PVE::QemuConfig->write_config($vmid, $conf);
+
+		PVE::QemuServer::template_create($vmid, $conf, $disk);
+	    });
 	};
 
-	return PVE::QemuConfig->lock_config($vmid, $updatefn);
+	return $rpcenv->fork_worker('qmtemplate', $vmid, $authuser, $realcmd);
     }});
 
 __PACKAGE__->register_method({
-- 
2.30.2






More information about the pve-devel mailing list