]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/API2/LXC/Status.pm
update_lxc_config: remove unused parameter
[pve-container.git] / src / PVE / API2 / LXC / Status.pm
index 761a3c83a5d33c553b0aef77997422c83f73d307..83a23f5332a7fc4ecfd715091247449d5a707f11 100644 (file)
@@ -15,6 +15,7 @@ use PVE::RESTHandler;
 use PVE::RPCEnvironment;
 use PVE::LXC;
 use PVE::LXC::Create;
+use PVE::HA::Env::PVE2;
 use PVE::HA::Config;
 use PVE::JSONSchema qw(get_standard_option);
 use base qw(PVE::RESTHandler);
@@ -51,7 +52,7 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        # test if VM exists
-       my $conf = PVE::LXC::load_config($param->{vmid});
+       my $conf = PVE::LXC::Config->load_config($param->{vmid});
 
        my $res = [
            { subdir => 'current' },
@@ -86,12 +87,12 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        # test if VM exists
-       my $conf = PVE::LXC::load_config($param->{vmid});
+       my $conf = PVE::LXC::Config->load_config($param->{vmid});
 
        my $vmstatus =  PVE::LXC::vmstatus($param->{vmid});
        my $status = $vmstatus->{$param->{vmid}};
 
-       $status->{ha} = PVE::HA::Config::vm_is_ha_managed($param->{vmid}) ? 1 : 0;
+       $status->{ha} = PVE::HA::Config::get_service_status("ct:$param->{vmid}");
 
        return $status;
     }});
