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 v5 container 1/3] fix #1532: add target-storage support to migration
Date: Wed,  9 Feb 2022 14:07:31 +0100	[thread overview]
Message-ID: <20220209130750.902245-3-f.gruenbichler@proxmox.com> (raw)
In-Reply-To: <20220209130750.902245-1-f.gruenbichler@proxmox.com>

re-using helpers that already exist for qemu-server. this is a
pre-requisite for extending remote migration support to containers.

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

Notes:
    new in v5, no GUI yet until possible wrinkles are ironed out
    
    requires pve-common/pve-guest-common with changes from this series

 src/PVE/API2/LXC.pm    | 32 ++++++++++++++++++++++
 src/PVE/LXC/Migrate.pm | 62 ++++++++++++++++++++++++++++++++----------
 2 files changed, 79 insertions(+), 15 deletions(-)

diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 7573814..61eaaf7 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -36,6 +36,18 @@ BEGIN {
     }
 }
 
+my $check_storage_access_migrate = sub {
+    my ($rpcenv, $authuser, $storecfg, $storage, $node) = @_;
+
+    PVE::Storage::storage_check_enabled($storecfg, $storage, $node);
+
+    $rpcenv->check($authuser, "/storage/$storage", ['Datastore.AllocateSpace']);
+
+    my $scfg = PVE::Storage::storage_config($storecfg, $storage);
+    die "storage '$storage' does not support CT rootdirs\n"
+	if !$scfg->{content}->{rootdir};
+};
+
 __PACKAGE__->register_method ({
     subclass => "PVE::API2::LXC::Config",
     path => '{vmid}/config',
@@ -1091,6 +1103,7 @@ __PACKAGE__->register_method({
 		description => "Target node.",
 		completion => \&PVE::Cluster::complete_migration_target,
 	    }),
+	    'target-storage' => get_standard_option('pve-targetstorage'),
 	    online => {
 		type => 'boolean',
 		description => "Use online/live migration.",
@@ -1149,6 +1162,25 @@ __PACKAGE__->register_method({
 		if !$param->{online} && !$param->{restart};
 	}
 
+	if (my $targetstorage = delete $param->{'target-storage'}) {
+	    my $storecfg = PVE::Storage::config();
+	    my $storagemap = eval { PVE::JSONSchema::parse_idmap($targetstorage, 'pve-storage-id') };
+	    raise_param_exc({ targetstorage => "failed to parse storage map: $@" })
+		if $@;
+
+	    $rpcenv->check_vm_perm($authuser, $vmid, undef, ['VM.Config.Disk'])
+		if !defined($storagemap->{identity});
+
+	    foreach my $target_sid (values %{$storagemap->{entries}}) {
+		$check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $target_sid, $target);
+	    }
+
+	    $check_storage_access_migrate->($rpcenv, $authuser, $storecfg, $storagemap->{default}, $target)
+		if $storagemap->{default};
+
+	    $param->{storagemap} = $storagemap;
+	}
+
 	if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
 
 	    my $hacmd = sub {
diff --git a/src/PVE/LXC/Migrate.pm b/src/PVE/LXC/Migrate.pm
index 95562e4..c85a09c 100644
--- a/src/PVE/LXC/Migrate.pm
+++ b/src/PVE/LXC/Migrate.pm
@@ -64,7 +64,8 @@ sub prepare {
 
 	# check if storage is available on both nodes
 	my $scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $storage);
-	PVE::Storage::storage_check_enabled($self->{storecfg}, $storage, $self->{node});
+
+	my $targetsid = $storage;
 
 	die "content type 'rootdir' is not available on storage '$storage'\n"
 	    if !$scfg->{content}->{rootdir};
@@ -78,8 +79,14 @@ sub prepare {
 	    # unless in restart mode because we shut the container down
 	    die "unable to migrate local mount point '$volid' while CT is running"
 		if $running && !$restart;
+
+	    $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $storage);
 	}
 
+	my $target_scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node});
+
+	die "$volid: content type 'rootdir' is not available on storage '$targetsid'\n"
+	    if !$target_scfg->{content}->{rootdir};
     });
 
     # todo: test if VM uses local resources
