]> git.proxmox.com Git - pve-container.git/blob - src/PVE/API2/LXC/Status.pm
Fix #1547: on migration abort, the CT starts again
[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::vm_start($vmid, $conf, $skiplock);
191
192 return;
193 };
194
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 my $service = "ct:$vmid";
246
247 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
248
249 print "Requesting HA stop for CT $vmid\n";
250
251 PVE::Tools::run_command($cmd);
252
253 return;
254 };
255
256 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
257
258 } else {
259
260 my $lockcmd = sub {
261 my $realcmd = sub {
262 my $upid = shift;
263
264 syslog('info', "stopping CT $vmid: $upid\n");
265
266 my $conf = PVE::LXC::Config->load_config($vmid);
267
268 if (!$skiplock && !PVE::LXC::Config->has_lock($conf, 'mounted')) {
269 PVE::LXC::Config->check_lock($conf);
270 }
271
272 PVE::LXC::vm_stop($vmid, 1);
273
274 return;
275 };
276
277 return $rpcenv->fork_worker('vzstop', $vmid, $authuser, $realcmd);
278 };
279
280 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
281 }
282 }});
283
284 __PACKAGE__->register_method({
285 name => 'vm_shutdown',
286 path => 'shutdown',
287 method => 'POST',
288 protected => 1,
289 proxyto => 'node',
290 description => "Shutdown the container. This will trigger a clean shutdown " .
291 "of the container, see lxc-stop(1) for details.",
292 permissions => {
293 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
294 },
295 parameters => {
296 additionalProperties => 0,
297 properties => {
298 node => get_standard_option('pve-node'),
299 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
300 timeout => {
301 description => "Wait maximal timeout seconds.",
302 type => 'integer',
303 minimum => 0,
304 optional => 1,
305 default => 60,
306 },
307 forceStop => {
308 description => "Make sure the Container stops.",
309 type => 'boolean',
310 optional => 1,
311 default => 0,
312 }
313 },
314 },
315 returns => {
316 type => 'string',
317 },
318 code => sub {
319 my ($param) = @_;
320
321 my $rpcenv = PVE::RPCEnvironment::get();
322
323 my $authuser = $rpcenv->get_user();
324
325 my $node = extract_param($param, 'node');
326
327 my $vmid = extract_param($param, 'vmid');
328
329 my $timeout = extract_param($param, 'timeout');
330
331 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
332
333 if (PVE::HA::Config::vm_is_ha_managed($vmid) &&
334 $rpcenv->{type} ne 'ha') {
335
336 my $hacmd = sub {
337 my $upid = shift;
338
339 my $service = "ct:$vmid";
340
341 my $cmd = ['ha-manager', 'set', $service, '--state', 'stopped'];
342
343 print "Requesting HA stop for CT $vmid\n";
344
345 PVE::Tools::run_command($cmd);
346
347 return;
348 };
349
350 return $rpcenv->fork_worker('hastop', $vmid, $authuser, $hacmd);
351 }
352
353 my $lockcmd = sub {
354 my $realcmd = sub {
355 my $upid = shift;
356
357 syslog('info', "shutdown CT $vmid: $upid\n");
358
359 $timeout = 60 if !defined($timeout);
360
361 my $conf = PVE::LXC::Config->load_config($vmid);
362
363 PVE::LXC::Config->check_lock($conf);
364
365 PVE::LXC::vm_stop($vmid, $param->{forceStop}, $timeout);
366
367 return;
368 };
369
370 return $rpcenv->fork_worker('vzshutdown', $vmid, $authuser, $realcmd);
371 };
372
373 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
374 }});
375
376 __PACKAGE__->register_method({
377 name => 'vm_suspend',
378 path => 'suspend',
379 method => 'POST',
380 protected => 1,
381 proxyto => 'node',
382 description => "Suspend the container.",
383 permissions => {
384 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
385 },
386 parameters => {
387 additionalProperties => 0,
388 properties => {
389 node => get_standard_option('pve-node'),
390 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_running }),
391 },
392 },
393 returns => {
394 type => 'string',
395 },
396 code => sub {
397 my ($param) = @_;
398
399 my $rpcenv = PVE::RPCEnvironment::get();
400
401 my $authuser = $rpcenv->get_user();
402
403 my $node = extract_param($param, 'node');
404
405 my $vmid = extract_param($param, 'vmid');
406
407 die "CT $vmid not running\n" if !PVE::LXC::check_running($vmid);
408
409 my $lockcmd = sub {
410 my $realcmd = sub {
411 my $upid = shift;
412
413 syslog('info', "suspend CT $vmid: $upid\n");
414
415 my $conf = PVE::LXC::Config->load_config($vmid);
416
417 PVE::LXC::Config->check_lock($conf);
418
419 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-s', '-D', '/var/lib/vz/dump'];
420
421 run_command($cmd);
422
423 return;
424 };
425
426 return $rpcenv->fork_worker('vzsuspend', $vmid, $authuser, $realcmd);
427 };
428
429 return PVE::LXC::Config->lock_config($vmid, $lockcmd);
430 }});
431
432 __PACKAGE__->register_method({
433 name => 'vm_resume',
434 path => 'resume',
435 method => 'POST',
436 protected => 1,
437 proxyto => 'node',
438 description => "Resume the container.",
439 permissions => {
440 check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
441 },
442 parameters => {
443 additionalProperties => 0,
444 properties => {
445 node => get_standard_option('pve-node'),
446 vmid => get_standard_option('pve-vmid', { completion => \&PVE::LXC::complete_ctid_stopped }),
447 },
448 },
449 returns => {
450 type => 'string',
451 },
452 code => sub {
453 my ($param) = @_;
454
455 my $rpcenv = PVE::RPCEnvironment::get();
456
457 my $authuser = $rpcenv->get_user();
458
459 my $node = extract_param($param, 'node');
460
461 my $vmid = extract_param($param, 'vmid');
462
463 die "CT $vmid already running\n" if PVE::LXC::check_running($vmid);
464
465 my $realcmd = sub {
466 my $upid = shift;
467
468 syslog('info', "resume CT $vmid: $upid\n");
469
470 my $cmd = ['lxc-checkpoint', '-n', $vmid, '-r', '--foreground',
471 '-D', '/var/lib/vz/dump'];
472
473 run_command($cmd);
474
475 return;
476 };
477
478 my $upid = $rpcenv->fork_worker('vzresume', $vmid, $authuser, $realcmd);
479
480 return $upid;
481 }});
482
483 1;