[pve-devel] [PATCH container] Refactor lock_container into lock_config_[xx]

Fabian Grünbichler f.gruenbichler at proxmox.com
Thu Feb 11 12:55:36 CET 2016


This mimics the "signatures" of the methods in
QemuServer.pm, and should allow us to use this methods in a
future abstract "Guest" class.

It also changes the various timeouts (ranging from 1, 10, 60
to undef) to use the default timeout defined in the
lock_config methods (10).

Also drop the now unused use statement for flock.
---
Note: this requires the "Add missing use statement" patch,
because the calls to lock_file[_full] are unqualified now.

 src/PVE/API2/LXC.pm          | 10 +++++-----
 src/PVE/API2/LXC/Config.pm   |  2 +-
 src/PVE/API2/LXC/Snapshot.pm |  2 +-
 src/PVE/API2/LXC/Status.pm   |  8 ++++----
 src/PVE/CLI/pct.pm           |  4 ++--
 src/PVE/LXC.pm               | 41 +++++++++++++++++++++++++++++------------
 src/PVE/LXC/Migrate.pm       |  2 +-
 src/PVE/VZDump/LXC.pm        |  4 ++--
 8 files changed, 45 insertions(+), 28 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 7db8488..32151c8 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -325,7 +325,7 @@ __PACKAGE__->register_method({
 	    PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
 	};
 
-	my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
+	my $realcmd = sub { PVE::LXC::lock_config($vmid, $code); };
 
 	&$check_vmid_usage(); # first check before locking
 
@@ -528,7 +528,7 @@ __PACKAGE__->register_method({
 	    PVE::Firewall::remove_vmfw_conf($vmid);
 	};
 
-	my $realcmd = sub { PVE::LXC::lock_container($vmid, 1, $code); };
+	my $realcmd = sub { PVE::LXC::lock_config($vmid, $code); };
 	
 	return $rpcenv->fork_worker('vzdestroy', $vmid, $authuser, $realcmd);
     }});
@@ -947,7 +947,7 @@ __PACKAGE__->register_method({
 	    return $rpcenv->fork_worker('vztemplate', $vmid, $authuser, $realcmd);
 	};
 
-	PVE::LXC::lock_container($vmid, undef, $updatefn);
+	PVE::LXC::lock_config($vmid, $updatefn);
 
 	return undef;
     }});
@@ -1186,7 +1186,7 @@ __PACKAGE__->register_method({
 
 	};
 
-	return PVE::LXC::lock_container($vmid, undef, $clonefn);
+	return PVE::LXC::lock_config($vmid, $clonefn);
     }});
 
 
@@ -1334,7 +1334,7 @@ __PACKAGE__->register_method({
 	    return $rpcenv->fork_worker('resize', $vmid, $authuser, $realcmd);
 	};
 
-	return PVE::LXC::lock_container($vmid, undef, $code);;
+	return PVE::LXC::lock_config($vmid, $code);;
     }});
 
 1;
diff --git a/src/PVE/API2/LXC/Config.pm b/src/PVE/API2/LXC/Config.pm
index 1a1190c..8e9295b 100644
--- a/src/PVE/API2/LXC/Config.pm
+++ b/src/PVE/API2/LXC/Config.pm
@@ -143,7 +143,7 @@ __PACKAGE__->register_method({
 	    PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
 	};
 
-	PVE::LXC::lock_container($vmid, undef, $code);
+	PVE::LXC::lock_config($vmid, $code);
 
 	return undef;
     }});
diff --git a/src/PVE/API2/LXC/Snapshot.pm b/src/PVE/API2/LXC/Snapshot.pm
index 36bbbf7..49fd4f5 100644
--- a/src/PVE/API2/LXC/Snapshot.pm
+++ b/src/PVE/API2/LXC/Snapshot.pm
@@ -311,7 +311,7 @@ __PACKAGE__->register_method({
 	    PVE::LXC::write_config($vmid, $conf, 1);
 	};
 
-	PVE::LXC::lock_container($vmid, 10, $updatefn);
+	PVE::LXC::lock_config($vmid, $updatefn);
 
 	return undef;
     }});
diff --git a/src/PVE/API2/LXC/Status.pm b/src/PVE/API2/LXC/Status.pm
index cd7f04d..e91ac7c 100644
--- a/src/PVE/API2/LXC/Status.pm
+++ b/src/PVE/API2/LXC/Status.pm
@@ -186,7 +186,7 @@ __PACKAGE__->register_method({
 		return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
 	    };
 
-	    return PVE::LXC::lock_container($vmid, 10, $lockcmd);
+	    return PVE::LXC::lock_config($vmid, $lockcmd);
 	}
     }});
 
@@ -268,7 +268,7 @@ __PACKAGE__->register_method({
 		return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
 	    };
 
-	    return PVE::LXC::lock_container($vmid, 10, $lockcmd);
+	    return PVE::LXC::lock_config($vmid, $lockcmd);
 	}
     }});
 
