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