]> git.proxmox.com Git - qemu-server.git/blob - PVE/CLI/qm.pm
d/copyright: update years
[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 file_get_contents);
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 (or missing permission)!\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 # check_running and vm_resume with nocheck, since local node
430 # might not have processed config move/rename yet
431 if (PVE::QemuServer::check_running($vmid, 1)) {
432 eval { PVE::QemuServer::vm_resume($vmid, 1, 1); };
433 if ($@) {
434 $tunnel_write->("ERR: resume failed - $@");
435 } else {
436 $tunnel_write->("OK");
437 }
438 } else {
439 $tunnel_write->("ERR: resume failed - VM $vmid not running");
440 }
441 }
442 }
443
444 return;
445 }});
446
447 __PACKAGE__->register_method ({
448 name => 'wait',
449 path => 'wait',
450 method => 'GET',
451 description => "Wait until the VM is stopped.",
452 parameters => {
453 additionalProperties => 0,
454 properties => {
455 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }),
456 timeout => {
457 description => "Timeout in seconds. Default is to wait forever.",
458 type => 'integer',
459 minimum => 1,
460 optional => 1,
461 }
462 },
463 },
464 returns => { type => 'null'},
465 code => sub {
466 my ($param) = @_;
467
468 my $vmid = $param->{vmid};
469 my $timeout = $param->{timeout};
470
471 my $pid = PVE::QemuServer::check_running ($vmid);
472 return if !$pid;
473
474 print "waiting until VM $vmid stopps (PID $pid)\n";
475
476 my $count = 0;
477 while ((!$timeout || ($count < $timeout)) && PVE::QemuServer::check_running ($vmid)) {
478 $count++;
479 sleep 1;
480 }
481
482 die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid);
483
484 return;
485 }});
486
487 __PACKAGE__->register_method ({
488 name => 'monitor',
489 path => 'monitor',
490 method => 'POST',
491 description => "Enter QEMU Monitor interface.",
492 parameters => {
493 additionalProperties => 0,
494 properties => {
495 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }),
496 },
497 },
498 returns => { type => 'null'},
499 code => sub {
500 my ($param) = @_;
501
502 my $vmid = $param->{vmid};
503
504 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
505
506 print "Entering QEMU Monitor for VM $vmid - type 'help' for help\n";
507
508 my $term = Term::ReadLine->new('qm');
509
510 while (defined(my $input = $term->readline('qm> '))) {
511 chomp $input;
512 next if $input =~ m/^\s*$/;
513 last if $input =~ m/^\s*q(uit)?\s*$/;
514
515 eval { print PVE::QemuServer::Monitor::hmp_cmd($vmid, $input) };
516 print "ERROR: $@" if $@;
517 }
518
519 return;
520
521 }});
522
523 __PACKAGE__->register_method ({
524 name => 'rescan',
525 path => 'rescan',
526 method => 'POST',
527 description => "Rescan all storages and update disk sizes and unused disk images.",
528 parameters => {
529 additionalProperties => 0,
530 properties => {
531 vmid => get_standard_option('pve-vmid', {
532 optional => 1,
533 completion => \&PVE::QemuServer::complete_vmid,
534 }),
535 dryrun => {
536 type => 'boolean',
537 optional => 1,
538 default => 0,
539 description => 'Do not actually write changes out to VM config(s).',
540 },
541 },
542 },
543 returns => { type => 'null'},
544 code => sub {
545 my ($param) = @_;
546
547 my $dryrun = $param->{dryrun};
548
549 print "NOTE: running in dry-run mode, won't write changes out!\n" if $dryrun;
550
551 PVE::QemuServer::rescan($param->{vmid}, 0, $dryrun);
552
553 return;
554 }});
555
556 __PACKAGE__->register_method ({
557 name => 'importdisk',
558 path => 'importdisk',
559 method => 'POST',
560 description => "Import an external disk image as an unused disk in a VM. The
561 image format has to be supported by qemu-img(1).",
562 parameters => {
563 additionalProperties => 0,
564 properties => {
565 vmid => get_standard_option('pve-vmid', {completion => \&PVE::QemuServer::complete_vmid}),
566 source => {
567 description => 'Path to the disk image to import',
568 type => 'string',
569 optional => 0,
570 },
571 storage => get_standard_option('pve-storage-id', {
572 description => 'Target storage ID',
573 completion => \&PVE::QemuServer::complete_storage,
574 optional => 0,
575 }),
576 format => {
577 type => 'string',
578 description => 'Target format',
579 enum => [ 'raw', 'qcow2', 'vmdk' ],
580 optional => 1,
581 },
582 },
583 },
584 returns => { type => 'null'},
585 code => sub {
586 my ($param) = @_;
587
588 my $vmid = extract_param($param, 'vmid');
589 my $source = extract_param($param, 'source');
590 my $storeid = extract_param($param, 'storage');
591 my $format = extract_param($param, 'format');
592
593 my $vm_conf = PVE::QemuConfig->load_config($vmid);
594 PVE::QemuConfig->check_lock($vm_conf);
595 die "$source: non-existent or non-regular file\n" if (! -f $source);
596
597 my $storecfg = PVE::Storage::config();
598 PVE::Storage::storage_check_enabled($storecfg, $storeid);
599
600 my $target_storage_config = PVE::Storage::storage_config($storecfg, $storeid);
601 die "storage $storeid does not support vm images\n"
602 if !$target_storage_config->{content}->{images};
603
604 print "importing disk '$source' to VM $vmid ...\n";
605 my ($drive_id, $volid) = PVE::QemuServer::ImportDisk::do_import($source, $vmid, $storeid, { format => $format });
606 print "Successfully imported disk as '$drive_id:$volid'\n";
607
608 return;
609 }});
610
611 __PACKAGE__->register_method ({
612 name => 'terminal',
613 path => 'terminal',
614 method => 'POST',
615 description => "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
616 parameters => {
617 additionalProperties => 0,
618 properties => {
619 vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid_running }),
620 iface => {
621 description => "Select the serial device. By default we simply use the first suitable device.",
622 type => 'string',
623 optional => 1,
624 enum => [qw(serial0 serial1 serial2 serial3)],
625 },
626 escape => {
627 description => "Escape character.",
628 type => 'string',
629 optional => 1,
630 default => '^O',
631 },
632 },
633 },
634 returns => { type => 'null'},
635 code => sub {
636 my ($param) = @_;
637
638 my $vmid = $param->{vmid};
639
640 my $escape = $param->{escape} // '^O';
641 if ($escape =~ /^\^([\x40-\x7a])$/) {
642 $escape = ord($1) & 0x1F;
643 } elsif ($escape =~ /^0x[0-9a-f]+$/i) {
644 $escape = hex($escape);
645 } elsif ($escape =~ /^[0-9]+$/) {
646 $escape = int($escape);
647 } else {
648 die "invalid escape character definition: $escape\n";
649 }
650 my $escapemsg = '';
651 if ($escape) {
652 $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40);
653 $escape = sprintf(',escape=0x%x', $escape);
654 } else {
655 $escape = '';
656 }
657
658 my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists
659
660 my $iface = $param->{iface};
661
662 if ($iface) {
663 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
664 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
665 } else {
666 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
667 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
668 $iface = $opt;
669 last;
670 }
671 }
672 die "unable to find a serial interface\n" if !$iface;
673 }
674
675 die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid);
676
677 my $socket = "/var/run/qemu-server/${vmid}.$iface";
678
679 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape";
680
681 print "starting serial terminal on interface ${iface}${escapemsg}\n";
682
683 system($cmd);
684
685 return;
686 }});
687
688 __PACKAGE__->register_method ({
689 name => 'importovf',
690 path => 'importovf',
691 description => "Create a new VM using parameters read from an OVF manifest",
692 parameters => {
693 additionalProperties => 0,
694 properties => {
695 vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
696 manifest => {
697 type => 'string',
698 description => 'path to the ovf file',
699 },
700 storage => get_standard_option('pve-storage-id', {
701 description => 'Target storage ID',
702 completion => \&PVE::QemuServer::complete_storage,
703 optional => 0,
704 }),
705 format => {
706 type => 'string',
707 description => 'Target format',
708 enum => [ 'raw', 'qcow2', 'vmdk' ],
709 optional => 1,
710 },
711 dryrun => {
712 type => 'boolean',
713 description => 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
714 optional => 1,
715 }
716 },
717 },
718 returns => { type => 'null' },
719 code => sub {
720 my ($param) = @_;
721
722 my $vmid = PVE::Tools::extract_param($param, 'vmid');
723 my $ovf_file = PVE::Tools::extract_param($param, 'manifest');
724 my $storeid = PVE::Tools::extract_param($param, 'storage');
725 my $format = PVE::Tools::extract_param($param, 'format');
726 my $dryrun = PVE::Tools::extract_param($param, 'dryrun');
727
728 die "$ovf_file: non-existent or non-regular file\n" if (! -f $ovf_file);
729 my $storecfg = PVE::Storage::config();
730 PVE::Storage::storage_check_enabled($storecfg, $storeid);
731
732 my $parsed = PVE::QemuServer::OVF::parse_ovf($ovf_file);
733
734 if ($dryrun) {
735 print to_json($parsed, { pretty => 1, canonical => 1});
736 return;
737 }
738
739 eval { PVE::QemuConfig->create_and_lock_config($vmid) };
740 die "Reserving empty config for OVF import to VM $vmid failed: $@" if $@;
741
742 my $conf = PVE::QemuConfig->load_config($vmid);
743 die "Internal error: Expected 'create' lock in config of VM $vmid!"
744 if !PVE::QemuConfig->has_lock($conf, "create");
745
746 $conf->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name});
747 $conf->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory});
748 $conf->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores});
749
750 my $imported_disks = [];
751 eval {
752 # order matters, as do_import() will load_config() internally
753 $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
754 $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid();
755 PVE::QemuConfig->write_config($vmid, $conf);
756
757 foreach my $disk (@{ $parsed->{disks} }) {
758 my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address});
759 my ($name, $volid) = PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid, {
760 drive_name => $drive,
761 format => $format,
762 skiplock => 1,
763 });
764 # for cleanup on (later) error
765 push @$imported_disks, $volid;
766 }
767
768 # reload after disks entries have been created
769 $conf = PVE::QemuConfig->load_config($vmid);
770 my $devs = PVE::QemuServer::get_default_bootdevices($conf);
771 $conf->{boot} = PVE::QemuServer::print_bootorder($devs);
772 PVE::QemuConfig->write_config($vmid, $conf);
773 };
774
775 if (my $err = $@) {
776 my $skiplock = 1;
777 warn "error during import, cleaning up created resources...\n";
778 for my $volid (@$imported_disks) {
779 eval { PVE::Storage::vdisk_free($storecfg, $volid) };
780 warn "cleanup of $volid failed: $@\n" if $@;
781 }
782 eval { PVE::QemuServer::destroy_vm($storecfg, $vmid, $skiplock) };
783 warn "Could not destroy VM $vmid: $@" if "$@";
784 die "import failed - $err";
785 }
786
787 PVE::QemuConfig->remove_lock($vmid, "create");
788
789 return;
790
791 }
792 });
793
794 __PACKAGE__->register_method({
795 name => 'exec',
796 path => 'exec',
797 method => 'POST',
798 protected => 1,
799 description => "Executes the given command via the guest agent",
800 parameters => {
801 additionalProperties => 0,
802 properties => {
803 node => get_standard_option('pve-node'),
804 vmid => get_standard_option('pve-vmid', {
805 completion => \&PVE::QemuServer::complete_vmid_running }),
806 synchronous => {
807 type => 'boolean',
808 optional => 1,
809 default => 1,
810 description => "If set to off, returns the pid immediately instead of waiting for the commmand to finish or the timeout.",
811 },
812 'timeout' => {
813 type => 'integer',
814 description => "The maximum time to wait synchronously for the command to finish. If reached, the pid gets returned. Set to 0 to deactivate",
815 minimum => 0,
816 optional => 1,
817 default => 30,
818 },
819 'pass-stdin' => {
820 type => 'boolean',
821 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.",
822 optional => 1,
823 default => 0,
824 },
825 'extra-args' => get_standard_option('extra-args'),
826 },
827 },
828 returns => {
829 type => 'object',
830 },
831 code => sub {
832 my ($param) = @_;
833
834 my $vmid = $param->{vmid};
835 my $sync = $param->{synchronous} // 1;
836 my $pass_stdin = $param->{'pass-stdin'};
837 if (defined($param->{timeout}) && !$sync) {
838 raise_param_exc({ synchronous => "needs to be set for 'timeout'"});
839 }
840
841 my $input_data = undef;
842 if ($pass_stdin) {
843 $input_data = '';
844 while (my $line = <STDIN>) {
845 $input_data .= $line;
846 if (length($input_data) > 1024*1024) {
847 # not sure how QEMU handles large amounts of data being
848 # passed into the QMP socket, so limit to be safe
849 die "'input-data' (STDIN) is limited to 1 MiB, aborting\n";
850 }
851 }
852 }
853
854 my $args = $param->{'extra-args'};
855 $args = undef if !$args || !@$args;
856
857 my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $input_data, $args);
858
859 if ($sync) {
860 my $pid = $res->{pid};
861 my $timeout = $param->{timeout} // 30;
862 my $starttime = time();
863
864 while ($timeout == 0 || (time() - $starttime) < $timeout) {
865 my $out = PVE::QemuServer::Agent::qemu_exec_status($vmid, $pid);
866 if ($out->{exited}) {
867 $res = $out;
868 last;
869 }
870 sleep 1;
871 }
872
873 if (!$res->{exited}) {
874 warn "timeout reached, returning pid\n";
875 }
876 }
877
878 return { result => $res };
879 }});
880
881 __PACKAGE__->register_method({
882 name => 'cleanup',
883 path => 'cleanup',
884 method => 'POST',
885 protected => 1,
886 description => "Cleans up resources like tap devices, vgpus, etc. Called after a vm shuts down, crashes, etc.",
887 parameters => {
888 additionalProperties => 0,
889 properties => {
890 node => get_standard_option('pve-node'),
891 vmid => get_standard_option('pve-vmid', {
892 completion => \&PVE::QemuServer::complete_vmid_running }),
893 'clean-shutdown' => {
894 type => 'boolean',
895 description => "Indicates if qemu shutdown cleanly.",
896 },
897 'guest-requested' => {
898 type => 'boolean',
899 description => "Indicates if the shutdown was requested by the guest or via qmp.",
900 },
901 },
902 },
903 returns => { type => 'null', },
904 code => sub {
905 my ($param) = @_;
906
907 my $vmid = $param->{vmid};
908 my $clean = $param->{'clean-shutdown'};
909 my $guest = $param->{'guest-requested'};
910 my $restart = 0;
911
912 # return if we do not have the config anymore
913 return if !-f PVE::QemuConfig->config_file($vmid);
914
915 my $storecfg = PVE::Storage::config();
916 warn "Starting cleanup for $vmid\n";
917
918 # mdev cleanup can take a while, so wait up to 60 seconds
919 PVE::QemuConfig->lock_config_full($vmid, 60, sub {
920 my $conf = PVE::QemuConfig->load_config ($vmid);
921 my $pid = PVE::QemuServer::check_running ($vmid);
922 die "vm still running\n" if $pid;
923
924 if (!$clean) {
925 # we have to cleanup the tap devices after a crash
926
927 foreach my $opt (keys %$conf) {
928 next if $opt !~ m/^net(\d+)$/;
929 my $interface = $1;
930 PVE::Network::tap_unplug("tap${vmid}i${interface}");
931 }
932 }
933
934 if (!$clean || $guest) {
935 # vm was shutdown from inside the guest or crashed, doing api cleanup
936 PVE::QemuServer::vm_stop_cleanup($storecfg, $vmid, $conf, 0, 0);
937 }
938 PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-stop');
939
940 $restart = eval { PVE::QemuServer::clear_reboot_request($vmid) };
941 warn $@ if $@;
942 });
943
944 warn "Finished cleanup for $vmid\n";
945
946 if ($restart) {
947 warn "Restarting VM $vmid\n";
948 PVE::API2::Qemu->vm_start({
949 vmid => $vmid,
950 %node,
951 });
952 }
953
954 return;
955 }});
956
957 __PACKAGE__->register_method({
958 name => 'vm_import',
959 path => 'vm-import',
960 description => "Import a foreign virtual guest from a supported import source, such as an ESXi storage.",
961 parameters => {
962 additionalProperties => 0,
963 properties => PVE::QemuServer::json_config_properties({
964 vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }),
965 'source' => {
966 type => 'string',
967 description => 'The import source volume id.',
968 },
969 storage => get_standard_option('pve-storage-id', {
970 description => "Default storage.",
971 completion => \&PVE::QemuServer::complete_storage,
972 }),
973 'live-import' => {
974 type => 'boolean',
975 optional => 1,
976 default => 0,
977 description => "Immediately start the VM and copy the data in the background.",
978 },
979 'dryrun' => {
980 type => 'boolean',
981 optional => 1,
982 default => 0,
983 description => "Show the create command and exit without doing anything.",
984 },
985 delete => {
986 type => 'string', format => 'pve-configid-list',
987 description => "A list of settings you want to delete.",
988 optional => 1,
989 },
990 format => {
991 type => 'string',
992 description => 'Target format',
993 enum => [ 'raw', 'qcow2', 'vmdk' ],
994 optional => 1,
995 },
996 }),
997 },
998 returns => { type => 'null' },
999 code => sub {
1000 my ($param) = @_;
1001
1002 my ($vmid, $source, $storage, $format, $live_import, $dryrun, $delete) =
1003 delete $param->@{qw(vmid source storage format live-import dryrun delete)};
1004
1005 if (defined($format)) {
1006 $format = ",format=$format";
1007 } else {
1008 $format = '';
1009 }
1010
1011 my $storecfg = PVE::Storage::config();
1012 my $metadata = PVE::Storage::get_import_metadata($storecfg, $source);
1013
1014 my $create_args = $metadata->{'create-args'};
1015 if (my $netdevs = $metadata->{net}) {
1016 for my $net (keys $netdevs->%*) {
1017 my $value = $netdevs->{$net};
1018 $create_args->{$net} = join(',', map { $_ . '=' . $value->{$_} } sort keys %$value);
1019 }
1020 }
1021 if (my $disks = $metadata->{disks}) {
1022 if (delete $disks->{efidisk0}) {
1023 $create_args->{efidisk0} = "$storage:1$format,efitype=4m";
1024 }
1025 for my $disk (keys $disks->%*) {
1026 my $value = $disks->{$disk}->{volid};
1027 $create_args->{$disk} = "$storage:0${format},import-from=$value";
1028 }
1029 }
1030
1031 $create_args->{'live-restore'} = 1 if $live_import;
1032
1033 $create_args->{$_} = $param->{$_} for keys $param->%*;
1034 delete $create_args->{$_} for PVE::Tools::split_list($delete);
1035
1036 if ($dryrun) {
1037 print("# dry-run – the resulting create command for the import would be:\n");
1038 print("qm create $vmid \\\n ");
1039 print(join(" \\\n ", map { "--$_ $create_args->{$_}" } sort keys $create_args->%*));
1040 print("\n");
1041 return;
1042 }
1043
1044 PVE::API2::Qemu->create_vm({
1045 %node,
1046 vmid => $vmid,
1047 %$create_args,
1048 });
1049 return;
1050 }
1051 });
1052
1053 my $print_agent_result = sub {
1054 my ($data) = @_;
1055
1056 my $result = $data->{result} // $data;
1057 return if !defined($result);
1058
1059 my $class = ref($result);
1060
1061 if (!$class) {
1062 chomp $result;
1063 return if $result =~ m/^\s*$/;
1064 print "$result\n";
1065 return;
1066 }
1067
1068 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
1069 return;
1070 }
1071
1072 print to_json($result, { pretty => 1, canonical => 1, utf8 => 1});
1073 };
1074
1075 sub param_mapping {
1076 my ($name) = @_;
1077
1078 my $ssh_key_map = ['sshkeys', sub {
1079 return URI::Escape::uri_escape(file_get_contents($_[0]));
1080 }];
1081 my $cipassword_map = PVE::CLIHandler::get_standard_mapping('pve-password', { name => 'cipassword' });
1082 my $password_map = PVE::CLIHandler::get_standard_mapping('pve-password');
1083 my $mapping = {
1084 'update_vm' => [$ssh_key_map, $cipassword_map],
1085 'create_vm' => [$ssh_key_map, $cipassword_map],
1086 'set-user-password' => [$password_map],
1087 };
1088
1089 return $mapping->{$name};
1090 }
1091
1092 our $cmddef = {
1093 list=> [ "PVE::API2::Qemu", 'vmlist', [], { %node }, sub {
1094 my $vmlist = shift;
1095 exit 0 if (!scalar(@$vmlist));
1096
1097 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
1098 qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID);
1099
1100 foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) {
1101 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name},
1102 $rec->{qmpstatus} || $rec->{status},
1103 ($rec->{maxmem} || 0)/(1024*1024),
1104 ($rec->{maxdisk} || 0)/(1024*1024*1024),
1105 $rec->{pid} || 0;
1106 }
1107 }],
1108
1109 create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { %node }, $upid_exit ],
1110 destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { %node }, $upid_exit ],
1111 clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { %node }, $upid_exit ],
1112
1113 migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { %node }, $upid_exit ],
1114 'remote-migrate' => [ __PACKAGE__, 'remote_migrate_vm', ['vmid', 'target-vmid', 'target-endpoint'], { %node }, $upid_exit ],
1115
1116 set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { %node } ],
1117
1118 config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], { %node }, sub {
1119 my $config = shift;
1120 foreach my $k (sort (keys %$config)) {
1121 next if $k eq 'digest';
1122 my $v = $config->{$k};
1123 if ($k eq 'description') {
1124 $v = PVE::Tools::encode_text($v);
1125 }
1126 print "$k: $v\n";
1127 }
1128 }],
1129
1130 pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'], { %node }, \&PVE::GuestHelpers::format_pending ],
1131 showcmd => [ __PACKAGE__, 'showcmd', ['vmid']],
1132
1133 status => [ __PACKAGE__, 'status', ['vmid']],
1134
1135 # FIXME: for 8.0 move to command group snapshot { create, list, destroy, rollback }
1136 snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { %node } , $upid_exit ],
1137 delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { %node } , $upid_exit ],
1138 listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { %node }, \&PVE::GuestHelpers::print_snapshot_tree],
1139 rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { %node } , $upid_exit ],
1140
1141 template => [ "PVE::API2::Qemu", 'template', ['vmid'], { %node }],
1142
1143 # FIXME: should be in a power command group?
1144 start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { %node } , $upid_exit ],
1145 stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { %node }, $upid_exit ],
1146 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { %node }, $upid_exit ],
1147 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { %node }, $upid_exit ],
1148 reboot => [ "PVE::API2::Qemu", 'vm_reboot', ['vmid'], { %node }, $upid_exit ],
1149 suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { %node }, $upid_exit ],
1150 resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { %node }, $upid_exit ],
1151
1152 sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { %node } ],
1153
1154 vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']],
1155
1156 wait => [ __PACKAGE__, 'wait', ['vmid']],
1157
1158 unlock => [ __PACKAGE__, 'unlock', ['vmid']],
1159
1160 # TODO: evluate dropping below aliases for 8.0, if no usage is left
1161 importdisk => { alias => 'disk import' },
1162 'move-disk' => { alias => 'disk move' },
1163 move_disk => { alias => 'disk move' },
1164 rescan => { alias => 'disk rescan' },
1165 resize => { alias => 'disk resize' },
1166 unlink => { alias => 'disk unlink' },
1167
1168 disk => {
1169 import => [ __PACKAGE__, 'importdisk', ['vmid', 'source', 'storage']],
1170 'move' => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { %node }, $upid_exit ],
1171 rescan => [ __PACKAGE__, 'rescan', []],
1172 resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { %node } ],
1173 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { %node } ],
1174 },
1175
1176 monitor => [ __PACKAGE__, 'monitor', ['vmid']],
1177
1178 agent => { alias => 'guest cmd' }, # FIXME: remove with PVE 8.0
1179
1180 guest => {
1181 cmd => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], { %node }, $print_agent_result ],
1182 passwd => [ "PVE::API2::Qemu::Agent", 'set-user-password', [ 'vmid', 'username' ], { %node }],
1183 exec => [ __PACKAGE__, 'exec', [ 'vmid', 'extra-args' ], { %node }, $print_agent_result],
1184 'exec-status' => [ "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { %node }, $print_agent_result],
1185 },
1186
1187 mtunnel => [ __PACKAGE__, 'mtunnel', []],
1188
1189 nbdstop => [ __PACKAGE__, 'nbdstop', ['vmid']],
1190
1191 terminal => [ __PACKAGE__, 'terminal', ['vmid']],
1192
1193 importovf => [ __PACKAGE__, 'importovf', ['vmid', 'manifest', 'storage']],
1194
1195 cleanup => [ __PACKAGE__, 'cleanup', ['vmid', 'clean-shutdown', 'guest-requested'], { %node }],
1196
1197 cloudinit => {
1198 dump => [ "PVE::API2::Qemu", 'cloudinit_generated_config_dump', ['vmid', 'type'], { %node }, sub { print "$_[0]\n"; }],
1199 pending => [ "PVE::API2::Qemu", 'cloudinit_pending', ['vmid'], { %node }, \&PVE::GuestHelpers::format_pending ],
1200 update => [ "PVE::API2::Qemu", 'cloudinit_update', ['vmid'], { node => $nodename }],
1201 },
1202
1203 import => [ __PACKAGE__, 'vm_import', ['vmid', 'source']],
1204 };
1205
1206 1;