public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: "Fabian Grünbichler" <f.gruenbichler@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v4 qemu-server 11/11] qm: add remote_migrate command
Date: Thu,  3 Feb 2022 13:41:39 +0100	[thread overview]
Message-ID: <20220203124143.1931377-19-f.gruenbichler@proxmox.com> (raw)
In-Reply-To: <20220203124143.1931377-1-f.gruenbichler@proxmox.com>

which wraps the remote_migrate_vm API endpoint, but does the
precondition checks that can be done up front itself.

this now just leaves the FP retrieval and target node name lookup to the
sync part of the API endpoint, which should be do-able in <30s ..

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
new in v4

 PVE/API2/Qemu.pm |  31 -------------
 PVE/CLI/qm.pm    | 118 +++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+), 31 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index dd76c276..9f024f33 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -4119,17 +4119,6 @@ __PACKAGE__->register_method({
 	    $param->{online} = 0;
 	}
 
-	# FIXME: fork worker hear to avoid timeout? or poll these periodically
-	# in pvestatd and access cached info here? all of the below is actually
-	# checked at the remote end anyway once we call the mtunnel endpoint,
-	# we could also punt it to the client and not do it here at all..
-	my $resources = $api_client->get("/cluster/resources", { type => 'vm' });
-	if (grep { defined($_->{vmid}) && $_->{vmid} eq $target_vmid } @$resources) {
-	    raise_param_exc({ target_vmid => "Guest with ID '$target_vmid' already exists on remote cluster" });
-	}
-
-	my $storages = $api_client->get("/nodes/localhost/storage", { enabled => 1 });
-
 	my $storecfg = PVE::Storage::config();
 	my $target_storage = extract_param($param, 'target-storage');
 	my $storagemap = eval { PVE::JSONSchema::parse_idmap($target_storage, 'pve-storage-id') };
@@ -4141,26 +4130,6 @@ __PACKAGE__->register_method({
 	raise_param_exc({ 'target-bridge' => "failed to parse bridge map: $@" })
 	    if $@;
 
-	my $check_remote_storage = sub {
-	    my ($storage) = @_;
-	    my $found = [ grep { $_->{storage} eq $storage } @$storages ];
-	    die "remote: storage '$storage' does not exist!\n"
-		if !@$found;
-
-	    $found = @$found[0];
-
-	    my $content_types = [ PVE::Tools::split_list($found->{content}) ];
-	    die "remote: storage '$storage' cannot store images\n"
-		if !grep { $_ eq 'images' } @$content_types;
-	};
-
-	foreach my $target_sid (values %{$storagemap->{entries}}) {
-	    $check_remote_storage->($target_sid);
-	}
-
-	$check_remote_storage->($storagemap->{default})
-	    if $storagemap->{default};
-
 	die "remote migration requires explicit storage mapping!\n"
 	    if $storagemap->{identity};
 
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index cf0d6f3d..272eef0b 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -15,6 +15,7 @@ use POSIX qw(strftime);
 use Term::ReadLine;
 use URI::Escape;
 
+use PVE::APIClient::LWP;
 use PVE::Cluster;
 use PVE::Exception qw(raise_param_exc);
 use PVE::GuestHelpers;
@@ -158,6 +159,122 @@ __PACKAGE__->register_method ({
 	return;
     }});
 
+
+__PACKAGE__->register_method({
+    name => 'remote_migrate_vm',
+    path => 'remote_migrate_vm',
+    method => 'POST',
+    description => "Migrate virtual machine to a remote 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-vmid' => get_standard_option('pve-vmid', { optional => 1 }),
+	    'target-endpoint' => get_standard_option('proxmox-remote', {
+		description => "Remote target endpoint",
+	    }),
+	    online => {
+		type => 'boolean',
+		description => "Use online/live migration if VM is running. Ignored if VM is stopped.",
+		optional => 1,
+	    },
+	    'with-local-disks' => {
+		type => 'boolean',
+		description => "Enable live storage migration for local disk",
+		optional => 1,
+	    },
+	    delete => {
+		type => 'boolean',
+		description => "Delete the original VM and related data after successful migration. By default the original VM is kept on the source cluster in a stopped state.",
+		optional => 1,
+		default => 0,
+	    },
+            'target-storage' => get_standard_option('pve-targetstorage', {
+		completion => \&PVE::QemuServer::complete_migration_storage,
+		optional => 0,
+            }),
+	    'target-bridge' => {
+		type => 'string',
+		description => "Mapping from source to target bridges. Providing only a single bridge ID maps all source bridges to that bridge. Providing the special value '1' will map each source bridge to itself.",
+		format => 'bridge-pair-list',
+	    },
+	    bwlimit => {
+		description => "Override I/O bandwidth limit (in KiB/s).",
+		optional => 1,
+		type => 'integer',
+		minimum => '0',
+		default => 'migrate limit from datacenter or storage config',
+	    },
+	},
+    },
+    returns => {
+	type => 'string',
+	description => "the task ID.",
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $authuser = $rpcenv->get_user();
+
+	my $source_vmid = $param->{vmid};
+	my $target_endpoint = $param->{'target-endpoint'};
+	my $target_vmid = $param->{'target-vmid'} // $source_vmid;
+
+	my $remote = PVE::JSONSchema::parse_property_string('proxmox-remote', $target_endpoint);
+
+	# TODO: move this as helper somewhere appropriate?
+	my $conn_args = {
+	    protocol => 'https',
+	    host => $remote->{host},
+	    port => $remote->{port} // 8006,
+	    apitoken => $remote->{apitoken},
+	};
+
+	$conn_args->{cached_fingerprints} = { uc($remote->{fingerprint}) => 1 }
+	    if defined($remote->{fingerprint});
+
+	my $api_client = PVE::APIClient::LWP->new(%$conn_args);
+	my $resources = $api_client->get("/cluster/resources", { type => 'vm' });
+	if (grep { defined($_->{vmid}) && $_->{vmid} eq $target_vmid } @$resources) {
+	    raise_param_exc({ target_vmid => "Guest with ID '$target_vmid' already exists on remote cluster" });
+	}
+
+	my $storages = $api_client->get("/nodes/localhost/storage", { enabled => 1 });
+
+	my $storecfg = PVE::Storage::config();
+	my $target_storage = $param->{'target-storage'};
+	my $storagemap = eval { PVE::JSONSchema::parse_idmap($target_storage, 'pve-storage-id') };
+	raise_param_exc({ 'target-storage' => "failed to parse storage map: $@" })
+	    if $@;
+
+	my $check_remote_storage = sub {
+	    my ($storage) = @_;
+	    my $found = [ grep { $_->{storage} eq $storage } @$storages ];
+	    die "remote: storage '$storage' does not exist!\n"
+		if !@$found;
+
+	    $found = @$found[0];
+
+	    my $content_types = [ PVE::Tools::split_list($found->{content}) ];
+	    die "remote: storage '$storage' cannot store images\n"
+		if !grep { $_ eq 'images' } @$content_types;
+	};
+
+	foreach my $target_sid (values %{$storagemap->{entries}}) {
+	    $check_remote_storage->($target_sid);
+	}
+
+	$check_remote_storage->($storagemap->{default})
+	    if $storagemap->{default};
+
+	return PVE::API2::Qemu->remote_migrate_vm($param);
+    }});
+
 __PACKAGE__->register_method ({
     name => 'status',
     path => 'status',
@@ -900,6 +1017,7 @@ our $cmddef = {
     clone => [ "PVE::API2::Qemu", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
 
     migrate => [ "PVE::API2::Qemu", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit ],
+    remote_migrate => [ __PACKAGE__, 'remote_migrate_vm', ['vmid', 'target-vmid', 'target-endpoint'], { node => $nodename }, $upid_exit ],
 
     set => [ "PVE::API2::Qemu", 'update_vm', ['vmid'], { node => $nodename } ],
 
-- 
2.30.2





  parent reply	other threads:[~2022-02-03 12:43 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-03 12:41 [pve-devel] [PATCH v4 guest-common 0/22] remote migration Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 guest-common 1/3] migrate: handle migration_network with " Fabian Grünbichler
2022-02-04 16:37   ` [pve-devel] applied: " Thomas Lamprecht
2022-02-03 12:41 ` [pve-devel] [PATCH v4 guest-common 2/3] add tunnel helper module Fabian Grünbichler
2022-02-04 11:44   ` Fabian Ebner
2022-02-03 12:41 ` [pve-devel] [PATCH v4 guest-common 3/3] add storage tunnel module Fabian Grünbichler
2022-02-04 12:49   ` Fabian Ebner
2022-02-03 12:41 ` [pve-devel] [PATCH v4 proxmox-websocket-tunnel 1/4] initial commit Fabian Grünbichler
2022-02-04  9:38   ` [pve-devel] partially-applied-series: " Thomas Lamprecht
2022-02-03 12:41 ` [pve-devel] [PATCH v4 proxmox-websocket-tunnel 2/4] add tunnel implementation Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 proxmox-websocket-tunnel 3/4] add fingerprint validation Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 proxmox-websocket-tunnel 4/4] add packaging Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 01/11] refactor map_storage to map_id Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 02/11] schema: use pve-bridge-id Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 03/11] parse_config: optional strict mode Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 04/11] update_vm: allow simultaneous setting of boot-order and dev Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 05/11] nbd alloc helper: allow passing in explicit format Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 06/11] migrate: move tunnel-helpers to pve-guest-common Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 07/11] mtunnel: add API endpoints Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 08/11] migrate: refactor remote VM/tunnel start Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 09/11] migrate: add remote migration handling Fabian Grünbichler
2022-02-04 13:45   ` Fabian Ebner
2022-02-03 12:41 ` [pve-devel] [PATCH v4 qemu-server 10/11] api: add remote migrate endpoint Fabian Grünbichler
2022-02-03 12:41 ` Fabian Grünbichler [this message]
2022-02-04 14:03   ` [pve-devel] [PATCH v4 qemu-server 11/11] qm: add remote_migrate command Fabian Ebner
2022-02-03 12:41 ` [pve-devel] [PATCH v4 storage 1/4] volname_for_storage: parse volname before calling Fabian Grünbichler
2022-02-04 16:33   ` [pve-devel] applied: " Thomas Lamprecht
2022-02-03 12:41 ` [pve-devel] [PATCH v4 storage 2/4] storage_migrate: pull out snapshot decision Fabian Grünbichler
2022-02-04 16:33   ` [pve-devel] applied: " Thomas Lamprecht
2022-02-03 12:41 ` [pve-devel] [PATCH v4 storage 3/4] storage_migrate: pull out import/export_prepare Fabian Grünbichler
2022-02-03 12:41 ` [pve-devel] [PATCH v4 storage 4/4] add volume_import/export_start helpers Fabian Grünbichler
2022-02-04 11:38   ` Fabian Ebner
2022-02-04 14:13 ` [pve-devel] [PATCH v4 guest-common 0/22] remote migration Fabian Ebner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220203124143.1931377-19-f.gruenbichler@proxmox.com \
    --to=f.gruenbichler@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal