1 package PVE
::API2
::LXC
::Status
;
7 use PVE
::Tools
qw(extract_param run_command);
8 use PVE
::Exception
qw(raise raise_param_exc);
10 use PVE
::Cluster
qw(cfs_read_file);
11 use PVE
::AccessControl
;
15 use PVE
::RPCEnvironment
;
18 use PVE
::JSONSchema
qw(get_standard_option);
20 use base
qw(PVE::RESTHandler);
23 if (!$ENV{PVE_GENERATING_DOCS
}) {
24 require PVE
::HA
::Env
::PVE2
;
25 import PVE
::HA
::Env
::PVE2
;
26 require PVE
::HA
::Config
;
27 import PVE
::HA
::Config
;
31 __PACKAGE__-
>register_method({
36 description
=> "Directory index",
41 additionalProperties
=> 0,
43 node
=> get_standard_option
('pve-node'),
44 vmid
=> get_standard_option
('pve-vmid'),
52 subdir
=> { type
=> 'string' },
55 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
61 my $conf = PVE
::LXC
::Config-
>load_config($param->{vmid
});
64 { subdir
=> 'current' },
65 { subdir
=> 'start' },
67 { subdir
=> 'shutdown' },
68 { subdir
=> 'reboot' },
69 { subdir
=> 'migrate' },
75 __PACKAGE__-
>register_method({
80 protected
=> 1, # /proc entries are only readable by root
81 description
=> "Get virtual machine status.",
83 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
86 additionalProperties
=> 0,
88 node
=> get_standard_option
('pve-node'),
89 vmid
=> get_standard_option
('pve-vmid'),
95 %$PVE::LXC
::vmstatus_return_properties
,
97 description
=> "HA manager service status.",
106 my $vmid = $param->{vmid
};
107 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
109 my $vmstatus = PVE
::LXC
::vmstatus
($vmid);
110 my $status = $vmstatus->{$vmid};
112 $status->{ha
} = PVE
::HA
::Config
::get_service_status
("ct:$vmid");
117 __PACKAGE__-
>register_method({
123 description
=> "Start the container.",
125 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
128 additionalProperties
=> 0,
130 node
=> get_standard_option
('pve-node'),
131 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
132 skiplock
=> get_standard_option
('skiplock'),
136 description
=> "If set, enables very verbose debug log-level on start.",
147 my $rpcenv = PVE
::RPCEnvironment
::get
();
148 my $authuser = $rpcenv->get_user();
150 my $node = extract_param
($param, 'node');
151 my $vmid = extract_param
($param, 'vmid');
153 my $skiplock = extract_param
($param, 'skiplock');
154 raise_param_exc
({ skiplock
=> "Only root may use this option." })
155 if $skiplock && $authuser ne 'root@pam';
157 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
159 PVE
::Cluster
::check_cfs_quorum
();
161 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
166 print "Requesting HA start for CT $vmid\n";
168 my $cmd = ['ha-manager', 'set', "ct:$vmid", '--state', 'started'];
169 PVE
::Tools
::run_command
($cmd);
172 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
179 PVE
::LXC
::Config-
>lock_config($vmid, sub {
180 syslog
('info', "starting CT $vmid: $upid\n");
182 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
184 die "you can't start a CT if it's a template\n"
185 if PVE
::LXC
::Config-
>is_template($conf);
187 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
188 PVE
::LXC
::Config-
>check_lock($conf);
191 if ($conf->{unprivileged
}) {
192 PVE
::LXC
::Config-
>foreach_volume($conf, sub {
193 my ($ms, $mountpoint) = @_;
194 die "Quotas are not supported by unprivileged containers.\n"
195 if $mountpoint->{quota
};
199 PVE
::LXC
::vm_start
($vmid, $conf, $skiplock, $param->{debug
});
203 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
207 __PACKAGE__-
>register_method({
213 description
=> "Stop the container. This will abruptly stop all processes running in the container.",
215 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
218 additionalProperties
=> 0,
220 node
=> get_standard_option
('pve-node'),
221 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
222 skiplock
=> get_standard_option
('skiplock'),
231 my $rpcenv = PVE
::RPCEnvironment
::get
();
232 my $authuser = $rpcenv->get_user();
233 my $node = extract_param
($param, 'node');
234 my $vmid = extract_param
($param, 'vmid');
236 my $skiplock = extract_param
($param, 'skiplock');
237 raise_param_exc
({ skiplock
=> "Only root may use this option." })
238 if $skiplock && $authuser ne 'root@pam';
240 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
242 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
247 print "Requesting HA stop for CT $vmid\n";
249 my $cmd = ['ha-manager', 'crm-command', 'stop', "ct:$vmid", '0'];
250 PVE
::Tools
::run_command
($cmd);
253 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
260 PVE
::LXC
::Config-
>lock_config($vmid, sub {
261 syslog
('info', "stopping CT $vmid: $upid\n");
263 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
264 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
265 PVE
::LXC
::Config-
>check_lock($conf);
268 PVE
::LXC
::vm_stop
($vmid, 1);
272 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
276 __PACKAGE__-
>register_method({
277 name
=> 'vm_shutdown',
282 description
=> "Shutdown the container. This will trigger a clean shutdown " .
283 "of the container, see lxc-stop(1) for details.",
285 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
288 additionalProperties
=> 0,
290 node
=> get_standard_option
('pve-node'),
291 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
293 description
=> "Wait maximal timeout seconds.",
300 description
=> "Make sure the Container stops.",
313 my $rpcenv = PVE
::RPCEnvironment
::get
();
314 my $authuser = $rpcenv->get_user();
316 my $node = extract_param
($param, 'node');
317 my $vmid = extract_param
($param, 'vmid');
319 my $timeout = extract_param
($param, 'timeout') // 60;
321 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
323 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
327 print "Requesting HA stop for CT $vmid\n";
329 my $cmd = ['ha-manager', 'crm-command', 'stop', "ct:$vmid", "$timeout"];
330 PVE
::Tools
::run_command
($cmd);
333 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
339 PVE
::LXC
::Config-
>lock_config($vmid, sub {
340 syslog
('info', "shutdown CT $vmid: $upid\n");
342 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
343 PVE
::LXC
::Config-
>check_lock($conf);
345 PVE
::LXC
::vm_stop
($vmid, 0, $timeout, $param->{forceStop
});
349 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
352 __PACKAGE__-
>register_method({
353 name
=> 'vm_suspend',
358 description
=> "Suspend the container. This is experimental.",
360 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
363 additionalProperties
=> 0,
365 node
=> get_standard_option
('pve-node'),
366 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
375 my $rpcenv = PVE
::RPCEnvironment
::get
();
376 my $authuser = $rpcenv->get_user();
378 my $node = extract_param
($param, 'node');
379 my $vmid = extract_param
($param, 'vmid');
381 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
386 PVE
::LXC
::Config-
>lock_config($vmid, sub {
387 syslog
('info', "suspend CT $vmid: $upid\n");
389 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
390 PVE
::LXC
::Config-
>check_lock($conf);
392 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
397 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
400 __PACKAGE__-
>register_method({
406 description
=> "Resume the container.",
408 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
411 additionalProperties
=> 0,
413 node
=> get_standard_option
('pve-node'),
414 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
423 my $rpcenv = PVE
::RPCEnvironment
::get
();
424 my $authuser = $rpcenv->get_user();
426 my $node = extract_param
($param, 'node');
427 my $vmid = extract_param
($param, 'vmid');
429 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
434 syslog
('info', "resume CT $vmid: $upid\n");
436 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground', '-D', '/var/lib/vz/dump'];
440 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
445 __PACKAGE__-
>register_method({
451 description
=> "Reboot the container by shutting it down, and starting it again. Applies pending changes.",
453 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
456 additionalProperties
=> 0,
458 node
=> get_standard_option
('pve-node'),
459 vmid
=> get_standard_option
('pve-vmid',
460 { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
462 description
=> "Wait maximal timeout seconds for the shutdown.",
475 my $rpcenv = PVE
::RPCEnvironment
::get
();
476 my $authuser = $rpcenv->get_user();
478 my $node = extract_param
($param, 'node');
479 my $vmid = extract_param
($param, 'vmid');
481 die "VM $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
486 syslog
('info', "requesting reboot of CT $vmid: $upid\n");
487 PVE
::LXC
::vm_reboot
($vmid, $param->{timeout
});
491 return $rpcenv->fork_worker('vzreboot', $vmid, $authuser, $realcmd);