[pve-devel] [PATCH 15/16] add qm move (storage migration)

Alexandre Derumier aderumier at odiso.com
Mon Jan 21 10:48:10 CET 2013


qm move <vmid> <disk> <storage> --format [raw|qcow2|vmdk]

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

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 633245c..d186d97 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -135,7 +135,7 @@ my $create_disks = sub {
 };
 
 my $clone_disks = sub {
-    my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $snap, $mode, $clonefrom, $running) = @_;
+    my ($rpcenv, $authuser, $conf, $storecfg, $vmid, $pool, $settings, $snap, $mode, $clonefrom, $running, $onlysettings) = @_;
 
     my $vollist = [];
     my $voliddst = undef;
@@ -144,6 +144,8 @@ my $clone_disks = sub {
     PVE::QemuServer::foreach_drive($conf, sub {
 	my ($ds, $disk) = @_;
 
+	return if ($onlysettings && !$settings->{$ds});
+
 	my $volid = $disk->{file};
 	if (PVE::QemuServer::drive_is_cdrom($disk)) {
 	    $res->{$ds} = PVE::QemuServer::print_drive($vmid, $disk);
@@ -605,6 +607,7 @@ __PACKAGE__->register_method({
 	    { subdir => 'vncproxy' },
 	    { subdir => 'migrate' },
 	    { subdir => 'resize' },
+	    { subdir => 'move' },
 	    { subdir => 'rrd' },
 	    { subdir => 'rrddata' },
 	    { subdir => 'monitor' },
@@ -2115,6 +2118,119 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
+    name => 'move_vm',
+    path => '{vmid}/move',
+    method => 'PUT',
+    protected => 1,
+    proxyto => 'node',
+    description => "Move volume to different storage.",
+    permissions => {
+        check => ['perm', '/vms/{vmid}', [ 'VM.Config.Disk' ]],
+    },
+    parameters => {
+        additionalProperties => 0,
+        properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid'),
+	    skiplock => get_standard_option('skiplock'),
+	    disk => {
+		type => 'string',
+		description => "The disk you want to resize.",
+		enum => [PVE::QemuServer::disknames()],
+	    },
+	    storage => {
+		type => 'string',
+		description => "Target 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 => 'null'},
+    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 $disk = extract_param($param, 'disk');
+ 
+	my $storeid = extract_param($param, 'storage');
+
+	my $format = extract_param($param, 'format');
+
+	my $skiplock = extract_param($param, 'skiplock');
+        raise_param_exc({ skiplock => "Only root may use this option." })
+            if $skiplock && $authuser ne 'root at pam';
+
+        my $storecfg = PVE::Storage::config();
+
+        my $updatefn =  sub {
+
+            my $conf = PVE::QemuServer::load_config($vmid);
+
+            die "checksum missmatch (file change by other user?)\n"
+                if $digest && $digest ne $conf->{digest};
+            PVE::QemuServer::check_lock($conf) if !$skiplock;
+
+
+	    die "disk '$disk' does not exist\n" if !$conf->{$disk};
+
+            my $drive = PVE::QemuServer::parse_drive($disk, $conf->{$disk});
+
+            my $volid = $drive->{file};
+
+            die "disk '$disk' has no associated volume\n" if !$volid;
+
+            die "you can't move a cdrom\n" if PVE::QemuServer::drive_is_cdrom($drive);
+	    my $oldfmt = undef;
+            my ($oldstoreid, $oldvolname) = PVE::Storage::parse_volume_id($volid);
+            if ($oldvolname =~ m/\.(raw|qcow2|vmdk)$/){
+                    $oldfmt = $1;
+            }
+
+	    die "you can't move on the same storage with same format" if ($oldstoreid eq $storeid && (!$format || $oldfmt eq $format));
+	   
+	    $rpcenv->check($authuser, "/storage/$storeid", ['Datastore.AllocateSpace']);
+
+            PVE::Cluster::log_msg('info', $authuser, "move disk VM $vmid: move --disk $disk --storage $storeid");
+	    my $setting->{$disk} = $storeid.':';
+	    $setting->{$disk} .= $format if $format;
+
+	    my $running = PVE::QemuServer::check_running($vmid);
+	    my $realcmd = sub {
+
+		&$clone_disks($rpcenv, $authuser, $conf, $storecfg, $vmid, undef, $setting, undef, 'copy', $vmid, $running, 1);
+		PVE::QemuServer::update_config_nolock($vmid, $conf, 1);
+
+		eval { PVE::Storage::vdisk_free($storecfg, $volid); };
+		warn $@ if $@;
+	    };
+
+	    return $rpcenv->fork_worker('qmmove', $vmid, $authuser, $realcmd);
+	};
+        PVE::QemuServer::lock_config($vmid, $updatefn);
+        return undef;
+    }});
+
+__PACKAGE__->register_method({
     name => 'snapshot_list',
     path => '{vmid}/snapshot',
     method => 'GET',
diff --git a/qm b/qm
index 93f621c..21ec645 100755
--- a/qm
+++ b/qm
@@ -354,6 +354,8 @@ my $cmddef = {
 
     resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
 
+    move => [ "PVE::API2::Qemu", 'move_vm', ['vmid', 'disk', 'storage'], { node => $nodename } ],
+
     unlink => [ "PVE::API2::Qemu", 'unlink', ['vmid', 'idlist'], { node => $nodename } ],
 
     config => [ "PVE::API2::Qemu", 'vm_config', ['vmid'], 
-- 
1.7.10.4




More information about the pve-devel mailing list