[pve-devel] [RFC PATCH qemu-server v2 4/5] Add a new command line option 'ovfimport', to create VMs from an OVF manifest

Emmanuel Kasper e.kasper at proxmox.com
Tue Feb 28 09:13:45 CET 2017


Currently the following extracted paramaters are used to create a VM:
     * VM name
     * Memory
     * Number of cores
---
 PVE/CLI/qm.pm         | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 PVE/QemuServer/OVF.pm | 22 +++++++++++++++++-----
 2 files changed, 64 insertions(+), 5 deletions(-)

diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index 44439dd..12d705c 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -17,10 +17,12 @@ use PVE::SafeSyslog;
 use PVE::INotify;
 use PVE::RPCEnvironment;
 use PVE::QemuServer;
+use PVE::QemuServer::OVF;
 use PVE::API2::Qemu;
 use JSON;
 use PVE::JSONSchema qw(get_standard_option);
 use Term::ReadLine;
+use Data::Dumper;
 
 use PVE::CLIHandler;
 
@@ -432,6 +434,48 @@ __PACKAGE__->register_method ({
 	return undef;
     }});
 
+__PACKAGE__->register_method ({
+    name => 'ovfimport',
+    path => 'ovfimport',
+    description => "Create a new VM using parameters read from an OVF manifest",
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+	    dryrun => {
+		type => 'boolean',
+		description => 'Print a text representation of the extracted OVF parameters, but do not create a VM',
+		optional => 1,
+		},
+	    manifest => {
+		type => 'string'
+		}
+	},
+    },
+    returns => {
+	type => 'string',
+    },
+    code => sub {
+	my ($param) = @_;
+	my $vmid = $param->{vmid};
+	my $ovf_file = PVE::Tools::extract_param($param, 'manifest');
+	my $dryrun = PVE::Tools::extract_param($param, 'dryrun');
+
+	my $parsed = PVE::QemuServer::OVF::parse_ovf($ovf_file);
+
+	if ($dryrun) {
+	    print Dumper($parsed);
+	    exit(0);
+	}
+
+	$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});
+	$param->{node} = $nodename;
+
+	PVE::API2::Qemu->create_vm($param);
+    }
+});
 
 my $print_agent_result = sub {
     my ($data) = @_;
@@ -587,6 +631,9 @@ our $cmddef = {
     nbdstop => [ __PACKAGE__, 'nbdstop', ['vmid']],
 
     terminal => [ __PACKAGE__, 'terminal', ['vmid']],
+
+    ovfimport => [ __PACKAGE__, 'ovfimport', ['vmid']],
+
 };
 
 1;
diff --git a/PVE/QemuServer/OVF.pm b/PVE/QemuServer/OVF.pm
index 0f63891..2daeb38 100644
--- a/PVE/QemuServer/OVF.pm
+++ b/PVE/QemuServer/OVF.pm
@@ -6,11 +6,13 @@ use strict;
 use warnings;
 
 use XML::Simple;
-use PVE::Tools;
 use File::Spec;
 use File::Basename;
 use Data::Dumper;
 
+use PVE::Tools;
+use PVE::Storage::Plugin;
+
 # map OVF resource types to descriptive strings
 # this will allow us to explore the xml tree without using magic numbers
 # http://schemas.dmtf.org/wbem/cim-html/2/CIM_ResourceAllocationSettingData.html
@@ -99,8 +101,10 @@ sub parse_ovf {
     my @disks;
 
     my $ovf_name = $tree->{VirtualSystem}->{Name};
-    $ovf_name =~ s/[^a-zA-Z0-9\-]//g; # PVE::QemuServer::confdesc requires a valid DNS name
-    if (!($qm->{name} = $ovf_name)) {
+
+    if ($ovf_name) {
+	($qm->{name} = $ovf_name) =~ s/[^a-zA-Z0-9\-]//g; # PVE::QemuServer::confdesc requires a valid DNS name
+    } else {
 	warn "warning: unable to parse the VM name in this OVF manifest, generating a default value\n";
     }
 
@@ -145,6 +149,8 @@ sub parse_ovf {
 	my $controller_node;
 	my $pve_disk;
 
+	print "disk item: ", Dumper($item_node) if $debug;
+
 	# from Item, find corresponding Disk node
 	my $host_resource = $item_node->{'rasd:HostResource'};
 	my $disk_section_path;
@@ -185,7 +191,7 @@ sub parse_ovf {
 		}
 	    }
 	}
-	print "file node: ", Dumper($file_node) if $debug;
+	print "file node: ", $file_node if $debug;
 
 	# from Item, find corresponding Controller node
 	foreach my $controller (@controllers_items) {
@@ -203,9 +209,15 @@ sub parse_ovf {
 	my $backing_file = $file_node->{'ovf:href'};
 	my $backing_file_abs_path = join ('/', dirname(File::Spec->rel2abs($ovf)), $backing_file);
 
+	my $virtual_size;
+	if ( !($virtual_size = PVE::Storage::Plugin::file_size_info($backing_file_abs_path)) ) {
+		die "error parsing $backing_file_abs_path, size seems to be $virtual_size";
+	}
+
 	$pve_disk = {
 	    disk_address => $pve_disk_address,
-	    backing_file => $backing_file_abs_path
+	    backing_file => $backing_file_abs_path,
+	    virtual_size => $virtual_size
 	};
 	push @disks, $pve_disk;
 
-- 
2.1.4





More information about the pve-devel mailing list