]> git.proxmox.com Git - pve-container.git/blobdiff - src/PVE/API2/LXC/Status.pm
api: stop: reword overrule-shutdown parameter description
[pve-container.git] / src / PVE / API2 / LXC / Status.pm
index 7c9c6f103b85c12d96fb8d87645bf83c70c4c79b..2eeecdf260a436810443280309bd434047653937 100644 (file)
@@ -16,6 +16,7 @@ use PVE::RPCEnvironment;
 use PVE::LXC;
 use PVE::LXC::Create;
 use PVE::JSONSchema qw(get_standard_option);
+
 use base qw(PVE::RESTHandler);
 
 BEGIN {
@@ -37,7 +38,7 @@ __PACKAGE__->register_method({
        user => 'all',
     },
     parameters => {
-       additionalProperties => 0,
+       additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid'),
@@ -64,8 +65,9 @@ __PACKAGE__->register_method({
            { subdir => 'start' },
            { subdir => 'stop' },
            { subdir => 'shutdown' },
+           { subdir => 'reboot' },
            { subdir => 'migrate' },
-           ];
+       ];
 
        return $res;
     }});
@@ -75,13 +77,13 @@ __PACKAGE__->register_method({
     path => 'current',
     method => 'GET',
     proxyto => 'node',
-    protected => 1, # openvz /proc entries are only readable by root
+    protected => 1, # /proc entries are only readable by root
     description => "Get virtual machine status.",
     permissions => {
        check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
     },
     parameters => {
-       additionalProperties => 0,
+       additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid'),
@@ -101,12 +103,13 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        # test if VM exists
-       my $conf = PVE::LXC::Config->load_config($param->{vmid});
+       my $vmid = $param->{vmid};
+       my $conf = PVE::LXC::Config->load_config($vmid);
 
-       my $vmstatus =  PVE::LXC::vmstatus($param->{vmid});
-       my $status = $vmstatus->{$param->{vmid}};
+       my $vmstatus = PVE::LXC::vmstatus($vmid);
+       my $status = $vmstatus->{$vmid};
 
-       $status->{ha} = PVE::HA::Config::get_service_status("ct:$param->{vmid}");
+       $status->{ha} = PVE::HA::Config::get_service_status("ct:$vmid");
 
        return $status;
     }});
@@ -122,11 +125,17 @@ __PACKAGE__->register_method({
        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
     parameters => {
-       additionalProperties => 0,
+       additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
            skiplock => get_standard_option('skiplock'),
+           debug => {
+               optional => 1,
+               type => 'boolean',
+               description => "If set, enables very verbose debug log-level on start.",
+               default => 0,
+           },
        },
     },
     returns => {
@@ -136,11 +145,9 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $node = extract_param($param, 'node');
-
        my $vmid = extract_param($param, 'vmid');
 
        my $skiplock = extract_param($param, 'skiplock');
@@ -156,15 +163,10 @@ __PACKAGE__->register_method({
            my $hacmd = sub {
                my $upid = shift;
 
-               my $service = "ct:$vmid";
-
-               my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
-
                print "Requesting HA start for CT $vmid\n";
 
+               my $cmd = ['ha-manager', 'set',  "ct:$vmid", '--state', 'started'];
                PVE::Tools::run_command($cmd);
-
-               return;
            };
 
            return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
@@ -174,32 +176,31 @@ __PACKAGE__->register_method({
            my $realcmd = sub {
                my $upid = shift;
 
-               syslog('info', "starting CT $vmid: $upid\n");
+               PVE::LXC::Config->lock_config($vmid, sub {
+                   syslog('info', "starting CT $vmid: $upid\n");
 
-               my $conf = PVE::LXC::Config->load_config($vmid);
-
-               die "you can't start a CT if it's a template\n"
-                   if PVE::LXC::Config->is_template($conf);
+                   my $conf = PVE::LXC::Config->load_config($vmid);
 
-               if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
-                   PVE::LXC::Config->check_lock($conf);
-               }
+                   die "you can't start a CT if it's a template\n"
+                       if PVE::LXC::Config->is_template($conf);
 
-               if ($conf->{unprivileged}) {
-                   PVE::LXC::Config->foreach_mountpoint($conf, sub {
-                       my ($ms, $mountpoint) = @_;
-                       die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota};
-                   });
-               }
+                   if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
+                       PVE::LXC::Config->check_lock($conf);
+                   }
 
-               PVE::LXC::vm_start($vmid, $conf, $skiplock);
-           };
+                   if ($conf->{unprivileged}) {
+                       PVE::LXC::Config->foreach_volume($conf, sub {
+                           my ($ms, $mountpoint) = @_;
+                           die "Quotas are not supported by unprivileged containers.\n"
+                               if $mountpoint->{quota};
+                       });
+                   }
 
-           my $lockcmd = sub {
-               return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
+                   PVE::LXC::vm_start($vmid, $conf, $skiplock, $param->{debug});
+               });
            };
 
-           return PVE::LXC::Config->lock_config($vmid, $lockcmd);
+           return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
        }
     }});
 
