[pve-devel] [PATCH cluster v5 17/17] lock locally on create and add

Thomas Lamprecht t.lamprecht at proxmox.com
Fri Jan 26 14:25:27 CET 2018


I fwe are not part of a cluster we do not need to worry about other
members messing with the config. But there may be local contenders,
e.g., two automation script instances started in parallel by mistake
or two admin (sessions) which start a create or join clsuter request
at the same time.
Reuse the local flock for this purpose.

Signed-off-by: Thomas Lamprecht <t.lamprecht at proxmox.com>
---
 data/PVE/API2/ClusterConfig.pm | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/data/PVE/API2/ClusterConfig.pm b/data/PVE/API2/ClusterConfig.pm
index 28f94a7..25f8965 100644
--- a/data/PVE/API2/ClusterConfig.pm
+++ b/data/PVE/API2/ClusterConfig.pm
@@ -16,6 +16,7 @@ use base qw(PVE::RESTHandler);
 
 my $clusterconf = "/etc/pve/corosync.conf";
 my $authfile = "/etc/corosync/authkey";
+my $local_cluster_change_lock = "/var/lock/pvecm.lock";
 
 my $ring0_desc = {
     type => 'string', format => 'address',
@@ -118,7 +119,7 @@ __PACKAGE__->register_method ({
 	my $rpcenv = PVE::RPCEnvironment::get();
 	my $authuser = $rpcenv->get_user();
 
-	my $worker = sub {
+	my $code = sub {
 	    PVE::Cluster::setup_sshd_config(1);
 	    PVE::Cluster::setup_rootsshconfig();
 	    PVE::Cluster::setup_ssh_keys();
@@ -144,6 +145,10 @@ __PACKAGE__->register_method ({
 	    PVE::Tools::run_command('systemctl restart corosync pve-cluster');
 	};
 
+	my $worker = sub {
+	    PVE::Tools::lock_file($local_cluster_change_lock, 10, $code);
+	};
+
 	return $rpcenv->fork_worker('clustercreate', '',  $authuser, $worker);
 }});
 
@@ -185,8 +190,7 @@ __PACKAGE__->register_method({
 my $config_change_lock = sub {
     my ($code) = @_;
 
-    my $local_lock_fn = "/var/lock/pvecm.lock";
-    PVE::Tools::lock_file($local_lock_fn, 10, sub {
+    PVE::Tools::lock_file($local_cluster_change_lock, 10, sub {
 	PVE::Cluster::cfs_update(1);
 	my $members = PVE::Cluster::get_members();
 	if (scalar(keys %$members) > 1) {
@@ -509,7 +513,7 @@ __PACKAGE__->register_method ({
 	my $rpcenv = PVE::RPCEnvironment::get();
 	my $authuser = $rpcenv->get_user();
 
-	my $worker = sub {
+	my $code = sub {
 	    PVE::Cluster::setup_sshd_config();
 	    PVE::Cluster::setup_rootsshconfig();
 	    PVE::Cluster::setup_ssh_keys();
@@ -567,6 +571,10 @@ __PACKAGE__->register_method ({
 	    PVE::Cluster::finish_join($nodename, $res->{corosync_conf}, $res->{corosync_authkey});
 	};
 
+	my $worker = sub {
+	    PVE::Tools::lock_file($local_cluster_change_lock, 10, $code);
+	};
+
 	return $rpcenv->fork_worker('clusterjoin', '',  $authuser, $worker);
     }});
 
-- 
2.14.2





More information about the pve-devel mailing list