]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/CLI/qm.pm
qm rescan: add dryrun option
[qemu-server.git] / PVE / CLI / qm.pm
index 0e17cc9671de177c0cc95910dccbd2c8302e4d37..cf26943ce3709f1a6d73f57a091658bcc86fc38f 100755 (executable)
@@ -10,6 +10,7 @@ use Fcntl ':flock';
 use File::Path;
 use IO::Socket::UNIX;
 use IO::Select;
+use URI::Escape;
 
 use PVE::Tools qw(extract_param);
 use PVE::Cluster;
@@ -20,6 +21,7 @@ use PVE::QemuServer;
 use PVE::QemuServer::ImportDisk;
 use PVE::QemuServer::OVF;
 use PVE::API2::Qemu;
+use PVE::API2::Qemu::Agent;
 use JSON;
 use PVE::JSONSchema qw(get_standard_option);
 use Term::ReadLine;
@@ -117,6 +119,12 @@ __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,
+           }
        },
     },
     returns => { type => 'null'},
@@ -124,7 +132,11 @@ __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});
+
+       $cmdline =~ s/ -/ \\\n  -/g if $param->{pretty};
+
+       print "$cmdline\n";
 
        return undef;
     }});
@@ -276,7 +288,7 @@ __PACKAGE__->register_method ({
        $tunnel_write->("tunnel online");
        $tunnel_write->("ver 1");
 
-       while (my $line = <>) {
+       while (my $line = <STDIN>) {
            chomp $line;
            if ($line =~ /^quit$/) {
                $tunnel_write->("OK");
@@ -392,13 +404,23 @@ __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 conifg.',
+           },
        },
     },
     returns => { type => 'null'},
     code => sub {
        my ($param) = @_;
 
-       PVE::QemuServer::rescan($param->{vmid});
+       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 undef;
     }});
@@ -471,7 +493,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'},
@@ -480,6 +508,24 @@ __PACKAGE__->register_method ({
 
        my $vmid = $param->{vmid};
 
+       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};
@@ -501,9 +547,9 @@ __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);
 
@@ -599,7 +645,9 @@ __PACKAGE__->register_method ({
        };
 
        my $wait_for_lock = 1;
-       return PVE::QemuConfig->lock_config_full($vmid, $wait_for_lock, $importfn);
+       PVE::QemuConfig->lock_config_full($vmid, $wait_for_lock, $importfn);
+
+       return undef;
 
     }
 });
@@ -626,6 +674,21 @@ my $print_agent_result = sub {
     print to_json($result, { pretty => 1, canonical => 1});
 };
 
+sub param_mapping {
+    my ($name) = @_;
+
+    my $ssh_key_map = ['sshkeys', sub {
+       return URI::Escape::uri_escape(PVE::Tools::file_get_contents($_[0]));
+    }];
+    my $cipassword_map = PVE::CLIHandler::get_standard_mapping('pve-password', { name => 'cipassword' });
+    my $mapping = {
+       'update_vm' => [$ssh_key_map, $cipassword_map],
+       'create_vm' => [$ssh_key_map, $cipassword_map],
+    };
+
+    return $mapping->{$name};
+}
+
 our $cmddef = {
     list => [ "PVE::API2::Qemu", 'vmlist', [],
             { node => $nodename }, sub {
@@ -750,7 +813,7 @@ our $cmddef = {
 
     monitor  => [ __PACKAGE__, 'monitor', ['vmid']],
 
-    agent  => [ "PVE::API2::Qemu", 'agent', ['vmid', 'command'],
+    agent  => [ "PVE::API2::Qemu::Agent", 'agent', ['vmid', 'command'],
                { node => $nodename }, $print_agent_result ],
 
     mtunnel => [ __PACKAGE__, 'mtunnel', []],