@@ -214,11 +215,17 @@ __PACKAGE__->register_method({
        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
     parameters => {
-       additionalProperties => 0,
+       additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
            skiplock => get_standard_option('skiplock'),
+           'overrule-shutdown' => {
+               description => "Try to abort active 'vzshutdown' tasks before stopping.",
+               optional => 1,
+               type => 'boolean',
+               default => 0,
+           }
        },
     },
     returns => {
@@ -228,33 +235,30 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
-
        my $node = extract_param($param, 'node');
-
        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';
 
+       my $overrule_shutdown = extract_param($param, 'overrule-shutdown');
+
        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') {
 
+           raise_param_exc({ 'overrule-shutdown' => "Not applicable for HA resources." })
+               if $overrule_shutdown;
+
            my $hacmd = sub {
                my $upid = shift;
 
-               my $service = "ct:$vmid";
-
-               my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
-
                print "Requesting HA stop for CT $vmid\n";
 
+               my $cmd = ['ha-manager', 'crm-command', 'stop', "ct:$vmid", '0'];
                PVE::Tools::run_command($cmd);
-
-               return;
            };
 
            return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
@@ -264,21 +268,27 @@ __PACKAGE__->register_method({
            my $realcmd = sub {
                my $upid = shift;
 
-               syslog('info', "stopping CT $vmid: $upid\n");
+               if ($overrule_shutdown) {
+                   my $overruled_tasks = PVE::GuestHelpers::abort_guest_tasks(
+                       $rpcenv, 'vzshutdown', $vmid);
+                   my $overruled_tasks_list = join(", ", $overruled_tasks->@*);
+                   print "overruled vzshutdown tasks: $overruled_tasks_list\n"
+                       if @$overruled_tasks;
+               };
 
-               my $conf = PVE::LXC::Config->load_config($vmid);
-               if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
-                   PVE::LXC::Config->check_lock($conf);
-               }
+               PVE::LXC::Config->lock_config($vmid, sub {
+                   syslog('info', "stopping CT $vmid: $upid\n");
 
-               PVE::LXC::vm_stop($vmid, 1);
-           };
+                   my $conf = PVE::LXC::Config->load_config($vmid);
+                   if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
+                       PVE::LXC::Config->check_lock($conf);
+                   }
 
-           my $lockcmd = sub {
-               return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
+                   PVE::LXC::vm_stop($vmid, 1);
+               });
            };
 
-           return PVE::LXC::Config->lock_config($vmid, $lockcmd);
+           return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
        }
     }});
 
