From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH guest-common 10/34] abstract config: let the VMID reservation return the previous state
Date: Mon, 16 Nov 2026 01:46:22 +0100 [thread overview]
Message-ID: <20261116004703.2908294-11-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261116004703.2908294-1-t.lamprecht@proxmox.com>
A restore needs to know whether its target existed and had an identity
before the operation, to decide between keeping the identity of the
target and taking over the uid of the backup. With identities enabled,
the reservation already assigns an identity to a target without one,
so reading the config afterwards cannot tell anymore, and reading it
before the locked section races with other writers.
Return that state from within the locked section, together with the
identity that the reservation wrote, so that taking over the uid of
the backup later replaces only that identity. With identities enabled,
the reservation reads the existing identity under the same lock as the
write. A config that fails to parse still counts as existing, with the
identity found in its raw text.
Existing callers ignore the return value.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
src/PVE/AbstractConfig.pm | 41 ++++++++++-
src/tests/config-identity-tests.pl | 105 +++++++++++++++++++++++++++++
2 files changed, 143 insertions(+), 3 deletions(-)
diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm
index e78b8cc..fbcc930 100644
--- a/src/PVE/AbstractConfig.pm
+++ b/src/PVE/AbstractConfig.pm
@@ -361,19 +361,54 @@ sub load_current_config {
return $conf;
}
+# Reserves $vmid for an operation by writing its config with the lock $lock, 'create' by default.
+# An existing config is only allowed with $allow_existing and must not be locked.
+#
+# Returns the state of the guest identity around the write, captured under the guest config lock,
+# as the write can assign an identity to a config without one. PVE::GuestIdentity::adopt needs it.
+# existed - whether a config existed before
+# identity - the guest identity of that config, undef if it had none or did not exist
+# reserved - the guest identity published by the write, undef with identities disabled
sub create_and_lock_config {
my ($class, $vmid, $allow_existing, $lock) = @_;
- $class->lock_config_full(
+ return $class->lock_config_full(
$vmid,
5,
sub {
PVE::Cluster::check_vmid_unused($vmid, $allow_existing);
- my $conf = eval { $class->load_config($vmid) } || {};
+ my $conf = eval { $class->load_config($vmid) };
+ my $state = {
+ existed => defined($conf) ? 1 : 0,
+ identity => defined($conf) ? $class->__identity_get($conf) : undef,
+ reserved => undef,
+ };
+
+ $conf //= {};
$class->check_lock($conf);
$conf->{lock} = $lock // 'create';
- $class->write_config($vmid, $conf);
+
+ if (!$class->__identity_enabled()) {
+ $class->write_config($vmid, $conf);
+ return $state;
+ }
+
+ # use what the write itself sees as published, see write_config
+ $class->__identity_lock(
+ $vmid,
+ sub {
+ my $published = $class->__identity_read_published($vmid);
+ $class->write_config($vmid, $conf);
+ $state = {
+ existed => defined($published) ? 1 : 0,
+ identity => $published && $published->{identity},
+ reserved => $class->__identity_get($conf),
+ };
+ },
+ );
+
+ return $state;
},
);
}
diff --git a/src/tests/config-identity-tests.pl b/src/tests/config-identity-tests.pl
index 92864ca..8bcc9d8 100755
--- a/src/tests/config-identity-tests.pl
+++ b/src/tests/config-identity-tests.pl
@@ -181,6 +181,12 @@ $cluster_module->redefine(
$versions->{$path} = ++$version_counter;
$cache->{$path}->{version} = undef if $cache->{$path};
},
+ check_vmid_unused => sub {
+ my ($vmid, $noerr) = @_;
+ return 1 if !grep { defined($files->{$_}) && vmid_of_path($_) == $vmid } keys %$files;
+ return undef if $noerr;
+ die "guest $vmid already exists\n";
+ },
get_config => sub {
my ($path) = @_;
my $vmid = vmid_of_path($path);
@@ -485,4 +491,103 @@ subtest 'overridden lock and published identity read' => sub {
ok(!-e "$PVE::GuestHelpers::lockdir/pve-guest-identity-160", 'no lock file is used');
};
+subtest 'create and lock config' => sub {
+ my $class = 'PVE::TestConfig';
+
+ my $state = $class->create_and_lock_config(150);
+ my $reserved = identity_of($class, 150);
+ ok($reserved, 'free VMID: reservation gets an identity');
+ is_deeply(
+ $state,
+ { existed => 0, identity => undef, reserved => $reserved },
+ 'free VMID: no previous identity',
+ );
+ is(conf_of($class, 150)->{lock}, 'create', 'free VMID: reserved');
+
+ eval { $class->create_and_lock_config(150) };
+ like($@, qr/already exists/, 'existing config is refused by default');
+
+ $class->write_config(151, { name => 'target', meta => 'ctime=1' });
+ my $published = identity_of($class, 151);
+ $now += 1000;
+ $state = $class->create_and_lock_config(151, 1, 'restore');
+ is_deeply(
+ $state,
+ { existed => 1, identity => $published, reserved => $published },
+ 'in place: previous identity',
+ );
+ my $conf = conf_of($class, 151);
+ is($conf->{lock}, 'restore', 'in place: locked');
+ is($conf->{name}, 'target', 'in place: config is kept');
+ is_deeply(identity_of($class, 151), $published, 'in place: identity is kept');
+
+ eval { $class->create_and_lock_config(151, 1) };
+ like($@, qr/^VM is locked \(restore\)/, 'locked config is refused');
+
+ set_file($class, 152, "meta: ctime=1\nname: old\n");
+ $state = $class->create_and_lock_config(152, 1);
+ is_deeply(
+ $state,
+ { existed => 1, identity => undef, reserved => identity_of($class, 152) },
+ 'in place without identity: reservation gets one',
+ );
+ ok($state->{reserved}, 'in place without identity: reserved identity');
+
+ set_file($class, 157, '');
+ $state = $class->create_and_lock_config(157, 1);
+ is_deeply(
+ $state,
+ { existed => 1, identity => undef, reserved => identity_of($class, 157) },
+ 'in place with empty config: reservation gets an identity',
+ );
+ ok($state->{reserved}, 'in place with empty config: reserved identity');
+
+ set_file($class, 156, "meta: rtime=5,uid=$foreign_uid\n!unparsable\n");
+ $state = $class->create_and_lock_config(156, 1);
+ my $kept = { uid => $foreign_uid, rtime => 5 };
+ is_deeply(
+ $state,
+ { existed => 1, identity => $kept, reserved => $kept },
+ 'in place, config fails to load: published identity',
+ );
+ is_deeply(
+ identity_of($class, 156),
+ $kept,
+ 'in place, config fails to load: identity is kept',
+ );
+
+ # clone: the target's first write must not publish the source's identity
+ $state = $class->create_and_lock_config(153);
+ $conf = conf_of($class, 151);
+ delete $conf->{lock};
+ $class->write_config(153, $conf);
+ is_deeply(identity_of($class, 153), $state->{reserved},
+ 'clone keeps the reserved identity');
+ like(file_of($class, 153), qr/^meta: ctime=1,/m, 'clone keeps other members of the source');
+
+ local $check_identity_lock = 0;
+ $class = 'PVE::TestConfig::NoIdentity';
+
+ $state = $class->create_and_lock_config(154);
+ is_deeply(
+ $state,
+ { existed => 0, identity => undef, reserved => undef },
+ 'disabled, free VMID: no previous identity',
+ );
+ is(file_of($class, 154), "lock: create\n", 'disabled, free VMID: no identity assigned');
+
+ set_file($class, 155, "meta: ctime=1,rtime=5,uid=$foreign_uid\nname: old\n");
+ $state = $class->create_and_lock_config(155, 1);
+ is_deeply(
+ $state,
+ { existed => 1, identity => { uid => $foreign_uid, rtime => 5 }, reserved => undef },
+ 'disabled, in place: identity from the config',
+ );
+ is(
+ file_of($class, 155),
+ "lock: create\nmeta: ctime=1,rtime=5,uid=$foreign_uid\nname: old\n",
+ 'disabled, in place: config is kept',
+ );
+};
+
done_testing();
--
2.47.3
next prev parent reply other threads:[~2026-11-16 0:48 UTC|newest]
Thread overview: 49+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-16 0:46 [PATCH cluster/guest-common/qemu-server/container/manager/docs 00/34] fix #7846: guest identity (uid) and cluster ID Thomas Lamprecht
2026-11-16 0:46 ` [PATCH cluster 01/34] pmxcfs: reject changes to the cluster ID file from user space Thomas Lamprecht
2026-11-16 0:46 ` [PATCH cluster 02/34] pmxcfs: generate a cluster ID on standalone nodes Thomas Lamprecht
2026-11-16 0:46 ` [PATCH cluster 03/34] pmxcfs: dfsm: add a callback that runs once all members are in sync Thomas Lamprecht
2026-11-16 0:46 ` [PATCH cluster 04/34] pmxcfs: quorum: pass the corosync membership to an optional listener Thomas Lamprecht
2026-11-16 0:46 ` [PATCH cluster 05/34] pmxcfs: dcdb: generate the cluster ID once the cluster is in sync Thomas Lamprecht
2026-11-16 0:46 ` [PATCH cluster 06/34] cluster: add helper to read the cluster ID Thomas Lamprecht
2026-11-16 0:46 ` [PATCH cluster 07/34] pmxcfs: allow removing the cluster ID file in forced local mode Thomas Lamprecht
2026-11-16 0:46 ` [PATCH guest-common 08/34] guest identity: add helpers for the guest uid and registration time Thomas Lamprecht
2026-11-17 10:47 ` Dominik Csapak
2026-11-16 0:46 ` [PATCH guest-common 09/34] abstract config: assign a guest identity and keep it on config writes Thomas Lamprecht
2026-11-17 10:47 ` Dominik Csapak
2026-11-17 12:47 ` Dominik Csapak
2026-11-16 0:46 ` Thomas Lamprecht [this message]
2026-11-16 0:46 ` [PATCH guest-common 11/34] guest identity: allow taking over a uid that is unused in the cluster Thomas Lamprecht
2026-11-17 13:14 ` Dominik Csapak
2026-11-16 0:46 ` [PATCH qemu-server 12/34] api: restore: release the VMID if a restore to an unused VMID fails Thomas Lamprecht
2026-11-16 0:46 ` [PATCH qemu-server 13/34] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-16 0:46 ` [PATCH qemu-server 14/34] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-16 0:46 ` [PATCH qemu-server 15/34] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-17 15:17 ` Dominik Csapak
2026-11-16 0:46 ` [PATCH qemu-server 16/34] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-17 15:26 ` Dominik Csapak
2026-11-16 0:46 ` [PATCH qemu-server 17/34] api: status: return the guest uid Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 18/34] config: add read-only meta property with the creation time Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 19/34] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 20/34] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 21/34] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 22/34] restore: refuse overwriting a container with another guest's backup Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 23/34] migrate: remote: clean up the target on a tunnel version mismatch Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 24/34] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-16 0:46 ` [PATCH container 25/34] api: status: return the guest uid Thomas Lamprecht
2026-11-16 0:46 ` [PATCH manager 26/34] api: cluster resources: add guest uid and registration time Thomas Lamprecht
2026-11-17 15:30 ` Dominik Csapak
2026-11-16 0:46 ` [PATCH manager 27/34] api: cluster status: add cluster ID Thomas Lamprecht
2026-11-16 0:46 ` [PATCH manager 28/34] ui: restore: allow overwriting a guest with a backup of another guest Thomas Lamprecht
2026-11-16 0:46 ` [PATCH manager 29/34] ui: migrate: refuse target nodes that would drop the guest identity Thomas Lamprecht
2026-11-16 0:46 ` [PATCH manager 30/34] ui: guest summary: show " Thomas Lamprecht
2026-11-17 15:44 ` Dominik Csapak
2026-11-16 0:46 ` [PATCH docs 31/34] vzdump: describe the guest identity of VMs and containers Thomas Lamprecht
2026-11-16 0:46 ` [PATCH docs 32/34] pvecm: document the cluster ID and how to get a new one Thomas Lamprecht
2026-11-16 0:46 ` [PATCH docs 33/34] qm: configuration: describe the read-only meta property Thomas Lamprecht
2026-11-16 0:46 ` [PATCH docs 34/34] pct: " Thomas Lamprecht
2026-11-17 10:47 ` [PATCH cluster/guest-common/qemu-server/container/manager/docs 00/34] fix #7846: guest identity (uid) and cluster ID Dominik Csapak
2026-11-17 11:29 ` Thomas Lamprecht
2026-11-17 11:55 ` Dominik Csapak
2026-11-17 12:47 ` Thomas Lamprecht
2026-11-17 13:54 ` Dominik Csapak
2026-11-17 15:55 ` Dominik Csapak
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=20261116004703.2908294-11-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.