[pve-devel] [PATCH 1/2] add qm import_disk

Alexandre Derumier aderumier at odiso.com
Thu Jan 19 07:17:30 CET 2017


This add a new method to import external disk image in a vm (as ununsed).

It very usefull to import vmware,xen,hyperv disk images in a vm (any destination storage, can be zfs,lvm,rbd...)

Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 PVE/API2/Qemu.pm  | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/CLI/qm.pm     |   2 +
 PVE/QemuServer.pm |   3 +-
 3 files changed, 124 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 33b8f5a..1f67c5d 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -2684,6 +2684,126 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
+    name => 'import_vm_disk',
+    path => '{vmid}/import_disk',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Import external volume.",
+    permissions => {
+	description => "You need 'VM.Config.Disk' permissions on /vms/{vmid}, and 'Datastore.AllocateSpace' permissions on the storage.",
+	check => [ 'and',
+		   ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
+		   ['perm', '/storage/{storage}', [ 'Datastore.AllocateSpace' ]],
+	    ],
+    },
+    parameters => {
+        additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+	    image => {
+	        type => 'string',
+		description => "The disk image you want to import.",
+	    },
+            storage => get_standard_option('pve-storage-id', {
+		description => "Target storage.",
+		completion => \&PVE::QemuServer::complete_storage,
+            }),
+            'format' => {
+                type => 'string',
+                description => "Target Format.",
+                enum => [ 'raw', 'qcow2', 'vmdk' ],
+                optional => 1,
+            },
+	    digest => {
+		type => 'string',
+		description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
+		maxLength => 40,
+		optional => 1,
+	    },
+	},
+    },
+    returns => {
+	type => 'string',
+	description => "the task ID.",
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $rpcenv = PVE::RPCEnvironment::get();
+
+	my $authuser = $rpcenv->get_user();
+
+	my $node = extract_param($param, 'node');
+
+	my $vmid = extract_param($param, 'vmid');
+
+	my $digest = extract_param($param, 'digest');
+
+	my $image = extract_param($param, 'image');
+
+	my $storeid = extract_param($param, 'storage');
+
+	my $format = extract_param($param, 'format');
+
+	my $storecfg = PVE::Storage::config();
+
+	my $drive = {};
+	$drive->{file} = $image;
+
+	my $updatefn =  sub {
+
+	    my $conf = PVE::QemuConfig->load_config($vmid);
+
+	    PVE::QemuConfig->check_lock($conf);
+
+	    die "checksum missmatch (file change by other user?)\n"
+		if $digest && $digest ne $conf->{digest};
+
+	    die "disk image '$image' does not exist\n" if !-e $image;
+
+	    PVE::Cluster::log_msg('info', $authuser, "import disk image VM $vmid: import --image $image --storage $storeid");
+
+	    my $realcmd = sub {
+
+		my $newvollist = [];
+
+		eval {
+		    local $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = sub { die "interrupted by signal\n"; };
+
+
+		    my $newdrive = PVE::QemuServer::clone_disk($storecfg, $vmid, undef, "", $drive, undef,
+							       $vmid, $storeid, $format, 1, $newvollist);
+
+		    PVE::QemuConfig->add_unused_volume($conf, $newdrive->{file});
+
+		    PVE::QemuConfig->write_config($vmid, $conf);
+
+		    eval {
+			# try to deactivate volumes - avoid lvm LVs to be active on several nodes
+			PVE::Storage::deactivate_volumes($storecfg, [ $newdrive->{file} ]);
+		    };
+		    warn $@ if $@;
+		};
+		if (my $err = $@) {
+
+                   foreach my $volid (@$newvollist) {
+                        eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+                        warn $@ if $@;
+                    }
+		    die "import disk image failed: $err";
+                }
+
+	    };
+
+            return $rpcenv->fork_worker('qmimportdisk', $vmid, $authuser, $realcmd);
+	};
+
+	return PVE::QemuConfig->lock_config($vmid, $updatefn);
+    }});
+
+__PACKAGE__->register_method({
     name => 'migrate_vm',
     path => '{vmid}/migrate',
     method => 'POST',
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index 44439dd..16e5d9c 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -490,6 +490,8 @@ our $cmddef = {
 
     move_disk => [ "PVE::API2::Qemu", 'move_vm_disk', ['vmid', 'disk', 'storage'], { node => $nodename }, $upid_exit ],
 
+    import_disk => [ "PVE::API2::Qemu", 'import_vm_disk', ['vmid', 'image', 'storage'], { node => $nodename }, $upid_exit ],
+
     unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid'], { node => $nodename } ],
 
     config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'],
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 07c0c05..b86a470 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -6137,7 +6137,8 @@ sub clone_disk {
 	push @$newvollist, $newvolid;
     } else {
 
-	my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file});
+	my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
+	$volname = $drive->{file} if !$storeid;
 	$storeid = $storage if $storage;
 
 	my ($defFormat, $validFormats) = PVE::Storage::storage_default_format($storecfg, $storeid);
-- 
2.1.4




More information about the pve-devel mailing list