From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH guest-common v3 13/38] migrate: add shared helpers for the identity command of the migration tunnel
Date: Wed, 18 Nov 2026 05:37:25 +0100 [thread overview]
Message-ID: <20261118043817.1659212-14-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261118043817.1659212-1-t.lamprecht@proxmox.com>
The source and the target of a remote migration exchange the uid of the
guest with the 'identity' tunnel command. VMs and containers would each
define the command, the rule that the target accepts it only once and
only before the config arrives, and the sender with its timeout and log
line, which all have to stay in sync with the next tunnel change.
Provide the parameter schema and the handler of the target in
PVE::GuestIdentity, as PVE::StorageTunnel does for the storage commands,
and the sender in PVE::AbstractMigrate, next to the sender of the
'bwlimit' command. The wire format stays the same.
If the parsed config of the source has no identity, the sender writes it
first. A guest without uid gets one that way, so the source keeps the
uid that it sends, instead of getting another one with a later write.
The write also restores a stored uid that the parser of the guest type
dropped, for example for a 'meta' member it does not know. Callers send
the command only after the version check of the tunnel, so that a
refused target leaves the source config unchanged.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
changes v2 -> v3:
* new in v3: command schema, target handler and sender moved here from
qemu-server and container
* the sender gives a guest without uid one before it sends it, and sends
the stored uid if the parser dropped 'meta'
src/PVE/AbstractMigrate.pm | 30 +++++
src/PVE/GuestIdentity.pm | 39 ++++++
src/tests/config-identity-tests.pl | 208 +++++++++++++++++++++++++++++
3 files changed, 277 insertions(+)
diff --git a/src/PVE/AbstractMigrate.pm b/src/PVE/AbstractMigrate.pm
index 919f12d..4375cb0 100644
--- a/src/PVE/AbstractMigrate.pm
+++ b/src/PVE/AbstractMigrate.pm
@@ -8,6 +8,7 @@ use POSIX qw(strftime);
use PVE::Cluster;
use PVE::DataCenterConfig;
+use PVE::GuestIdentity;
use PVE::ReplicationState;
use PVE::SSHInfo;
use PVE::Tools;
@@ -384,6 +385,35 @@ sub get_bwlimit {
return $bwlimit;
}
+# Lets the target of a remote migration take over the uid of the guest, whose config class is
+# $config_class, through the tunnel $tunnel, before anything gets transferred there. The target
+# refuses a uid that another guest there has, unless the migration allows a new uid, see
+# PVE::GuestIdentity::handle_tunnel_identity. Call it only after the version check of the tunnel,
+# so that a target that gets refused leaves the config of the source unchanged.
+sub take_over_identity_on_target {
+ my ($self, $config_class, $tunnel) = @_;
+
+ my $conf = $self->{vmconf};
+ my $identity = $config_class->get_identity($conf);
+ if (!$identity) {
+ # A guest without uid gets one first, so the source keeps the uid that it sends. The write
+ # also restores a stored uid that the parser of the guest type dropped.
+ $config_class->write_config($self->{vmid}, $conf);
+ $identity = $config_class->get_identity($conf) or return;
+ }
+
+ my $params = {
+ uid => $identity->{uid},
+ 'allow-new-uid' => $self->{opts}->{'allow-new-uid'} ? 1 : 0,
+ };
+
+ # the target waits for the cluster-wide guest identity lock
+ my $res = PVE::Tunnel::write_tunnel($tunnel, 60, 'identity', $params);
+ $self->log(PVE::GuestIdentity::remote_adopt_result_log($res->{identity})) if $res->{identity};
+
+ return;
+}
+
sub map_storage {
my ($self, $scfg, $storeid) = @_;
diff --git a/src/PVE/GuestIdentity.pm b/src/PVE/GuestIdentity.pm
index 8474c88..9c2de3d 100644
--- a/src/PVE/GuestIdentity.pm
+++ b/src/PVE/GuestIdentity.pm
@@ -412,6 +412,45 @@ sub remote_adopt_result_log($res) {
return ('info', $message);
}
+# Returns the properties of the parameters of the 'identity' command of the migration tunnel, see
+# handle_tunnel_identity. Every call returns a new copy.
+sub tunnel_identity_cmd_schema() {
+ return {
+ uid => {
+ type => 'string',
+ description => 'uid of the source guest',
+ },
+ 'allow-new-uid' => {
+ type => 'boolean',
+ description => 'go on with a new uid if another guest has the uid',
+ optional => 1,
+ default => 0,
+ },
+ };
+}
+
+# Handles the 'identity' command with the parameters $params on the target of a remote migration of
+# a guest of the config class $class: takes over the uid of the source guest with adopt_from_source
+# for the guest that the tunnel reserved with $reservation, and returns the reply for the source.
+# $state is the state of the tunnel, with the VMID in 'vmid' and the lock of the guest in 'lock'.
+# The command is only accepted once, and only before the config arrives, while the guest still has
+# the 'create' lock of the reservation.
+sub handle_tunnel_identity($class, $state, $reservation, $params) {
+ die "the uid can only be taken over once, before the config arrives\n"
+ if ($state->{lock} // '') ne 'create' || $state->{'identity-handled'};
+ $state->{'identity-handled'} = 1;
+
+ my $res = adopt_from_source(
+ $class,
+ $state->{vmid},
+ $params->{uid},
+ $reservation,
+ $params->{'allow-new-uid'},
+ );
+
+ return { identity => $res };
+}
+
# Returns undef if $node runs a version of Proxmox VE that keeps the identity when writing guest
# configs, see MIN_PVE_MANAGER_VERSION. Missing or invalid version information counts as too old.
# Otherwise returns a hash with 'required-version', the minimum version, and 'version', the version
diff --git a/src/tests/config-identity-tests.pl b/src/tests/config-identity-tests.pl
index 62e8692..ae8b75d 100755
--- a/src/tests/config-identity-tests.pl
+++ b/src/tests/config-identity-tests.pl
@@ -11,8 +11,10 @@ use Test::MockModule;
use Test::More;
use PVE::AbstractConfig;
+use PVE::AbstractMigrate;
use PVE::GuestHelpers;
use PVE::GuestIdentity;
+use PVE::JSONSchema;
my $tmpdir = tempdir('config-identity-XXXXXX', DIR => '.', CLEANUP => 1);
$PVE::GuestHelpers::lockdir = "$tmpdir/lock";
@@ -1180,4 +1182,210 @@ subtest 'drop pending meta' => sub {
}
};
+# Runs $code with the output to STDOUT captured, returns the error and the output.
+my sub capture_output($code) {
+ my $output = '';
+ open(my $fh, '>', \$output) or die "unable to capture output - $!\n";
+ my $previous_fh = select($fh);
+ eval { $code->() };
+ my $err = $@;
+ select($previous_fh);
+ close($fh);
+
+ return ($err, $output);
+}
+
+subtest 'tunnel identity command on the target' => sub {
+ my $class = 'PVE::TestConfig';
+
+ # validates the parameters like the tunnel endpoints do, returns the error, output and reply
+ my $handle = sub($state, $reservation, $params) {
+ my $schema = {
+ type => 'object',
+ properties => PVE::GuestIdentity::tunnel_identity_cmd_schema(),
+ additionalProperties => 0,
+ };
+ my $reply;
+ my ($err, $output) = capture_output(sub {
+ PVE::JSONSchema::validate($params, $schema);
+ $reply =
+ PVE::GuestIdentity::handle_tunnel_identity($class, $state, $reservation, $params);
+ });
+ return ($err, $output, $reply);
+ };
+ my $once = "the uid can only be taken over once, before the config arrives\n";
+
+ my $uid = new_uid();
+ my $reservation = $class->create_and_lock_config(600);
+ my $state = { vmid => 600, lock => 'create' };
+ my ($err, $output, $reply) = $handle->($state, $reservation, { uid => $uid });
+ is($err, '', 'free uid: accepted');
+ is_deeply(
+ $reply,
+ { identity => { result => 'adopted', uid => $uid, requested => $uid } },
+ 'free uid: reply',
+ );
+ is($output, "guest identity: adopted uid $uid from the source guest\n", 'free uid: logged');
+ is(identity_of($class, 600)->{uid}, $uid, 'free uid: stored');
+
+ ($err) = $handle->($state, $reservation, { uid => new_uid() });
+ is($err, $once, 'second command is refused');
+ is(identity_of($class, 600)->{uid}, $uid, 'second command: uid is unchanged');
+
+ $reservation = $class->create_and_lock_config(601);
+ my $reserved = identity_of($class, 601);
+ ($err) = $handle->({ vmid => 601, lock => 'migrate' }, $reservation, { uid => new_uid() });
+ is($err, $once, 'command after the config is refused');
+ is_deeply(identity_of($class, 601), $reserved, 'after the config: identity is kept');
+
+ ($err, $output) =
+ $handle->({ vmid => 601, lock => 'create' }, $reservation, { uid => $uid });
+ is(
+ $err,
+ "guest 600 in the target cluster already has the uid '$uid' of this guest - use"
+ . " 'allow-new-uid' to migrate it with a new uid\n",
+ 'uid in use: refused',
+ );
+ is($output, '', 'uid in use: no result logged');
+ is_deeply(identity_of($class, 601), $reserved, 'uid in use: reserved identity stays');
+
+ ($err, $output, $reply) = $handle->(
+ { vmid => 601, lock => 'create' },
+ $reservation,
+ { uid => $uid, 'allow-new-uid' => 1 },
+ );
+ is($err, '', 'uid in use with allow-new-uid: accepted');
+ is_deeply(
+ $reply,
+ {
+ identity => {
+ result => 'in-use',
+ uid => $reserved->{uid},
+ requested => $uid,
+ 'in-use-by' => 600,
+ },
+ },
+ 'uid in use with allow-new-uid: reply',
+ );
+ is_deeply(identity_of($class, 601), $reserved, 'uid in use with allow-new-uid: kept');
+
+ $reservation = $class->create_and_lock_config(602);
+ ($err) = $handle->({ vmid => 602, lock => 'create' }, $reservation, {});
+ like($err, qr/^uid: property is missing/m, 'missing uid is refused');
+ ($err) =
+ $handle->({ vmid => 602, lock => 'create' }, $reservation, { uid => $uid, x => 1 });
+ like($err, qr/^x: property is not defined in schema/m, 'unknown parameter is refused');
+
+ delete PVE::GuestIdentity::tunnel_identity_cmd_schema()->{uid};
+ ok(PVE::GuestIdentity::tunnel_identity_cmd_schema()->{uid}, 'schema is a new copy');
+};
+
+subtest 'tunnel identity command on the source' => sub {
+ my $class = 'PVE::TestConfig';
+
+ my $tunnel = { version => 3 };
+ my ($commands, $reply);
+ local *PVE::Tunnel::write_tunnel = sub {
+ my ($used_tunnel, $timeout, $command, $params) = @_;
+ push $commands->@*, [$used_tunnel, $timeout, $command, $params];
+ die $reply if !ref($reply);
+ return $reply;
+ };
+
+ # like the migration of the guest $vmid with the options $opts after its preparation loaded
+ # the config
+ my $migration = sub($vmid, $opts = {}) {
+ PVE::Cluster::cfs_update();
+ my $conf = $class->load_config($vmid);
+ return bless { vmid => $vmid, vmconf => $conf, opts => $opts }, 'PVE::AbstractMigrate';
+ };
+ # returns the error, the output, and the sent parameters
+ my $take_over = sub($name, $migrate) {
+ $commands = [];
+ my ($err, $output) =
+ capture_output(sub { $migrate->take_over_identity_on_target($class, $tunnel) });
+ my @sent = map { [$_->@[0 .. 2]] } $commands->@*;
+ is_deeply(\@sent, [[$tunnel, 60, 'identity']], "$name: one command, up to a minute");
+ return ($err, $output, $commands->[0]->[3]);
+ };
+
+ $class->write_config(610, { name => 'source' });
+ my $source = identity_of($class, 610);
+ $reply =
+ { identity => { result => 'adopted', uid => $source->{uid}, requested => $source->{uid} } };
+ my $writes_before = $writes;
+ my ($err, $output, $params) = $take_over->('source with uid', $migration->(610));
+ is($err, '', 'source with uid: no error');
+ is_deeply($params, { uid => $source->{uid}, 'allow-new-uid' => 0 },
+ 'source with uid: sent');
+ is($writes, $writes_before, 'source with uid: nothing is written');
+ like(
+ $output,
+ qr/^\S+ \S+ guest identity: adopted uid $source->{uid} from the source guest\n\z/,
+ 'source with uid: result is logged',
+ );
+
+ $reply = {
+ identity => {
+ result => 'in-use',
+ uid => 'targettargettargetta',
+ requested => $source->{uid},
+ 'in-use-by' => 7,
+ },
+ };
+ ($err, $output, $params) =
+ $take_over->('allow-new-uid', $migration->(610, { 'allow-new-uid' => 1 }));
+ is($params->{'allow-new-uid'}, 1, 'allow-new-uid is sent');
+ like(
+ $output,
+ qr/ guest identity: source guest uid $source->{uid} is in use by guest 7, keeping uid/,
+ 'uid in use on the target: result is logged',
+ );
+
+ set_file($class, 611, "name: source\n");
+ my $migrate = $migration->(611);
+ $reply = { success => 1 };
+ $writes_before = $writes;
+ ($err, $output, $params) = $take_over->('source without uid', $migrate);
+ my $assigned = identity_of($class, 611);
+ ok($assigned, 'source without uid: gets one');
+ is($writes, $writes_before + 1, 'source without uid: written once');
+ is($params->{uid}, $assigned->{uid}, 'source without uid: sends the uid it got');
+ is_deeply(
+ $class->get_identity($migrate->{vmconf}),
+ $assigned,
+ 'source without uid: loaded config has it',
+ );
+ is($output, '', 'source without uid: reply without result is not logged');
+
+ my $stored_uid = new_uid();
+ set_file($class, 612, "meta: ctime=1,rtime=5,uid=$stored_uid\nname: source\n");
+ $migrate = $migration->(612);
+ # like a parser that drops a 'meta' property with a member it does not know
+ delete $migrate->{vmconf}->{meta};
+ ($err, $output, $params) = $take_over->('stored uid only', $migrate);
+ is(
+ $params->{uid}, $stored_uid, 'stored uid only: sends the stored uid',
+ );
+ is_deeply(
+ identity_of($class, 612),
+ { uid => $stored_uid, rtime => 5 },
+ 'stored uid only: stored identity is kept',
+ );
+
+ $reply =
+ { identity => { result => 'future', uid => $stored_uid, requested => $stored_uid } };
+ ($err, $output) = $take_over->('unknown result', $migration->(612));
+ is($err, '', 'unknown result: no error');
+ like(
+ $output,
+ qr/ unknown result of guest identity adoption 'future'$/m,
+ 'unknown result: warning',
+ );
+
+ $reply = "error - tunnel command 'identity' failed - refused\n";
+ ($err, $output) = $take_over->('failed command', $migration->(612));
+ is($err, $reply, 'failed command: error is passed on');
+};
+
done_testing();
--
2.47.3
next prev parent reply other threads:[~2026-11-18 4:40 UTC|newest]
Thread overview: 42+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-18 4:37 [PATCH cluster/guest-common/qemu-server/container/manager/docs v3 00/38] fix #7846: guest identity (uid) and cluster ID Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 01/38] pmxcfs: reject changes to the cluster ID file from user space Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 02/38] pmxcfs: generate a cluster ID on standalone nodes Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 03/38] pmxcfs: dfsm: add a callback that runs once all members are in sync Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 04/38] pmxcfs: quorum: pass the corosync membership to an optional listener Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 05/38] pmxcfs: confdb: call an optional listener when the nodes get published Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 06/38] pmxcfs: dcdb: generate the cluster ID once the cluster is in sync Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 07/38] cluster: add helper to read the cluster ID Thomas Lamprecht
2026-11-18 4:37 ` [PATCH cluster v3 08/38] pmxcfs: allow removing the cluster ID file in forced local mode Thomas Lamprecht
2026-11-18 4:37 ` [PATCH guest-common v3 09/38] guest identity: add helpers for the guest uid and registration time Thomas Lamprecht
2026-11-18 4:37 ` [PATCH guest-common v3 10/38] abstract config: assign a guest identity and keep it on config writes Thomas Lamprecht
2026-11-18 4:37 ` [PATCH guest-common v3 11/38] abstract config: let the VMID reservation return the previous state Thomas Lamprecht
2026-11-18 4:37 ` [PATCH guest-common v3 12/38] guest identity: allow taking over a uid that is unused in the cluster Thomas Lamprecht
2026-11-18 4:37 ` Thomas Lamprecht [this message]
2026-11-18 4:37 ` [PATCH qemu-server v3 14/38] api: restore: release the VMID if a restore to an unused VMID fails Thomas Lamprecht
2026-11-18 4:37 ` [PATCH qemu-server v3 15/38] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-18 4:37 ` [PATCH qemu-server v3 16/38] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-18 4:37 ` [PATCH qemu-server v3 17/38] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-18 4:37 ` [PATCH qemu-server v3 18/38] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-18 4:37 ` [PATCH qemu-server v3 19/38] api: status: return the guest uid Thomas Lamprecht
2026-11-18 4:37 ` [PATCH qemu-server v3 20/38] tests: remote migration: check a failed command and an early tunnel end Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 21/38] config: add read-only meta property with the creation time Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 22/38] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 23/38] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 24/38] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 25/38] restore: refuse overwriting a container with another guest's backup Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 26/38] test: remote migration: check the cleanup of the target after failures Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 27/38] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-18 4:37 ` [PATCH container v3 28/38] api: status: return the guest uid Thomas Lamprecht
2026-11-18 4:37 ` [PATCH manager v3 29/38] api: cluster resources: add guest uid and registration time Thomas Lamprecht
2026-11-18 4:37 ` [PATCH manager v3 30/38] api: cluster status: add cluster ID Thomas Lamprecht
2026-11-18 4:37 ` [PATCH manager v3 31/38] ui: restore: allow overwriting a guest with a backup of another guest Thomas Lamprecht
2026-11-18 4:37 ` [PATCH manager v3 32/38] ui: migrate: refuse target nodes that would drop the guest identity Thomas Lamprecht
2026-11-18 4:37 ` [PATCH manager v3 33/38] ui: guest summary: show " Thomas Lamprecht
2026-11-18 4:37 ` [PATCH manager v3 34/38] api: bulk migrate: refuse targets that would drop " Thomas Lamprecht
2026-11-18 4:37 ` [PATCH docs v3 35/38] vzdump: describe the guest identity of VMs and containers Thomas Lamprecht
2026-11-18 4:37 ` [PATCH docs v3 36/38] pvecm: document the cluster ID and how to get a new one Thomas Lamprecht
2026-11-18 4:37 ` [PATCH docs v3 37/38] qm: configuration: describe the read-only meta property Thomas Lamprecht
2026-11-18 4:37 ` [PATCH docs v3 38/38] pct: " Thomas Lamprecht
2026-11-18 13:49 ` [PATCH cluster/guest-common/qemu-server/container/manager/docs v3 00/38] fix #7846: guest identity (uid) and cluster ID Dominik Csapak
2026-11-18 14:13 ` Thomas Lamprecht
2026-11-19 10:38 ` superseded: " Thomas Lamprecht
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=20261118043817.1659212-14-t.lamprecht@proxmox.com \
--to=t.lamprecht@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