]>
Commit | Line | Data |
---|---|---|
1 | package PVE::CLI::qm; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | # Note: disable '+' prefix for Getopt::Long (for resize command) | |
7 | use Getopt::Long qw(:config no_getopt_compat); | |
8 | ||
9 | use Fcntl ':flock'; | |
10 | use File::Path; | |
11 | use IO::Socket::UNIX; | |
12 | use IO::Select; | |
13 | ||
14 | use PVE::Tools qw(extract_param); | |
15 | use PVE::Cluster; | |
16 | use PVE::SafeSyslog; | |
17 | use PVE::INotify; | |
18 | use PVE::RPCEnvironment; | |
19 | use PVE::QemuServer; | |
20 | use PVE::API2::Qemu; | |
21 | use JSON; | |
22 | use PVE::JSONSchema qw(get_standard_option); | |
23 | use Term::ReadLine; | |
24 | ||
25 | use PVE::CLIHandler; | |
26 | ||
27 | use base qw(PVE::CLIHandler); | |
28 | ||
29 | my $upid_exit = sub { | |
30 | my $upid = shift; | |
31 | my $status = PVE::Tools::upid_read_status($upid); | |
32 | exit($status eq 'OK' ? 0 : -1); | |
33 | }; | |
34 | ||
35 | my $nodename = PVE::INotify::nodename(); | |
36 | ||
37 | sub run_vnc_proxy { | |
38 | my ($path) = @_; | |
39 | ||
40 | my $c; | |
41 | while ( ++$c < 10 && !-e $path ) { sleep(1); } | |
42 | ||
43 | my $s = IO::Socket::UNIX->new(Peer => $path, Timeout => 120); | |
44 | ||
45 | die "unable to connect to socket '$path' - $!" if !$s; | |
46 | ||
47 | my $select = new IO::Select; | |
48 | ||
49 | $select->add(\*STDIN); | |
50 | $select->add($s); | |
51 | ||
52 | my $timeout = 60*15; # 15 minutes | |
53 | ||
54 | my @handles; | |
55 | while ($select->count && | |
56 | scalar(@handles = $select->can_read ($timeout))) { | |
57 | foreach my $h (@handles) { | |
58 | my $buf; | |
59 | my $n = $h->sysread($buf, 4096); | |
60 | ||
61 | if ($h == \*STDIN) { | |
62 | if ($n) { | |
63 | syswrite($s, $buf); | |
64 | } else { | |
65 | exit(0); | |
66 | } | |
67 | } elsif ($h == $s) { | |
68 | if ($n) { | |
69 | syswrite(\*STDOUT, $buf); | |
70 | } else { | |
71 | exit(0); | |
72 | } | |
73 | } | |
74 | } | |
75 | } | |
76 | exit(0); | |
77 | } | |
78 | ||
79 | sub print_recursive_hash { | |
80 | my ($prefix, $hash, $key) = @_; | |
81 | ||
82 | if (ref($hash) eq 'HASH') { | |
83 | if (defined($key)) { | |
84 | print "$prefix$key:\n"; | |
85 | } | |
86 | foreach my $itemkey (keys %$hash) { | |
87 | print_recursive_hash("\t$prefix", $hash->{$itemkey}, $itemkey); | |
88 | } | |
89 | } elsif (ref($hash) eq 'ARRAY') { | |
90 | if (defined($key)) { | |
91 | print "$prefix$key:\n"; | |
92 | } | |
93 | foreach my $item (@$hash) { | |
94 | print_recursive_hash("\t$prefix", $item); | |
95 | } | |
96 | } elsif (!ref($hash) && defined($hash)) { | |
97 | if (defined($key)) { | |
98 | print "$prefix$key: $hash\n"; | |
99 | } else { | |
100 | print "$prefix$hash\n"; | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | __PACKAGE__->register_method ({ | |
106 | name => 'showcmd', | |
107 | path => 'showcmd', | |
108 | method => 'GET', | |
109 | description => "Show command line which is used to start the VM (debug info).", | |
110 | parameters => { | |
111 | additionalProperties => 0, | |
112 | properties => { | |
113 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
114 | }, | |
115 | }, | |
116 | returns => { type => 'null'}, | |
117 | code => sub { | |
118 | my ($param) = @_; | |
119 | ||
120 | my $storecfg = PVE::Storage::config(); | |
121 | print PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}) . "\n"; | |
122 | ||
123 | return undef; | |
124 | }}); | |
125 | ||
126 | __PACKAGE__->register_method ({ | |
127 | name => 'status', | |
128 | path => 'status', | |
129 | method => 'GET', | |
130 | description => "Show VM status.", | |
131 | parameters => { | |
132 | additionalProperties => 0, | |
133 | properties => { | |
134 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
135 | verbose => { | |
136 | description => "Verbose output format", | |
137 | type => 'boolean', | |
138 | optional => 1, | |
139 | } | |
140 | }, | |
141 | }, | |
142 | returns => { type => 'null'}, | |
143 | code => sub { | |
144 | my ($param) = @_; | |
145 | ||
146 | # test if VM exists | |
147 | my $conf = PVE::QemuConfig->load_config ($param->{vmid}); | |
148 | ||
149 | my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); | |
150 | my $stat = $vmstatus->{$param->{vmid}}; | |
151 | if ($param->{verbose}) { | |
152 | foreach my $k (sort (keys %$stat)) { | |
153 | next if $k eq 'cpu' || $k eq 'relcpu'; # always 0 | |
154 | my $v = $stat->{$k}; | |
155 | print_recursive_hash("", $v, $k); | |
156 | } | |
157 | } else { | |
158 | my $status = $stat->{qmpstatus} || 'unknown'; | |
159 | print "status: $status\n"; | |
160 | } | |
161 | ||
162 | return undef; | |
163 | }}); | |
164 | ||
165 | __PACKAGE__->register_method ({ | |
166 | name => 'vncproxy', | |
167 | path => 'vncproxy', | |
168 | method => 'PUT', | |
169 | description => "Proxy VM VNC traffic to stdin/stdout", | |
170 | parameters => { | |
171 | additionalProperties => 0, | |
172 | properties => { | |
173 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), | |
174 | }, | |
175 | }, | |
176 | returns => { type => 'null'}, | |
177 | code => sub { | |
178 | my ($param) = @_; | |
179 | ||
180 | my $vmid = $param->{vmid}; | |
181 | my $vnc_socket = PVE::QemuServer::vnc_socket($vmid); | |
182 | ||
183 | if (my $ticket = $ENV{LC_PVE_TICKET}) { # NOTE: ssh on debian only pass LC_* variables | |
184 | PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,password"); | |
185 | PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'vnc', password => $ticket); | |
186 | PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'vnc', time => "+30"); | |
187 | } else { | |
188 | PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,x509,password"); | |
189 | } | |
190 | ||
191 | run_vnc_proxy($vnc_socket); | |
192 | ||
193 | return undef; | |
194 | }}); | |
195 | ||
196 | __PACKAGE__->register_method ({ | |
197 | name => 'unlock', | |
198 | path => 'unlock', | |
199 | method => 'PUT', | |
200 | description => "Unlock the VM.", | |
201 | parameters => { | |
202 | additionalProperties => 0, | |
203 | properties => { | |
204 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), | |
205 | }, | |
206 | }, | |
207 | returns => { type => 'null'}, | |
208 | code => sub { | |
209 | my ($param) = @_; | |
210 | ||
211 | my $vmid = $param->{vmid}; | |
212 | ||
213 | PVE::QemuConfig->lock_config ($vmid, sub { | |
214 | my $conf = PVE::QemuConfig->load_config($vmid); | |
215 | delete $conf->{lock}; | |
216 | delete $conf->{pending}->{lock} if $conf->{pending}; # just to be sure | |
217 | PVE::QemuConfig->write_config($vmid, $conf); | |
218 | }); | |
219 | ||
220 | return undef; | |
221 | }}); | |
222 | ||
223 | __PACKAGE__->register_method ({ | |
224 | name => 'mtunnel', | |
225 | path => 'mtunnel', | |
226 | method => 'POST', | |
227 | description => "Used by qmigrate - do not use manually.", | |
228 | parameters => { | |
229 | additionalProperties => 0, | |
230 | properties => {}, | |
231 | }, | |
232 | returns => { type => 'null'}, | |
233 | code => sub { | |
234 | my ($param) = @_; | |
235 | ||
236 | if (!PVE::Cluster::check_cfs_quorum(1)) { | |
237 | print "no quorum\n"; | |
238 | return undef; | |
239 | } | |
240 | ||
241 | print "tunnel online\n"; | |
242 | *STDOUT->flush(); | |
243 | ||
244 | while (my $line = <>) { | |
245 | chomp $line; | |
246 | last if $line =~ m/^quit$/; | |
247 | } | |
248 | ||
249 | return undef; | |
250 | }}); | |
251 | ||
252 | __PACKAGE__->register_method ({ | |
253 | name => 'wait', | |
254 | path => 'wait', | |
255 | method => 'GET', | |
256 | description => "Wait until the VM is stopped.", | |
257 | parameters => { | |
258 | additionalProperties => 0, | |
259 | properties => { | |
260 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), | |
261 | timeout => { | |
262 | description => "Timeout in seconds. Default is to wait forever.", | |
263 | type => 'integer', | |
264 | minimum => 1, | |
265 | optional => 1, | |
266 | } | |
267 | }, | |
268 | }, | |
269 | returns => { type => 'null'}, | |
270 | code => sub { | |
271 | my ($param) = @_; | |
272 | ||
273 | my $vmid = $param->{vmid}; | |
274 | my $timeout = $param->{timeout}; | |
275 | ||
276 | my $pid = PVE::QemuServer::check_running ($vmid); | |
277 | return if !$pid; | |
278 | ||
279 | print "waiting until VM $vmid stopps (PID $pid)\n"; | |
280 | ||
281 | my $count = 0; | |
282 | while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) { | |
283 | $count++; | |
284 | sleep 1; | |
285 | } | |
286 | ||
287 | die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid); | |
288 | ||
289 | return undef; | |
290 | }}); | |
291 | ||
292 | __PACKAGE__->register_method ({ | |
293 | name => 'monitor', | |
294 | path => 'monitor', | |
295 | method => 'POST', | |
296 | description => "Enter Qemu Monitor interface.", | |
297 | parameters => { | |
298 | additionalProperties => 0, | |
299 | properties => { | |
300 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), | |
301 | }, | |
302 | }, | |
303 | returns => { type => 'null'}, | |
304 | code => sub { | |
305 | my ($param) = @_; | |
306 | ||
307 | my $vmid = $param->{vmid}; | |
308 | ||
309 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists | |
310 | ||
311 | print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n"; | |
312 | ||
313 | my $term = new Term::ReadLine ('qm'); | |
314 | ||
315 | my $input; | |
316 | while (defined ($input = $term->readline('qm> '))) { | |
317 | chomp $input; | |
318 | ||
319 | next if $input =~ m/^\s*$/; | |
320 | ||
321 | last if $input =~ m/^\s*q(uit)?\s*$/; | |
322 | ||
323 | eval { | |
324 | print PVE::QemuServer::vm_human_monitor_command ($vmid, $input); | |
325 | }; | |
326 | print "ERROR: $@" if $@; | |
327 | } | |
328 | ||
329 | return undef; | |
330 | ||
331 | }}); | |
332 | ||
333 | __PACKAGE__->register_method ({ | |
334 | name => 'rescan', | |
335 | path => 'rescan', | |
336 | method => 'POST', | |
337 | description => "Rescan all storages and update disk sizes and unused disk images.", | |
338 | parameters => { | |
339 | additionalProperties => 0, | |
340 | properties => { | |
341 | vmid => get_standard_option('pve-vmid', { | |
342 | optional => 1, | |
343 | completion => \&PVE::QemuServer::complete_vmid, | |
344 | }), | |
345 | }, | |
346 | }, | |
347 | returns => { type => 'null'}, | |
348 | code => sub { | |
349 | my ($param) = @_; | |
350 | ||
351 | PVE::QemuServer::rescan($param->{vmid}); | |
352 | ||
353 | return undef; | |
354 | }}); | |
355 | ||
356 | __PACKAGE__->register_method ({ | |
357 | name => 'terminal', | |
358 | path => 'terminal', | |
359 | method => 'POST', | |
360 | description => "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')", | |
361 | parameters => { | |
362 | additionalProperties => 0, | |
363 | properties => { | |
364 | vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }), | |
365 | iface => { | |
366 | description => "Select the serial device. By default we simply use the first suitable device.", | |
367 | type => 'string', | |
368 | optional => 1, | |
369 | enum => [qw(serial0 serial1 serial2 serial3)], | |
370 | } | |
371 | }, | |
372 | }, | |
373 | returns => { type => 'null'}, | |
374 | code => sub { | |
375 | my ($param) = @_; | |
376 | ||
377 | my $vmid = $param->{vmid}; | |
378 | ||
379 | my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists | |
380 | ||
381 | my $iface = $param->{iface}; | |
382 | ||
383 | if ($iface) { | |
384 | die "serial interface '$iface' is not configured\n" if !$conf->{$iface}; | |
385 | die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket'; | |
386 | } else { | |
387 | foreach my $opt (qw(serial0 serial1 serial2 serial3)) { | |
388 | if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) { | |
389 | $iface = $opt; | |
390 | last; | |
391 | } | |
392 | } | |
393 | die "unable to find a serial interface\n" if !$iface; | |
394 | } | |
395 | ||
396 | die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); | |
397 | ||
398 | my $socket = "/var/run/qemu-server/${vmid}.$iface"; | |
399 | ||
400 | my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f"; | |
401 | ||
402 | print "starting serial terminal on interface $iface (press control-O to exit)\n"; | |
403 | ||
404 | system($cmd); | |
405 | ||
406 | return undef; | |
407 | }}); | |
408 | ||
409 | ||
410 | my $print_agent_result = sub { | |
411 | my ($data) = @_; | |
412 | ||
413 | my $result = $data->{result}; | |
414 | return if !defined($result); | |
415 | ||
416 | my $class = ref($result); | |
417 | ||
418 | if (!$class) { | |
419 | chomp $result; | |
420 | return if $result =~ m/^\s*$/; | |
421 | print "$result\n"; | |
422 | return; | |
423 | } | |
424 | ||
425 | if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash | |
426 | return; | |
427 | } | |
428 | ||
429 | print to_json($result, { pretty => 1, canonical => 1}); | |
430 | }; | |
431 | ||
432 | our $cmddef = { | |
433 | list => [ "PVE::API2::Qemu", 'vmlist', [], | |
434 | { node => $nodename }, sub { | |
435 | my $vmlist = shift; | |
436 | ||
437 | exit 0 if (!scalar(@$vmlist)); | |
438 | ||
439 | printf "%10s %-20s %-10s %-10s %12s %-10s\n", | |
440 | qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID); | |
441 | ||
442 | foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) { | |
443 | printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, | |
444 | $rec->{qmpstatus} || $rec->{status}, | |
445 | ($rec->{maxmem} || 0)/(1024*1024), | |
446 | ($rec->{maxdisk} || 0)/(1024*1024*1024), | |
447 | $rec->{pid}||0; | |
448 | } | |
449 | ||
450 | ||
451 | } ], | |
452 | ||
453 | create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename }, $upid_exit ], | |
454 | ||
455 | destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ], | |
456 | ||
457 | clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ], | |
458 | ||
459 | migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ], | |
460 | ||
461 | set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ], | |
462 | ||
463 | resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ], | |
464 | ||
465 | move_disk => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node => $nodename }, $upid_exit ], | |
466 | ||
467 | unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node => $nodename } ], | |
468 | ||
469 | config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], | |
470 | { node => $nodename }, sub { | |
471 | my $config = shift; | |
472 | foreach my $k (sort (keys %$config)) { | |
473 | next if $k eq 'digest'; | |
474 | my $v = $config->{$k}; | |
475 | if ($k eq 'description') { | |
476 | $v = PVE::Tools::encode_text($v); | |
477 | } | |
478 | print "$k: $v\n"; | |
479 | } | |
480 | }], | |
481 | ||
482 | pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'], | |
483 | { node => $nodename }, sub { | |
484 | my $data = shift; | |
485 | foreach my $item (sort { $a->{key} cmp $b->{key}} @$data) { | |
486 | my $k = $item->{key}; | |
487 | next if $k eq 'digest'; | |
488 | my $v = $item->{value}; | |
489 | my $p = $item->{pending}; | |
490 | if ($k eq 'description') { | |
491 | $v = PVE::Tools::encode_text($v) if defined($v); | |
492 | $p = PVE::Tools::encode_text($p) if defined($p); | |
493 | } | |
494 | if (defined($v)) { | |
495 | if ($item->{delete}) { | |
496 | print "del $k: $v\n"; | |
497 | } elsif (defined($p)) { | |
498 | print "cur $k: $v\n"; | |
499 | print "new $k: $p\n"; | |
500 | } else { | |
501 | print "cur $k: $v\n"; | |
502 | } | |
503 | } elsif (defined($p)) { | |
504 | print "new $k: $p\n"; | |
505 | } | |
506 | } | |
507 | }], | |
508 | ||
509 | showcmd => [ __PACKAGE__, 'showcmd', ['vmid']], | |
510 | ||
511 | status => [ __PACKAGE__, 'status', ['vmid']], | |
512 | ||
513 | snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
514 | ||
515 | delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
516 | ||
517 | listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node => $nodename }, | |
518 | sub { | |
519 | my $res = shift; | |
520 | foreach my $e (@$res) { | |
521 | my $headline = $e->{description} || 'no-description'; | |
522 | $headline =~ s/\n.*//sg; | |
523 | my $parent = $e->{parent} // 'no-parent'; | |
524 | printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline); | |
525 | } | |
526 | }], | |
527 | ||
528 | rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], | |
529 | ||
530 | template => [ "PVE::API2::Qemu", 'template', ['vmid'], { node => $nodename }], | |
531 | ||
532 | start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } , $upid_exit ], | |
533 | ||
534 | stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit ], | |
535 | ||
536 | reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename }, $upid_exit ], | |
537 | ||
538 | shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit ], | |
539 | ||
540 | suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit ], | |
541 | ||
542 | resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit ], | |
543 | ||
544 | sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ], | |
545 | ||
546 | vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], | |
547 | ||
548 | wait => [ __PACKAGE__, 'wait', ['vmid']], | |
549 | ||
550 | unlock => [ __PACKAGE__, 'unlock', ['vmid']], | |
551 | ||
552 | rescan => [ __PACKAGE__, 'rescan', []], | |
553 | ||
554 | monitor => [ __PACKAGE__, 'monitor', ['vmid']], | |
555 | ||
556 | agent => [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'], | |
557 | { node => $nodename }, $print_agent_result ], | |
558 | ||
559 | mtunnel => [ __PACKAGE__, 'mtunnel', []], | |
560 | ||
561 | terminal => [ __PACKAGE__, 'terminal', ['vmid']], | |
562 | }; | |
563 | ||
564 | 1; |