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);
19 use base
qw(PVE::RESTHandler);
22 if (!$ENV{PVE_GENERATING_DOCS
}) {
23 require PVE
::HA
::Env
::PVE2
;
24 import PVE
::HA
::Env
::PVE2
;
25 require PVE
::HA
::Config
;
26 import PVE
::HA
::Config
;
30 __PACKAGE__-
>register_method({
35 description
=> "Directory index",
40 additionalProperties
=> 0,
42 node
=> get_standard_option
('pve-node'),
43 vmid
=> get_standard_option
('pve-vmid'),
51 subdir
=> { type
=> 'string' },
54 links
=> [ { rel
=> 'child', href
=> "{subdir}" } ],
60 my $conf = PVE
::LXC
::Config-
>load_config($param->{vmid
});
63 { subdir
=> 'current' },
64 { subdir
=> 'start' },
66 { subdir
=> 'shutdown' },
67 { subdir
=> 'migrate' },
73 __PACKAGE__-
>register_method({
78 protected
=> 1, # openvz /proc entries are only readable by root
79 description
=> "Get virtual machine status.",
81 check
=> ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
84 additionalProperties
=> 0,
86 node
=> get_standard_option
('pve-node'),
87 vmid
=> get_standard_option
('pve-vmid'),
93 %$PVE::LXC
::vmstatus_return_properties
,
95 description
=> "HA manager service status.",
104 my $conf = PVE
::LXC
::Config-
>load_config($param->{vmid
});
106 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
107 my $status = $vmstatus->{$param->{vmid
}};
109 $status->{ha
} = PVE
::HA
::Config
::get_service_status
("ct:$param->{vmid}");
114 __PACKAGE__-
>register_method({
120 description
=> "Start the container.",
122 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
125 additionalProperties
=> 0,
127 node
=> get_standard_option
('pve-node'),
128 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
129 skiplock
=> get_standard_option
('skiplock'),
138 my $rpcenv = PVE
::RPCEnvironment
::get
();
140 my $authuser = $rpcenv->get_user();
142 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 my $service = "ct:$vmid";
161 my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
163 print "Requesting HA start for CT $vmid\n";
165 PVE
::Tools
::run_command
($cmd);
170 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
178 syslog
('info', "starting CT $vmid: $upid\n");
180 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
182 die "you can't start a CT if it's a template\n"
183 if PVE
::LXC
::Config-
>is_template($conf);
185 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
186 PVE
::LXC
::Config-
>check_lock($conf);
189 if ($conf->{unprivileged
}) {
190 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
191 my ($ms, $mountpoint) = @_;
192 die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota
};
197 PVE
::LXC
::vm_start
($vmid, $conf, $skiplock);
202 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
205 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
209 __PACKAGE__-
>register_method({
215 description
=> "Stop the container. This will abruptly stop all processes running in the container.",
217 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
220 additionalProperties
=> 0,
222 node
=> get_standard_option
('pve-node'),
223 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
224 skiplock
=> get_standard_option
('skiplock'),
233 my $rpcenv = PVE
::RPCEnvironment
::get
();
235 my $authuser = $rpcenv->get_user();
237 my $node = extract_param
($param, 'node');
239 my $vmid = extract_param
($param, 'vmid');
241 my $skiplock = extract_param
($param, 'skiplock');
242 raise_param_exc
({ skiplock
=> "Only root may use this option." })
243 if $skiplock && $authuser ne 'root@pam';
245 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
247 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
252 my $service = "ct:$vmid";
254 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
256 print "Requesting HA stop for CT $vmid\n";
258 PVE
::Tools
::run_command
($cmd);
263 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
271 syslog
('info', "stopping CT $vmid: $upid\n");
273 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
275 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
276 PVE
::LXC
::Config-
>check_lock($conf);
279 PVE
::LXC
::vm_stop
($vmid, 1);
284 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
287 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
291 __PACKAGE__-
>register_method({
292 name
=> 'vm_shutdown',
297 description
=> "Shutdown the container. This will trigger a clean shutdown " .
298 "of the container, see lxc-stop(1) for details.",
300 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
303 additionalProperties
=> 0,
305 node
=> get_standard_option
('pve-node'),
306 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
308 description
=> "Wait maximal timeout seconds.",
315 description
=> "Make sure the Container stops.",
328 my $rpcenv = PVE
::RPCEnvironment
::get
();
330 my $authuser = $rpcenv->get_user();
332 my $node = extract_param
($param, 'node');
334 my $vmid = extract_param
($param, 'vmid');
336 my $timeout = extract_param
($param, 'timeout');
338 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
340 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) &&
341 $rpcenv->{type
} ne 'ha') {
346 my $service = "ct:$vmid";
348 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
350 print "Requesting HA stop for CT $vmid\n";
352 PVE
::Tools
::run_command
($cmd);
357 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
364 syslog
('info', "shutdown CT $vmid: $upid\n");
366 $timeout = 60 if !defined($timeout);
368 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
370 PVE
::LXC
::Config-
>check_lock($conf);
372 PVE
::LXC
::vm_stop
($vmid, 0, $timeout, $param->{forceStop
});
377 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
380 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
383 __PACKAGE__-
>register_method({
384 name
=> 'vm_suspend',
389 description
=> "Suspend the container.",
391 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
394 additionalProperties
=> 0,
396 node
=> get_standard_option
('pve-node'),
397 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
406 my $rpcenv = PVE
::RPCEnvironment
::get
();
408 my $authuser = $rpcenv->get_user();
410 my $node = extract_param
($param, 'node');
412 my $vmid = extract_param
($param, 'vmid');
414 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
420 syslog
('info', "suspend CT $vmid: $upid\n");
422 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
424 PVE
::LXC
::Config-
>check_lock($conf);
426 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
433 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
436 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
439 __PACKAGE__-
>register_method({
445 description
=> "Resume the container.",
447 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
450 additionalProperties
=> 0,
452 node
=> get_standard_option
('pve-node'),
453 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
462 my $rpcenv = PVE
::RPCEnvironment
::get
();
464 my $authuser = $rpcenv->get_user();
466 my $node = extract_param
($param, 'node');
468 my $vmid = extract_param
($param, 'vmid');
470 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
475 syslog
('info', "resume CT $vmid: $upid\n");
477 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
478 '-D', '/var/lib/vz/dump'];
485 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);