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
=> 'migrate' },
74 __PACKAGE__-
>register_method({
79 protected
=> 1, # /proc entries are only readable by root
80 description
=> "Get virtual machine status.",
82 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
85 additionalProperties
=> 0,
87 node
=> get_standard_option
('pve-node'),
88 vmid
=> get_standard_option
('pve-vmid'),
94 %$PVE::LXC
::vmstatus_return_properties
,
96 description
=> "HA manager service status.",
105 my $vmid = $param->{vmid
};
106 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
108 my $vmstatus = PVE
::LXC
::vmstatus
($vmid);
109 my $status = $vmstatus->{$vmid};
111 $status->{ha
} = PVE
::HA
::Config
::get_service_status
("ct:$vmid");
116 __PACKAGE__-
>register_method({
122 description
=> "Start the container.",
124 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
127 additionalProperties
=> 0,
129 node
=> get_standard_option
('pve-node'),
130 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
131 skiplock
=> get_standard_option
('skiplock'),
140 my $rpcenv = PVE
::RPCEnvironment
::get
();
141 my $authuser = $rpcenv->get_user();
143 my $node = extract_param
($param, 'node');
144 my $vmid = extract_param
($param, 'vmid');
146 my $skiplock = extract_param
($param, 'skiplock');
147 raise_param_exc
({ skiplock
=> "Only root may use this option." })
148 if $skiplock && $authuser ne 'root@pam';
150 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
152 PVE
::Cluster
::check_cfs_quorum
();
154 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
159 print "Requesting HA start for CT $vmid\n";
161 my $cmd = ['ha-manager', 'set', "ct:$vmid", '--state', 'started'];
162 PVE
::Tools
::run_command
($cmd);
165 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
172 syslog
('info', "starting CT $vmid: $upid\n");
174 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
176 die "you can't start a CT if it's a template\n"
177 if PVE
::LXC
::Config-
>is_template($conf);
179 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
180 PVE
::LXC
::Config-
>check_lock($conf);
183 if ($conf->{unprivileged
}) {
184 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
185 my ($ms, $mountpoint) = @_;
186 die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota
};
190 PVE
::LXC
::vm_start
($vmid, $conf, $skiplock);
194 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
197 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
201 __PACKAGE__-
>register_method({
207 description
=> "Stop the container. This will abruptly stop all processes running in the container.",
209 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
212 additionalProperties
=> 0,
214 node
=> get_standard_option
('pve-node'),
215 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
216 skiplock
=> get_standard_option
('skiplock'),
225 my $rpcenv = PVE
::RPCEnvironment
::get
();
227 my $authuser = $rpcenv->get_user();
229 my $node = extract_param
($param, 'node');
231 my $vmid = extract_param
($param, 'vmid');
233 my $skiplock = extract_param
($param, 'skiplock');
234 raise_param_exc
({ skiplock
=> "Only root may use this option." })
235 if $skiplock && $authuser ne 'root@pam';
237 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
239 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
244 print "Requesting HA stop for CT $vmid\n";
246 my $cmd = ['ha-manager', 'crm-command', 'stop', "ct:$vmid", '0'];
247 PVE
::Tools
::run_command
($cmd);
250 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
257 syslog
('info', "stopping CT $vmid: $upid\n");
259 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
260 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
261 PVE
::LXC
::Config-
>check_lock($conf);
264 PVE
::LXC
::vm_stop
($vmid, 1);
268 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
271 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
275 __PACKAGE__-
>register_method({
276 name
=> 'vm_shutdown',
281 description
=> "Shutdown the container. This will trigger a clean shutdown " .
282 "of the container, see lxc-stop(1) for details.",
284 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
287 additionalProperties
=> 0,
289 node
=> get_standard_option
('pve-node'),
290 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
292 description
=> "Wait maximal timeout seconds.",
299 description
=> "Make sure the Container stops.",
312 my $rpcenv = PVE
::RPCEnvironment
::get
();
313 my $authuser = $rpcenv->get_user();
315 my $node = extract_param
($param, 'node');
316 my $vmid = extract_param
($param, 'vmid');
318 my $timeout = extract_param
($param, 'timeout') // 60;
320 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
322 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
326 print "Requesting HA stop for CT $vmid\n";
328 my $cmd = ['ha-manager', 'crm-command', 'stop', "ct:$vmid", "$timeout"];
329 PVE
::Tools
::run_command
($cmd);
332 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
338 syslog
('info', "shutdown CT $vmid: $upid\n");
340 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
341 PVE
::LXC
::Config-
>check_lock($conf);
343 PVE
::LXC
::vm_stop
($vmid, 0, $timeout, $param->{forceStop
});
347 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
350 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
353 __PACKAGE__-
>register_method({
354 name
=> 'vm_suspend',
359 description
=> "Suspend the container.",
361 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
364 additionalProperties
=> 0,
366 node
=> get_standard_option
('pve-node'),
367 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
376 my $rpcenv = PVE
::RPCEnvironment
::get
();
377 my $authuser = $rpcenv->get_user();
379 my $node = extract_param
($param, 'node');
380 my $vmid = extract_param
($param, 'vmid');
382 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
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 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
403 __PACKAGE__-
>register_method({
409 description
=> "Resume the container.",
411 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
414 additionalProperties
=> 0,
416 node
=> get_standard_option
('pve-node'),
417 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
426 my $rpcenv = PVE
::RPCEnvironment
::get
();
427 my $authuser = $rpcenv->get_user();
429 my $node = extract_param
($param, 'node');
430 my $vmid = extract_param
($param, 'vmid');
432 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
437 syslog
('info', "resume CT $vmid: $upid\n");
439 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground', '-D', '/var/lib/vz/dump'];
443 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);