From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id DF9481FF0AA for ; Tue, 17 Nov 2026 23:41:01 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 44C1421781; Tue, 17 Nov 2026 23:40:19 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH guest-common v2 10/36] abstract config: let the VMID reservation return the previous state Date: Tue, 17 Nov 2026 23:34:20 +0100 Message-ID: <20261117223945.862914-11-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261117223945.862914-1-t.lamprecht@proxmox.com> References: <20261117223945.862914-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794955191121 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.200 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: ZRR3EMPKHXCHXJ5U4QZVSUPDQKIWUIT4 X-Message-ID-Hash: ZRR3EMPKHXCHXJ5U4QZVSUPDQKIWUIT4 X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 guest has after the reservation. 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 --- 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 | 39 ++++++++++- src/tests/config-identity-tests.pl | 107 ++++++++++++++++++++++++++++- 2 files changed, 142 insertions(+), 4 deletions(-) diff --git a/src/PVE/AbstractConfig.pm b/src/PVE/AbstractConfig.pm index 40cbe86..5cc1d7f 100644 --- a/src/PVE/AbstractConfig.pm +++ b/src/PVE/AbstractConfig.pm @@ -332,10 +332,19 @@ 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. Taking over 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 { @@ -343,10 +352,34 @@ sub create_and_lock_config { PVE::GuestID::register_used_id($vmid); - my $conf = eval { $class->load_config($vmid) } || {}; + my $conf = eval { $class->load_config($vmid) }; + 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); + $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..09ac9e4 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,103 @@ 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'); + + 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'); + + 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, previous => $kept, assigned => $kept }, + 'in place, config fails to load: stored 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 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