]> git.proxmox.com Git - pve-container.git/commitdiff
use empty config to reserve container ID
authorFabian Grünbichler <f.gruenbichler@proxmox.com>
Fri, 3 Jun 2016 07:56:17 +0000 (09:56 +0200)
committerDietmar Maurer <dietmar@proxmox.com>
Fri, 3 Jun 2016 08:05:08 +0000 (10:05 +0200)
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
src/PVE/LXC.pm

index be765d89647f2a88208e56972de9e4795d4610c9..1dd94052bd3900722bbb02d452634c246d92289a 100644 (file)
@@ -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);
index 3a7d2a274d8cb96c45a260eb648c5d95515ca8f9..caadce11d2b03d4d2c04050354cacd53b4e92f9c 100644 (file)
@@ -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);