]>
git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC/Status.pm
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'),
90 returns
=> { type
=> 'object' },
95 my $conf = PVE
::LXC
::Config-
>load_config($param->{vmid
});
97 my $vmstatus = PVE
::LXC
::vmstatus
($param->{vmid
});
98 my $status = $vmstatus->{$param->{vmid
}};
100 $status->{ha
} = PVE
::HA
::Config
::get_service_status
("ct:$param->{vmid}");
105 __PACKAGE__-
>register_method({
111 description
=> "Start the container.",
113 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
116 additionalProperties
=> 0,
118 node
=> get_standard_option
('pve-node'),
119 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
120 skiplock
=> get_standard_option
('skiplock'),
129 my $rpcenv = PVE
::RPCEnvironment
::get
();
131 my $authuser = $rpcenv->get_user();
133 my $node = extract_param
($param, 'node');
135 my $vmid = extract_param
($param, 'vmid');
137 my $skiplock = extract_param
($param, 'skiplock');
138 raise_param_exc
({ skiplock
=> "Only root may use this option." })
139 if $skiplock && $authuser ne 'root@pam';
141 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
143 PVE
::Cluster
::check_cfs_quorum
();
145 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
150 my $service = "ct:$vmid";
152 my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
154 print "Requesting HA start for CT $vmid\n";
156 PVE
::Tools
::run_command
($cmd);
161 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
169 syslog
('info', "starting CT $vmid: $upid\n");
171 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
173 die "you can't start a CT if it's a template\n"
174 if PVE
::LXC
::Config-
>is_template($conf);
176 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
177 PVE
::LXC
::Config-
>check_lock($conf);
180 if ($conf->{unprivileged
}) {
181 PVE
::LXC
::Config-
>foreach_mountpoint($conf, sub {
182 my ($ms, $mountpoint) = @_;
183 die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota
};
188 my $storage_cfg = cfs_read_file
("storage.cfg");
190 PVE
::LXC
::update_lxc_config
($vmid, $conf);
192 local $ENV{PVE_SKIPLOCK
}=1 if $skiplock;
194 my $cmd = ['systemctl', 'start', "pve-container\@$vmid"];
201 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
204 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
208 __PACKAGE__-
>register_method({
214 description
=> "Stop the container. This will abruptly stop all processes running in the container.",
216 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
219 additionalProperties
=> 0,
221 node
=> get_standard_option
('pve-node'),
222 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
223 skiplock
=> get_standard_option
('skiplock'),
232 my $rpcenv = PVE
::RPCEnvironment
::get
();
234 my $authuser = $rpcenv->get_user();
236 my $node = extract_param
($param, 'node');
238 my $vmid = extract_param
($param, 'vmid');
240 my $skiplock = extract_param
($param, 'skiplock');
241 raise_param_exc
({ skiplock
=> "Only root may use this option." })
242 if $skiplock && $authuser ne 'root@pam';
244 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
246 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) && $rpcenv->{type
} ne 'ha') {
251 my $service = "ct:$vmid";
253 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
255 print "Requesting HA stop for CT $vmid\n";
257 PVE
::Tools
::run_command
($cmd);
262 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
270 syslog
('info', "stopping CT $vmid: $upid\n");
272 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
274 if (!$skiplock && !PVE
::LXC
::Config-
>has_lock($conf, 'mounted')) {
275 PVE
::LXC
::Config-
>check_lock($conf);
278 PVE
::LXC
::vm_stop
($vmid, 1);
283 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
286 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
290 __PACKAGE__-
>register_method({
291 name
=> 'vm_shutdown',
296 description
=> "Shutdown the container. This will trigger a clean shutdown " .
297 "of the container, see lxc-stop(1) for details.",
299 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
302 additionalProperties
=> 0,
304 node
=> get_standard_option
('pve-node'),
305 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
307 description
=> "Wait maximal timeout seconds.",
314 description
=> "Make sure the Container stops.",
327 my $rpcenv = PVE
::RPCEnvironment
::get
();
329 my $authuser = $rpcenv->get_user();
331 my $node = extract_param
($param, 'node');
333 my $vmid = extract_param
($param, 'vmid');
335 my $timeout = extract_param
($param, 'timeout');
337 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
339 if (PVE
::HA
::Config
::vm_is_ha_managed
($vmid) &&
340 $rpcenv->{type
} ne 'ha') {
345 my $service = "ct:$vmid";
347 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
349 print "Requesting HA stop for CT $vmid\n";
351 PVE
::Tools
::run_command
($cmd);
356 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
363 syslog
('info', "shutdown CT $vmid: $upid\n");
365 $timeout = 60 if !defined($timeout);
367 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
369 PVE
::LXC
::Config-
>check_lock($conf);
371 PVE
::LXC
::vm_stop
($vmid, $param->{forceStop
}, $timeout);
376 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
379 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
382 __PACKAGE__-
>register_method({
383 name
=> 'vm_suspend',
388 description
=> "Suspend the container.",
390 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
393 additionalProperties
=> 0,
395 node
=> get_standard_option
('pve-node'),
396 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_running
}),
405 my $rpcenv = PVE
::RPCEnvironment
::get
();
407 my $authuser = $rpcenv->get_user();
409 my $node = extract_param
($param, 'node');
411 my $vmid = extract_param
($param, 'vmid');
413 die "CT $vmid not running\n" if !PVE
::LXC
::check_running
($vmid);
419 syslog
('info', "suspend CT $vmid: $upid\n");
421 my $conf = PVE
::LXC
::Config-
>load_config($vmid);
423 PVE
::LXC
::Config-
>check_lock($conf);
425 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
432 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
435 return PVE
::LXC
::Config-
>lock_config($vmid, $lockcmd);
438 __PACKAGE__-
>register_method({
444 description
=> "Resume the container.",
446 check
=> ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
449 additionalProperties
=> 0,
451 node
=> get_standard_option
('pve-node'),
452 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::LXC
::complete_ctid_stopped
}),
461 my $rpcenv = PVE
::RPCEnvironment
::get
();
463 my $authuser = $rpcenv->get_user();
465 my $node = extract_param
($param, 'node');
467 my $vmid = extract_param
($param, 'vmid');
469 die "CT $vmid already running\n" if PVE
::LXC
::check_running
($vmid);
474 syslog
('info', "resume CT $vmid: $upid\n");
476 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
477 '-D', '/var/lib/vz/dump'];
484 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);