]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/CLI/qm.pm
qm status: sort hash keys on verbose output
[qemu-server.git] / PVE / CLI / qm.pm
index 44be39d74bb41adc5bfa8dd8d889522f79d1419a..f8972bd325f7e7004cb91d4b7b099a8465f0f7e2 100755 (executable)
@@ -27,9 +27,12 @@ use PVE::Tools qw(extract_param);
 
 use PVE::API2::Qemu::Agent;
 use PVE::API2::Qemu;
+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;
 
@@ -58,7 +61,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);
@@ -97,17 +100,17 @@ sub print_recursive_hash {
        if (defined($key)) {
            print "$prefix$key:\n";
        }
-       foreach my $itemkey (keys %$hash) {
+       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";
        }
-       foreach my $item (@$hash) {
+       for my $item (@$hash) {
            print_recursive_hash("\t$prefix", $item);
        }
-    } elsif (!ref($hash) && defined($hash)) {
+    } elsif ((!ref($hash) && defined($hash)) || ref($hash) eq 'JSON::PP::Boolean') {
        if (defined($key)) {
            print "$prefix$key: $hash\n";
        } else {
@@ -152,7 +155,7 @@ __PACKAGE__->register_method ({
 
        print "$cmdline\n";
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -191,7 +194,7 @@ __PACKAGE__->register_method ({
            print "status: $status\n";
        }
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -210,20 +213,21 @@ __PACKAGE__->register_method ({
        my ($param) = @_;
 
        my $vmid = $param->{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, "change", device => 'vnc', target => "unix:$vnc_socket,password");
+           mon_cmd($vmid, "set_password", protocol => 'vnc', password => $ticket);
+           mon_cmd($vmid, "expire_password", protocol => 'vnc', time => "+30");
        } else {
            # FIXME: remove or allow to add tls-creds object, as x509 vnc param is removed with qemu 4??
-           PVE::QemuServer::vm_mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,password");
+           mon_cmd($vmid, "change", device => 'vnc', target => "unix:$vnc_socket,password");
        }
 
        run_vnc_proxy($vnc_socket);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -250,7 +254,7 @@ __PACKAGE__->register_method ({
            PVE::QemuConfig->write_config($vmid, $conf);
        });
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -270,9 +274,10 @@ __PACKAGE__->register_method ({
 
        my $vmid = $param->{vmid};
 
-       PVE::QemuServer::nbd_stop($vmid);
+       eval { PVE::QemuServer::nbd_stop($vmid) };
+       warn $@ if $@;
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -290,7 +295,7 @@ __PACKAGE__->register_method ({
 
        if (!PVE::Cluster::check_cfs_quorum(1)) {
            print "no quorum\n";
-           return undef;
+           return;
        }
 
        my $tunnel_write = sub {
@@ -323,7 +328,7 @@ __PACKAGE__->register_method ({
            }
        }
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -363,7 +368,7 @@ __PACKAGE__->register_method ({
 
        die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -387,23 +392,18 @@ __PACKAGE__->register_method ({
 
        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;
 
     }});
 
@@ -437,7 +437,7 @@ __PACKAGE__->register_method ({
 
        PVE::QemuServer::rescan($param->{vmid}, 0, $dryrun);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -492,7 +492,7 @@ __PACKAGE__->register_method ({
        my ($drive_id, $volid) = PVE::QemuServer::ImportDisk::do_import($source, $vmid, $storeid, { format => $format });
        print "Successfully imported disk as '$drive_id:$volid'\n";
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -569,7 +569,7 @@ __PACKAGE__->register_method ({
 
        system($cmd);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -634,6 +634,7 @@ __PACKAGE__->register_method ({
        $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();
@@ -642,24 +643,29 @@ __PACKAGE__->register_method ({
 
            foreach my $disk (@{ $parsed->{disks} }) {
                my ($file, $drive) = ($disk->{backing_file}, $disk->{disk_address});
-               PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid, {
+               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;
            }
 
            # reload after disks entries have been created
            $conf = PVE::QemuConfig->load_config($vmid);
-           my $firstdisk = PVE::QemuServer::resolve_first_disk($conf);
-           $conf->{bootdisk} = $firstdisk if $firstdisk;
+           my $devs = PVE::QemuServer::get_default_bootdevices($conf);
+           $conf->{boot} = PVE::QemuServer::print_bootorder($devs);
            PVE::QemuConfig->write_config($vmid, $conf);
        };
 
-       my $err = $@;
-       if ($err) {
+       if (my $err = $@) {
            my $skiplock = 1;
-           # eval for additional safety in error path
+           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";
@@ -667,7 +673,7 @@ __PACKAGE__->register_method ({
 
        PVE::QemuConfig->remove_lock($vmid, "create");
 
-       return undef;
+       return;
 
     }
 });
@@ -697,6 +703,12 @@ __PACKAGE__->register_method({
                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'),
        },
     },
@@ -708,14 +720,28 @@ __PACKAGE__->register_method({
 
        my $vmid = $param->{vmid};
        my $sync = $param->{synchronous} // 1;
-       if (!$param->{'extra-args'} || !@{$param->{'extra-args'}}) {
-           raise_param_exc( { 'extra-args' => "No command given" });
-       }
+       my $pass_stdin = $param->{'pass-stdin'};
        if (defined($param->{timeout}) && !$sync) {
            raise_param_exc({ synchronous => "needs to be set for 'timeout'"});
        }
 
-       my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $param->{'extra-args'});
+       my $input_data = undef;
+       if ($pass_stdin) {
+           $input_data = '';
+           while (my $line = <STDIN>) {
+               $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";
+               }
+           }
+       }
+
+       my $args = $param->{'extra-args'};
+       $args = undef if !$args || !@$args;
+
+       my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $input_data, $args);
 
        if ($sync) {
            my $pid = $res->{pid};
@@ -811,7 +837,7 @@ __PACKAGE__->register_method({
            });
        }
 
-       return undef;
+       return;
     }});
 
 my $print_agent_result = sub {