]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC/Status.pm
add vm_stop helper
[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 => { type => 'object' },
91 code => sub {
92 my ($param) = @_;
93
94 # test if VM exists
95 my $conf = PVE::LXC::Config->load_config($param->{vmid});
96
97 my $vmstatus = PVE::LXC::vmstatus($param->{vmid});
98 my $status = $vmstatus->{$param->{vmid}};
99
100 $status->{ha} = PVE::HA::Config::get_service_status("ct:$param->{vmid}");
101
102 return $status;
103 }});
104
105 __PACKAGE__->register_method({
106 name => 'vm_start',
107 path => 'start',
108 method => 'POST',
109 protected => 1,
110 proxyto => 'node',
111 description => "Start the container.",
112 permissions => {
113 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
114 },
115 parameters => {
116 additionalProperties => 0,
117 properties => {
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'),
121 },
122 },
123 returns => {
124 type => 'string',
125 },
126 code => sub {
127 my ($param) = @_;
128
129 my $rpcenv = PVE::RPCEnvironment::get();
130
131 my $authuser = $rpcenv->get_user();
132
133 my $node = extract_param($param, 'node');
134
135 my $vmid = extract_param($param, 'vmid');
136
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';
140
141 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
142
143 PVE::Cluster::check_cfs_quorum();
144
145 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
146
147 my $hacmd = sub {
148 my $upid = shift;
149
150 my $service = "ct:$vmid";
151
152 my $cmd = ['ha-manager', 'set', $service, '--state', 'started'];
153
154 print "Requesting HA start for CT $vmid\n";
155
156 PVE::Tools::run_command($cmd);
157
158 return;
159 };
160
161 return $rpcenv->fork_worker('hastart', $vmid, $authuser, $hacmd);
162
163 } else {
164
165 my $lockcmd = sub {
166 my $realcmd = sub {
167 my $upid = shift;
168
169 syslog('info', "starting CT $vmid: $upid\n");
170
171 my $conf = PVE::LXC::Config->load_config($vmid);
172
173 die "you can't start a CT if it's a template\n"
174 if PVE::LXC::Config->is_template($conf);
175
176 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
177 PVE::LXC::Config->check_lock($conf);
178 }
179
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};
184 });
185
186 }
187
188 my $storage_cfg = cfs_read_file("storage.cfg");
189
190 PVE::LXC::update_lxc_config($vmid, $conf);
191
192 local $ENV{PVE_SKIPLOCK}=1 if $skiplock;
193
194 my $cmd = ['systemctl', 'start', "pve-container\@$vmid"];
195
196 run_command($cmd);
197
198 return;
199 };
200
201 return $rpcenv->fork_worker('vzstart', $vmid, $authuser, $realcmd);
202 };
203
204 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
205 }
206 }});
207
208 __PACKAGE__->register_method({
209 name => 'vm_stop',
210 path => 'stop',
211 method => 'POST',
212 protected => 1,
213 proxyto => 'node',
214 description => "Stop the container. This will abruptly stop all processes running in the container.",
215 permissions => {
216 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
217 },
218 parameters => {
219 additionalProperties => 0,
220 properties => {
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'),
224 },
225 },
226 returns => {
227 type => 'string',
228 },
229 code => sub {
230 my ($param) = @_;
231
232 my $rpcenv = PVE::RPCEnvironment::get();
233
234 my $authuser = $rpcenv->get_user();
235
236 my $node = extract_param($param, 'node');
237
238 my $vmid = extract_param($param, 'vmid');
239
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';
243
244 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
245
246 if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
247
248 my $hacmd = sub {
249 my $upid = shift;
250
251 my $service = "ct:$vmid";
252
253 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
254
255 print "Requesting HA stop for CT $vmid\n";
256
257 PVE::Tools::run_command($cmd);
258
259 return;
260 };
261
262 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
263
264 } else {
265
266 my $lockcmd = sub {
267 my $realcmd = sub {
268 my $upid = shift;
269
270 syslog('info', "stopping CT $vmid: $upid\n");
271
272 my $conf = PVE::LXC::Config->load_config($vmid);
273
274 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
275 PVE::LXC::Config->check_lock($conf);
276 }
277
278 PVE::LXC::vm_stop($vmid, 1);
279
280 return;
281 };
282
283 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
284 };
285
286 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
287 }
288 }});
289
290 __PACKAGE__->register_method({
291 name => 'vm_shutdown',
292 path => 'shutdown',
293 method => 'POST',
294 protected => 1,
295 proxyto => 'node',
296 description => "Shutdown the container. This will trigger a clean shutdown " .
297 "of the container, see lxc-stop(1) for details.",
298 permissions => {
299 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
300 },
301 parameters => {
302 additionalProperties => 0,
303 properties => {
304 node => get_standard_option('pve-node'),
305 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
306 timeout => {
307 description => "Wait maximal timeout seconds.",
308 type => 'integer',
309 minimum => 0,
310 optional => 1,
311 default => 60,
312 },
313 forceStop => {
314 description => "Make sure the Container stops.",
315 type => 'boolean',
316 optional => 1,
317 default => 0,
318 }
319 },
320 },
321 returns => {
322 type => 'string',
323 },
324 code => sub {
325 my ($param) = @_;
326
327 my $rpcenv = PVE::RPCEnvironment::get();
328
329 my $authuser = $rpcenv->get_user();
330
331 my $node = extract_param($param, 'node');
332
333 my $vmid = extract_param($param, 'vmid');
334
335 my $timeout = extract_param($param, 'timeout');
336
337 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
338
339 if (PVE::HA::Config::vm_is_ha_managed($vmid) &&
340 $rpcenv->{type} ne 'ha') {
341
342 my $hacmd = sub {
343 my $upid = shift;
344
345 my $service = "ct:$vmid";
346
347 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
348
349 print "Requesting HA stop for CT $vmid\n";
350
351 PVE::Tools::run_command($cmd);
352
353 return;
354 };
355
356 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
357 }
358
359 my $lockcmd = sub {
360 my $realcmd = sub {
361 my $upid = shift;
362
363 syslog('info', "shutdown CT $vmid: $upid\n");
364
365 $timeout = 60 if !defined($timeout);
366
367 my $conf = PVE::LXC::Config->load_config($vmid);
368
369 PVE::LXC::Config->check_lock($conf);
370
371 PVE::LXC::vm_stop($vmid, $param->{forceStop}, $timeout);
372
373 return;
374 };
375
376 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
377 };
378
379 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
380 }});
381
382 __PACKAGE__->register_method({
383 name => 'vm_suspend',
384 path => 'suspend',
385 method => 'POST',
386 protected => 1,
387 proxyto => 'node',
388 description => "Suspend the container.",
389 permissions => {
390 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
391 },
392 parameters => {
393 additionalProperties => 0,
394 properties => {
395 node => get_standard_option('pve-node'),
396 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
397 },
398 },
399 returns => {
400 type => 'string',
401 },
402 code => sub {
403 my ($param) = @_;
404
405 my $rpcenv = PVE::RPCEnvironment::get();
406
407 my $authuser = $rpcenv->get_user();
408
409 my $node = extract_param($param, 'node');
410
411 my $vmid = extract_param($param, 'vmid');
412
413 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
414
415 my $lockcmd = sub {
416 my $realcmd = sub {
417 my $upid = shift;
418
419 syslog('info', "suspend CT $vmid: $upid\n");
420
421 my $conf = PVE::LXC::Config->load_config($vmid);
422
423 PVE::LXC::Config->check_lock($conf);
424
425 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
426
427 run_command($cmd);
428
429 return;
430 };
431
432 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
433 };
434
435 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
436 }});
437
438 __PACKAGE__->register_method({
439 name => 'vm_resume',
440 path => 'resume',
441 method => 'POST',
442 protected => 1,
443 proxyto => 'node',
444 description => "Resume the container.",
445 permissions => {
446 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
447 },
448 parameters => {
449 additionalProperties => 0,
450 properties => {
451 node => get_standard_option('pve-node'),
452 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
453 },
454 },
455 returns => {
456 type => 'string',
457 },
458 code => sub {
459 my ($param) = @_;
460
461 my $rpcenv = PVE::RPCEnvironment::get();
462
463 my $authuser = $rpcenv->get_user();
464
465 my $node = extract_param($param, 'node');
466
467 my $vmid = extract_param($param, 'vmid');
468
469 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
470
471 my $realcmd = sub {
472 my $upid = shift;
473
474 syslog('info', "resume CT $vmid: $upid\n");
475
476 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
477 '-D', '/var/lib/vz/dump'];
478
479 run_command($cmd);
480
481 return;
482 };
483
484 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
485
486 return $upid;
487 }});
488
489 1;