From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 331B81FF0E1 for ; Mon, 16 Nov 2026 01:49:45 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 323D42183B; Mon, 16 Nov 2026 01:48:33 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH container 19/34] fix #7846: config: add a guest identity to the 'meta' property Date: Mon, 16 Nov 2026 01:46:31 +0100 Message-ID: <20261116004703.2908294-20-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261116004703.2908294-1-t.lamprecht@proxmox.com> References: <20261116004703.2908294-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: 1794790029890 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.216 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: 6OLSTWBR72B7MYVWCGLDUO473DA26IT6 X-Message-ID-Hash: 6OLSTWBR72B7MYVWCGLDUO473DA26IT6 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: Give containers a guest identity in the 'meta' property: a random identifier, 'uid', and the time the container was registered in the cluster, 'rtime'. With identities enabled for the config class, every config write keeps the identity a container already has, or assigns one if it has none, whatever the written config carries. So create and clone get a new identity when the VMID is reserved, while config changes, pending changes applied by the post-stop hook, snapshot rollback and a restore over an existing container keep it. A container without identity gets one with its first config write by an upgraded node. The post-stop hook writes the config it loaded before, without the guest lock. As the write reads the existing identity under the per-VMID identity lock, an identity that another writer assigned in the meantime is kept and not replaced by a second one. Signed-off-by: Thomas Lamprecht --- src/PVE/LXC/Config.pm | 8 ++ src/test/lifecycle_tests.pl | 248 +++++++++++++++++++++++++++++++++++- 2 files changed, 253 insertions(+), 3 deletions(-) diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm index 7273cf2..8b6cf1d 100644 --- a/src/PVE/LXC/Config.pm +++ b/src/PVE/LXC/Config.pm @@ -9,6 +9,7 @@ use PVE::AbstractConfig; use PVE::Cluster qw(cfs_register_file); use PVE::DataCenterConfig; use PVE::GuestHelpers; +use PVE::GuestIdentity; use PVE::INotify; use PVE::JSONSchema qw(get_standard_option); use PVE::Network::SDN::Vnets; @@ -59,6 +60,12 @@ sub cfs_config_path { return "nodes/$node/lxc/$vmid.conf"; } +sub __identity_enabled { + my ($class) = @_; + + return 1; +} + sub mountpoint_backup_enabled { my ($class, $mp_key, $mountpoint) = @_; @@ -511,6 +518,7 @@ my $meta_info_fmt = { minimum => 0, optional => 1, }, + PVE::GuestIdentity::meta_format_properties()->%*, }; my $confdesc = { diff --git a/src/test/lifecycle_tests.pl b/src/test/lifecycle_tests.pl index e6083c9..2e31984 100755 --- a/src/test/lifecycle_tests.pl +++ b/src/test/lifecycle_tests.pl @@ -8,10 +8,12 @@ use warnings; use lib qw(..); -# The API modules load modules of pve-ha-manager, which is no build dependency, so stub them. Tests -# that call into them mock the functions they need. +# The API modules load modules of pve-ha-manager and the backup plugin PVE::VZDump of pve-manager, +# which are no build dependencies, so stub them. Tests that call into them mock the functions they +# need. BEGIN { $INC{'PVE/HA/Config.pm'} = $INC{'PVE/HA/Env/PVE2.pm'} = __FILE__; + $INC{'PVE/VZDump.pm'} = __FILE__; } use File::Path qw(make_path remove_tree); @@ -136,6 +138,24 @@ $config_module->mock( }, ); +# Guest identities are generated deterministically, the local identity lock is taken in the working +# directory. + +$PVE::GuestHelpers::lockdir = "$WORKDIR/lock"; + +my $random_counter = 0; +my $now_ms = 1_700_000_000_000; + +my $identity_module = Test::MockModule->new('PVE::GuestIdentity'); +$identity_module->mock( + __random_bytes => sub { + my ($len) = @_; + die "unexpected length $len\n" if $len != 13; + return pack('x6Nx3', ++$random_counter); + }, + __time_ms => sub { return $now_ms }, +); + my $repl_config_module = Test::MockModule->new('PVE::ReplicationConfig'); $repl_config_module->mock( new => sub { return bless {}, 'PVE::ReplicationConfig' }, @@ -183,6 +203,7 @@ $restenv_module->mock( chomp($msg); $task_log .= "WARN: $msg\n"; }, + setup_default_cli_env => sub { }, ); # Runs $code like a task, with its output going to the task log. Returns the error, if any. @@ -224,6 +245,8 @@ $storage_module->mock( }, storage_has_feature => sub { return 0 }, get_bandwidth_limit => sub { return undef }, + volume_rollback_is_possible => sub { return 1 }, + volume_snapshot_rollback => sub { }, ); my $lxc_module = Test::MockModule->new('PVE::LXC'); @@ -255,6 +278,10 @@ $lxc_module->mock( return $rootdir; }, umount_all => sub { }, + copy_volume => sub { + my ($mp, $newid) = @_; + return "local:$newid/vm-$newid-disk-0.raw"; + }, create_ifaces_ipams_ips => sub { }, delete_ifaces_ipams_ips => sub { }, update_lxc_config => sub { }, @@ -291,7 +318,10 @@ package PVE::LXC::Test::Setup { } my $firewall_module = Test::MockModule->new('PVE::Firewall'); -$firewall_module->mock(remove_vmfw_conf => sub { }); +$firewall_module->mock( + clone_vmfw_conf => sub { }, + remove_vmfw_conf => sub { }, +); my $access_control_module = Test::MockModule->new('PVE::AccessControl'); $access_control_module->mock(assemble_tunnel_ticket => sub { return 'ticket' }); @@ -334,6 +364,19 @@ my sub update_ct { }); } +my sub clone_ct { + my ($vmid, $newid, $param) = @_; + + return run_task(sub { + PVE::API2::LXC->clone_vm({ + node => $NODENAME, + vmid => $vmid, + newid => $newid, + ($param // {})->%*, + }); + }); +} + # Runs the migration tunnel for $vmid with the commands @commands, which are sent at once, and # returns the error of the task, if any, and the replies. my sub run_mtunnel { @@ -415,6 +458,12 @@ my sub meta_of { return { map { split(/=/, $_, 2) } split(/,/, $conf->{meta} // '') }; } +my sub identity_of { + my ($vmid) = @_; + + return PVE::LXC::Config->get_identity(PVE::LXC::Config->load_config($vmid)); +} + # Tests subtest 'meta cannot be changed through the API' => sub { @@ -559,6 +608,199 @@ subtest 'remote migration keeps the meta information of the source' => sub { is($conf->{lock}, 'migrate', 'config is locked for the migration'); }; +subtest 'create assigns a new guest identity' => sub { + reset_configs(); + $now_ms = 1_700_000_000_000; + + my $err = create_ct(201); + is($err, '', 'create succeeds'); + + my $meta = meta_of(201); + like($meta->{uid}, qr/^[a-z2-7]{20}$/, 'uid is assigned'); + is($meta->{rtime}, 1_700_000_000_000, 'rtime is the time of the create'); + ok(defined($meta->{ctime}), 'ctime is kept next to the identity'); + ok(!defined(PVE::LXC::Config->load_config(201)->{lock}), 'create lock is removed'); + + $err = create_ct(202); + isnt(identity_of(202)->{uid}, $meta->{uid}, 'another container gets another uid'); +}; + +subtest 'config changes keep the guest identity' => sub { + reset_configs(); + my $meta = 'ctime=1500000000,rtime=1500000000000,uid=existingexistingexis'; + write_raw_config(200, existing_config($meta)); + $now_ms = 1_700_000_000_000; + + my $err = update_ct(200, { hostname => 'changed', memory => 1024 }); + is($err, '', 'update succeeds'); + my $conf = PVE::LXC::Config->load_config(200); + is($conf->{hostname}, 'changed', 'change is applied'); + is($conf->{meta}, $meta, 'meta is unchanged'); + + write_raw_config(200, existing_config('ctime=1500000000')); + $err = update_ct(200, { hostname => 'first-write' }); + is($err, '', 'update of a container without identity succeeds'); + my $identity = identity_of(200); + like($identity->{uid}, qr/^[a-z2-7]{20}$/, 'first write assigns a uid'); + is($identity->{rtime}, 1_700_000_000_000, 'first write sets rtime'); + is(meta_of(200)->{ctime}, 1500000000, 'ctime is kept'); + + $now_ms += 1000; + $err = update_ct(200, { hostname => 'second-write' }); + is_deeply(identity_of(200), $identity, 'later writes keep the identity'); +}; + +subtest 'clone assigns a new guest identity' => sub { + reset_configs(); + write_raw_config( + 200, + existing_config('ctime=1500000000,rtime=1500000000000,uid=existingexistingexis'), + ); + $now_ms = 1_700_000_000_000; + + my $err = clone_ct(200, 201, { hostname => 'clone' }); + is($err, '', 'clone succeeds') or diag($task_log); + + my $meta = meta_of(201); + like($meta->{uid}, qr/^[a-z2-7]{20}$/, 'clone has a uid'); + isnt($meta->{uid}, 'existingexistingexis', 'clone does not get the uid of the source'); + is($meta->{rtime}, 1_700_000_000_000, 'rtime is the time of the clone'); + is($meta->{ctime}, 1500000000, 'ctime of the source is kept'); + is(identity_of(200)->{uid}, 'existingexistingexis', 'source keeps its uid'); +}; + +subtest 'snapshot rollback keeps the current guest identity' => sub { + reset_configs(); + write_raw_config(200, <<~'EOF'); + arch: amd64 + hostname: existing + memory: 512 + meta: ctime=1500000000,rtime=1600000000000,uid=existingexistingexis + ostype: debian + parent: snap + rootfs: local:200/vm-200-disk-0.raw,size=4G + unprivileged: 1 + + [snap] + arch: amd64 + hostname: snapshot + memory: 512 + meta: ctime=1500000000,rtime=1500000000000,uid=snapshotsnapshotsnap + ostype: debian + rootfs: local:200/vm-200-disk-0.raw,size=4G + snaptime: 1500000000 + unprivileged: 1 + EOF + + my $err = run_task(sub { PVE::LXC::Config->snapshot_rollback(200, 'snap') }); + is($err, '', 'rollback succeeds') or diag($task_log); + + my $conf = PVE::LXC::Config->load_config(200); + is($conf->{hostname}, 'snapshot', 'config of the snapshot is restored'); + is_deeply( + identity_of(200), + { uid => 'existingexistingexis', rtime => 1600000000000 }, + 'current identity is kept', + ); +}; + +subtest 'in-place restore keeps the guest identity of the target' => sub { + reset_configs(); + my $meta = 'ctime=1500000000,rtime=1500000000000,uid=existingexistingexis'; + write_raw_config(200, existing_config($meta)); + + for my $backup_meta ( + undef, 'ctime=1400000000,rtime=1400000000000,uid=existingexistingexis', + ) { + $backup_raw_config = backup_config($backup_meta); + my $err = restore_ct(200, { force => 1 }); + is($err, '', 'restore succeeds') or diag($task_log); + is_deeply( + identity_of(200), + { uid => 'existingexistingexis', rtime => 1500000000000 }, + 'identity is kept', + ); + is(PVE::LXC::Config->load_config(200)->{hostname}, 'backup', 'backup is restored'); + } +}; + +subtest 'backup assigns the guest identity and includes it' => sub { + reset_configs(); + write_raw_config(987, existing_config('ctime=1500000000')); + $now_ms = 1_700_000_000_000; + + require PVE::VZDump::LXC; + my $plugin = bless { vzdump => { opts => { pbs => 1 } } }, 'PVE::VZDump::LXC'; + + $plugin->lock_vm(987); + my $identity = identity_of(987); + like($identity->{uid}, qr/^[a-z2-7]{20}$/, 'backup lock assigns a uid'); + is($identity->{rtime}, $now_ms, 'backup lock sets rtime'); + + $plugin->assemble({ tmpdir => "$WORKDIR/backup" }, 987); + my $raw = file_get_contents("$WORKDIR/backup/etc/vzdump/pct.conf"); + my $backup_conf = PVE::LXC::Config::parse_pct_config('/lxc/987.conf', $raw); + is_deeply( + PVE::LXC::Config->get_identity($backup_conf), + $identity, + 'backup has the identity', + ); + + $now_ms += 1000; + $plugin->unlock_vm(987); + is_deeply(identity_of(987), $identity, 'removing the backup lock keeps the identity'); +}; + +subtest 'post-stop hook keeps an identity published while it ran' => sub { + reset_configs(); + write_raw_config(200, existing_config('ctime=1500000000')); + $now_ms = 1_700_000_000_000; + + my $published; + $lxc_module->mock( + get_staging_tempfs => sub { return "$WORKDIR/staging" }, + vm_stop_cleanup => sub { + # another writer assigns the identity after the hook loaded the config + PVE::LXC::Config->lock_config( + 200, + sub { + my $conf = PVE::LXC::Config->load_config(200); + $conf->{description} = 'written while the hook ran'; + PVE::LXC::Config->write_config(200, $conf); + }, + ); + $published = identity_of(200); + $now_ms += 1000; + }, + ); + my $tools_module = Test::MockModule->new('PVE::Tools'); + $tools_module->mock(run_command => sub { }); + my $guest_helpers_module = Test::MockModule->new('PVE::GuestHelpers'); + $guest_helpers_module->mock(exec_hookscript => sub { }); + + local $ENV{LXC_HOOK_VERSION} = '1'; + local $ENV{LXC_NAME} = '200'; + local $ENV{LXC_HOOK_SECTION} = 'lxc'; + local $ENV{LXC_HOOK_TYPE} = 'post-stop'; + local $ENV{LXC_ROOTFS_MOUNT} = "$WORKDIR/rootfs/200"; + local $ENV{LXC_ROOTFS_PATH} = "$WORKDIR/rootfs/200"; + local $ENV{LXC_CONFIG_FILE} = "$WORKDIR/lxc-200.config"; + delete local $ENV{LXC_TARGET}; + + my $err = run_task(sub { + do '../lxc-pve-poststop-hook'; + die $@ if $@; + }); + is($err, '', 'hook succeeds') or diag($task_log); + unlike($task_log, qr/WARN/, 'hook does not warn') or diag($task_log); + + like($published->{uid}, qr/^[a-z2-7]{20}$/, 'other writer assigned a uid'); + ok(!defined(PVE::LXC::Config->load_config(200)->{description}), 'hook wrote its config'); + is_deeply(identity_of(200), $published, 'hook keeps the identity'); + + $lxc_module->unmock('get_staging_tempfs', 'vm_stop_cleanup'); +}; + remove_tree($WORKDIR); done_testing(); -- 2.47.3