]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/CLI/qm.pm
qm showcmd --pretty: indent new lines by 2 spaces
[qemu-server.git] / PVE / CLI / qm.pm
index 07e3b2ef38f6567dab6c6c813fc64bcf477eefaa..564e443951bf9c5356ed4cf37195e6464036e039 100755 (executable)
@@ -18,6 +18,7 @@ use PVE::INotify;
 use PVE::RPCEnvironment;
 use PVE::QemuServer;
 use PVE::QemuServer::ImportDisk;
+use PVE::QemuServer::OVF;
 use PVE::API2::Qemu;
 use JSON;
 use PVE::JSONSchema qw(get_standard_option);
@@ -116,6 +117,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'},
@@ -123,7 +130,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;
     }});
@@ -265,15 +276,34 @@ __PACKAGE__->register_method ({
            return undef;
        }
 
-       print "tunnel online\n";
-       *STDOUT->flush();
+       my $tunnel_write = sub {
+           my $text = shift;
+           chomp $text;
+           print "$text\n";
+           *STDOUT->flush();
+       };
 
-       print "ver 1\n";
-       *STDOUT->flush();
+       $tunnel_write->("tunnel online");
+       $tunnel_write->("ver 1");
 
        while (my $line = <>) {
            chomp $line;
-           last if $line =~ m/^quit$/;
+           if ($line =~ /^quit$/) {
+               $tunnel_write->("OK");
+               last;
+           } elsif ($line =~ /^resume (\d+)$/) {
+               my $vmid = $1;
+               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;
@@ -451,7 +481,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'},
@@ -460,6 +496,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};
@@ -481,15 +535,110 @@ __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;
     }});
 
+__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) = @_;
+
+       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');
+
+       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);
+
+       my $parsed = PVE::QemuServer::OVF::parse_ovf($ovf_file);
+
+       if ($dryrun) {
+           print to_json($parsed, { pretty => 1, canonical => 1});
+           return;
+       }
+
+       $param->{name} = $parsed->{qm}->{name} if defined($parsed->{qm}->{name});
+       $param->{memory} = $parsed->{qm}->{memory} if defined($parsed->{qm}->{memory});
+       $param->{cores} = $parsed->{qm}->{cores} if defined($parsed->{qm}->{cores});
+
+       my $importfn = sub {
+
+           PVE::Cluster::check_vmid_unused($vmid);
+
+           my $conf = $param;
+
+           eval {
+               # order matters, as do_import() will load_config() internally
+               $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});
+                   PVE::QemuServer::ImportDisk::do_import($file, $vmid, $storeid,
+                       { drive_name => $drive, format => $format });
+               }
+
+               # reload after disks entries have been created
+               $conf = PVE::QemuConfig->load_config($vmid);
+               PVE::QemuConfig->check_lock($conf);
+               my $firstdisk = PVE::QemuServer::resolve_first_disk($conf);
+               $conf->{bootdisk} = $firstdisk if $firstdisk;
+               PVE::QemuConfig->write_config($vmid, $conf);
+           };
+
+           my $err = $@;
+           if ($err) {
+               my $skiplock = 1;
+               eval { PVE::QemuServer::vm_destroy($storecfg, $vmid, $skiplock); };
+               die "import failed - $err";
+           }
+       };
+
+       my $wait_for_lock = 1;
+       PVE::QemuConfig->lock_config_full($vmid, $wait_for_lock, $importfn);
+
+       return undef;
+
+    }
+});
 
 my $print_agent_result = sub {
     my ($data) = @_;
@@ -647,6 +796,9 @@ our $cmddef = {
     terminal => [ __PACKAGE__, 'terminal', ['vmid']],
 
     importdisk => [ __PACKAGE__, 'importdisk', ['vmid', 'source', 'storage']],
+
+    importovf => [ __PACKAGE__, 'importovf', ['vmid', 'manifest', 'storage']],
+
 };
 
 1;