6 # Note: disable '+' prefix for Getopt::Long (for resize command)
7 use Getopt
::Long
qw(:config no_getopt_compat);
14 use PVE
::Tools
qw(extract_param);
18 use PVE
::RPCEnvironment
;
20 use PVE
::QemuServer
::ImportDisk
;
21 use PVE
::QemuServer
::OVF
;
24 use PVE
::JSONSchema
qw(get_standard_option);
29 use base
qw(PVE::CLIHandler);
33 my $status = PVE
::Tools
::upid_read_status
($upid);
34 exit($status eq 'OK' ?
0 : -1);
37 my $nodename = PVE
::INotify
::nodename
();
39 sub setup_environment
{
40 PVE
::RPCEnvironment-
>setup_default_cli_env();
47 while ( ++$c < 10 && !-e
$path ) { sleep(1); }
49 my $s = IO
::Socket
::UNIX-
>new(Peer
=> $path, Timeout
=> 120);
51 die "unable to connect to socket '$path' - $!" if !$s;
53 my $select = new IO
::Select
;
55 $select->add(\
*STDIN
);
58 my $timeout = 60*15; # 15 minutes
61 while ($select->count &&
62 scalar(@handles = $select->can_read ($timeout))) {
63 foreach my $h (@handles) {
65 my $n = $h->sysread($buf, 4096);
75 syswrite(\
*STDOUT
, $buf);
85 sub print_recursive_hash
{
86 my ($prefix, $hash, $key) = @_;
88 if (ref($hash) eq 'HASH') {
90 print "$prefix$key:\n";
92 foreach my $itemkey (keys %$hash) {
93 print_recursive_hash
("\t$prefix", $hash->{$itemkey}, $itemkey);
95 } elsif (ref($hash) eq 'ARRAY') {
97 print "$prefix$key:\n";
99 foreach my $item (@$hash) {
100 print_recursive_hash
("\t$prefix", $item);
102 } elsif (!ref($hash) && defined($hash)) {
104 print "$prefix$key: $hash\n";
106 print "$prefix$hash\n";
111 __PACKAGE__-
>register_method ({
115 description
=> "Show command line which is used to start the VM (debug info).",
117 additionalProperties
=> 0,
119 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
121 description
=> "Puts each option on a new line to enhance human readability",
128 returns
=> { type
=> 'null'},
132 my $storecfg = PVE
::Storage
::config
();
133 my $cmdline = PVE
::QemuServer
::vm_commandline
($storecfg, $param->{vmid
});
135 $cmdline =~ s/ -/ \\\n -/g if $param->{pretty
};
142 __PACKAGE__-
>register_method ({
146 description
=> "Show VM status.",
148 additionalProperties
=> 0,
150 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
152 description
=> "Verbose output format",
158 returns
=> { type
=> 'null'},
163 my $conf = PVE
::QemuConfig-
>load_config ($param->{vmid
});
165 my $vmstatus = PVE
::QemuServer
::vmstatus
($param->{vmid
}, 1);
166 my $stat = $vmstatus->{$param->{vmid
}};
167 if ($param->{verbose
}) {
168 foreach my $k (sort (keys %$stat)) {
169 next if $k eq 'cpu' || $k eq 'relcpu'; # always 0
171 print_recursive_hash
("", $v, $k);
174 my $status = $stat->{qmpstatus
} || 'unknown';
175 print "status: $status\n";
181 __PACKAGE__-
>register_method ({
185 description
=> "Proxy VM VNC traffic to stdin/stdout",
187 additionalProperties
=> 0,
189 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
192 returns
=> { type
=> 'null'},
196 my $vmid = $param->{vmid
};
197 my $vnc_socket = PVE
::QemuServer
::vnc_socket
($vmid);
199 if (my $ticket = $ENV{LC_PVE_TICKET
}) { # NOTE: ssh on debian only pass LC_* variables
200 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,password");
201 PVE
::QemuServer
::vm_mon_cmd
($vmid, "set_password", protocol
=> 'vnc', password
=> $ticket);
202 PVE
::QemuServer
::vm_mon_cmd
($vmid, "expire_password", protocol
=> 'vnc', time => "+30");
204 PVE
::QemuServer
::vm_mon_cmd
($vmid, "change", device
=> 'vnc', target
=> "unix:$vnc_socket,x509,password");
207 run_vnc_proxy
($vnc_socket);
212 __PACKAGE__-
>register_method ({
216 description
=> "Unlock the VM.",
218 additionalProperties
=> 0,
220 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
223 returns
=> { type
=> 'null'},
227 my $vmid = $param->{vmid
};
229 PVE
::QemuConfig-
>lock_config ($vmid, sub {
230 my $conf = PVE
::QemuConfig-
>load_config($vmid);
231 delete $conf->{lock};
232 delete $conf->{pending
}->{lock} if $conf->{pending
}; # just to be sure
233 PVE
::QemuConfig-
>write_config($vmid, $conf);
239 __PACKAGE__-
>register_method ({
243 description
=> "Stop embedded nbd server.",
245 additionalProperties
=> 0,
247 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
250 returns
=> { type
=> 'null'},
254 my $vmid = $param->{vmid
};
256 PVE
::QemuServer
::nbd_stop
($vmid);
261 __PACKAGE__-
>register_method ({
265 description
=> "Used by qmigrate - do not use manually.",
267 additionalProperties
=> 0,
270 returns
=> { type
=> 'null'},
274 if (!PVE
::Cluster
::check_cfs_quorum
(1)) {
279 my $tunnel_write = sub {
286 $tunnel_write->("tunnel online");
287 $tunnel_write->("ver 1");
289 while (my $line = <>) {
291 if ($line =~ /^quit$/) {
292 $tunnel_write->("OK");
294 } elsif ($line =~ /^resume (\d+)$/) {
296 if (PVE
::QemuServer
::check_running
($vmid, 1)) {
297 eval { PVE
::QemuServer
::vm_resume
($vmid, 1, 1); };
299 $tunnel_write->("ERR: resume failed - $@");
301 $tunnel_write->("OK");
304 $tunnel_write->("ERR: resume failed - VM $vmid not running");
312 __PACKAGE__-
>register_method ({
316 description
=> "Wait until the VM is stopped.",
318 additionalProperties
=> 0,
320 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
322 description
=> "Timeout in seconds. Default is to wait forever.",
329 returns
=> { type
=> 'null'},
333 my $vmid = $param->{vmid
};
334 my $timeout = $param->{timeout
};
336 my $pid = PVE
::QemuServer
::check_running
($vmid);
339 print "waiting until VM $vmid stopps (PID $pid)\n";
342 while ((!$timeout || ($count < $timeout)) && PVE
::QemuServer
::check_running
($vmid)) {
347 die "wait failed - got timeout\n" if PVE
::QemuServer
::check_running
($vmid);
352 __PACKAGE__-
>register_method ({
356 description
=> "Enter Qemu Monitor interface.",
358 additionalProperties
=> 0,
360 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
363 returns
=> { type
=> 'null'},
367 my $vmid = $param->{vmid
};
369 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
371 print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n";
373 my $term = new Term
::ReadLine
('qm');
376 while (defined ($input = $term->readline('qm> '))) {
379 next if $input =~ m/^\s*$/;
381 last if $input =~ m/^\s*q(uit)?\s*$/;
384 print PVE
::QemuServer
::vm_human_monitor_command
($vmid, $input);
386 print "ERROR: $@" if $@;
393 __PACKAGE__-
>register_method ({
397 description
=> "Rescan all storages and update disk sizes and unused disk images.",
399 additionalProperties
=> 0,
401 vmid
=> get_standard_option
('pve-vmid', {
403 completion
=> \
&PVE
::QemuServer
::complete_vmid
,
407 returns
=> { type
=> 'null'},
411 PVE
::QemuServer
::rescan
($param->{vmid
});
416 __PACKAGE__-
>register_method ({
417 name
=> 'importdisk',
418 path
=> 'importdisk',
420 description
=> "Import an external disk image as an unused disk in a VM. The
421 image format has to be supported by qemu-img(1).",
423 additionalProperties
=> 0,
425 vmid
=> get_standard_option
('pve-vmid', {completion
=> \
&PVE
::QemuServer
::complete_vmid
}),
427 description
=> 'Path to the disk image to import',
431 storage
=> get_standard_option
('pve-storage-id', {
432 description
=> 'Target storage ID',
433 completion
=> \
&PVE
::QemuServer
::complete_storage
,
438 description
=> 'Target format',
439 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
444 returns
=> { type
=> 'null'},
448 my $vmid = extract_param
($param, 'vmid');
449 my $source = extract_param
($param, 'source');
450 my $storeid = extract_param
($param, 'storage');
451 my $format = extract_param
($param, 'format');
453 my $vm_conf = PVE
::QemuConfig-
>load_config($vmid);
454 PVE
::QemuConfig-
>check_lock($vm_conf);
455 die "$source: non-existent or non-regular file\n" if (! -f
$source);
457 my $storecfg = PVE
::Storage
::config
();
458 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
460 my $target_storage_config =
461 PVE
::Storage
::storage_config
($storecfg, $storeid);
462 die "storage $storeid does not support vm images\n"
463 if !$target_storage_config->{content
}->{images
};
465 PVE
::QemuServer
::ImportDisk
::do_import
($source, $vmid, $storeid, { format
=> $format });
470 __PACKAGE__-
>register_method ({
474 description
=> "Open a terminal using a serial device (The VM need to have a serial device configured, for example 'serial0: socket')",
476 additionalProperties
=> 0,
478 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::QemuServer
::complete_vmid_running
}),
480 description
=> "Select the serial device. By default we simply use the first suitable device.",
483 enum
=> [qw(serial0 serial1 serial2 serial3)],
486 description
=> "Escape character.",
493 returns
=> { type
=> 'null'},
497 my $vmid = $param->{vmid
};
499 my $escape = $param->{escape
} // '^O';
500 if ($escape =~ /^\^([\x40-\x7a])$/) {
501 $escape = ord($1) & 0x1F;
502 } elsif ($escape =~ /^0x[0-9a-f]+$/i) {
503 $escape = hex($escape);
504 } elsif ($escape =~ /^[0-9]+$/) {
505 $escape = int($escape);
507 die "invalid escape character definition: $escape\n";
511 $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40);
512 $escape = sprintf(',escape=0x%x', $escape);
517 my $conf = PVE
::QemuConfig-
>load_config ($vmid); # check if VM exists
519 my $iface = $param->{iface
};
522 die "serial interface '$iface' is not configured\n" if !$conf->{$iface};
523 die "wrong serial type on interface '$iface'\n" if $conf->{$iface} ne 'socket';
525 foreach my $opt (qw(serial0 serial1 serial2 serial3)) {
526 if ($conf->{$opt} && ($conf->{$opt} eq 'socket')) {
531 die "unable to find a serial interface\n" if !$iface;
534 die "VM $vmid not running\n" if !PVE
::QemuServer
::check_running
($vmid);
536 my $socket = "/var/run/qemu-server/${vmid}.$iface";
538 my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape";
540 print "starting serial terminal on interface ${iface}${escapemsg}\n";
547 __PACKAGE__-
>register_method ({
550 description
=> "Create a new VM using parameters read from an OVF manifest",
552 additionalProperties
=> 0,
554 vmid
=> get_standard_option
('pve-vmid', { completion
=> \
&PVE
::Cluster
::complete_next_vmid
}),
557 description
=> 'path to the ovf file',
559 storage
=> get_standard_option
('pve-storage-id', {
560 description
=> 'Target storage ID',
561 completion
=> \
&PVE
::QemuServer
::complete_storage
,
566 description
=> 'Target format',
567 enum
=> [ 'raw', 'qcow2', 'vmdk' ],
572 description
=> 'Print a parsed representation of the extracted OVF parameters, but do not create a VM',
577 returns
=> { type
=> 'null' },
581 my $vmid = PVE
::Tools
::extract_param
($param, 'vmid');
582 my $ovf_file = PVE
::Tools
::extract_param
($param, 'manifest');
583 my $storeid = PVE
::Tools
::extract_param
($param, 'storage');
584 my $format = PVE
::Tools
::extract_param
($param, 'format');
585 my $dryrun = PVE
::Tools
::extract_param
($param, 'dryrun');
587 die "$ovf_file: non-existent or non-regular file\n" if (! -f
$ovf_file);
588 my $storecfg = PVE
::Storage
::config
();
589 PVE
::Storage
::storage_check_enabled
($storecfg, $storeid);
591 my $parsed = PVE
::QemuServer
::OVF
::parse_ovf
($ovf_file);
594 print to_json
($parsed, { pretty
=> 1, canonical
=> 1});
598 $param->{name
} = $parsed->{qm
}->{name
} if defined($parsed->{qm
}->{name
});
599 $param->{memory
} = $parsed->{qm
}->{memory
} if defined($parsed->{qm
}->{memory
});
600 $param->{cores
} = $parsed->{qm
}->{cores
} if defined($parsed->{qm
}->{cores
});
604 PVE
::Cluster
::check_vmid_unused
($vmid);
609 # order matters, as do_import() will load_config() internally
610 $conf->{smbios1
} = PVE
::QemuServer
::generate_smbios1_uuid
();
611 PVE
::QemuConfig-
>write_config($vmid, $conf);
613 foreach my $disk (@{ $parsed->{disks
} }) {
614 my ($file, $drive) = ($disk->{backing_file
}, $disk->{disk_address
});
615 PVE
::QemuServer
::ImportDisk
::do_import
($file, $vmid, $storeid,
616 { drive_name
=> $drive, format
=> $format });
619 # reload after disks entries have been created
620 $conf = PVE
::QemuConfig-
>load_config($vmid);
621 PVE
::QemuConfig-
>check_lock($conf);
622 my $firstdisk = PVE
::QemuServer
::resolve_first_disk
($conf);
623 $conf->{bootdisk
} = $firstdisk if $firstdisk;
624 PVE
::QemuConfig-
>write_config($vmid, $conf);
630 eval { PVE
::QemuServer
::vm_destroy
($storecfg, $vmid, $skiplock); };
631 die "import failed - $err";
635 my $wait_for_lock = 1;
636 PVE
::QemuConfig-
>lock_config_full($vmid, $wait_for_lock, $importfn);
643 my $print_agent_result = sub {
646 my $result = $data->{result
};
647 return if !defined($result);
649 my $class = ref($result);
653 return if $result =~ m/^\s*$/;
658 if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash
662 print to_json
($result, { pretty
=> 1, canonical
=> 1});
666 list
=> [ "PVE::API2::Qemu", 'vmlist', [],
667 { node
=> $nodename }, sub {
670 exit 0 if (!scalar(@$vmlist));
672 printf "%10s %-20s %-10s %-10s %12s %-10s\n",
673 qw(VMID NAME STATUS MEM(MB) BOOTDISK
(GB
) PID
);
675 foreach my $rec (sort { $a->{vmid
} <=> $b->{vmid
} } @$vmlist) {
676 printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid
}, $rec->{name
},
677 $rec->{qmpstatus
} || $rec->{status
},
678 ($rec->{maxmem
} || 0)/(1024*1024),
679 ($rec->{maxdisk
} || 0)/(1024*1024*1024),
686 create
=> [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
688 destroy
=> [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node
=> $nodename }, $upid_exit ],
690 clone
=> [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node
=> $nodename }, $upid_exit ],
692 migrate
=> [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node
=> $nodename }, $upid_exit ],
694 set
=> [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node
=> $nodename } ],
696 resize
=> [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node
=> $nodename } ],
698 move_disk
=> [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node
=> $nodename }, $upid_exit ],
700 unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node
=> $nodename } ],
702 config
=> [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
703 { node
=> $nodename }, sub {
705 foreach my $k (sort (keys %$config)) {
706 next if $k eq 'digest';
707 my $v = $config->{$k};
708 if ($k eq 'description') {
709 $v = PVE
::Tools
::encode_text
($v);
715 pending
=> [ "PVE::API2::Qemu", 'vm_pending', ['vmid'],
716 { node
=> $nodename }, sub {
718 foreach my $item (sort { $a->{key
} cmp $b->{key
}} @$data) {
719 my $k = $item->{key
};
720 next if $k eq 'digest';
721 my $v = $item->{value
};
722 my $p = $item->{pending
};
723 if ($k eq 'description') {
724 $v = PVE
::Tools
::encode_text
($v) if defined($v);
725 $p = PVE
::Tools
::encode_text
($p) if defined($p);
728 if ($item->{delete}) {
729 print "del $k: $v\n";
730 } elsif (defined($p)) {
731 print "cur $k: $v\n";
732 print "new $k: $p\n";
734 print "cur $k: $v\n";
736 } elsif (defined($p)) {
737 print "new $k: $p\n";
742 showcmd
=> [ __PACKAGE__
, 'showcmd', ['vmid']],
744 status
=> [ __PACKAGE__
, 'status', ['vmid']],
746 snapshot
=> [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
748 delsnapshot
=> [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
750 listsnapshot
=> [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { node
=> $nodename },
753 foreach my $e (@$res) {
754 my $headline = $e->{description
} || 'no-description';
755 $headline =~ s/\n.*//sg;
756 my $parent = $e->{parent
} // 'no-parent';
757 printf("%-20s %-20s %s\n", $e->{name
}, $parent, $headline);
761 rollback
=> [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node
=> $nodename } , $upid_exit ],
763 template
=> [ "PVE::API2::Qemu", 'template', ['vmid'], { node
=> $nodename }],
765 start
=> [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node
=> $nodename } , $upid_exit ],
767 stop
=> [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node
=> $nodename }, $upid_exit ],
769 reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node
=> $nodename }, $upid_exit ],
771 shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node
=> $nodename }, $upid_exit ],
773 suspend
=> [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node
=> $nodename }, $upid_exit ],
775 resume
=> [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node
=> $nodename }, $upid_exit ],
777 sendkey
=> [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node
=> $nodename } ],
779 vncproxy
=> [ __PACKAGE__
, 'vncproxy', ['vmid']],
781 wait => [ __PACKAGE__
, 'wait', ['vmid']],
783 unlock
=> [ __PACKAGE__
, 'unlock', ['vmid']],
785 rescan
=> [ __PACKAGE__
, 'rescan', []],
787 monitor
=> [ __PACKAGE__
, 'monitor', ['vmid']],
789 agent
=> [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
790 { node
=> $nodename }, $print_agent_result ],
792 mtunnel
=> [ __PACKAGE__
, 'mtunnel', []],
794 nbdstop
=> [ __PACKAGE__
, 'nbdstop', ['vmid']],
796 terminal
=> [ __PACKAGE__
, 'terminal', ['vmid']],
798 importdisk
=> [ __PACKAGE__
, 'importdisk', ['vmid', 'source', 'storage']],
800 importovf
=> [ __PACKAGE__
, 'importovf', ['vmid', 'manifest', 'storage']],