]> 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 f99d4015c49d564de3f69e43b46005663d005049..f8972bd325f7e7004cb91d4b7b099a8465f0f7e2 100755 (executable)
@@ -61,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);
@@ -100,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 {
@@ -155,7 +155,7 @@ __PACKAGE__->register_method ({
 
        print "$cmdline\n";
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -194,7 +194,7 @@ __PACKAGE__->register_method ({
            print "status: $status\n";
        }
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -227,7 +227,7 @@ __PACKAGE__->register_method ({
 
        run_vnc_proxy($vnc_socket);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -254,7 +254,7 @@ __PACKAGE__->register_method ({
            PVE::QemuConfig->write_config($vmid, $conf);
        });
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -274,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 ({
@@ -294,7 +295,7 @@ __PACKAGE__->register_method ({
 
        if (!PVE::Cluster::check_cfs_quorum(1)) {
            print "no quorum\n";
-           return undef;
+           return;
        }
 
        my $tunnel_write = sub {
@@ -327,7 +328,7 @@ __PACKAGE__->register_method ({
            }
        }
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -367,7 +368,7 @@ __PACKAGE__->register_method ({
 
        die "wait failed - got timeout\n" if PVE::QemuServer::check_running ($vmid);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -391,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::Monitor::hmp_cmd($vmid, $input);
-           };
+           eval { print PVE::QemuServer::Monitor::hmp_cmd($vmid, $input) };
            print "ERROR: $@" if $@;
        }
 
-       return undef;
+       return;
 
     }});
 
@@ -441,7 +437,7 @@ __PACKAGE__->register_method ({
 
        PVE::QemuServer::rescan($param->{vmid}, 0, $dryrun);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -496,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 ({
@@ -573,7 +569,7 @@ __PACKAGE__->register_method ({
 
        system($cmd);
 
-       return undef;
+       return;
     }});
 
 __PACKAGE__->register_method ({
@@ -638,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();
@@ -646,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::Drive::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";
@@ -671,7 +673,7 @@ __PACKAGE__->register_method ({
 
        PVE::QemuConfig->remove_lock($vmid, "create");
 
-       return undef;
+       return;
 
     }
 });
@@ -835,7 +837,7 @@ __PACKAGE__->register_method({
            });
        }
 
-       return undef;
+       return;
     }});
 
 my $print_agent_result = sub {