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);
177 syslog
('info', "starting CT $vmid: $upid\n");
179 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
181 die "you can't start a CT if it's a template\n"
182 if PVE
::LXC
::Config-
>is_template($conf);
184 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
185 PVE
::LXC
::Config-
>check_lock($conf);
188 if ($conf->{unprivileged
}) {
189 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
190 my ($ms, $mountpoint) = @_;
191 die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota
};
195 PVE
::LXC
::vm_start
($vmid, $conf, $skiplock);
199 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
202 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
206 __PACKAGE__-
>register_method({
212 description
=> "Stop the container. This will abruptly stop all processes running in the container.",
214 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
217 additionalProperties
=> 0,
219 node
=> get_standard_option
('pve-node'),
220 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
221 skiplock
=> get_standard_option
('skiplock'),
230 my $rpcenv = PVE
::RPCEnvironment
::get
();
232 my $authuser = $rpcenv->get_user();
234 my $node = extract_param
($param, 'node');
236 my $vmid = extract_param
($param, 'vmid');
238 my $skiplock = extract_param
($param, 'skiplock');
239 raise_param_exc
({ skiplock
=> "Only root may use this option." })
240 if $skiplock && $authuser ne 'root@pam';
242 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
244 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
249 my $service = "ct:$vmid";
251 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
253 print "Requesting HA stop for CT $vmid\n";
255 PVE
::Tools
::run_command
($cmd);
260 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
267 syslog
('info', "stopping CT $vmid: $upid\n");
269 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
270 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
271 PVE
::LXC
::Config-
>check_lock($conf);
274 PVE
::LXC
::vm_stop
($vmid, 1);
278 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
281 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
285 __PACKAGE__-
>register_method({
286 name
=> 'vm_shutdown',
291 description
=> "Shutdown the container. This will trigger a clean shutdown " .
292 "of the container, see lxc-stop(1) for details.",
294 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
297 additionalProperties
=> 0,
299 node
=> get_standard_option
('pve-node'),
300 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
302 description
=> "Wait maximal timeout seconds.",
309 description
=> "Make sure the Container stops.",
322 my $rpcenv = PVE
::RPCEnvironment
::get
();
324 my $authuser = $rpcenv->get_user();
326 my $node = extract_param
($param, 'node');
328 my $vmid = extract_param
($param, 'vmid');
330 my $timeout = extract_param
($param, 'timeout') // 60;
332 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
334 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) &&
335 $rpcenv->{type
} ne 'ha') {
340 my $service = "ct:$vmid";
342 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
344 print "Requesting HA stop for CT $vmid\n";
346 PVE
::Tools
::run_command
($cmd);
351 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
357 syslog
('info', "shutdown CT $vmid: $upid\n");
359 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
360 PVE
::LXC
::Config-
>check_lock($conf);
362 PVE
::LXC
::vm_stop
($vmid, 0, $timeout, $param->{forceStop
});
366 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
369 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
372 __PACKAGE__-
>register_method({
373 name
=> 'vm_suspend',
378 description
=> "Suspend the container.",
380 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
383 additionalProperties
=> 0,
385 node
=> get_standard_option
('pve-node'),
386 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
395 my $rpcenv = PVE
::RPCEnvironment
::get
();
396 my $authuser = $rpcenv->get_user();
398 my $node = extract_param
($param, 'node');
399 my $vmid = extract_param
($param, 'vmid');
401 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
406 syslog
('info', "suspend CT $vmid: $upid\n");
408 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
409 PVE
::LXC
::Config-
>check_lock($conf);
411 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
416 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
419 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
422 __PACKAGE__-
>register_method({
428 description
=> "Resume the container.",
430 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
433 additionalProperties
=> 0,
435 node
=> get_standard_option
('pve-node'),
436 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
445 my $rpcenv = PVE
::RPCEnvironment
::get
();
446 my $authuser = $rpcenv->get_user();
448 my $node = extract_param
($param, 'node');
449 my $vmid = extract_param
($param, 'vmid');
451 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
456 syslog
('info', "resume CT $vmid: $upid\n");
458 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground', '-D', '/var/lib/vz/dump'];
462 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);