]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/API2/LXC.pm
Fix #2109: resize rbd volume for container failed
[pve-container.git] / src / PVE / API2 / LXC.pm
index 042448d37e8dd7832dda36335cb6c5d7311d6a56..1cbd3d83b82fe365c3cf03598bbcffff5a54941d 100644 (file)
@@ -180,6 +180,8 @@ __PACKAGE__->register_method({
     code => sub {
        my ($param) = @_;
 
+       PVE::Cluster::check_cfs_quorum();
+
        my $rpcenv = PVE::RPCEnvironment::get();
        my $authuser = $rpcenv->get_user();
 
@@ -205,6 +207,7 @@ __PACKAGE__->register_method({
        if (!($same_container_exists && $restore && $force)) {
            PVE::Cluster::check_vmid_unused($vmid);
        } else {
+           die "can't overwrite running container\n" if PVE::LXC::check_running($vmid);
            my $conf = PVE::LXC::Config->load_config($vmid);
            PVE::LXC::Config->check_protection($conf, "unable to restore CT $vmid");
        }
@@ -317,40 +320,22 @@ __PACKAGE__->register_method({
 
        $conf->{unprivileged} = 1 if $unprivileged;
 
-       my $check_vmid_usage = sub {
-           if ($force) {
-               die "can't overwrite running container\n"
-                   if PVE::LXC::check_running($vmid);
-           } else {
-               PVE::Cluster::check_vmid_unused($vmid);
-           }
-       };
+       my $emsg = $restore ? "unable to restore CT $vmid -" : "unable to create CT $vmid -";
 
-       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, {});
-               };
+       eval { PVE::LXC::Config->create_and_lock_config($vmid, $force) };
+       die "$emsg $@" if $@;
 
-               # another node was faster, abort
-               die "Could not reserve ID $vmid, already taken\n" if $@;
-           }
+       my $code = sub {
+           my $old_conf = PVE::LXC::Config->load_config($vmid);
 
-           PVE::Cluster::check_cfs_quorum();
            my $vollist = [];
            eval {
                my $orig_mp_param; # only used if $restore
                if ($restore) {
-                   (my $orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
-                   if ($is_root) {
+                   die "can't overwrite running container\n" if PVE::LXC::check_running($vmid);
+                   if ($is_root && $archive ne '-') {
+                       my $orig_conf;
+                       ($orig_conf, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
                        # When we're root call 'restore_configuration' with ristricted=0,
                        # causing it to restore the raw lxc entries, among which there may be
                        # 'lxc.idmap' entries. We need to make sure that the extracted contents
@@ -360,6 +345,9 @@ __PACKAGE__->register_method({
                }
                if ($storage_only_mode) {
                    if ($restore) {
+                       if (!defined($orig_mp_param)) {
+                           (undef, $orig_mp_param) = PVE::LXC::Create::recover_config($archive);
+                       }
                        $mp_param = $orig_mp_param;
                        die "rootfs configuration could not be recovered, please check and specify manually!\n"
                            if !defined($mp_param->{rootfs});
@@ -397,9 +385,10 @@ __PACKAGE__->register_method({
 
                $vollist = PVE::LXC::create_disks($storage_cfg, $vmid, $mp_param, $conf);
 
-               if (defined($old_conf)) {
+               # we always have the 'create' lock so check for more than 1 entry
+               if (scalar(keys %$old_conf) > 1) {
                    # destroy old container volumes
-                   PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, {});
+                   PVE::LXC::destroy_lxc_container($storage_cfg, $vmid, $old_conf, { lock => 'create' });
                }
 
                eval {
@@ -430,8 +419,9 @@ __PACKAGE__->register_method({
            };
            if (my $err = $@) {
                PVE::LXC::destroy_disks($storage_cfg, $vollist);
-               PVE::LXC::destroy_config($vmid);
-               die $err;
+               eval { PVE::LXC::destroy_config($vmid) };
+               warn $@ if $@;
+               die "$emsg $err";
            }
            PVE::AccessControl::add_vm_to_pool($vmid, $pool) if $pool;
 
@@ -439,13 +429,10 @@ __PACKAGE__->register_method({
                if $start_after_create;
        };
 
+       my $workername = $restore ? 'vzrestore' : 'vzcreate';
        my $realcmd = sub { PVE::LXC::Config->lock_config($vmid, $code); };
 
-       &$check_vmid_usage(); # first check before locking
-
-       return $rpcenv->fork_worker($restore ? 'vzrestore' : 'vzcreate',
-                                   $vmid, $authuser, $realcmd);
-
+       return $rpcenv->fork_worker($workername, $vmid, $authuser, $realcmd);
     }});
 
 __PACKAGE__->register_method({
@@ -1619,7 +1606,7 @@ __PACKAGE__->register_method({
                PVE::LXC::Config->write_config($vmid, $conf);
 
                if ($format eq 'raw') {
-                   my $path = PVE::Storage::path($storage_cfg, $volid, undef);
+                   my $path = PVE::Storage::map_volume($storage_cfg, $volid) // PVE::Storage::path($storage_cfg, $volid);
                    if ($running) {
 
                        $mp->{mp} = '/';
@@ -1647,6 +1634,8 @@ __PACKAGE__->register_method({
                            PVE::Tools::run_command(['resize2fs', $path]);
                        };
                        warn "Failed to update the container's filesystem: $@\n" if $@;
+
+                       PVE::Storage::unmap_volume($storage_cfg, $volid);
                    }
                }
            };