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);
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' },
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;
}});
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 => {
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 {
} 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);
}
}});
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' ]],
},
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 => {
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') {
} 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);
}
}});
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' ]],
},
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({
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({
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);