[pve-devel] [PATCH v5 qemu-server 1/3] api2 : add migrate_vm_external

Alexandre Derumier aderumier at odiso.com
Tue Jan 29 02:20:38 CET 2019


qm migrate_external <vmid> <node> [--targetstorage otherstorage] [--targetvmid] [--targetkey]       
   --net[n] [,bridge=<bridge>] [,firewall=<1|0>] [,link_down=<1|0>] [,rate=<number>] [,tag=<integer>] [,trunks=<vlanid[;vlanid...]>]


- node is a ip or fqdn host from another cluster. 


OPTIONS:
- targetstorage : allow to choose a different storage name than source.e

- targetvmid : allow to choose a specific vmid. (default is auto/first available)

- targetkey : allow to choose a specific ssh key located in /etc/pve/priv/migrate_external/

- net[n] : allow to override net config 


ssh private key must exist in
/etc/pve/priv/external_migration/id_rsa_targetremotenode_fqdn_or_ip
---
 PVE/API2/Qemu.pm  | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 PVE/CLI/qm.pm     |  2 ++
 PVE/QemuServer.pm | 12 ++++++++
 3 files changed, 104 insertions(+)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index b55fd13..97bae46 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -21,6 +21,7 @@ use PVE::GuestHelpers;
 use PVE::QemuConfig;
 use PVE::QemuServer;
 use PVE::QemuMigrate;
+use PVE::QemuMigrateExternal;
 use PVE::RPCEnvironment;
 use PVE::AccessControl;
 use PVE::INotify;
@@ -3164,6 +3165,95 @@ __PACKAGE__->register_method({
     }});
 
 __PACKAGE__->register_method({
+    name => 'migrate_vm_external',
+    path => '{vmid}/migrate_external',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Experimental! Use at your own risk. Migrate virtual machine to an external cluster. Creates a new migration task.",
+    permissions => {
+	check => ['perm', '/vms/{vmid}', [ 'VM.Migrate' ]],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => PVE::QemuServer::json_migrate_external_config_properties({
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid', { completion => \&PVE::QemuServer::complete_vmid }),
+	    target => {
+		type => 'string',
+		description => "Target node fqdn or ip address.",
+            },
+	    targetstorage => get_standard_option('pve-storage-id', {
+		description => "Target remote storage.",
+		optional => 1,
+	    }),
+	    targetvmid => get_standard_option('pve-vmid', {
+		description => "Target vmid. If not specified the next available vmid will be used.",
+		optional => 1,
+	    }),
+	    targetkey => {
+		type => 'string',
+		description => "Ssh private key file located in /etc/pve/priv/migrate_external/.",
+		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');
+
+	my $targetkey = extract_param($param, 'targetkey');
+
+	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 $migration_external_sshkey = $targetkey ? "/etc/pve/priv/migrate_external/$targetkey" : "/etc/pve/priv/migrate_external/id_rsa_$target";
+
+	die "ssh privatekey is missing for $target" if !-e $migration_external_sshkey;
+
+	my $targetip = PVE::Network::get_ip_from_hostname($target, 0);
+
+	# 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_sshkey} = $migration_external_sshkey;
+
+	my $realcmd = sub {
+	    PVE::QemuMigrateExternal->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 26d4217..c271964 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -853,6 +853,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 } ],
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 1ccdccf..8acda64 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -2419,6 +2419,18 @@ sub json_config_properties {
     return $prop;
 }
 
+# add JSON properties for external migration (net override)
+sub json_migrate_external_config_properties {
+    my $prop = shift;
+
+    foreach my $opt (keys %$confdesc) {
+	next if $opt !~ m/^net(\d+)$/;
+	$prop->{$opt} = $confdesc->{$opt};
+    }
+
+    return $prop;
+}
+
 # return copy of $confdesc_cloudinit to generate documentation
 sub cloudinit_config_properties {
 
-- 
2.11.0




More information about the pve-devel mailing list