[pve-devel] [PATCH v2 qemu-server 1/7] api2 : add migrate_vm_external

Alexandre Derumier aderumier at odiso.com
Tue Nov 20 01:23:06 CET 2018


qm migrate_external <vmid> <targetremotenode> [--targetstorage otherstorage] [--targetbridge otherbridge]
---
 PVE/API2/Qemu.pm | 79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/CLI/qm.pm    |  2 ++
 2 files changed, 81 insertions(+)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index c82e2fa..e40c6a0 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -3162,6 +3162,85 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
+    name => 'migrate_vm_external',
+    path => '{vmid}/migrate_external',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Migrate virtual machine to an external cluster. Creates a new migration task.",
+    permissions => {
+	check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+	    target => get_standard_option('pve-node', {
+		description => "Target node.",
+            }),
+            targetstorage => get_standard_option('pve-storage-id', {
+		description => "Target remote storage.",
+		optional => 1,
+            }),
+	    targetbridge => {
+		type => 'string',
+		description => "Target remote bridge.",
+		format_description => 'bridge',
+		optional => 1,
+	    },
+	},
+    },
+    returns => {
+	type => 'string',
+	description => "the task ID.",
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $rpcenv = PVE::RPCEnvironment::get();
+
+	my $authuser = $rpcenv->get_user();
+
+	die "Only root can do external migration." if $authuser ne 'root at pam';
+
+	my $target = extract_param($param, 'target');
+
+	my $vmid = extract_param($param, 'vmid');
+
+	PVE::Cluster::check_cfs_quorum();
+
+	raise_param_exc({ target => "target is member of local cluster."}) if PVE::Cluster::check_node_exists($target, 1);
+
+        die "HA must be disable for external migration." if PVE::HA::Config::vm_is_ha_managed($vmid);
+
+	my $targetip = PVE::Network::get_ip_from_hostname($target, 1);
+
+	# test if VM exists
+	my $conf = PVE::QemuConfig->load_config($vmid);
+
+	# try to detect errors early
+
+	PVE::QemuConfig->check_lock($conf);
+
+	die "VM need to be online for external migration" if !PVE::QemuServer::check_running($vmid);
+
+	$param->{online} = 1;
+	$param->{migration_external} = 1;
+
+	my $realcmd = sub {
+	    PVE::QemuMigrate->migrate($target, $targetip, $vmid, $param);
+	};
+
+	my $worker = sub {
+	    return PVE::GuestHelpers::guest_migration_lock($vmid, 10, $realcmd);
+	};
+
+	return $rpcenv->fork_worker('qmigrate', $vmid, $authuser, $worker);
+
+    }});
+
+__PACKAGE__->register_method({
     name => 'monitor',
     path => '{vmid}/monitor',
     method => 'POST',
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index eceb9b3..5aa1d48 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -850,6 +850,8 @@ our $cmddef = {
 
     migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ],
 
+    migrate_external => [ "PVE::API2::Qemu", 'migrate_vm_external', ['vmid', 'target'], { node => $nodename }, $upid_exit ],
+
     set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ],
 
     resize => [ "PVE::API2::Qemu", 'resize_vm', ['vmid', 'disk', 'size'], { node => $nodename } ],
-- 
2.11.0




More information about the pve-devel mailing list