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