@@ -111,6 +112,7 @@ __PACKAGE__->register_method({
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
+           skiplock => get_standard_option('skiplock'),
        },
     },
     returns => {
@@ -127,8 +129,14 @@ __PACKAGE__->register_method({
 
        my $vmid = extract_param($param, 'vmid');
 
+       my $skiplock = extract_param($param, 'skiplock');
+       raise_param_exc({ skiplock => "Only root may use this option." })
+           if $skiplock && $authuser ne 'root@pam';
+
        die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
 
+       PVE::Cluster::check_cfs_quorum();
+
        if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
 
            my $hacmd = sub {
@@ -149,28 +157,38 @@ __PACKAGE__->register_method({
 
        } else {
 
-           my $realcmd = sub {
-               my $upid = shift;
+           my $lockcmd = sub {
+               my $realcmd = sub {
+                   my $upid = shift;
 
-               syslog('info', "starting CT $vmid: $upid\n");
+                   syslog('info', "starting CT $vmid: $upid\n");
 
-               my $conf = PVE::LXC::load_config($vmid);
+                   my $conf = PVE::LXC::Config->load_config($vmid);
 
-               die "you can't start a CT if it's a template\n"
-                   if PVE::LXC::is_template($conf);
+                   die "you can't start a CT if it's a template\n"
+                       if PVE::LXC::Config->is_template($conf);
 
-               my $storage_cfg = cfs_read_file("storage.cfg");
+                   if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
+                       PVE::LXC::Config->check_lock($conf);
+                   }
 
-               PVE::LXC::update_lxc_config($storage_cfg, $vmid, $conf);
+                   my $storage_cfg = cfs_read_file("storage.cfg");
 
-               my $cmd = ['lxc-start', '-n', $vmid];
+                   PVE::LXC::update_lxc_config($vmid, $conf);
 
-               run_command($cmd);
+                   local $ENV{PVE_SKIPLOCK}=1 if $skiplock;
 
-               return;
+                   my $cmd = ['lxc-start', '-n', $vmid];
+
+                   run_command($cmd);
+
+                   return;
+               };
+
+               return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
            };
 
-           return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
+           return PVE::LXC::Config->lock_config($vmid, $lockcmd);
        }
     }});
 
@@ -180,7 +198,7 @@ __PACKAGE__->register_method({
     method => 'POST',
     protected => 1,
     proxyto => 'node',
-    description => "Stop the container.",
+    description => "Stop the container. This will abruptly stop all processes running in the container.",
     permissions => {
        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
@@ -189,6 +207,7 @@ __PACKAGE__->register_method({
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
+           skiplock => get_standard_option('skiplock'),
        },
     },
     returns => {
@@ -205,6 +224,10 @@ __PACKAGE__->register_method({
 
        my $vmid = extract_param($param, 'vmid');
 
+       my $skiplock = extract_param($param, 'skiplock');
+       raise_param_exc({ skiplock => "Only root may use this option." })
+           if $skiplock && $authuser ne 'root@pam';
+
        die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
 
        if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
@@ -227,19 +250,29 @@ __PACKAGE__->register_method({
 
        } else {
 
-           my $realcmd = sub {
-               my $upid = shift;
+           my $lockcmd = sub {
+               my $realcmd = sub {
+                   my $upid = shift;
 
-               syslog('info', "stopping CT $vmid: $upid\n");
+                   syslog('info', "stopping CT $vmid: $upid\n");
 
-               my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
+                   my $conf = PVE::LXC::Config->load_config($vmid);
 
-               run_command($cmd);
+                   if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
+                       PVE::LXC::Config->check_lock($conf);
+                   }
 
-               return;
+                   my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
+
+                   run_command($cmd);
+
+                   return;
+               };
+
+               return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
            };
 
-           return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
+           return PVE::LXC::Config->lock_config($vmid, $lockcmd);
        }
     }});
 
@@ -249,7 +282,8 @@ __PACKAGE__->register_method({
     method => 'POST',
     protected => 1,
     proxyto => 'node',
-    description => "Shutdown the container.",
+    description => "Shutdown the container. This will trigger a clean shutdown " .
+       "of the container, see lxc-stop(1) for details.",
     permissions => {
        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
@@ -291,39 +325,48 @@ __PACKAGE__->register_method({
 
        die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
 
-       my $realcmd = sub {
-           my $upid = shift;
+       my $lockcmd = sub {
+           my $realcmd = sub {
+               my $upid = shift;
+
+               syslog('info', "shutdown CT $vmid: $upid\n");
 
-           syslog('info', "shutdown CT $vmid: $upid\n");
+               my $cmd = ['lxc-stop', '-n', $vmid];
 
-           my $cmd = ['lxc-stop', '-n', $vmid];
+               $timeout = 60 if !defined($timeout);
 
-           $timeout = 60 if !defined($timeout);
+               my $conf = PVE::LXC::Config->load_config($vmid);
 
-           my $conf = PVE::LXC::load_config($vmid);
+               PVE::LXC::Config->check_lock($conf);
 
-           my $storage_cfg = PVE::Storage::config();
+               my $storage_cfg = PVE::Storage::config();
 
-           push @$cmd, '--timeout', $timeout;
+               push @$cmd, '--timeout', $timeout;
 
-           eval { run_command($cmd, timeout => $timeout+5); };
-           my $err = $@;
-           if ($err && $param->{forceStop}) {
-               $err = undef;
-               warn "shutdown failed - forcing stop now\n";
+               eval { run_command($cmd, timeout => $timeout+5); };
+               my $err = $@;
+               if ($err && $param->{forceStop}) {
+                   $err = undef;
+                   warn "shutdown failed - forcing stop now\n";
 
-               my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
+                   my $cmd = ['lxc-stop', '-n', $vmid, '--kill'];
+                   run_command($cmd);
+               }
+
+               # make sure container is stopped
+               $cmd = ['lxc-wait',  '-n', $vmid, '-t', 5, '-s', 'STOPPED'];
                run_command($cmd);
-           }
+               $err = $@;
 
-           die $err if !$err;
+               die $err if $err;
 
-           return;
-       };
+               return;
+           };
 
-       my $upid = $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
+           return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
+       };
 
-       return $upid;
+       return PVE::LXC::Config->lock_config($vmid, $lockcmd);
     }});
 
 __PACKAGE__->register_method({
@@ -359,21 +402,27 @@ __PACKAGE__->register_method({
 
         die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
 
-        my $realcmd = sub {
-            my $upid = shift;
+       my $lockcmd = sub {
+           my $realcmd = sub {
+               my $upid = shift;
 
-            syslog('info', "suspend CT $vmid: $upid\n");
+               syslog('info', "suspend CT $vmid: $upid\n");
 
-           my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/liv/vz/dump'];
+               my $conf = PVE::LXC::Config->load_config($vmid);
 
-           run_command($cmd);
+               PVE::LXC::Config->check_lock($conf);
 
-            return;
-        };
+               my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
 
-        my $upid = $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
+               run_command($cmd);
 
-        return $upid;
+               return;
+           };
+
+           return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
+       };
+
+       return PVE::LXC::Config->lock_config($vmid, $lockcmd);
     }});
 
 __PACKAGE__->register_method({
@@ -415,7 +464,7 @@ __PACKAGE__->register_method({
             syslog('info', "resume CT $vmid: $upid\n");
 
            my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
-                      '-D', '/var/liv/vz/dump'];
+                      '-D', '/var/lib/vz/dump'];
 
            run_command($cmd);