]> git.proxmox.com Git - qemu-server.git/blob - PVE/CLI/qm.pm
qm: rename 'ga' command group to 'guest'
[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 use URI::Escape;
14
15 use PVE::Tools qw(extract_param);
16 use PVE::Cluster;
17 use PVE::SafeSyslog;
18 use PVE::INotify;
19 use PVE::RPCEnvironment;
20 use PVE::Exception qw(raise_param_exc);
21 use PVE::QemuServer;
22 use PVE::QemuServer::ImportDisk;
23 use PVE::QemuServer::OVF;
24 use PVE::QemuServer::Agent qw(agent_available);
25 use PVE::API2::Qemu;
26 use PVE::API2::Qemu::Agent;
27 use JSON;
28 use PVE::JSONSchema qw(get_standard_option);
29 use Term::ReadLine;
30
31 use PVE::CLIHandler;
32
33 use base qw(PVE::CLIHandler);
34
35 my $upid_exit = sub {
36 my $upid = shift;
37 my $status = PVE::Tools::upid_read_status($upid);
38 exit($status eq 'OK' ? 0 : -1);
39 };
40
41 my $nodename = PVE::INotify::nodename();
42
43 sub setup_environment {
44 PVE::RPCEnvironment->setup_default_cli_env();
45 }
46
47 sub run_vnc_proxy {
48 my ($path) = @_;
49
50 my $c;
51 while ( ++$c < 10 && !-e $path ) { sleep(1); }
52
53 my $s = IO::Socket::UNIX->new(Peer => $path, Timeout => 120);
54
55 die "unable to connect to socket '$path' - $!" if !$s;
56
57 my $select = new IO::Select;
58
59 $select->add(\*STDIN);
60 $select->add($s);
61
62 my $timeout = 60*15; # 15 minutes
63
64 my @handles;
65 while ($select->count &&
66 scalar(@handles = $select->can_read ($timeout))) {
67 foreach my $h (@handles) {
68 my $buf;
69 my $n = $h->sysread($buf, 4096);
70
71 if ($h == \*STDIN) {
72 if ($n) {
73 syswrite($s, $buf);
74 } else {
75 exit(0);
76 }
77 } elsif ($h == $s) {
78 if ($n) {
79 syswrite(\*STDOUT, $buf);
80 } else {
81 exit(0);
82 }
83 }
84 }
85 }
86 exit(0);
87 }
88
89 sub print_recursive_hash {
90 my ($prefix, $hash, $key) = @_;
91
92 if (ref($hash) eq 'HASH') {
93 if (defined($key)) {
94 print "$prefix$key:\n";
95 }
96 foreach my $itemkey (keys %$hash) {
97 print_recursive_hash("\t$prefix", $hash->{$itemkey}, $itemkey);
98 }
99 } elsif (ref($hash) eq 'ARRAY') {
100 if (defined($key)) {
101 print "$prefix$key:\n";
102 }
103 foreach my $item (@$hash) {
104 print_recursive_hash("\t$prefix", $item);
105 }
106 } elsif (!ref($hash) && defined($hash)) {
107 if (defined($key)) {
108 print "$prefix$key: $hash\n";
109 } else {
110 print "$prefix$hash\n";
111 }
112 }
113 }
114
115 __PACKAGE__->register_method ({
116 name => 'showcmd',
117 path => 'showcmd',
118 method => 'GET',
119 description => "Show command line which is used to start the VM (debug info).",
120 parameters => {
121 additionalProperties => 0,
122 properties => {
123 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
124 pretty => {
125 description => "Puts each option on a new line to enhance human readability",
126 type => 'boolean',
127 optional => 1,
128 default => 0,
129 }
130 },
131 },
132 returns => { type => 'null'},
133 code => sub {
134 my ($param) = @_;
135
136 my $storecfg = PVE::Storage::config();
137 my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $param->{vmid});
138
139 $cmdline =~ s/ -/ \\\n -/g if $param->{pretty};
140
141 print "$cmdline\n";
142
143 return undef;
144 }});
145
146 __PACKAGE__->register_method ({
147 name => 'status',
148 path => 'status',
149 method => 'GET',
150 description => "Show VM status.",
151 parameters => {
152 additionalProperties => 0,
153 properties => {
154 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
155 verbose => {
156 description => "Verbose output format",
157 type => 'boolean',
158 optional => 1,
159 }
160 },
161 },
162 returns => { type => 'null'},
163 code => sub {
164 my ($param) = @_;
165
166 # test if VM exists
167 my $conf = PVE::QemuConfig->load_config ($param->{vmid});
168
169 my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1);
170 my $stat = $vmstatus->{$param->{vmid}};
171 if ($param->{verbose}) {
172 foreach my $k (sort (keys %$stat)) {
173 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
174 my $v = $stat->{$k};
175 print_recursive_hash("", $v, $k);
176 }
177 } else {
178 my $status = $stat->{qmpstatus} || 'unknown';
179 print "status: $status\n";
180 }
181
182 return undef;
183 }});
184
185 __PACKAGE__->register_method ({
186 name => 'vncproxy',
187 path => 'vncproxy',
188 method => 'PUT',
189 description => "Proxy VM VNC traffic to stdin/stdout",
190 parameters => {
191 additionalProperties => 0,
192 properties => {
193 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }),
194 },
195 },
196 returns => { type => 'null'},
197 code => sub {
198 my ($param) = @_;
199
200 my $vmid = $param->{vmid};
201 my $vnc_socket = PVE::QemuServer::vnc_socket($vmid);
202
203 if (my $ticket = $ENV{LC_PVE_TICKET}) { # NOTE: ssh on debian only pass LC_* variables
204 PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,password");
205 PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'vnc', password => $ticket);
206 PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'vnc', time => "+30");
207 } else {
208 PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,x509,password");
209 }
210
211 run_vnc_proxy($vnc_socket);
212
213 return undef;
214 }});
215
216 __PACKAGE__->register_method ({
217 name => 'unlock',
218 path => 'unlock',
219 method => 'PUT',
220 description => "Unlock the VM.",
221 parameters => {
222 additionalProperties => 0,
223 properties => {
224 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
225 },
226 },
227 returns => { type => 'null'},
228 code => sub {
229 my ($param) = @_;
230
231 my $vmid = $param->{vmid};
232
233 PVE::QemuConfig->lock_config ($vmid, sub {
234 my $conf = PVE::QemuConfig->load_config($vmid);
235 delete $conf->{lock};
236 delete $conf->{pending}->{lock} if $conf->{pending}; # just to be sure
237 PVE::QemuConfig->write_config($vmid, $conf);
238 });
239
240 return undef;
241 }});
242
243 __PACKAGE__->register_method ({
244 name => 'nbdstop',
245 path => 'nbdstop',
246 method => 'PUT',
247 description => "Stop embedded nbd server.",
248 parameters => {
249 additionalProperties => 0,
250 properties => {
251 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
252 },
253 },
254 returns => { type => 'null'},
255 code => sub {
256 my ($param) = @_;
257
258 my $vmid = $param->{vmid};
259
260 PVE::QemuServer::nbd_stop($vmid);
261
262 return undef;
263 }});
264
265 __PACKAGE__->register_method ({
266 name => 'mtunnel',
267 path => 'mtunnel',
268 method => 'POST',
269 description => "Used by qmigrate - do not use manually.",
270 parameters => {
271 additionalProperties => 0,
272 properties => {},
273 },
274 returns => { type => 'null'},
275 code => sub {
276 my ($param) = @_;
277
278 if (!PVE::Cluster::check_cfs_quorum(1)) {
279 print "no quorum\n";
280 return undef;
281 }
282
283 my $tunnel_write = sub {
284 my $text = shift;
285 chomp $text;
286 print "$text\n";
287 *STDOUT->flush();
288 };
289
290 $tunnel_write->("tunnel online");
291 $tunnel_write->("ver 1");
292
293 while (my $line = <STDIN>) {
294 chomp $line;
295 if ($line =~ /^quit$/) {
296 $tunnel_write->("OK");
297 last;
298 } elsif ($line =~ /^resume (\d+)$/) {
299 my $vmid = $1;
300 if (PVE::QemuServer::check_running($vmid, 1)) {
301 eval { PVE::QemuServer::vm_resume($vmid, 1, 1); };
302 if ($@) {
303 $tunnel_write->("ERR: resume failed - $@");
304 } else {
305 $tunnel_write->("OK");
306 }
307 } else {
308 $tunnel_write->("ERR: resume failed - VM $vmid not running");
309 }
310 }
311 }
312
313 return undef;
314 }});
315
316 __PACKAGE__->register_method ({
317 name => 'wait',
318 path => 'wait',
319 method => 'GET',
320 description => "Wait until the VM is stopped.",
321 parameters => {
322 additionalProperties => 0,
323 properties => {
324 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }),
325 timeout => {
326 description => "Timeout in seconds. Default is to wait forever.",
327 type => 'integer',
328 minimum => 1,
329 optional => 1,
330 }
331 },
332 },
333 returns => { type => 'null'},
334 code => sub {
335 my ($param) = @_;
336
337 my $vmid = $param->{vmid};
338 my $timeout = $param->{timeout};
339
340 my $pid = PVE::QemuServer::check_running ($vmid);
341 return if !$pid;
342
343 print "waiting until VM $vmid stopps (PID $pid)\n";
344
345 my $count = 0;
346 while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) {
347 $count++;
348 sleep 1;
349 }
350
351 die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid);
352
353 return undef;
354 }});
355
356 __PACKAGE__->register_method ({
357 name => 'monitor',
358 path => 'monitor',
359 method => 'POST',
360 description => "Enter Qemu Monitor interface.",
361 parameters => {
362 additionalProperties => 0,
363 properties => {
364 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }),
365 },
366 },
367 returns => { type => 'null'},
368 code => sub {
369 my ($param) = @_;
370
371 my $vmid = $param->{vmid};
372
373 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
374
375 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
376
377 my $term = new Term::ReadLine ('qm');
378
379 my $input;
380 while (defined ($input = $term->readline('qm> '))) {
381 chomp $input;
382
383 next if $input =~ m/^\s*$/;
384
385 last if $input =~ m/^\s*q(uit)?\s*$/;
386
387 eval {
388 print PVE::QemuServer::vm_human_monitor_command ($vmid, $input);
389 };
390 print "ERROR: $@" if $@;
391 }
392
393 return undef;
394
395 }});
396
397 __PACKAGE__->register_method ({
398 name => 'rescan',
399 path => 'rescan',
400 method => 'POST',
401 description => "Rescan all storages and update disk sizes and unused disk images.",
402 parameters => {
403 additionalProperties => 0,
404 properties => {
405 vmid => get_standard_option('pve-vmid', {
406 optional => 1,
407 completion => \&PVE::QemuServer::complete_vmid,
408 }),
409 dryrun => {
410 type => 'boolean',
411 optional => 1,
412 default => 0,
413 description => 'Do not actually write changes out to conifg.',
414 },
415 },
416 },
417 returns => { type => 'null'},
418 code => sub {
419 my ($param) = @_;
420
421 my $dryrun = $param->{dryrun};
422
423 print "NOTE: running in dry-run mode, won't write changes out!\n" if $dryrun;
424
425 PVE::QemuServer::rescan($param->{vmid}, 0, $dryrun);
426
427 return undef;
428 }});
429
430 __PACKAGE__->register_method ({
431 name => 'importdisk',
432 path => 'importdisk',
433 method => 'POST',
434 description => "Import an external disk image as an unused disk in a VM. The
435 image format has to be supported by qemu-img(1).",
436 parameters => {
437 additionalProperties => 0,
438 properties => {
439 vmid => get_standard_option('pve-vmid', {completion => \&PVE::QemuServer::complete_vmid}),
440 source => {
441 description => 'Path to the disk image to import',
442 type => 'string',
443 optional => 0,
444 },
445 storage => get_standard_option('pve-storage-id', {
446 description => 'Target storage ID',
447 completion => \&PVE::QemuServer::complete_storage,
448 optional => 0,
449 }),
450 format => {
451 type => 'string',
452 description => 'Target format',
453 enum => [ 'raw', 'qcow2', 'vmdk' ],
454 optional => 1,
455 },
456 },
457 },
458 returns => { type => 'null'},
459 code => sub {
460 my ($param) = @_;
461
462 my $vmid = extract_param($param, 'vmid');
463 my $source = extract_param($param, 'source');
464 my $storeid = extract_param($param, 'storage');
465 my $format = extract_param($param, 'format');
466
467 my $vm_conf = PVE::QemuConfig->load_config($vmid);
468 PVE::QemuConfig->check_lock($vm_conf);
469 die "$source: non-existent or non-regular file\n" if (! -f $source);
470
471 my $storecfg = PVE::Storage::config();
472 PVE::Storage::storage_check_enabled($storecfg, $storeid);
473
474 my $target_storage_config =
475 PVE::Storage::storage_config($storecfg, $storeid);
476 die "storage $storeid does not support vm images\n"
477 if !$target_storage_config->{content}->{images};
478
479 PVE::QemuServer::ImportDisk::do_import($source, $vmid, $storeid, { format => $format });
480
481 return undef;
482 }});
483
484 __PACKAGE__->register_method ({
485 name => 'terminal',
486 path => 'terminal',
487 method => 'POST',
488 description => "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
489 parameters => {
490 additionalProperties => 0,
491 properties => {
492 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }),
493 iface => {
494 description => "Select the serial device. By default we simply use the first suitable device.",
495 type => 'string',
496 optional => 1,
497 enum => [qw(serial0 serial1 serial2 serial3)],
498 },
499 escape => {
500 description => "Escape character.",
501 type => 'string',
502 optional => 1,
503 default => '^O',
504 },
505 },
506 },
507 returns => { type => 'null'},
508 code => sub {
509 my ($param) = @_;
510
511 my $vmid = $param->{vmid};
512
513 my $escape = $param->{escape} // '^O';
514 if ($escape =~ /^\^([\x40-\x7a])$/) {
515 $escape = ord($1) & 0x1F;
516 } elsif ($escape =~ /^0x[0-9a-f]+$/i) {
517 $escape = hex($escape);
518 } elsif ($escape =~ /^[0-9]+$/) {
519 $escape = int($escape);
520 } else {
521 die "invalid escape character definition: $escape\n";
522 }
523 my $escapemsg = '';
524 if ($escape) {
525 $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40);
526 $escape = sprintf(',escape=0x%x', $escape);
527 } else {
528 $escape = '';
529 }
530
531 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
532
533 my $iface = $param->{iface};
534
535 if ($iface) {
536 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
537 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
538 } else {
539 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
540 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
541 $iface = $opt;
542 last;
543 }
544 }
545 die "unable to find a serial interface\n" if !$iface;
546 }
547
548 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
549
550 my $socket = "/var/run/qemu-server/${vmid}.$iface";
551
552 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape";
553
554 print "starting serial terminal on interface ${iface}${escapemsg}\n";
555
556 system($cmd);
557
558 return undef;
559 }});
560
561 __PACKAGE__->register_method ({
562 name => 'importovf',
563 path => 'importovf',
564 description => "Create a new VM using parameters read from an OVF manifest",
565 parameters => {
566 additionalProperties => 0,
567 properties => {
568 vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
569 manifest => {
570 type => 'string',
571 description => 'path to the ovf file',
572 },
573 storage => get_standard_option('pve-storage-id', {
574 description => 'Target storage ID',
575 completion => \&PVE::QemuServer::complete_storage,
576 optional => 0,
577 }),
578 format => {
579 type => 'string',
580 description => 'Target format',
581 enum => [ 'raw', 'qcow2', 'vmdk' ],
582 optional => 1,
583 },
584 dryrun => {
585 type => 'boolean',
586 description => 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
587 optional => 1,
588 }
589 },
590 },
591 returns => { type => 'null' },
592 code => sub {
593 my ($param) = @_;
594
595 my $vmid = PVE::Tools::extract_param($param, 'vmid');
596 my $ovf_file = PVE::Tools::extract_param($param, 'manifest');
597 my $storeid = PVE::Tools::extract_param($param, 'storage');
598 my $format = PVE::Tools::extract_param($param, 'format');
599 my $dryrun = PVE::Tools::extract_param($param, 'dryrun');
600
601 die "$ovf_file: non-existent or non-regular file\n" if (! -f $ovf_file);
602 my $storecfg = PVE::Storage::config();
603 PVE::Storage::storage_check_enabled($storecfg, $storeid);
604
605 my $parsed = PVE::QemuServer::OVF::parse_ovf($ovf_file);
606
607 if ($dryrun) {
608 print to_json($parsed, { pretty => 1, canonical => 1});
609 return;
610 }
611
612 $param->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name});
613 $param->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory});
614 $param->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores});
615
616 my $importfn = sub {
617
618 PVE::Cluster::check_vmid_unused($vmid);
619
620 my $conf = $param;
621
622 eval {
623 # order matters, as do_import() will load_config() internally
624 $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
625 PVE::QemuConfig->write_config($vmid, $conf);
626
627 foreach my $disk (@{ $parsed->{disks} }) {
628 my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address});
629 PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid,
630 { drive_name => $drive, format => $format });
631 }
632
633 # reload after disks entries have been created
634 $conf = PVE::QemuConfig->load_config($vmid);
635 PVE::QemuConfig->check_lock($conf);
636 my $firstdisk = PVE::QemuServer::resolve_first_disk($conf);
637 $conf->{bootdisk} = $firstdisk if $firstdisk;
638 PVE::QemuConfig->write_config($vmid, $conf);
639 };
640
641 my $err = $@;
642 if ($err) {
643 my $skiplock = 1;
644 eval { PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); };
645 die "import failed - $err";
646 }
647 };
648
649 my $wait_for_lock = 1;
650 PVE::QemuConfig->lock_config_full($vmid, $wait_for_lock, $importfn);
651
652 return undef;
653
654 }
655 });
656
657 __PACKAGE__->register_method({
658 name => 'exec',
659 path => 'exec',
660 method => 'POST',
661 protected => 1,
662 description => "Executes the given command via the guest agent",
663 parameters => {
664 additionalProperties => 0,
665 properties => {
666 node => get_standard_option('pve-node'),
667 vmid => get_standard_option('pve-vmid', {
668 completion => \&PVE::QemuServer::complete_vmid_running }),
669 synchronous => {
670 type => 'boolean',
671 optional => 1,
672 default => 1,
673 description => "If set to off, returns the pid immediately instead of waiting for the commmand to finish or the timeout.",
674 },
675 'timeout' => {
676 type => 'integer',
677 description => "The maximum time to wait synchronously for the command to finish. If reached, the pid gets returned. Set to 0 to deactivate",
678 minimum => 0,
679 optional => 1,
680 default => 30,
681 },
682 'extra-args' => get_standard_option('extra-args'),
683 },
684 },
685 returns => {
686 type => 'object',
687 },
688 code => sub {
689 my ($param) = @_;
690
691 my $vmid = $param->{vmid};
692 my $sync = $param->{synchronous} // 1;
693 if (!$param->{'extra-args'} || !@{$param->{'extra-args'}}) {
694 raise_param_exc( { 'extra-args' => "No command given" });
695 }
696 if (defined($param->{timeout}) && !$sync) {
697 raise_param_exc({ synchronous => "needs to be set for 'timeout'"});
698 }
699
700 my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $param->{'extra-args'});
701
702 if ($sync) {
703 my $pid = $res->{pid};
704 my $timeout = $param->{timeout} // 30;
705 my $starttime = time();
706
707 while ($timeout == 0 || (time() - $starttime) < $timeout) {
708 my $out = PVE::QemuServer::Agent::qemu_exec_status($vmid, $pid);
709 if ($out->{exited}) {
710 $res = $out;
711 last;
712 }
713 sleep 1;
714 }
715
716 if (!$res->{exited}) {
717 warn "timeout reached, returning pid\n";
718 }
719 }
720
721 return { result => $res };
722 }});
723
724 my $print_agent_result = sub {
725 my ($data) = @_;
726
727 my $result = $data->{result} // $data;
728 return if !defined($result);
729
730 my $class = ref($result);
731
732 if (!$class) {
733 chomp $result;
734 return if $result =~ m/^\s*$/;
735 print "$result\n";
736 return;
737 }
738
739 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
740 return;
741 }
742
743 print to_json($result, { pretty => 1, canonical => 1});
744 };
745
746 sub param_mapping {
747 my ($name) = @_;
748
749 my $ssh_key_map = ['sshkeys', sub {
750 return URI::Escape::uri_escape(PVE::Tools::file_get_contents($_[0]));
751 }];
752 my $cipassword_map = PVE::CLIHandler::get_standard_mapping('pve-password', { name => 'cipassword' });
753 my $password_map = PVE::CLIHandler::get_standard_mapping('pve-password');
754 my $mapping = {
755 'update_vm' => [$ssh_key_map, $cipassword_map],
756 'create_vm' => [$ssh_key_map, $cipassword_map],
757 'set-user-password' => [$password_map],
758 };
759
760 return $mapping->{$name};
761 }
762
763 our $cmddef = {
764 list => [ "PVE::API2::Qemu", 'vmlist', [],
765 { node => $nodename }, sub {
766 my $vmlist = shift;
767
768 exit 0 if (!scalar(@$vmlist));
769
770 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
771 qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID);
772
773 foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) {
774 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name},
775 $rec->{qmpstatus} || $rec->{status},
776 ($rec->{maxmem} || 0)/(1024*1024),
777 ($rec->{maxdisk} || 0)/(1024*1024*1024),
778 $rec->{pid}||0;
779 }
780
781
782 } ],
783
784 create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename }, $upid_exit ],
785
786 destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ],
787
788 clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
789
790 migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ],
791
792 set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ],
793
794 resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
795
796 move_disk => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node => $nodename }, $upid_exit ],
797
798 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node => $nodename } ],
799
800 config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
801 { node => $nodename }, sub {
802 my $config = shift;
803 foreach my $k (sort (keys %$config)) {
804 next if $k eq 'digest';
805 my $v = $config->{$k};
806 if ($k eq 'description') {
807 $v = PVE::Tools::encode_text($v);
808 }
809 print "$k: $v\n";
810 }
811 }],
812
813 pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
814 { node => $nodename }, sub {
815 my $data = shift;
816 foreach my $item (sort { $a->{key} cmp $b->{key}} @$data) {
817 my $k = $item->{key};
818 next if $k eq 'digest';
819 my $v = $item->{value};
820 my $p = $item->{pending};
821 if ($k eq 'description') {
822 $v = PVE::Tools::encode_text($v) if defined($v);
823 $p = PVE::Tools::encode_text($p) if defined($p);
824 }
825 if (defined($v)) {
826 if ($item->{delete}) {
827 print "del $k: $v\n";
828 } elsif (defined($p)) {
829 print "cur $k: $v\n";
830 print "new $k: $p\n";
831 } else {
832 print "cur $k: $v\n";
833 }
834 } elsif (defined($p)) {
835 print "new $k: $p\n";
836 }
837 }
838 }],
839
840 showcmd => [ __PACKAGE__, 'showcmd', ['vmid']],
841
842 status => [ __PACKAGE__, 'status', ['vmid']],
843
844 snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
845
846 delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
847
848 listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node => $nodename },
849 sub {
850 my $res = shift;
851 foreach my $e (@$res) {
852 my $headline = $e->{description} || 'no-description';
853 $headline =~ s/\n.*//sg;
854 my $parent = $e->{parent} // 'no-parent';
855 printf("%-20s %-20s %s\n", $e->{name}, $parent, $headline);
856 }
857 }],
858
859 rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ],
860
861 template => [ "PVE::API2::Qemu", 'template', ['vmid'], { node => $nodename }],
862
863 start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } , $upid_exit ],
864
865 stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit ],
866
867 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename }, $upid_exit ],
868
869 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit ],
870
871 suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit ],
872
873 resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit ],
874
875 sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ],
876
877 vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']],
878
879 wait => [ __PACKAGE__, 'wait', ['vmid']],
880
881 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
882
883 rescan => [ __PACKAGE__, 'rescan', []],
884
885 monitor => [ __PACKAGE__, 'monitor', ['vmid']],
886
887 agent => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'],
888 { node => $nodename }, $print_agent_result ],
889
890 guest => {
891 passwd => [ "PVE::API2::Qemu::Agent", 'set-user-password', [ 'vmid', 'username' ], { node => $nodename }],
892 exec => [ __PACKAGE__, 'exec', [ 'vmid', 'extra-args' ], { node => $nodename }, $print_agent_result],
893 'exec-status' => [ "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { node => $nodename }, $print_agent_result],
894 },
895
896 mtunnel => [ __PACKAGE__, 'mtunnel', []],
897
898 nbdstop => [ __PACKAGE__, 'nbdstop', ['vmid']],
899
900 terminal => [ __PACKAGE__, 'terminal', ['vmid']],
901
902 importdisk => [ __PACKAGE__, 'importdisk', ['vmid', 'source', 'storage']],
903
904 importovf => [ __PACKAGE__, 'importovf', ['vmid', 'manifest', 'storage']],
905
906 };
907
908 1;