From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH guest-common v4 10/41] abstract config: let the VMID reservation return the previous state
Date: Thu, 19 Nov 2026 11:33:56 +0100 [thread overview]
Message-ID: <20261119103551.1666964-11-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261119103551.1666964-1-t.lamprecht@proxmox.com>
A restore has to know whether its target existed before the operation
and whether it had an identity. It needs this to decide if the target
keeps its identity or adopts the uid of the backup. The VMID of the
target gets reserved with create_and_lock_config, which writes the
config with the 'create' lock. With identities enabled, that write
already assigns an identity to a target without one. So once the
reservation is done, the config no longer shows what was there before.
Reading the config before the reservation does not work either, as other
writers can change it in between.
Return the previous state from within the locked section, together with
the identity that the guest has after the reservation. With identities
enabled, the reservation reads the existing identity under the same lock
under which it writes the config.
That read also finds a config that exists but cannot be loaded. Refuse
such a config. Replacing it with an empty one would drop the lock and
the protection of the guest, even if the restore gets refused
afterwards.
Existing callers ignore the return value.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
Reviewed-by: Dominik Csapak <d.csapak@proxmox.com>
Tested-by: Dominik Csapak <d.csapak@proxmox.com>
---
changes v3 -> v4:
* rebase onto the independent fix before the series that lets a
reservation require an existing config, with a test that a missing one
is refused before anything gets written
* comment and commit message: 'adopt' instead of 'take over'
* commit message: plainer wording, same content
changes v2 -> v3:
* refuse an existing config that fails to load instead of replacing it
with an empty one, which dropped its lock and protection
* tests for that and for the reservation of a VMID whose config appeared
after the request started (refused by an independent fix before the
series)
changes v1 -> v2:
* rebase: the used guest ID gets registered before the state is captured
* name the keys of the returned state after what they hold (previous,
assigned)
src/PVE/AbstractConfig.pm | 46 +++++++++++-
src/tests/config-identity-tests.pl | 115 ++++++++++++++++++++++++++++-
2 files changed, 156 insertions(+), 5 deletions(-)
diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm
index d6dfd66..ebdbde6 100644
--- a/src/PVE/AbstractConfig.pm
+++ b/src/PVE/AbstractConfig.pm
@@ -332,12 +332,20 @@ sub load_current_config {
return $conf;
}
-# Reserves $vmid by writing its config with the lock $lock, 'create' by default. An existing config
-# is only allowed with $allow_existing, and required if that is 'require'.
+# 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 required if that is 'require'. It
+# must not be locked. With identities enabled, one that fails to load is refused as well.
+#
+# 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. Adopting a uid later needs it.
+# existed - whether a config existed before
+# previous - the guest identity of that config, undef if it had none or did not exist
+# assigned - the guest identity of the config after 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 {
@@ -351,10 +359,40 @@ sub create_and_lock_config {
die $class->guest_type() . " $vmid already exists\n"
if defined($conf) && !$allow_existing;
die $load_err if !defined($conf) && ($allow_existing // '') eq 'require';
+ my $existed = defined($conf);
+ my $previous = $existed ? $class->get_identity($conf) : undef;
+
$conf //= {};
$class->check_lock($conf);
$conf->{lock} = $lock // 'create';
- $class->write_config($vmid, $conf);
+
+ if ($class->__identity_enabled()) {
+ # use what the write itself sees as stored, see write_config
+ $class->__identity_lock(
+ $vmid,
+ sub {
+ my $stored = $class->__identity_read_stored($vmid);
+ # this also finds a config that failed to load, replacing it with an
+ # empty one would drop its lock and protection
+ if ($stored) {
+ die $class->guest_type() . " $vmid already exists\n"
+ if !$allow_existing;
+ die $load_err if !$existed;
+ }
+ $existed = defined($stored);
+ $previous = $stored ? $stored->{identity} : undef;
+ $class->write_config($vmid, $conf);
+ },
+ );
+ } else {
+ $class->write_config($vmid, $conf);
+ }
+
+ return {
+ existed => $existed ? 1 : 0,
+ previous => $previous,
+ assigned => $class->__identity_enabled() ? $class->get_identity($conf) : undef,
+ };
},
);
}
diff --git a/src/tests/config-identity-tests.pl b/src/tests/config-identity-tests.pl
index fcec452..bf67e9c 100755
--- a/src/tests/config-identity-tests.pl
+++ b/src/tests/config-identity-tests.pl
@@ -151,6 +151,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);
@@ -167,7 +173,7 @@ $cluster_module->redefine(
my $uid_counter = 0;
my $now = 1_700_000_000_000;
-# recording used guest IDs needs the cluster filesystem and is not what gets tested here
+# recording used guest IDs needs the cluster filesystem, only one subtest fakes the list of them
my $guest_id_module = Test::MockModule->new('PVE::GuestID');
$guest_id_module->redefine(register_used_id => sub { });
@@ -380,4 +386,111 @@ subtest 'snapshot rollback' => sub {
);
};
+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, previous => undef, assigned => $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 $stored = identity_of($class, 151);
+ $now += 1000;
+ $state = $class->create_and_lock_config(151, 1, 'restore');
+ is_deeply(
+ $state,
+ { existed => 1, previous => $stored, assigned => $stored },
+ '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), $stored, 'in place: identity is kept');
+
+ eval { $class->create_and_lock_config(151, 1) };
+ like($@, qr/^VM is locked \(restore\)/, 'locked config is refused');
+
+ eval { $class->create_and_lock_config(159, 'require') };
+ like($@, qr/^Configuration file '.*' does not exist/,
+ 'required config is missing: refused');
+ ok(!defined(file_of($class, 159)), 'required config is missing: nothing is written');
+
+ set_file($class, 152, "meta: ctime=1\nname: old\n");
+ $state = $class->create_and_lock_config(152, 1);
+ is_deeply(
+ $state,
+ { existed => 1, previous => undef, assigned => identity_of($class, 152) },
+ 'in place without identity: reservation gets one',
+ );
+ ok($state->{assigned}, 'in place without identity: reserved identity');
+
+ set_file($class, 157, '');
+ $state = $class->create_and_lock_config(157, 1);
+ is_deeply(
+ $state,
+ { existed => 1, previous => undef, assigned => identity_of($class, 157) },
+ 'in place with empty config: reservation gets an identity',
+ );
+ ok($state->{assigned}, 'in place with empty config: reserved identity');
+
+ my $unloadable = "lock: backup\nmeta: rtime=5,uid=$foreign_uid\nname: old\n!unparsable\n";
+ set_file($class, 156, $unloadable);
+ eval { $class->create_and_lock_config(156, 1) };
+ like($@, qr/^test error: unable to parse/, 'in place, config fails to load: refused');
+ is(file_of($class, 156), $unloadable, 'in place, config fails to load: file is unchanged');
+
+ # the guest list of the process can predate a config created since
+ my $check_vmid_unused = PVE::Cluster->can('check_vmid_unused');
+ $cluster_module->redefine(check_vmid_unused => sub { return 1; });
+ for my $raw ("name: other\n", $unloadable) {
+ set_file($class, 158, $raw);
+ eval { $class->create_and_lock_config(158) };
+ is($@, "VM 158 already exists\n", 'outdated guest list: existing config is refused');
+ is(file_of($class, 158), $raw, 'outdated guest list: file is unchanged');
+ }
+ $cluster_module->redefine(check_vmid_unused => $check_vmid_unused);
+
+ # clone: the target's first write must not store 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->{assigned},
+ '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, previous => undef, assigned => 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, previous => { uid => $foreign_uid, rtime => 5 }, assigned => 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-19 10:37 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 10:33 [PATCH cluster/guest-common/qemu-server/container/manager/docs v4 00/41] fix #7846: guest identity (uid) and cluster ID Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 01/41] pmxcfs: reject changes to the cluster ID file from user space Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 02/41] pmxcfs: generate a cluster ID on standalone nodes Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 03/41] pmxcfs: dfsm: add a callback that runs once all members are in sync Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 04/41] pmxcfs: quorum: record the corosync membership Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 05/41] pmxcfs: dcdb: generate the cluster ID once the cluster is in sync Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 06/41] cluster: add helper to read the cluster ID Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 07/41] pmxcfs: allow removing the cluster ID file in forced local mode Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 08/41] guest identity: add helpers for the guest uid and registration time Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 09/41] abstract config: assign a guest identity and keep it on config writes Thomas Lamprecht
2026-11-19 10:33 ` Thomas Lamprecht [this message]
2026-11-19 10:33 ` [PATCH guest-common v4 11/41] guest identity: allow adopting a uid that is unused in the cluster Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 12/41] migrate: add shared helpers for the identity command of the migration tunnel Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 13/41] vzdump: notes template: list the uid variable Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 14/41] api: restore: release the VMID if a restore to an unused VMID fails Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 15/41] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 16/41] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 17/41] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 18/41] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 19/41] api: status: return the guest uid Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 20/41] restore: decide by the reservation whether the container is new Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 21/41] config: add read-only meta property with the creation time Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 22/41] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 23/41] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 24/41] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 25/41] restore: refuse overwriting a container with another guest's backup Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 26/41] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 27/41] api: status: return the guest uid Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 28/41] api: cluster resources: add guest uid and registration time Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 29/41] api: cluster status: add cluster ID Thomas Lamprecht
2026-11-19 12:32 ` Lukas Wagner
2026-11-19 12:51 ` Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 30/41] ui: datacenter summary: show the " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 31/41] ui: restore: allow overwriting a guest with a backup of another guest Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 32/41] ui: migrate: refuse target nodes that would drop the guest identity Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 33/41] ui: guest summary: show " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 34/41] api: bulk migrate: refuse targets that would drop " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 35/41] vzdump: notes template: add the guest uid as variable Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 36/41] vzdump: restore: describe what a failed restore can leave behind Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 37/41] vzdump: describe the guest identity of VMs and containers Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 38/41] pvecm: document the cluster ID and how to get a new one Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 39/41] qm: configuration: describe the read-only meta property Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 40/41] pct: " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 41/41] vzdump: notes: document the uid template variable Thomas Lamprecht
2026-11-19 14:46 ` [PATCH cluster/guest-common/qemu-server/container/manager/docs v4 00/41] fix #7846: guest identity (uid) and cluster ID Dominik Csapak
2026-11-19 19:49 ` 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=20261119103551.1666964-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.