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 $vmid = $param->{vmid
};
105 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
107 my $vmstatus = PVE
::LXC
::vmstatus
($vmid);
108 my $status = $vmstatus->{$vmid};
110 $status->{ha
} = PVE
::HA
::Config
::get_service_status
("ct:$vmid");
115 __PACKAGE__-
>register_method({
121 description
=> "Start the container.",
123 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
126 additionalProperties
=> 0,
128 node
=> get_standard_option
('pve-node'),
129 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
130 skiplock
=> get_standard_option
('skiplock'),
139 my $rpcenv = PVE
::RPCEnvironment
::get
();
141 my $authuser = $rpcenv->get_user();
143 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 my $service = "ct:$vmid";
162 my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
164 print "Requesting HA start for CT $vmid\n";
166 PVE
::Tools
::run_command
($cmd);
171 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
};
196 PVE
::LXC
::vm_start
($vmid, $conf, $skiplock);
200 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
203 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
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
();
233 my $authuser = $rpcenv->get_user();
235 my $node = extract_param
($param, 'node');
237 my $vmid = extract_param
($param, 'vmid');
239 my $skiplock = extract_param
($param, 'skiplock');
240 raise_param_exc
({ skiplock
=> "Only root may use this option." })
241 if $skiplock && $authuser ne 'root@pam';
243 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
245 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
250 my $service = "ct:$vmid";
252 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
254 print "Requesting HA stop for CT $vmid\n";
256 PVE
::Tools
::run_command
($cmd);
261 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
268 syslog
('info', "stopping CT $vmid: $upid\n");
270 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
271 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
272 PVE
::LXC
::Config-
>check_lock($conf);
275 PVE
::LXC
::vm_stop
($vmid, 1);
279 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
282 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
286 __PACKAGE__-
>register_method({
287 name
=> 'vm_shutdown',
292 description
=> "Shutdown the container. This will trigger a clean shutdown " .
293 "of the container, see lxc-stop(1) for details.",
295 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
298 additionalProperties
=> 0,
300 node
=> get_standard_option
('pve-node'),
301 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
303 description
=> "Wait maximal timeout seconds.",
310 description
=> "Make sure the Container stops.",
323 my $rpcenv = PVE
::RPCEnvironment
::get
();
325 my $authuser = $rpcenv->get_user();
327 my $node = extract_param
($param, 'node');
329 my $vmid = extract_param
($param, 'vmid');
331 my $timeout = extract_param
($param, 'timeout') // 60;
333 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
335 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) &&
336 $rpcenv->{type
} ne 'ha') {
341 my $service = "ct:$vmid";
343 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
345 print "Requesting HA stop for CT $vmid\n";
347 PVE
::Tools
::run_command
($cmd);
352 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
358 syslog
('info', "shutdown CT $vmid: $upid\n");
360 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
361 PVE
::LXC
::Config-
>check_lock($conf);
363 PVE
::LXC
::vm_stop
($vmid, 0, $timeout, $param->{forceStop
});
367 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
370 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
373 __PACKAGE__-
>register_method({
374 name
=> 'vm_suspend',
379 description
=> "Suspend the container.",
381 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
384 additionalProperties
=> 0,
386 node
=> get_standard_option
('pve-node'),
387 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
396 my $rpcenv = PVE
::RPCEnvironment
::get
();
397 my $authuser = $rpcenv->get_user();
399 my $node = extract_param
($param, 'node');
400 my $vmid = extract_param
($param, 'vmid');
402 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
407 syslog
('info', "suspend CT $vmid: $upid\n");
409 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
410 PVE
::LXC
::Config-
>check_lock($conf);
412 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
417 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
420 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
423 __PACKAGE__-
>register_method({
429 description
=> "Resume the container.",
431 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
434 additionalProperties
=> 0,
436 node
=> get_standard_option
('pve-node'),
437 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
446 my $rpcenv = PVE
::RPCEnvironment
::get
();
447 my $authuser = $rpcenv->get_user();
449 my $node = extract_param
($param, 'node');
450 my $vmid = extract_param
($param, 'vmid');
452 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
457 syslog
('info', "resume CT $vmid: $upid\n");
459 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground', '-D', '/var/lib/vz/dump'];
463 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);