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