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