@@ -361,7 +361,7 @@ __PACKAGE__->register_method({
 	    return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
 	};
 
-	return PVE::LXC::lock_container($vmid, 10, $lockcmd);
+	return PVE::LXC::lock_config($vmid, $lockcmd);
     }});
 
 __PACKAGE__->register_method({
@@ -417,7 +417,7 @@ __PACKAGE__->register_method({
 	    return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
 	};
 
-	return PVE::LXC::lock_container($vmid, 10, $lockcmd);
+	return PVE::LXC::lock_config($vmid, $lockcmd);
     }});
 
 __PACKAGE__->register_method({
diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index e7a2292..b2bc361 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -44,7 +44,7 @@ __PACKAGE__->register_method ({
 
 	my $vmid = $param->{vmid};
 
-	PVE::LXC::lock_container($vmid, 5, sub {
+	PVE::LXC::lock_config($vmid, sub {
 	    my $conf = PVE::LXC::load_config($vmid);
 	    delete $conf->{lock};
 	    PVE::LXC::write_config($vmid, $conf);
@@ -199,7 +199,7 @@ __PACKAGE__->register_method ({
 	    PVE::Tools::run_command($command);
 	};
 
-	PVE::LXC::lock_container($vmid, undef, $do_fsck);
+	PVE::LXC::lock_config($vmid, $do_fsck);
 	return undef;
     }});
 
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index a2cb67d..74d15da 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -7,7 +7,6 @@ use POSIX qw(EINTR);
 use File::Path;
 use File::Spec;
 use Cwd qw();
-use Fcntl ':flock';
 
 use PVE::Cluster qw(cfs_register_file cfs_read_file);
 use PVE::Storage;
@@ -638,28 +637,46 @@ sub write_config {
 my $lock_handles =  {};
 my $lockdir = "/run/lock/lxc";
 
-sub lock_filename {
+sub config_file_lock {
     my ($vmid) = @_;
 
     return "$lockdir/pve-config-${vmid}.lock";
 }
 
-sub lock_container {
+sub lock_config_full {
     my ($vmid, $timeout, $code, @param) = @_;
 
-    $timeout = 10 if !$timeout;
+    my $filename = config_file_lock($vmid);
 
-    my $filename = lock_filename($vmid);
+    mkdir $lockdir if !-d $lockdir;
+
+    my $res = lock_file($filename, $timeout, $code, @param);
+
+    die $@ if $@;
+
+    return $res;
+}
+
+sub lock_config_mode {
+    my ($vmid, $timeout, $shared, $code, @param) = @_;
+
+    my $filename = config_file_lock($vmid);
 
     mkdir $lockdir if !-d $lockdir;
 
-    my $res = PVE::Tools::lock_file_full($filename, $timeout, 0, $code, @param);
+    my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
 
     die $@ if $@;
 
     return $res;
 }
 
+sub lock_config {
+    my ($vmid, $code, @param) = @_;
+
+    return lock_config_full($vmid, 10, $code, @param);
+}
+
 sub option_exists {
     my ($name) = @_;
 
@@ -1730,7 +1747,7 @@ my $snapshot_prepare = sub {
 	write_config($vmid, $conf);
     };
 
-    lock_container($vmid, 10, $updatefn);
+    lock_config($vmid, $updatefn);
 
     return $snap;
 };
@@ -1759,7 +1776,7 @@ my $snapshot_commit = sub {
 	write_config($vmid, $conf);
     };
 
-    lock_container($vmid, 10 ,$updatefn);
+    lock_config($vmid ,$updatefn);
 };
 
 sub has_feature {
@@ -1856,7 +1873,7 @@ sub snapshot_delete {
 	write_config($vmid, $conf);
     };
 
-    lock_container($vmid, 10, $updatefn);
+    lock_config($vmid, $updatefn);
 
     my $storecfg = PVE::Storage::config();
 
@@ -1905,7 +1922,7 @@ sub snapshot_delete {
     my $err = $@;
 
     if(!$err || ($err && $force)) {
-	lock_container($vmid, 10, $del_snap);
+	lock_config($vmid, $del_snap);
 	if ($err) {
 	    die "Can't delete snapshot: $vmid $snapname $err\n";
 	}
@@ -1964,11 +1981,11 @@ sub snapshot_rollback {
 	write_config($vmid, $conf);
     };
 
-    lock_container($vmid, 10, $updatefn);
+    lock_config($vmid, $updatefn);
 
     PVE::Storage::volume_snapshot_rollback($storecfg, $volid, $snapname);
 
-    lock_container($vmid, 5, $unlockfn);
+    lock_config($vmid, $unlockfn);
 }
 
 sub template_create {
diff --git a/src/PVE/LXC/Migrate.pm b/src/PVE/LXC/Migrate.pm
index 58e4ea2..94d53a2 100644
--- a/src/PVE/LXC/Migrate.pm
+++ b/src/PVE/LXC/Migrate.pm
@@ -16,7 +16,7 @@ use base qw(PVE::AbstractMigrate);
 sub lock_vm {
     my ($self, $vmid, $code, @param) = @_;
 
-    return PVE::LXC::lock_container($vmid, undef, $code, @param);
+    return PVE::LXC::lock_config($vmid, $code, @param);
 }
 
 sub prepare {
diff --git a/src/PVE/VZDump/LXC.pm b/src/PVE/VZDump/LXC.pm
index 57dcbe2..0979f15 100644
--- a/src/PVE/VZDump/LXC.pm
+++ b/src/PVE/VZDump/LXC.pm
@@ -185,7 +185,7 @@ sub lock_vm {
 	PVE::LXC::write_config($vmid, $conf);
     };
 
-    PVE::LXC::lock_container($vmid, 10, $lockconfig, ($self, $vmid));
+    PVE::LXC::lock_config($vmid, $lockconfig, ($self, $vmid));
 }
 
 sub unlock_vm {
@@ -202,7 +202,7 @@ sub unlock_vm {
 	}
     };
 
-    PVE::LXC::lock_container($vmid, 60, $unlockconfig, ($self, $vmid));
+    PVE::LXC::lock_config($vmid, $unlockconfig, ($self, $vmid));
 }
 
 sub snapshot {
-- 
2.1.4





More information about the pve-devel mailing list