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'),
141 my $rpcenv = PVE
::RPCEnvironment
::get
();
142 my $authuser = $rpcenv->get_user();
144 my $node = extract_param
($param, 'node');
145 my $vmid = extract_param
($param, 'vmid');
147 my $skiplock = extract_param
($param, 'skiplock');
148 raise_param_exc
({ skiplock
=> "Only root may use this option." })
149 if $skiplock && $authuser ne 'root@pam';
151 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
153 PVE
::Cluster
::check_cfs_quorum
();
155 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
160 print "Requesting HA start for CT $vmid\n";
162 my $cmd = ['ha-manager', 'set', "ct:$vmid", '--state', 'started'];
163 PVE
::Tools
::run_command
($cmd);
166 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
173 syslog
('info', "starting CT $vmid: $upid\n");
175 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
177 die "you can't start a CT if it's a template\n"
178 if PVE
::LXC
::Config-
>is_template($conf);
180 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
181 PVE
::LXC
::Config-
>check_lock($conf);
184 if ($conf->{unprivileged
}) {
185 PVE
::LXC
::Config-
>foreach_volume($conf, sub {
186 my ($ms, $mountpoint) = @_;
187 die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota
};
191 PVE
::LXC
::vm_start
($vmid, $conf, $skiplock);
195 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
198 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
202 __PACKAGE__-
>register_method({
208 description
=> "Stop the container. This will abruptly stop all processes running in the container.",
210 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
213 additionalProperties
=> 0,
215 node
=> get_standard_option
('pve-node'),
216 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
217 skiplock
=> get_standard_option
('skiplock'),
226 my $rpcenv = PVE
::RPCEnvironment
::get
();
228 my $authuser = $rpcenv->get_user();
230 my $node = extract_param
($param, 'node');
232 my $vmid = extract_param
($param, 'vmid');
234 my $skiplock = extract_param
($param, 'skiplock');
235 raise_param_exc
({ skiplock
=> "Only root may use this option." })
236 if $skiplock && $authuser ne 'root@pam';
238 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
240 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
245 print "Requesting HA stop for CT $vmid\n";
247 my $cmd = ['ha-manager', 'crm-command', 'stop', "ct:$vmid", '0'];
248 PVE
::Tools
::run_command
($cmd);
251 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
258 syslog
('info', "stopping CT $vmid: $upid\n");
260 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
261 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
262 PVE
::LXC
::Config-
>check_lock($conf);
265 PVE
::LXC
::vm_stop
($vmid, 1);
269 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
272 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
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 syslog
('info', "shutdown CT $vmid: $upid\n");
341 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
342 PVE
::LXC
::Config-
>check_lock($conf);
344 PVE
::LXC
::vm_stop
($vmid, 0, $timeout, $param->{forceStop
});
348 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
351 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
354 __PACKAGE__-
>register_method({
355 name
=> 'vm_suspend',
360 description
=> "Suspend the container.",
362 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
365 additionalProperties
=> 0,
367 node
=> get_standard_option
('pve-node'),
368 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
377 my $rpcenv = PVE
::RPCEnvironment
::get
();
378 my $authuser = $rpcenv->get_user();
380 my $node = extract_param
($param, 'node');
381 my $vmid = extract_param
($param, 'vmid');
383 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
388 syslog
('info', "suspend CT $vmid: $upid\n");
390 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
391 PVE
::LXC
::Config-
>check_lock($conf);
393 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
398 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
401 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
404 __PACKAGE__-
>register_method({
410 description
=> "Resume the container.",
412 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
415 additionalProperties
=> 0,
417 node
=> get_standard_option
('pve-node'),
418 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
427 my $rpcenv = PVE
::RPCEnvironment
::get
();
428 my $authuser = $rpcenv->get_user();
430 my $node = extract_param
($param, 'node');
431 my $vmid = extract_param
($param, 'vmid');
433 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
438 syslog
('info', "resume CT $vmid: $upid\n");
440 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground', '-D', '/var/lib/vz/dump'];
444 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
449 __PACKAGE__-
>register_method({
455 description
=> "Reboot the container by shutting it down, and starting it again. Applies pending changes.",
457 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
460 additionalProperties
=> 0,
462 node
=> get_standard_option
('pve-node'),
463 vmid
=> get_standard_option
('pve-vmid',
464 { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
466 description
=> "Wait maximal timeout seconds for the shutdown.",
479 my $rpcenv = PVE
::RPCEnvironment
::get
();
480 my $authuser = $rpcenv->get_user();
482 my $node = extract_param
($param, 'node');
483 my $vmid = extract_param
($param, 'vmid');
485 die "VM $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
490 syslog
('info', "requesting reboot of CT $vmid: $upid\n");
491 PVE
::LXC
::vm_reboot
($vmid, $param->{timeout
});
495 return $rpcenv->fork_worker('vzreboot', $vmid, $authuser, $realcmd);