@@ -135,18 +142,27 @@ sub phase1 {
 
 	my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
 
-	# check if storage is available on both nodes
+	# check if storage is available on source node
 	my $scfg = PVE::Storage::storage_check_enabled($self->{storecfg}, $sid);
-	PVE::Storage::storage_check_enabled($self->{storecfg}, $sid, $self->{node});
+
+	my $targetsid = $sid;
 
 	if ($scfg->{shared}) {
 	    $self->log('info', "volume '$volid' is on shared storage '$sid'")
 		if !$snapname;
 	    return;
+	} else {
+	    $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $sid);
 	}
 
+	PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node});
+
+	my $bwlimit = $self->get_bwlimit($sid, $targetsid);
+
 	$volhash->{$volid}->{ref} = defined($snapname) ? 'snapshot' : 'config';
 	$volhash->{$volid}->{snapshots} = 1 if defined($snapname);
+	$volhash->{$volid}->{targetsid} = $targetsid;
+	$volhash->{$volid}->{bwlimit} = $bwlimit;
 
 	my ($path, $owner) = PVE::Storage::path($self->{storecfg}, $volid);
 
@@ -194,7 +210,8 @@ sub phase1 {
 	next if @{$dl->{$storeid}} == 0;
 
 	# check if storage is available on target node
-	PVE::Storage::storage_check_enabled($self->{storecfg}, $storeid, $self->{node});
+	my $targetsid = PVE::JSONSchema::map_id($self->{opts}->{storagemap}, $storeid);
+	PVE::Storage::storage_check_enabled($self->{storecfg}, $targetsid, $self->{node});
 
 	die "content type 'rootdir' is not available on storage '$storeid'\n"
 	    if !$scfg->{content}->{rootdir};
@@ -275,25 +292,38 @@ sub phase1 {
 	next if $rep_volumes->{$volid};
 	my ($sid, $volname) = PVE::Storage::parse_volume_id($volid);
 	push @{$self->{volumes}}, $volid;
-	my $bwlimit = PVE::Storage::get_bandwidth_limit('migration', [$sid], $opts->{bwlimit});
+
 	# JSONSchema and get_bandwidth_limit use kbps - storage_migrate bps
+	my $bwlimit = $volhash->{$volid}->{bwlimit};
 	$bwlimit = $bwlimit * 1024 if defined($bwlimit);
 
-	my $storage_migrate_opts = {
-	    'ratelimit_bps' => $bwlimit,
-	    'insecure' => $opts->{migration_type} eq 'insecure',
-	    'with_snapshots' => $volhash->{$volid}->{snapshots},
+	my $targetsid = $volhash->{$volid}->{targetsid};
+
+	my $new_volid = eval {
+	    my $storage_migrate_opts = {
+		'ratelimit_bps' => $bwlimit,
+		'insecure' => $opts->{migration_type} eq 'insecure',
+		'with_snapshots' => $volhash->{$volid}->{snapshots},
+	    };
+
+	    my $logfunc = sub { $self->log('info', $_[0]); };
+	    return PVE::Storage::storage_migrate(
+		$self->{storecfg},
+		$volid,
+		$self->{ssh_info},
+		$targetsid,
+		$storage_migrate_opts,
+		$logfunc,
+	    );
 	};
 
-	my $logfunc = sub { $self->log('info', $_[0]); };
-	eval {
-	    PVE::Storage::storage_migrate($self->{storecfg}, $volid, $self->{ssh_info},
-					  $sid, $storage_migrate_opts, $logfunc);
-	};
 	if (my $err = $@) {
-	    die "storage migration for '$volid' to storage '$sid' failed - $err\n";
+	    die "storage migration for '$volid' to storage '$targetsid' failed - $err\n";
 	}
 
+	$self->{volume_map}->{$volid} = $new_volid;
+	$self->log('info', "volume '$volid' is '$new_volid' on the target\n");
+
 	eval { PVE::Storage::deactivate_volumes($self->{storecfg}, [$volid]); };
 	if (my $err = $@) {
 	    $self->log('warn', $err);
@@ -316,6 +346,8 @@ sub phase1 {
 
     # transfer replication state before moving config
     $self->transfer_replication_state() if $rep_volumes;
+    PVE::LXC::Config->update_volume_ids($conf, $self->{volume_map});
+    PVE::LXC::Config->write_config($vmid, $conf);
     PVE::LXC::Config->move_config_to_node($vmid, $self->{node});
     $self->{conf_migrated} = 1;
     $self->switch_replication_job_target() if $rep_volumes;
-- 
2.30.2





  parent reply	other threads:[~2022-02-09 13:07 UTC|newest]

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-09 13:07 [pve-devel] [PATCH-SERIES 0/21] remote migration Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 common 1/1] add 'map_id' helper for ID maps Fabian Grünbichler
2022-02-09 13:07 ` Fabian Grünbichler [this message]
2022-02-10 11:52   ` [pve-devel] [PATCH v5 container 1/3] fix #1532: add target-storage support to migration Fabian Ebner
2022-02-11  8:33     ` Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 container 2/3] config: add strict parser Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH PoC v5 container 3/3] migration: add remote migration Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 guest-common 1/3] migrate: add get_bwlimit helper Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 guest-common 2/3] add tunnel helper module Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 guest-common 3/3] add storage tunnel module Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 01/11] move map_storage to PVE::JSONSchema::map_id Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 02/11] schema: use pve-bridge-id Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 03/11] parse_config: optional strict mode Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 04/11] update_vm: allow simultaneous setting of boot-order and dev Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 05/11] nbd alloc helper: allow passing in explicit format Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 06/11] migrate: move tunnel-helpers to pve-guest-common Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 07/11] mtunnel: add API endpoints Fabian Grünbichler
2022-02-11 13:01   ` Fabian Ebner
     [not found]     ` <<0b8626f8-df25-05a6-3db3-698591688eab@proxmox.com>
2022-02-16 12:57       ` Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 08/11] migrate: refactor remote VM/tunnel start Fabian Grünbichler
2022-02-11 13:01   ` Fabian Ebner
     [not found]     ` <<ce49d9a8-03b6-01ed-ad01-5cc500bfba19@proxmox.com>
2022-02-16 12:58       ` Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 09/11] migrate: add remote migration handling Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 10/11] api: add remote migrate endpoint Fabian Grünbichler
2022-02-11 13:01   ` Fabian Ebner
     [not found]     ` <<e5069cdd-7a84-9664-2dea-1ac3e68e339c@proxmox.com>
2022-02-16 12:58       ` Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 qemu-server 11/11] qm: add remote-migrate command Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 storage 1/3] storage_migrate_snapshot: skip for btrfs without snapshots Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 storage 2/3] storage_migrate: pull out import/export_prepare Fabian Grünbichler
2022-02-09 13:07 ` [pve-devel] [PATCH v5 storage 3/3] add volume_import/export_start helpers Fabian Grünbichler
2022-02-09 17:56 ` [pve-devel] [PATCH-SERIES 0/21] remote migration Thomas Lamprecht
2022-02-11 10:38 ` [pve-devel] [PATCH qemu-server follow-up] schema: move 'pve-targetstorage' to pve-common Fabian Grünbichler
2022-02-11 10:38   ` [pve-devel] [PATCH common follow-up] schema: take over 'pve-targetstorage' option Fabian Grünbichler
2022-02-11 11:31   ` [pve-devel] [PATCH qemu-server follow-up] schema: move 'pve-targetstorage' to pve-common Fabian Ebner
2022-02-11 13:08 ` [pve-devel] [PATCH-SERIES 0/21] 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=20220209130750.902245-3-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