]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC/Status.pm
7fcd7b82b72559c1d52f7074b4d3ce89c4fbcc1f
[pve-container.git] / src / PVE / API2 / LXC / Status.pm
1 package PVE::API2::LXC::Status;
2
3 use strict;
4 use warnings;
5
6 use PVE::SafeSyslog;
7 use PVE::Tools qw(extract_param run_command);
8 use PVE::Exception qw(raise raise_param_exc);
9 use PVE::INotify;
10 use PVE::Cluster qw(cfs_read_file);
11 use PVE::AccessControl;
12 use PVE::Firewall;
13 use PVE::Storage;
14 use PVE::RESTHandler;
15 use PVE::RPCEnvironment;
16 use PVE::LXC;
17 use PVE::LXC::Create;
18 use PVE::JSONSchema qw(get_standard_option);
19 use base qw(PVE::RESTHandler);
20
21 BEGIN {
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;
27 }
28 }
29
30 __PACKAGE__->register_method({
31 name => 'vmcmdidx',
32 path => '',
33 method => 'GET',
34 proxyto => 'node',
35 description => "Directory index",
36 permissions => {
37 user => 'all',
38 },
39 parameters => {
40 additionalProperties => 0,
41 properties => {
42 node => get_standard_option('pve-node'),
43 vmid => get_standard_option('pve-vmid'),
44 },
45 },
46 returns => {
47 type => 'array',
48 items => {
49 type => "object",
50 properties => {
51 subdir => { type => 'string' },
52 },
53 },
54 links => [ { rel => 'child', href => "{subdir}" } ],
55 },
56 code => sub {
57 my ($param) = @_;
58
59 # test if VM exists
60 my $conf = PVE::LXC::Config->load_config($param->{vmid});
61
62 my $res = [
63 { subdir => 'current' },
64 { subdir => 'start' },
65 { subdir => 'stop' },
66 { subdir => 'shutdown' },
67 { subdir => 'migrate' },
68 ];
69
70 return $res;
71 }});
72
73 __PACKAGE__->register_method({
74 name => 'vm_status',
75 path => 'current',
76 method => 'GET',
77 proxyto => 'node',
78 protected => 1, # openvz /proc entries are only readable by root
79 description => "Get virtual machine status.",
80 permissions => {
81 check => ['perm', '/vms/{vmid}', [ 'VM.Audit' ]],
82 },
83 parameters => {
84 additionalProperties => 0,
85 properties => {
86 node => get_standard_option('pve-node'),
87 vmid => get_standard_option('pve-vmid'),
88 },
89 },
90 returns => {
91 type => 'object',
92 properties => {
93 %$PVE::LXC::vmstatus_return_properties,
94 ha => {
95 description => "HA manager service status.",
96 type => 'object',
97 },
98 },
99 },
100 code => sub {
101 my ($param) = @_;
102
103 # test if VM exists
104 my $vmid = $param->{vmid};
105 my $conf = PVE::LXC::Config->load_config($vmid);
106
107 my $vmstatus = PVE::LXC::vmstatus($vmid);
108 my $status = $vmstatus->{$vmid};
109
110 $status->{ha} = PVE::HA::Config::get_service_status("ct:$vmid");
111
112 return $status;
113 }});
114
115 __PACKAGE__->register_method({
116 name => 'vm_start',
117 path => 'start',
118 method => 'POST',
119 protected => 1,
120 proxyto => 'node',
121 description => "Start the container.",
122 permissions => {
123 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
124 },
125 parameters => {
126 additionalProperties => 0,
127 properties => {
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'),
131 },
132 },
133 returns => {
134 type => 'string',
135 },
136 code => sub {
137 my ($param) = @_;
138
139 my $rpcenv = PVE::RPCEnvironment::get();
140
141 my $authuser = $rpcenv->get_user();
142
143 my $node = extract_param($param, 'node');
144
145 my $vmid = extract_param($param, 'vmid');
146
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';
150
151 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
152
153 PVE::Cluster::check_cfs_quorum();
154
155 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
156
157 my $hacmd = sub {
158 my $upid = shift;
159
160 print "Requesting HA start for CT $vmid\n";
161
162 my $cmd = ['ha-manager', 'set', "ct:$vmid", '--state', 'started'];
163 PVE::Tools::run_command($cmd);
164 };
165
166 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
167
168 } else {
169
170 my $realcmd = sub {
171 my $upid = shift;
172
173 syslog('info', "starting CT $vmid: $upid\n");
174
175 my $conf = PVE::LXC::Config->load_config($vmid);
176
177 die "you can't start a CT if it's a template\n"
178 if PVE::LXC::Config->is_template($conf);
179
180 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
181 PVE::LXC::Config->check_lock($conf);
182 }
183
184 if ($conf->{unprivileged}) {
185 PVE::LXC::Config->foreach_mountpoint($conf, sub {
186 my ($ms, $mountpoint) = @_;
187 die "Quotas are not supported by unprivileged containers.\n" if $mountpoint->{quota};
188 });
189 }
190
191 PVE::LXC::vm_start($vmid, $conf, $skiplock);
192 };
193
194 my $lockcmd = sub {
195 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
196 };
197
198 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
199 }
200 }});
201
202 __PACKAGE__->register_method({
203 name => 'vm_stop',
204 path => 'stop',
205 method => 'POST',
206 protected => 1,
207 proxyto => 'node',
208 description => "Stop the container. This will abruptly stop all processes running in the container.",
209 permissions => {
210 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
211 },
212 parameters => {
213 additionalProperties => 0,
214 properties => {
215 node => get_standard_option('pve-node'),
216 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
217 skiplock => get_standard_option('skiplock'),
218 },
219 },
220 returns => {
221 type => 'string',
222 },
223 code => sub {
224 my ($param) = @_;
225
226 my $rpcenv = PVE::RPCEnvironment::get();
227
228 my $authuser = $rpcenv->get_user();
229
230 my $node = extract_param($param, 'node');
231
232 my $vmid = extract_param($param, 'vmid');
233
234 my $skiplock = extract_param($param, 'skiplock');
235 raise_param_exc({ skiplock => "Only root may use this option." })
236 if $skiplock && $authuser ne 'root@pam';
237
238 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
239
240 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
241
242 my $hacmd = sub {
243 my $upid = shift;
244
245 print "Requesting HA stop for CT $vmid\n";
246
247 my $cmd = ['ha-manager', 'set', "ct:$vmid", '--state', 'stopped'];
248 PVE::Tools::run_command($cmd);
249 };
250
251 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
252
253 } else {
254
255 my $realcmd = sub {
256 my $upid = shift;
257
258 syslog('info', "stopping CT $vmid: $upid\n");
259
260 my $conf = PVE::LXC::Config->load_config($vmid);
261 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
262 PVE::LXC::Config->check_lock($conf);
263 }
264
265 PVE::LXC::vm_stop($vmid, 1);
266 };
267
268 my $lockcmd = sub {
269 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
270 };
271
272 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
273 }
274 }});
275
276 __PACKAGE__->register_method({
277 name => 'vm_shutdown',
278 path => 'shutdown',
279 method => 'POST',
280 protected => 1,
281 proxyto => 'node',
282 description => "Shutdown the container. This will trigger a clean shutdown " .
283 "of the container, see lxc-stop(1) for details.",
284 permissions => {
285 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
286 },
287 parameters => {
288 additionalProperties => 0,
289 properties => {
290 node => get_standard_option('pve-node'),
291 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
292 timeout => {
293 description => "Wait maximal timeout seconds.",
294 type => 'integer',
295 minimum => 0,
296 optional => 1,
297 default => 60,
298 },
299 forceStop => {
300 description => "Make sure the Container stops.",
301 type => 'boolean',
302 optional => 1,
303 default => 0,
304 }
305 },
306 },
307 returns => {
308 type => 'string',
309 },
310 code => sub {
311 my ($param) = @_;
312
313 my $rpcenv = PVE::RPCEnvironment::get();
314 my $authuser = $rpcenv->get_user();
315
316 my $node = extract_param($param, 'node');
317 my $vmid = extract_param($param, 'vmid');
318
319 my $timeout = extract_param($param, 'timeout') // 60;
320
321 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
322
323 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
324 my $hacmd = sub {
325 my $upid = shift;
326
327 print "Requesting HA stop for CT $vmid\n";
328
329 my $cmd = ['ha-manager', 'set', "ct:$vmid", '--state', 'stopped'];
330 PVE::Tools::run_command($cmd);
331 };
332
333 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
334 }
335
336 my $realcmd = sub {
337 my $upid = shift;
338
339 syslog('info', "shutdown CT $vmid: $upid\n");
340
341 my $conf = PVE::LXC::Config->load_config($vmid);
342 PVE::LXC::Config->check_lock($conf);
343
344 PVE::LXC::vm_stop($vmid, 0, $timeout, $param->{forceStop});
345 };
346
347 my $lockcmd = sub {
348 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
349 };
350
351 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
352 }});
353
354 __PACKAGE__->register_method({
355 name => 'vm_suspend',
356 path => 'suspend',
357 method => 'POST',
358 protected => 1,
359 proxyto => 'node',
360 description => "Suspend the container.",
361 permissions => {
362 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
363 },
364 parameters => {
365 additionalProperties => 0,
366 properties => {
367 node => get_standard_option('pve-node'),
368 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
369 },
370 },
371 returns => {
372 type => 'string',
373 },
374 code => sub {
375 my ($param) = @_;
376
377 my $rpcenv = PVE::RPCEnvironment::get();
378 my $authuser = $rpcenv->get_user();
379
380 my $node = extract_param($param, 'node');
381 my $vmid = extract_param($param, 'vmid');
382
383 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
384
385 my $realcmd = sub {
386 my $upid = shift;
387
388 syslog('info', "suspend CT $vmid: $upid\n");
389
390 my $conf = PVE::LXC::Config->load_config($vmid);
391 PVE::LXC::Config->check_lock($conf);
392
393 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
394 run_command($cmd);
395 };
396
397 my $lockcmd = sub {
398 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
399 };
400
401 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
402 }});
403
404 __PACKAGE__->register_method({
405 name => 'vm_resume',
406 path => 'resume',
407 method => 'POST',
408 protected => 1,
409 proxyto => 'node',
410 description => "Resume the container.",
411 permissions => {
412 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
413 },
414 parameters => {
415 additionalProperties => 0,
416 properties => {
417 node => get_standard_option('pve-node'),
418 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
419 },
420 },
421 returns => {
422 type => 'string',
423 },
424 code => sub {
425 my ($param) = @_;
426
427 my $rpcenv = PVE::RPCEnvironment::get();
428 my $authuser = $rpcenv->get_user();
429
430 my $node = extract_param($param, 'node');
431 my $vmid = extract_param($param, 'vmid');
432
433 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
434
435 my $realcmd = sub {
436 my $upid = shift;
437
438 syslog('info', "resume CT $vmid: $upid\n");
439
440 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground', '-D', '/var/lib/vz/dump'];
441 run_command($cmd);
442 };
443
444 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
445
446 return $upid;
447 }});
448
449 1;