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