[pve-devel] [PATCH container 3/3] use empty config to reserve container ID

Fabian Grünbichler f.gruenbichler at proxmox.com
Fri Jun 3 09:56:17 CEST 2016


also adapt PVE::LXC::destroy_lxc_container to optionally
write an arbitray new config instead of deleting it
altogether. the old configuration is replaced by an empty
temporary configuration at the moment, but this could easily
be reused if/when a "create"-locked config is used instead
of an empty one.
---
 src/PVE/API2/LXC.pm | 26 ++++++++++++++++++--------
 src/PVE/LXC.pm      |  8 ++++++--
 2 files changed, 24 insertions(+), 10 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index be765d8..1dd9405 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -314,7 +314,22 @@ __PACKAGE__->register_method({
 
 	my $code = sub {
 	    &$check_vmid_usage(); # final check after locking
-	    	    
+	    my $old_conf;
+
+	    my $config_fn = PVE::LXC::Config->config_file($vmid);
+	    if (-f $config_fn) {
+		die "container exists" if !$restore; # just to be sure
+		$old_conf = PVE::LXC::Config->load_config($vmid);
+	    } else {
+		eval {
+		    # try to create empty config on local node, we have an flock
+		    PVE::LXC::Config->write_config($vmid, {});
+		};
+
+		# another node was faster, abort
+		die "Could not reserve ID $vmid, already taken\n" if $@;
+	    }
+
 	    PVE::Cluster::check_cfs_quorum();
 	    my $vollist = [];
 
@@ -355,15 +370,10 @@ __PACKAGE__->register_method({
 
 		$vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
 
-		my $config_fn = PVE::LXC::Config->config_file($vmid);
-		if (-f $config_fn) {
-		    die "container exists" if !$restore; # just to be sure
-			my $old_conf = PVE::LXC::Config->load_config($vmid);
-
+		if (defined($old_conf)) {
 		    # destroy old container volumes
-		    PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf);
+		    PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, {});
 		}
-		PVE::LXC::Config->write_config($vmid, $conf);
 
 		eval {
 		    my $rootdir = PVE::LXC::mount_all($vmid, $storage_cfg, $conf, 1);
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 7c5a5e6..87dd5d5 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -518,7 +518,7 @@ sub delete_mountpoint_volume {
 }
 
 sub destroy_lxc_container {
-    my ($storage_cfg, $vmid, $conf) = @_;
+    my ($storage_cfg, $vmid, $conf, $replacement_conf) = @_;
 
     PVE::LXC::Config->foreach_mountpoint($conf, sub {
 	my ($ms, $mountpoint) = @_;
@@ -528,7 +528,11 @@ sub destroy_lxc_container {
     rmdir "/var/lib/lxc/$vmid/rootfs";
     unlink "/var/lib/lxc/$vmid/config";
     rmdir "/var/lib/lxc/$vmid";
-    destroy_config($vmid);
+    if (defined $replacement_conf) {
+	PVE::LXC::Config->write_config($vmid, $replacement_conf);
+    } else {
+	destroy_config($vmid);
+    }
 
     #my $cmd = ['lxc-destroy', '-n', $vmid ];
     #PVE::Tools::run_command($cmd);
-- 
2.1.4




More information about the pve-devel mailing list