@@ -294,7 +304,7 @@ __PACKAGE__->register_method({
        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
     parameters => {
-       additionalProperties => 0,
+       additionalProperties => 0,
        properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
@@ -320,32 +330,23 @@ __PACKAGE__->register_method({
        my ($param) = @_;
 
        my $rpcenv = PVE::RPCEnvironment::get();
-
        my $authuser = $rpcenv->get_user();
 
        my $node = extract_param($param, 'node');
-
        my $vmid = extract_param($param, 'vmid');
 
        my $timeout = extract_param($param, 'timeout') // 60;
 
        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') {
-
+       if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
            my $hacmd = sub {
                my $upid = shift;
 
-               my $service = "ct:$vmid";
-
-               my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
-
                print "Requesting HA stop for CT $vmid\n";
 
+               my $cmd = ['ha-manager', 'crm-command', 'stop',  "ct:$vmid", "$timeout"];
                PVE::Tools::run_command($cmd);
-
-               return;
            };
 
            return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
@@ -354,19 +355,17 @@ __PACKAGE__->register_method({
        my $realcmd = sub {
            my $upid = shift;
 
-           syslog('info', "shutdown CT $vmid: $upid\n");
-
-           my $conf = PVE::LXC::Config->load_config($vmid);
-           PVE::LXC::Config->check_lock($conf);
+           PVE::LXC::Config->lock_config($vmid, sub {
+               syslog('info', "shutdown CT $vmid: $upid\n");
 
-           PVE::LXC::vm_stop($vmid, 0, $timeout, $param->{forceStop});
-       };
+               my $conf = PVE::LXC::Config->load_config($vmid);
+               PVE::LXC::Config->check_lock($conf);
 
-       my $lockcmd = sub {
-           return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
+               PVE::LXC::vm_stop($vmid, 0, $timeout, $param->{forceStop});
+           });
        };
 
-       return PVE::LXC::Config->lock_config($vmid, $lockcmd);
+       return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
     }});
 
 __PACKAGE__->register_method({
@@ -375,19 +374,19 @@ __PACKAGE__->register_method({
     method => 'POST',
     protected => 1,
     proxyto => 'node',
-    description => "Suspend the container.",
+    description => "Suspend the container. This is experimental.",
     permissions => {
        check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
     parameters => {
-        additionalProperties => 0,
-        properties => {
+       additionalProperties => 0,
+       properties => {
            node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
-        },
+       },
     },
     returns => {
-        type => 'string',
+       type => 'string',
     },
     code => sub {
        my ($param) = @_;
@@ -403,20 +402,18 @@ __PACKAGE__->register_method({
        my $realcmd = sub {
            my $upid = shift;
 
-           syslog('info', "suspend CT $vmid: $upid\n");
-
-           my $conf = PVE::LXC::Config->load_config($vmid);
-           PVE::LXC::Config->check_lock($conf);
+           PVE::LXC::Config->lock_config($vmid, sub {
+               syslog('info', "suspend CT $vmid: $upid\n");
 
-           my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
-           run_command($cmd);
-       };
+               my $conf = PVE::LXC::Config->load_config($vmid);
+               PVE::LXC::Config->check_lock($conf);
 
-       my $lockcmd = sub {
-           return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
+               my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
+               run_command($cmd);
+           });
        };
 
-       return PVE::LXC::Config->lock_config($vmid, $lockcmd);
+       return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
     }});
 
 __PACKAGE__->register_method({
@@ -430,14 +427,14 @@ __PACKAGE__->register_method({
         check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
     },
     parameters => {
-        additionalProperties => 0,
-        properties => {
-            node => get_standard_option('pve-node'),
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
            vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
-        },
+       },
     },
     returns => {
-        type => 'string',
+       type => 'string',
     },
     code => sub {
        my ($param) = @_;
@@ -464,4 +461,55 @@ __PACKAGE__->register_method({
        return $upid;
     }});
 
+__PACKAGE__->register_method({
+    name => 'vm_reboot',
+    path => 'reboot',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Reboot the container by shutting it down, and starting it again. Applies pending changes.",
+    permissions => {
+       check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+       additionalProperties => 0,
+       properties => {
+           node => get_standard_option('pve-node'),
+           vmid => get_standard_option('pve-vmid',
+                                       { completion => \&PVE::LXC::complete_ctid_running }),
+           timeout => {
+               description => "Wait maximal timeout seconds for the shutdown.",
+               type => 'integer',
+               minimum => 0,
+               optional => 1,
+           },
+       },
+    },
+    returns => {
+       type => 'string',
+    },
+    code => sub {
+       my ($param) = @_;
+
+       my $rpcenv = PVE::RPCEnvironment::get();
+       my $authuser = $rpcenv->get_user();
+
+       my $node = extract_param($param, 'node');
+       my $vmid = extract_param($param, 'vmid');
+
+       die "VM $vmid not running\n" if !PVE::LXC::check_running($vmid);
+
+       my $realcmd = sub {
+           my $upid = shift;
+
+           syslog('info', "requesting reboot of CT $vmid: $upid\n");
+           PVE::LXC::vm_reboot($vmid, $param->{timeout});
+           return;
+       };
+
+       return $rpcenv->fork_worker('vzreboot', $vmid, $authuser, $realcmd);
+    }});
+
+
+
 1;