use PVE::LXC;
use PVE::LXC::Create;
use PVE::JSONSchema qw(get_standard_option);
+
use base qw(PVE::RESTHandler);
BEGIN {
user => 'all',
},
parameters => {
- additionalProperties => 0,
+ additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
vmid => get_standard_option('pve-vmid'),
{ subdir => 'start' },
{ subdir => 'stop' },
{ subdir => 'shutdown' },
+ { subdir => 'reboot' },
{ subdir => 'migrate' },
- ];
+ ];
return $res;
}});
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'),
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;
}});
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 => {
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');
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);
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);
}
}});
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 => {
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);
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);
}
}});
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 }),
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);
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({
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) = @_;
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({
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) = @_;
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;