X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FCLI%2Fqm.pm;h=b105830fa6f558e0efae0eb6efe66a764cc6b066;hb=HEAD;hp=0dd81590d6bacb5b4aee45c002d24087d611734e;hpb=12612b09ae4cadae5b654892fefd20ad24c5f5e3;p=qemu-server.git diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm index 0dd8159..b105830 100755 --- a/PVE/CLI/qm.pm +++ b/PVE/CLI/qm.pm @@ -8,30 +8,50 @@ use Getopt::Long qw(:config no_getopt_compat); use Fcntl ':flock'; use File::Path; -use IO::Socket::UNIX; use IO::Select; +use IO::Socket::UNIX; +use JSON; +use POSIX qw(strftime); +use Term::ReadLine; +use URI::Escape; -use PVE::Tools qw(extract_param); +use PVE::APIClient::LWP; use PVE::Cluster; -use PVE::SafeSyslog; +use PVE::Exception qw(raise_param_exc); +use PVE::GuestHelpers; use PVE::INotify; +use PVE::JSONSchema qw(get_standard_option); +use PVE::Network; use PVE::RPCEnvironment; -use PVE::QemuServer; +use PVE::SafeSyslog; +use PVE::Tools qw(extract_param file_get_contents); + +use PVE::API2::Qemu::Agent; use PVE::API2::Qemu; -use PVE::JSONSchema qw(get_standard_option); -use Term::ReadLine; +use PVE::QemuConfig; +use PVE::QemuServer::Drive; +use PVE::QemuServer::Helpers; +use PVE::QemuServer::Agent qw(agent_available); +use PVE::QemuServer::ImportDisk; +use PVE::QemuServer::Monitor qw(mon_cmd); +use PVE::QemuServer::OVF; +use PVE::QemuServer; use PVE::CLIHandler; - use base qw(PVE::CLIHandler); my $upid_exit = sub { my $upid = shift; my $status = PVE::Tools::upid_read_status($upid); - exit($status eq 'OK' ? 0 : -1); + exit(PVE::Tools::upid_status_is_error($status) ? -1 : 0); }; my $nodename = PVE::INotify::nodename(); +my %node = (node => $nodename); + +sub setup_environment { + PVE::RPCEnvironment->setup_default_cli_env(); +} sub run_vnc_proxy { my ($path) = @_; @@ -43,7 +63,7 @@ sub run_vnc_proxy { die "unable to connect to socket '$path' - $!" if !$s; - my $select = new IO::Select; + my $select = IO::Select->new(); $select->add(\*STDIN); $select->add($s); @@ -75,6 +95,32 @@ sub run_vnc_proxy { exit(0); } +sub print_recursive_hash { + my ($prefix, $hash, $key) = @_; + + if (ref($hash) eq 'HASH') { + if (defined($key)) { + print "$prefix$key:\n"; + } + for my $itemkey (sort keys %$hash) { + print_recursive_hash("\t$prefix", $hash->{$itemkey}, $itemkey); + } + } elsif (ref($hash) eq 'ARRAY') { + if (defined($key)) { + print "$prefix$key:\n"; + } + for my $item (@$hash) { + print_recursive_hash("\t$prefix", $item); + } + } elsif ((!ref($hash) && defined($hash)) || ref($hash) eq 'JSON::PP::Boolean') { + if (defined($key)) { + print "$prefix$key: $hash\n"; + } else { + print "$prefix$hash\n"; + } + } +} + __PACKAGE__->register_method ({ name => 'showcmd', path => 'showcmd', @@ -84,6 +130,20 @@ __PACKAGE__->register_method ({ additionalProperties => 0, properties => { vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), + pretty => { + description => "Puts each option on a new line to enhance human readability", + type => 'boolean', + optional => 1, + default => 0, + }, + snapshot => get_standard_option('pve-snapshot-name', { + description => "Fetch config values from given snapshot.", + optional => 1, + completion => sub { + my ($cmd, $pname, $cur, $args) = @_; + PVE::QemuConfig->snapshot_list($args->[0]); + } + }), }, }, returns => { type => 'null'}, @@ -91,9 +151,124 @@ __PACKAGE__->register_method ({ my ($param) = @_; my $storecfg = PVE::Storage::config(); - print PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}) . "\n"; + my $cmdline = PVE::QemuServer::vm_commandline($storecfg, $param->{vmid}, $param->{snapshot}); + + $cmdline =~ s/ -/ \\\n -/g if $param->{pretty}; + + print "$cmdline\n"; + + return; + }}); + + +__PACKAGE__->register_method({ + name => 'remote_migrate_vm', + path => 'remote_migrate_vm', + method => 'POST', + description => "Migrate virtual machine to a remote cluster. Creates a new migration task. EXPERIMENTAL feature!", + permissions => { + check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]], + }, + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), + 'target-vmid' => get_standard_option('pve-vmid', { optional => 1 }), + 'target-endpoint' => get_standard_option('proxmox-remote', { + description => "Remote target endpoint", + }), + online => { + type => 'boolean', + description => "Use online/live migration if VM is running. Ignored if VM is stopped.", + optional => 1, + }, + delete => { + type => 'boolean', + 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.", + optional => 1, + default => 0, + }, + 'target-storage' => get_standard_option('pve-targetstorage', { + completion => \&PVE::QemuServer::complete_migration_storage, + optional => 0, + }), + 'target-bridge' => { + type => 'string', + 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.", + format => 'bridge-pair-list', + }, + bwlimit => { + description => "Override I/O bandwidth limit (in KiB/s).", + optional => 1, + type => 'integer', + minimum => '0', + default => 'migrate limit from datacenter or storage config', + }, + }, + }, + returns => { + type => 'string', + description => "the task ID.", + }, + code => sub { + my ($param) = @_; + + my $rpcenv = PVE::RPCEnvironment::get(); + my $authuser = $rpcenv->get_user(); + + my $source_vmid = $param->{vmid}; + my $target_endpoint = $param->{'target-endpoint'}; + my $target_vmid = $param->{'target-vmid'} // $source_vmid; + + my $remote = PVE::JSONSchema::parse_property_string('proxmox-remote', $target_endpoint); + + # TODO: move this as helper somewhere appropriate? + my $conn_args = { + protocol => 'https', + host => $remote->{host}, + port => $remote->{port} // 8006, + apitoken => $remote->{apitoken}, + }; + + $conn_args->{cached_fingerprints} = { uc($remote->{fingerprint}) => 1 } + if defined($remote->{fingerprint}); + + my $api_client = PVE::APIClient::LWP->new(%$conn_args); + my $resources = $api_client->get("/cluster/resources", { type => 'vm' }); + if (grep { defined($_->{vmid}) && $_->{vmid} eq $target_vmid } @$resources) { + raise_param_exc({ target_vmid => "Guest with ID '$target_vmid' already exists on remote cluster" }); + } + + my $storages = $api_client->get("/nodes/localhost/storage", { enabled => 1 }); + + my $storecfg = PVE::Storage::config(); + my $target_storage = $param->{'target-storage'}; + my $storagemap = eval { PVE::JSONSchema::parse_idmap($target_storage, 'pve-storage-id') }; + raise_param_exc({ 'target-storage' => "failed to parse storage map: $@" }) + if $@; + + my $check_remote_storage = sub { + my ($storage) = @_; + my $found = [ grep { $_->{storage} eq $storage } @$storages ]; + die "remote: storage '$storage' does not exist (or missing permission)!\n" + if !@$found; + + $found = @$found[0]; + + my $content_types = [ PVE::Tools::split_list($found->{content}) ]; + die "remote: storage '$storage' cannot store images\n" + if !grep { $_ eq 'images' } @$content_types; + }; + + foreach my $target_sid (values %{$storagemap->{entries}}) { + $check_remote_storage->($target_sid); + } - return undef; + $check_remote_storage->($storagemap->{default}) + if $storagemap->{default}; + + return PVE::API2::Qemu->remote_migrate_vm($param); }}); __PACKAGE__->register_method ({ @@ -117,7 +292,7 @@ __PACKAGE__->register_method ({ my ($param) = @_; # test if VM exists - my $conf = PVE::QemuServer::load_config ($param->{vmid}); + my $conf = PVE::QemuConfig->load_config ($param->{vmid}); my $vmstatus = PVE::QemuServer::vmstatus($param->{vmid}, 1); my $stat = $vmstatus->{$param->{vmid}}; @@ -125,15 +300,14 @@ __PACKAGE__->register_method ({ foreach my $k (sort (keys %$stat)) { next if $k eq 'cpu' || $k eq 'relcpu'; # always 0 my $v = $stat->{$k}; - next if !defined($v); - print "$k: $v\n"; + print_recursive_hash("", $v, $k); } } else { my $status = $stat->{qmpstatus} || 'unknown'; print "status: $status\n"; } - return undef; + return; }}); __PACKAGE__->register_method ({ @@ -152,19 +326,19 @@ __PACKAGE__->register_method ({ my ($param) = @_; my $vmid = $param->{vmid}; - my $vnc_socket = PVE::QemuServer::vnc_socket($vmid); + PVE::QemuConfig::assert_config_exists_on_node($vmid); + my $vnc_socket = PVE::QemuServer::Helpers::vnc_socket($vmid); if (my $ticket = $ENV{LC_PVE_TICKET}) { # NOTE: ssh on debian only pass LC_* variables - PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,password"); - PVE::QemuServer::vm_mon_cmd($vmid, "set_password", protocol => 'vnc', password => $ticket); - PVE::QemuServer::vm_mon_cmd($vmid, "expire_password", protocol => 'vnc', time => "+30"); + mon_cmd($vmid, "set_password", protocol => 'vnc', password => $ticket); + mon_cmd($vmid, "expire_password", protocol => 'vnc', time => "+30"); } else { - PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,x509,password"); + die "LC_PVE_TICKET not set, VNC proxy without password is forbidden\n"; } run_vnc_proxy($vnc_socket); - return undef; + return; }}); __PACKAGE__->register_method ({ @@ -184,14 +358,37 @@ __PACKAGE__->register_method ({ my $vmid = $param->{vmid}; - PVE::QemuServer::lock_config ($vmid, sub { - my $conf = PVE::QemuServer::load_config($vmid); + PVE::QemuConfig->lock_config ($vmid, sub { + my $conf = PVE::QemuConfig->load_config($vmid); delete $conf->{lock}; delete $conf->{pending}->{lock} if $conf->{pending}; # just to be sure - PVE::QemuServer::write_config($vmid, $conf); + PVE::QemuConfig->write_config($vmid, $conf); }); - return undef; + return; + }}); + +__PACKAGE__->register_method ({ + name => 'nbdstop', + path => 'nbdstop', + method => 'PUT', + description => "Stop embedded nbd server.", + parameters => { + additionalProperties => 0, + properties => { + vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }), + }, + }, + returns => { type => 'null'}, + code => sub { + my ($param) = @_; + + my $vmid = $param->{vmid}; + + eval { PVE::QemuServer::nbd_stop($vmid) }; + warn $@ if $@; + + return; }}); __PACKAGE__->register_method ({ @@ -209,18 +406,42 @@ __PACKAGE__->register_method ({ if (!PVE::Cluster::check_cfs_quorum(1)) { print "no quorum\n"; - return undef; + return; } - print "tunnel online\n"; - *STDOUT->flush(); + my $tunnel_write = sub { + my $text = shift; + chomp $text; + print "$text\n"; + *STDOUT->flush(); + }; + + $tunnel_write->("tunnel online"); + $tunnel_write->("ver 1"); - while (my $line = <>) { + while (my $line = ) { chomp $line; - last if $line =~ m/^quit$/; + if ($line =~ /^quit$/) { + $tunnel_write->("OK"); + last; + } elsif ($line =~ /^resume (\d+)$/) { + my $vmid = $1; + # check_running and vm_resume with nocheck, since local node + # might not have processed config move/rename yet + if (PVE::QemuServer::check_running($vmid, 1)) { + eval { PVE::QemuServer::vm_resume($vmid, 1, 1); }; + if ($@) { + $tunnel_write->("ERR: resume failed - $@"); + } else { + $tunnel_write->("OK"); + } + } else { + $tunnel_write->("ERR: resume failed - VM $vmid not running"); + } + } } - return undef; + return; }}); __PACKAGE__->register_method ({ @@ -260,14 +481,14 @@ __PACKAGE__->register_method ({ die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid); - return undef; + return; }}); __PACKAGE__->register_method ({ name => 'monitor', path => 'monitor', method => 'POST', - description => "Enter Qemu Monitor interface.", + description => "Enter QEMU Monitor interface.", parameters => { additionalProperties => 0, properties => { @@ -280,27 +501,22 @@ __PACKAGE__->register_method ({ my $vmid = $param->{vmid}; - my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists + my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists - print "Entering Qemu Monitor for VM $vmid - type 'help' for help\n"; + print "Entering QEMU Monitor for VM $vmid - type 'help' for help\n"; - my $term = new Term::ReadLine ('qm'); + my $term = Term::ReadLine->new('qm'); - my $input; - while (defined ($input = $term->readline('qm> '))) { + while (defined(my $input = $term->readline('qm> '))) { chomp $input; - next if $input =~ m/^\s*$/; - last if $input =~ m/^\s*q(uit)?\s*$/; - eval { - print PVE::QemuServer::vm_human_monitor_command ($vmid, $input); - }; + eval { print PVE::QemuServer::Monitor::hmp_cmd($vmid, $input) }; print "ERROR: $@" if $@; } - return undef; + return; }}); @@ -316,15 +532,80 @@ __PACKAGE__->register_method ({ optional => 1, completion => \&PVE::QemuServer::complete_vmid, }), + dryrun => { + type => 'boolean', + optional => 1, + default => 0, + description => 'Do not actually write changes out to VM config(s).', + }, + }, + }, + returns => { type => 'null'}, + code => sub { + my ($param) = @_; + + my $dryrun = $param->{dryrun}; + + print "NOTE: running in dry-run mode, won't write changes out!\n" if $dryrun; + + PVE::QemuServer::rescan($param->{vmid}, 0, $dryrun); + + return; + }}); + +__PACKAGE__->register_method ({ + name => 'importdisk', + path => 'importdisk', + method => 'POST', + description => "Import an external disk image as an unused disk in a VM. The + image format has to be supported by qemu-img(1).", + parameters => { + additionalProperties => 0, + properties => { + vmid => get_standard_option('pve-vmid', {completion => \&PVE::QemuServer::complete_vmid}), + source => { + description => 'Path to the disk image to import', + type => 'string', + optional => 0, + }, + storage => get_standard_option('pve-storage-id', { + description => 'Target storage ID', + completion => \&PVE::QemuServer::complete_storage, + optional => 0, + }), + format => { + type => 'string', + description => 'Target format', + enum => [ 'raw', 'qcow2', 'vmdk' ], + optional => 1, + }, }, }, returns => { type => 'null'}, code => sub { my ($param) = @_; - PVE::QemuServer::rescan($param->{vmid}); + my $vmid = extract_param($param, 'vmid'); + my $source = extract_param($param, 'source'); + my $storeid = extract_param($param, 'storage'); + my $format = extract_param($param, 'format'); + + my $vm_conf = PVE::QemuConfig->load_config($vmid); + PVE::QemuConfig->check_lock($vm_conf); + die "$source: non-existent or non-regular file\n" if (! -f $source); + + my $storecfg = PVE::Storage::config(); + PVE::Storage::storage_check_enabled($storecfg, $storeid); + + my $target_storage_config = PVE::Storage::storage_config($storecfg, $storeid); + die "storage $storeid does not support vm images\n" + if !$target_storage_config->{content}->{images}; - return undef; + print "importing disk '$source' to VM $vmid ...\n"; + my ($drive_id, $volid) = PVE::QemuServer::ImportDisk::do_import($source, $vmid, $storeid, { format => $format }); + print "Successfully imported disk as '$drive_id:$volid'\n"; + + return; }}); __PACKAGE__->register_method ({ @@ -341,7 +622,13 @@ __PACKAGE__->register_method ({ type => 'string', optional => 1, enum => [qw(serial0 serial1 serial2 serial3)], - } + }, + escape => { + description => "Escape character.", + type => 'string', + optional => 1, + default => '^O', + }, }, }, returns => { type => 'null'}, @@ -350,7 +637,25 @@ __PACKAGE__->register_method ({ my $vmid = $param->{vmid}; - my $conf = PVE::QemuServer::load_config ($vmid); # check if VM exists + my $escape = $param->{escape} // '^O'; + if ($escape =~ /^\^([\x40-\x7a])$/) { + $escape = ord($1) & 0x1F; + } elsif ($escape =~ /^0x[0-9a-f]+$/i) { + $escape = hex($escape); + } elsif ($escape =~ /^[0-9]+$/) { + $escape = int($escape); + } else { + die "invalid escape character definition: $escape\n"; + } + my $escapemsg = ''; + if ($escape) { + $escapemsg = sprintf(' (press Ctrl+%c to exit)', $escape+0x40); + $escape = sprintf(',escape=0x%x', $escape); + } else { + $escape = ''; + } + + my $conf = PVE::QemuConfig->load_config ($vmid); # check if VM exists my $iface = $param->{iface}; @@ -371,187 +676,531 @@ __PACKAGE__->register_method ({ my $socket = "/var/run/qemu-server/${vmid}.$iface"; - my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0,escape=0x0f"; + my $cmd = "socat UNIX-CONNECT:$socket STDIO,raw,echo=0$escape"; - print "starting serial terminal on interface $iface (press control-O to exit)\n"; + print "starting serial terminal on interface ${iface}${escapemsg}\n"; system($cmd); - return undef; + return; }}); -our $cmddef = { - list => [ "PVE::API2::Qemu", 'vmlist', [], - { node => $nodename }, sub { - my $vmlist = shift; +__PACKAGE__->register_method ({ + name => 'importovf', + path => 'importovf', + description => "Create a new VM using parameters read from an OVF manifest", + parameters => { + additionalProperties => 0, + properties => { + vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), + manifest => { + type => 'string', + description => 'path to the ovf file', + }, + storage => get_standard_option('pve-storage-id', { + description => 'Target storage ID', + completion => \&PVE::QemuServer::complete_storage, + optional => 0, + }), + format => { + type => 'string', + description => 'Target format', + enum => [ 'raw', 'qcow2', 'vmdk' ], + optional => 1, + }, + dryrun => { + type => 'boolean', + description => 'Print a parsed representation of the extracted OVF parameters, but do not create a VM', + optional => 1, + } + }, + }, + returns => { type => 'null' }, + code => sub { + my ($param) = @_; - exit 0 if (!scalar(@$vmlist)); + my $vmid = PVE::Tools::extract_param($param, 'vmid'); + my $ovf_file = PVE::Tools::extract_param($param, 'manifest'); + my $storeid = PVE::Tools::extract_param($param, 'storage'); + my $format = PVE::Tools::extract_param($param, 'format'); + my $dryrun = PVE::Tools::extract_param($param, 'dryrun'); - printf "%10s %-20s %-10s %-10s %12s %-10s\n", - qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID); + die "$ovf_file: non-existent or non-regular file\n" if (! -f $ovf_file); + my $storecfg = PVE::Storage::config(); + PVE::Storage::storage_check_enabled($storecfg, $storeid); - foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) { - printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, - $rec->{qmpstatus} || $rec->{status}, - ($rec->{maxmem} || 0)/(1024*1024), - ($rec->{maxdisk} || 0)/(1024*1024*1024), - $rec->{pid}||0; - } + my $parsed = PVE::QemuServer::OVF::parse_ovf($ovf_file); + if ($dryrun) { + print to_json($parsed, { pretty => 1, canonical => 1}); + return; + } - } ], + eval { PVE::QemuConfig->create_and_lock_config($vmid) }; + die "Reserving empty config for OVF import to VM $vmid failed: $@" if $@; + + my $conf = PVE::QemuConfig->load_config($vmid); + die "Internal error: Expected 'create' lock in config of VM $vmid!" + if !PVE::QemuConfig->has_lock($conf, "create"); + + $conf->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name}); + $conf->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory}); + $conf->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores}); + + my $imported_disks = []; + eval { + # order matters, as do_import() will load_config() internally + $conf->{vmgenid} = PVE::QemuServer::generate_uuid(); + $conf->{smbios1} = PVE::QemuServer::generate_smbios1_uuid(); + PVE::QemuConfig->write_config($vmid, $conf); + + foreach my $disk (@{ $parsed->{disks} }) { + my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address}); + my ($name, $volid) = PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid, { + drive_name => $drive, + format => $format, + skiplock => 1, + }); + # for cleanup on (later) error + push @$imported_disks, $volid; + } - create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { node => $nodename }, $upid_exit ], + # reload after disks entries have been created + $conf = PVE::QemuConfig->load_config($vmid); + my $devs = PVE::QemuServer::get_default_bootdevices($conf); + $conf->{boot} = PVE::QemuServer::print_bootorder($devs); + PVE::QemuConfig->write_config($vmid, $conf); + }; + + if (my $err = $@) { + my $skiplock = 1; + warn "error during import, cleaning up created resources...\n"; + for my $volid (@$imported_disks) { + eval { PVE::Storage::vdisk_free($storecfg, $volid) }; + warn "cleanup of $volid failed: $@\n" if $@; + } + eval { PVE::QemuServer::destroy_vm($storecfg, $vmid, $skiplock) }; + warn "Could not destroy VM $vmid: $@" if "$@"; + die "import failed - $err"; + } - destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { node => $nodename }, $upid_exit ], + PVE::QemuConfig->remove_lock($vmid, "create"); - clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ], + return; - migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ], + } +}); - set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ], +__PACKAGE__->register_method({ + name => 'exec', + path => 'exec', + method => 'POST', + protected => 1, + description => "Executes the given command via the guest agent", + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + vmid => get_standard_option('pve-vmid', { + completion => \&PVE::QemuServer::complete_vmid_running }), + synchronous => { + type => 'boolean', + optional => 1, + default => 1, + description => "If set to off, returns the pid immediately instead of waiting for the commmand to finish or the timeout.", + }, + 'timeout' => { + type => 'integer', + description => "The maximum time to wait synchronously for the command to finish. If reached, the pid gets returned. Set to 0 to deactivate", + minimum => 0, + optional => 1, + default => 30, + }, + 'pass-stdin' => { + type => 'boolean', + 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.", + optional => 1, + default => 0, + }, + 'extra-args' => get_standard_option('extra-args'), + }, + }, + returns => { + type => 'object', + }, + code => sub { + my ($param) = @_; - resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ], + my $vmid = $param->{vmid}; + my $sync = $param->{synchronous} // 1; + my $pass_stdin = $param->{'pass-stdin'}; + if (defined($param->{timeout}) && !$sync) { + raise_param_exc({ synchronous => "needs to be set for 'timeout'"}); + } - move_disk => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node => $nodename }, $upid_exit ], + my $input_data = undef; + if ($pass_stdin) { + $input_data = ''; + while (my $line = ) { + $input_data .= $line; + if (length($input_data) > 1024*1024) { + # not sure how QEMU handles large amounts of data being + # passed into the QMP socket, so limit to be safe + die "'input-data' (STDIN) is limited to 1 MiB, aborting\n"; + } + } + } - unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node => $nodename } ], + my $args = $param->{'extra-args'}; + $args = undef if !$args || !@$args; - config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], - { node => $nodename }, sub { - my $config = shift; - foreach my $k (sort (keys %$config)) { - next if $k eq 'digest'; - my $v = $config->{$k}; - if ($k eq 'description') { - $v = PVE::Tools::encode_text($v); - } - print "$k: $v\n"; - } - }], - - pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'], - { node => $nodename }, sub { - my $data = shift; - foreach my $item (sort { $a->{key} cmp $b->{key}} @$data) { - my $k = $item->{key}; - next if $k eq 'digest'; - my $v = $item->{value}; - my $p = $item->{pending}; - if ($k eq 'description') { - $v = PVE::Tools::encode_text($v) if defined($v); - $p = PVE::Tools::encode_text($p) if defined($p); - } - if (defined($v)) { - if ($item->{delete}) { - print "del $k: $v\n"; - } elsif (defined($p)) { - print "cur $k: $v\n"; - print "new $k: $p\n"; - } else { - print "cur $k: $v\n"; - } - } elsif (defined($p)) { - print "new $k: $p\n"; - } - } - }], + my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $input_data, $args); - showcmd => [ __PACKAGE__, 'showcmd', ['vmid']], + if ($sync) { + my $pid = $res->{pid}; + my $timeout = $param->{timeout} // 30; + my $starttime = time(); - status => [ __PACKAGE__, 'status', ['vmid']], + while ($timeout == 0 || (time() - $starttime) < $timeout) { + my $out = PVE::QemuServer::Agent::qemu_exec_status($vmid, $pid); + if ($out->{exited}) { + $res = $out; + last; + } + sleep 1; + } - snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], + if (!$res->{exited}) { + warn "timeout reached, returning pid\n"; + } + } - delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], + return { result => $res }; + }}); - rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { node => $nodename } , $upid_exit ], +__PACKAGE__->register_method({ + name => 'cleanup', + path => 'cleanup', + method => 'POST', + protected => 1, + description => "Cleans up resources like tap devices, vgpus, etc. Called after a vm shuts down, crashes, etc.", + parameters => { + additionalProperties => 0, + properties => { + node => get_standard_option('pve-node'), + vmid => get_standard_option('pve-vmid', { + completion => \&PVE::QemuServer::complete_vmid_running }), + 'clean-shutdown' => { + type => 'boolean', + description => "Indicates if qemu shutdown cleanly.", + }, + 'guest-requested' => { + type => 'boolean', + description => "Indicates if the shutdown was requested by the guest or via qmp.", + }, + }, + }, + returns => { type => 'null', }, + code => sub { + my ($param) = @_; - template => [ "PVE::API2::Qemu", 'template', ['vmid'], { node => $nodename }], + my $vmid = $param->{vmid}; + my $clean = $param->{'clean-shutdown'}; + my $guest = $param->{'guest-requested'}; + my $restart = 0; - start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { node => $nodename } , $upid_exit ], + # return if we do not have the config anymore + return if !-f PVE::QemuConfig->config_file($vmid); - stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit ], + my $storecfg = PVE::Storage::config(); + warn "Starting cleanup for $vmid\n"; - reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { node => $nodename }, $upid_exit ], + # mdev cleanup can take a while, so wait up to 60 seconds + PVE::QemuConfig->lock_config_full($vmid, 60, sub { + my $conf = PVE::QemuConfig->load_config ($vmid); + my $pid = PVE::QemuServer::check_running ($vmid); + die "vm still running\n" if $pid; - shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit ], + if (!$clean) { + # we have to cleanup the tap devices after a crash - suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit ], + foreach my $opt (keys %$conf) { + next if $opt !~ m/^net(\d+)$/; + my $interface = $1; + PVE::Network::tap_unplug("tap${vmid}i${interface}"); + } + } - resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit ], + if (!$clean || $guest) { + # vm was shutdown from inside the guest or crashed, doing api cleanup + PVE::QemuServer::vm_stop_cleanup($storecfg, $vmid, $conf, 0, 0); + } + PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-stop'); - sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { node => $nodename } ], + $restart = eval { PVE::QemuServer::clear_reboot_request($vmid) }; + warn $@ if $@; + }); - vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], + warn "Finished cleanup for $vmid\n"; - wait => [ __PACKAGE__, 'wait', ['vmid']], + if ($restart) { + warn "Restarting VM $vmid\n"; + PVE::API2::Qemu->vm_start({ + vmid => $vmid, + %node, + }); + } - unlock => [ __PACKAGE__, 'unlock', ['vmid']], + return; + }}); - rescan => [ __PACKAGE__, 'rescan', []], +__PACKAGE__->register_method({ + name => 'vm_import', + path => 'vm-import', + description => "Import a foreign virtual guest from a supported import source, such as an ESXi storage.", + parameters => { + additionalProperties => 0, + properties => PVE::QemuServer::json_config_properties({ + vmid => get_standard_option('pve-vmid', { completion => \&PVE::Cluster::complete_next_vmid }), + 'source' => { + type => 'string', + description => 'The import source volume id.', + }, + storage => get_standard_option('pve-storage-id', { + description => "Default storage.", + completion => \&PVE::QemuServer::complete_storage, + }), + 'live-import' => { + type => 'boolean', + optional => 1, + default => 0, + description => "Immediately start the VM and copy the data in the background.", + }, + 'dryrun' => { + type => 'boolean', + optional => 1, + default => 0, + description => "Show the create command and exit without doing anything.", + }, + delete => { + type => 'string', format => 'pve-configid-list', + description => "A list of settings you want to delete.", + optional => 1, + }, + format => { + type => 'string', + description => 'Target format', + enum => [ 'raw', 'qcow2', 'vmdk' ], + optional => 1, + }, + }), + }, + returns => { type => 'null' }, + code => sub { + my ($param) = @_; - monitor => [ __PACKAGE__, 'monitor', ['vmid']], + my ($vmid, $source, $storage, $format, $live_import, $dryrun, $delete) = + delete $param->@{qw(vmid source storage format live-import dryrun delete)}; - mtunnel => [ __PACKAGE__, 'mtunnel', []], + if (defined($format)) { + $format = ",format=$format"; + } else { + $format = ''; + } - terminal => [ __PACKAGE__, 'terminal', ['vmid']], + my $storecfg = PVE::Storage::config(); + my $metadata = PVE::Storage::get_import_metadata($storecfg, $source); + + my $create_args = $metadata->{'create-args'}; + if (my $netdevs = $metadata->{net}) { + for my $net (keys $netdevs->%*) { + my $value = $netdevs->{$net}; + $create_args->{$net} = join(',', map { $_ . '=' . $value->{$_} } sort keys %$value); + } + } + if (my $disks = $metadata->{disks}) { + if (delete $disks->{efidisk0}) { + $create_args->{efidisk0} = "$storage:1$format,efitype=4m"; + } + for my $disk (keys $disks->%*) { + my $value = $disks->{$disk}->{volid}; + $create_args->{$disk} = "$storage:0${format},import-from=$value"; + } + } + + $create_args->{'live-restore'} = 1 if $live_import; + + $create_args->{$_} = $param->{$_} for keys $param->%*; + delete $create_args->{$_} for PVE::Tools::split_list($delete); + + if ($dryrun) { + print("# dry-run – the resulting create command for the import would be:\n"); + print("qm create $vmid \\\n "); + print(join(" \\\n ", map { "--$_ $create_args->{$_}" } sort keys $create_args->%*)); + print("\n"); + return; + } + + PVE::API2::Qemu->create_vm({ + %node, + vmid => $vmid, + %$create_args, + }); + return; + } +}); + +my $print_agent_result = sub { + my ($data) = @_; + + my $result = $data->{result} // $data; + return if !defined($result); + + my $class = ref($result); + + if (!$class) { + chomp $result; + return if $result =~ m/^\s*$/; + print "$result\n"; + return; + } + + if (($class eq 'HASH') && !scalar(keys %$result)) { # empty hash + return; + } + + print to_json($result, { pretty => 1, canonical => 1, utf8 => 1}); }; -1; +sub param_mapping { + my ($name) = @_; + + my $ssh_key_map = ['sshkeys', sub { + return URI::Escape::uri_escape(file_get_contents($_[0])); + }]; + my $cipassword_map = PVE::CLIHandler::get_standard_mapping('pve-password', { name => 'cipassword' }); + my $password_map = PVE::CLIHandler::get_standard_mapping('pve-password'); + my $mapping = { + 'update_vm' => [$ssh_key_map, $cipassword_map], + 'create_vm' => [$ssh_key_map, $cipassword_map], + 'set-user-password' => [$password_map], + }; + + return $mapping->{$name}; +} + +our $cmddef = { + list=> [ "PVE::API2::Qemu", 'vmlist', [], { %node }, sub { + my $vmlist = shift; + exit 0 if (!scalar(@$vmlist)); + + printf "%10s %-20s %-10s %-10s %12s %-10s\n", + qw(VMID NAME STATUS MEM(MB) BOOTDISK(GB) PID); + + foreach my $rec (sort { $a->{vmid} <=> $b->{vmid} } @$vmlist) { + printf "%10s %-20s %-10s %-10s %12.2f %-10s\n", $rec->{vmid}, $rec->{name}, + $rec->{qmpstatus} || $rec->{status}, + ($rec->{maxmem} || 0)/(1024*1024), + ($rec->{maxdisk} || 0)/(1024*1024*1024), + $rec->{pid} || 0; + } + }], + + create => [ "PVE::API2::Qemu", 'create_vm', ['vmid'], { %node }, $upid_exit ], + destroy => [ "PVE::API2::Qemu", 'destroy_vm', ['vmid'], { %node }, $upid_exit ], + clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { %node }, $upid_exit ], -__END__ + migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { %node }, $upid_exit ], + 'remote-migrate' => [ __PACKAGE__, 'remote_migrate_vm', ['vmid', 'target-vmid', 'target-endpoint'], { %node }, $upid_exit ], -=head1 NAME + set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { %node } ], -qm - qemu/kvm virtual machine manager + config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], { %node }, sub { + my $config = shift; + foreach my $k (sort (keys %$config)) { + next if $k eq 'digest'; + my $v = $config->{$k}; + if ($k eq 'description') { + $v = PVE::Tools::encode_text($v); + } + print "$k: $v\n"; + } + }], + + pending => [ "PVE::API2::Qemu", 'vm_pending', ['vmid'], { %node }, \&PVE::GuestHelpers::format_pending ], + showcmd => [ __PACKAGE__, 'showcmd', ['vmid']], -=head1 SYNOPSIS + status => [ __PACKAGE__, 'status', ['vmid']], -=include synopsis + # FIXME: for 8.0 move to command group snapshot { create, list, destroy, rollback } + snapshot => [ "PVE::API2::Qemu", 'snapshot', ['vmid', 'snapname'], { %node } , $upid_exit ], + delsnapshot => [ "PVE::API2::Qemu", 'delsnapshot', ['vmid', 'snapname'], { %node } , $upid_exit ], + listsnapshot => [ "PVE::API2::Qemu", 'snapshot_list', ['vmid'], { %node }, \&PVE::GuestHelpers::print_snapshot_tree], + rollback => [ "PVE::API2::Qemu", 'rollback', ['vmid', 'snapname'], { %node } , $upid_exit ], -=head1 DESCRIPTION + template => [ "PVE::API2::Qemu", 'template', ['vmid'], { %node }], -qm is a script to manage virtual machines with qemu/kvm. You can -create and destroy virtual machines, and control execution -(start/stop/suspend/resume). Besides that, you can use qm to set -parameters in the associated config file. It is also possible to -create and delete virtual disks. + # FIXME: should be in a power command group? + start => [ "PVE::API2::Qemu", 'vm_start', ['vmid'], { %node } , $upid_exit ], + stop => [ "PVE::API2::Qemu", 'vm_stop', ['vmid'], { %node }, $upid_exit ], + reset => [ "PVE::API2::Qemu", 'vm_reset', ['vmid'], { %node }, $upid_exit ], + shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { %node }, $upid_exit ], + reboot => [ "PVE::API2::Qemu", 'vm_reboot', ['vmid'], { %node }, $upid_exit ], + suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { %node }, $upid_exit ], + resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { %node }, $upid_exit ], -=head1 CONFIGURATION + sendkey => [ "PVE::API2::Qemu", 'vm_sendkey', ['vmid', 'key'], { %node } ], -All configuration files consists of lines in the form + vncproxy => [ __PACKAGE__, 'vncproxy', ['vmid']], - PARAMETER: value + wait => [ __PACKAGE__, 'wait', ['vmid']], -See L for a complete list of options. + unlock => [ __PACKAGE__, 'unlock', ['vmid']], -Configuration files are stored inside the Proxmox configuration file system, and can be access at F.conf>. + # TODO: evluate dropping below aliases for 8.0, if no usage is left + importdisk => { alias => 'disk import' }, + 'move-disk' => { alias => 'disk move' }, + move_disk => { alias => 'disk move' }, + rescan => { alias => 'disk rescan' }, + resize => { alias => 'disk resize' }, + unlink => { alias => 'disk unlink' }, + + disk => { + import => [ __PACKAGE__, 'importdisk', ['vmid', 'source', 'storage']], + 'move' => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { %node }, $upid_exit ], + rescan => [ __PACKAGE__, 'rescan', []], + resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { %node } ], + unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { %node } ], + }, -The default for option 'keyboard' is read from -F. + monitor => [ __PACKAGE__, 'monitor', ['vmid']], -=head1 Locks + agent => { alias => 'guest cmd' }, # FIXME: remove with PVE 8.0 -Online migration and backups (vzdump) set a lock to prevent -unintentional action on such VMs. Sometimes you need remove such lock -manually (power failure). + guest => { + cmd => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'], { %node }, $print_agent_result ], + passwd => [ "PVE::API2::Qemu::Agent", 'set-user-password', [ 'vmid', 'username' ], { %node }], + exec => [ __PACKAGE__, 'exec', [ 'vmid', 'extra-args' ], { %node }, $print_agent_result], + 'exec-status' => [ "PVE::API2::Qemu::Agent", 'exec-status', [ 'vmid', 'pid' ], { %node }, $print_agent_result], + }, - qm unlock + mtunnel => [ __PACKAGE__, 'mtunnel', []], -=head1 EXAMPLES + nbdstop => [ __PACKAGE__, 'nbdstop', ['vmid']], - # create a new VM with 4 GB ide disk - qm create 300 -ide0 4 -net0 e1000 -cdrom proxmox-mailgateway_2.1.iso + terminal => [ __PACKAGE__, 'terminal', ['vmid']], - # start the new VM - qm start 300 + importovf => [ __PACKAGE__, 'importovf', ['vmid', 'manifest', 'storage']], - # send shutdown, then wait until VM is stopped - qm shutdown 300 && qm wait 300 + cleanup => [ __PACKAGE__, 'cleanup', ['vmid', 'clean-shutdown', 'guest-requested'], { %node }], - # same as above, but only wait for 40 seconds - qm shutdown 300 && qm wait 300 -timeout 40 + cloudinit => { + dump => [ "PVE::API2::Qemu", 'cloudinit_generated_config_dump', ['vmid', 'type'], { %node }, sub { print "$_[0]\n"; }], + pending => [ "PVE::API2::Qemu", 'cloudinit_pending', ['vmid'], { %node }, \&PVE::GuestHelpers::format_pending ], + update => [ "PVE::API2::Qemu", 'cloudinit_update', ['vmid'], { node => $nodename }], + }, + import => [ __PACKAGE__, 'vm_import', ['vmid', 'source']], +}; -=include pve_copyright +1;