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 02DBD1FF0DE for ; Thu, 19 Nov 2026 11:39:47 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B967A219C4; Thu, 19 Nov 2026 11:36:59 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH container v4 21/41] config: add read-only meta property with the creation time Date: Thu, 19 Nov 2026 11:34:07 +0100 Message-ID: <20261119103551.1666964-22-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261119103551.1666964-1-t.lamprecht@proxmox.com> References: <20261119103551.1666964-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: 1795084556523 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.159 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: ZVKOJM75HCCQZRF5G5XQWRI6QE4U6AEI X-Message-ID-Hash: ZVKOJM75HCCQZRF5G5XQWRI6QE4U6AEI 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: Containers need a place for information that the system records about them, including the guest identity. Add a read-only 'meta' property with the same 'ctime' member as for VMs. It is set when a container gets created; a restore takes it from the backup and a clone from the source. 'meta' is not accepted as a parameter by the create and update API calls, and deleting or reverting it is rejected for every user, including root. A manually queued change gets dropped with a warning instead of applied. The target of a remote migration takes the property from the source as it is, bypassing the generic update path, which rejects read-only properties. The config API returns 'meta' as a plain string, so that members from newer versions do not fail the result validation of 'pct config'. Older pve-container versions drop the property on their next config write, and their strict parser for remote migrations rejects it after the volumes were transferred. Signed-off-by: Thomas Lamprecht --- changes v3 -> v4: * the API method declares 'meta' in the schema of the returned config itself, instead of a parameter of json_config_properties for it * the lifecycle test script and its tests of existing behavior moved to an independent test commit before the series * tests: the three cases for the creation time are one subtest * rebase onto the independent fixes before the series changes v2 -> v3: * description of 'meta' says what it has * tests for the independent fixes before the series (failed overwrite keeps the container, create over a container that appeared after the request started) changes v1 -> v2: * use the shared helper of pve-guest-common to drop a pending change of 'meta' * test script: use v5.36, mock the registration of used guest IDs * wording of the commit message src/PVE/API2/LXC.pm | 4 + src/PVE/API2/LXC/Config.pm | 8 ++ src/PVE/LXC/Config.pm | 34 ++++++++- src/test/config_tests.pl | 22 ++++++ src/test/lifecycle_tests.pl | 146 ++++++++++++++++++++++++++++++++++++ 5 files changed, 212 insertions(+), 2 deletions(-) diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm index 9997ca8..a14a8be 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -488,6 +488,7 @@ __PACKAGE__->register_method({ PVE::LXC::Config->queue_pct_config_changes($vmid, $conf, $no_disk_param, [], []); $conf->{unprivileged} = 1 if $unprivileged; + $conf->{meta} = PVE::LXC::Config->new_meta_info_string() if !$restore; my $emsg = $restore ? "unable to restore CT $vmid -" : "unable to create CT $vmid -"; @@ -3389,6 +3390,8 @@ __PACKAGE__->register_method({ my $unprivileged = delete $new_conf->{unprivileged}; my $arch = delete $new_conf->{arch}; + # read-only for the update below, the source's values are kept as they are + my $meta = delete $new_conf->{meta}; # TODO handle properly? delete $new_conf->{snapshots}; @@ -3423,6 +3426,7 @@ __PACKAGE__->register_method({ $state->{vmid}, $conf, 0, $new_conf, [], [], ); raise_param_exc($errors) if scalar(keys %$errors); + $conf->{meta} = $meta if defined($meta); PVE::LXC::Config->write_config($state->{vmid}, $conf); PVE::LXC::update_lxc_config($vmid, $conf); }; diff --git a/src/PVE/API2/LXC/Config.pm b/src/PVE/API2/LXC/Config.pm index 805eac0..b5d7a7d 100644 --- a/src/PVE/API2/LXC/Config.pm +++ b/src/PVE/API2/LXC/Config.pm @@ -70,6 +70,14 @@ __PACKAGE__->register_method({ description => 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.', }, + # without a format, as a config written by a newer version can have unknown members + meta => { + type => 'string', + description => + "Read-only meta-information about this container, for example its" + . " creation time.", + optional => 1, + }, }), }, code => sub { diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm index b6eaf0b..73272bb 100644 --- a/src/PVE/LXC/Config.pm +++ b/src/PVE/LXC/Config.pm @@ -505,6 +505,15 @@ my $features_desc = { }, }; +my $meta_info_fmt = { + ctime => { + type => 'integer', + description => "The guest creation timestamp as UNIX epoch time", + minimum => 0, + optional => 1, + }, +}; + my $confdesc = { lock => { optional => 1, @@ -717,6 +726,13 @@ my $confdesc = { "Try to be more verbose. For now this only enables debug log-level on start.", default => 0, }, + meta => { + optional => 1, + type => 'string', + format => $meta_info_fmt, + description => + "Read-only meta-information about this container, for example its creation time.", + }, }; my $valid_lxc_conf_keys = { @@ -1310,6 +1326,7 @@ sub queue_pct_config_changes { my $storage_cfg = PVE::Storage::config(); foreach my $opt (@$revert) { + die "unable to revert read-only option: '$opt'\n" if $opt eq 'meta'; delete $conf->{pending}->{$opt}; $class->remove_from_pending_delete($conf, $opt); # also remove from deletion queue } @@ -1318,6 +1335,7 @@ sub queue_pct_config_changes { my $modified = {}; # record modified options foreach my $opt (@$delete) { + die "unable to delete read-only option: '$opt'\n" if $opt eq 'meta'; if (!defined($conf->{$opt}) && !defined($conf->{pending}->{$opt})) { warn "cannot delete '$opt' - not set in current configuration!\n"; next; @@ -1358,7 +1376,7 @@ sub queue_pct_config_changes { $value = PVE::LXC::verify_nameserver_list($value); } elsif ($opt eq 'searchdomain') { $value = PVE::LXC::verify_searchdomain_list($value); - } elsif ($opt eq 'unprivileged') { + } elsif ($opt eq 'unprivileged' || $opt eq 'meta') { die "unable to modify read-only option: '$opt'\n"; } elsif ($opt eq 'tags') { $value = PVE::GuestHelpers::get_unique_tags($value); @@ -1454,7 +1472,8 @@ sub json_config_properties { my ($class, $prop) = @_; foreach my $opt (keys %$confdesc) { - next if $opt eq 'parent' || $opt eq 'snaptime'; + # 'meta' is read-only + next if $opt eq 'parent' || $opt eq 'snaptime' || $opt eq 'meta'; next if $prop->{$opt}; $prop->{$opt} = $confdesc->{$opt}; } @@ -1462,6 +1481,13 @@ sub json_config_properties { return $prop; } +# Returns the 'meta' property string for a newly created container. +sub new_meta_info_string { + my ($class) = @_; + + return PVE::JSONSchema::print_property_string({ ctime => int(time()) }, $meta_info_fmt); +} + my $parse_ct_mountpoint_full = sub { my ($class, $desc, $data, $noerr) = @_; @@ -1653,6 +1679,8 @@ my $LXC_FASTPLUG_OPTIONS = { sub vmconfig_hotplug_pending { my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_; + $class->drop_pending_meta($conf); + my $pid = PVE::LXC::find_lxc_pid($vmid); my $rootdir = "/proc/$pid/root"; @@ -1779,6 +1807,8 @@ sub vmconfig_hotplug_pending { sub vmconfig_apply_pending { my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_; + $class->drop_pending_meta($conf); + my $add_apply_error = sub { my ($opt, $msg) = @_; my $err_msg = "unable to apply pending change $opt : $msg"; diff --git a/src/test/config_tests.pl b/src/test/config_tests.pl index beb7e37..739420b 100755 --- a/src/test/config_tests.pl +++ b/src/test/config_tests.pl @@ -175,4 +175,26 @@ subtest 'restore does not take over pending changes of the backup' => sub { } }; +subtest 'meta property' => sub { + like(PVE::LXC::Config->new_meta_info_string(), qr/^ctime=\d+$/, 'creation time is set'); + + my $raw = "hostname: ct100\nmeta: ctime=1700000000\n"; + my $conf = PVE::LXC::Config::parse_pct_config('/lxc/100.conf', $raw, 1); + is($conf->{meta}, 'ctime=1700000000', 'meta is parsed'); + is(PVE::LXC::Config::write_pct_config('/lxc/100.conf', $conf), $raw, 'meta is written'); + + $raw = "hostname: ct100\nmeta: ctime=1700000000,unknown=value\n"; + my $warnings = ''; + { + local $SIG{__WARN__} = sub { $warnings .= $_[0] }; + $conf = PVE::LXC::Config::parse_pct_config('/lxc/100.conf', $raw); + } + like($warnings, qr/unable to parse value of 'meta'/, 'unknown member causes a warning'); + is($conf->{meta}, 'ctime=1700000000,unknown=value', 'meta with unknown member is kept'); + eval { PVE::LXC::Config::parse_pct_config('/lxc/100.conf', $raw, 1) }; + like($@, qr/unable to parse value of 'meta'/, 'unknown member fails strict parsing'); + + ok(!exists(PVE::LXC::Config->json_config_properties({})->{meta}), 'meta cannot be set'); +}; + done_testing(); diff --git a/src/test/lifecycle_tests.pl b/src/test/lifecycle_tests.pl index 0f5aad2..026596a 100755 --- a/src/test/lifecycle_tests.pl +++ b/src/test/lifecycle_tests.pl @@ -30,6 +30,7 @@ use PVE::LXC::Create; use PVE::LXC::Migrate; use PVE::API2::LXC; +use PVE::API2::LXC::Config; my $WORKDIR = 'lifecycle-working'; my $NODENAME = PVE::INotify::nodename(); @@ -463,6 +464,12 @@ my sub restore_ct($vmid, $param = undef) { }); } +my sub update_ct($vmid, $param) { + return run_task(sub { + PVE::API2::LXC::Config->update_vm({ node => $NODENAME, vmid => $vmid, $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($vmid, @commands) { @@ -495,6 +502,7 @@ my $INCOMING_CONFIG = <<~'EOF'; arch: amd64 hostname: incoming memory: 1024 + meta: ctime=1600000000 ostype: debian rootfs: local:300/vm-300-disk-0.raw,size=4G unprivileged: 1 @@ -522,6 +530,13 @@ my sub migrate_remote($vmid, $opts, $tunnel) { return ($err, $log); } +# Returns the members of the 'meta' property in the config of $vmid. +my sub meta_of($vmid) { + my $conf = PVE::LXC::Config->load_config($vmid); + + return { map { split(/=/, $_, 2) } split(/,/, $conf->{meta} // '') }; +} + # Tests subtest 'create and restore write the config of the container' => sub { @@ -1169,6 +1184,137 @@ subtest 'restore treats a container that got removed before the reservation as n ok(!defined(read_raw_config(200)), 'privileged: the VMID is released'); }; +subtest 'meta cannot be changed through the API' => sub { + reset_state(); + write_raw_config(200, existing_config('ctime=1500000000')); + my $raw = read_raw_config(200); + + for my $user ('root@pam', 'admin@pve') { + my $rejected = sub($name, $expected_err, $call) { + like(as_user($user, {}, $call), $expected_err, "$user cannot $name"); + }; + my $unknown = qr/meta: property is not defined in schema/; + $rejected->('set meta', $unknown, sub { update_ct(200, { meta => 'ctime=1' }) }); + $rejected->('create with meta', $unknown, + sub { create_ct(201, { meta => 'ctime=1' }) }); + $rejected->( + 'restore with meta', + $unknown, + sub { restore_ct(200, { force => 1, meta => 'ctime=1' }) }, + ); + $rejected->( + 'delete meta', + qr/unable to delete read-only option: 'meta'/, + sub { update_ct(200, { delete => 'meta' }) }, + ); + $rejected->( + 'revert meta', + qr/unable to revert read-only option: 'meta'/, + sub { update_ct(200, { revert => 'meta' }) }, + ); + } + + is(read_raw_config(200), $raw, 'config is unchanged'); + ok(!defined(read_raw_config(201)), 'no container got created'); + + my $conf = PVE::LXC::Config->load_config(200); + eval { PVE::LXC::Config->update_pct_config(200, $conf, 0, { meta => 'ctime=1' }, [], []) }; + like($@, qr/unable to modify read-only option: 'meta'/, 'update cannot set meta'); + + write_raw_config(200, existing_config()); + my $err = update_ct(200, { delete => 'meta' }); + like($err, qr/unable to delete read-only option: 'meta'/, + 'meta cannot be deleted if unset'); +}; + +subtest 'pending changes of meta are dropped' => sub { + my $raw = existing_config('ctime=1500000000') . <<~'EOF'; + + [pve:pending] + delete: meta + meta: ctime=1 + EOF + + reset_state(); + write_raw_config(200, $raw); + my $err = update_ct(200, { hostname => 'changed' }); + is($err, '', 'update succeeds'); + like($task_log, qr/ignoring pending change of read-only option 'meta'/, 'drop is logged'); + my $conf = PVE::LXC::Config->load_config(200); + is(meta_of(200)->{ctime}, 1500000000, 'ctime is kept'); + is_deeply($conf->{pending}, {}, 'pending change of meta is dropped'); + + for my $pending ('meta: ctime=1', 'delete: meta') { + $conf = PVE::LXC::Config::parse_pct_config( + '/lxc/200.conf', + existing_config('ctime=1500000000') . "\n[pve:pending]\n$pending\n", + ); + { + local $SIG{__WARN__} = sub { }; + PVE::LXC::Config->vmconfig_apply_pending(200, $conf, $storage_cfg); + } + is($conf->{meta}, 'ctime=1500000000', "applying '$pending' keeps meta"); + is_deeply($conf->{pending}, {}, "applying drops '$pending'"); + } + + $conf = PVE::LXC::Config::parse_pct_config('/lxc/200.conf', $raw); + my $cgroup_module = Test::MockModule->new('PVE::LXC::CGroup'); + $cgroup_module->mock(new => sub { return bless {}, 'PVE::LXC::CGroup' }); + with_mock( + $lxc_module, + { find_lxc_pid => sub { return 1 } }, + sub { + local $SIG{__WARN__} = sub { }; + PVE::LXC::Config->vmconfig_hotplug_pending(200, $conf, $storage_cfg, undef, {}); + }, + ); + is($conf->{meta}, 'ctime=1500000000', 'hotplugging keeps meta'); + is_deeply($conf->{pending}, {}, 'hotplugging drops the pending change of meta'); +}; + +subtest 'meta with members unknown to this version can be read' => sub { + reset_state(); + write_raw_config(200, existing_config('ctime=1500000000,future=value')); + + my $info = PVE::API2::LXC::Config->map_method_by_name('vm_config'); + my $conf = eval { + local $SIG{__WARN__} = sub { }; + PVE::API2::LXC::Config->handle($info, { node => $NODENAME, vmid => 200 }, 1); + }; + is($@, '', 'config passes the result verification'); + is($conf->{meta}, 'ctime=1500000000,future=value', 'meta is returned as it is'); +}; + +subtest 'create sets the creation time, restore and remote migration keep it' => sub { + reset_state(); + + my $before = time(); + my $err = create_ct(201); + my $after = time(); + is($err, '', 'create succeeds'); + my $ctime = meta_of(201)->{ctime}; + ok($ctime >= $before && $ctime <= $after, 'ctime is the time of the create'); + + $backup_raw_config = backup_config('ctime=1400000000'); + $err = restore_ct(202); + is($err, '', 'restore to an unused VMID succeeds'); + is(meta_of(202)->{ctime}, 1400000000, 'ctime of the backup is kept'); + + write_raw_config(200, existing_config('ctime=1500000000')); + $err = restore_ct(200, { force => 1 }); + is($err, '', 'restore over an existing container succeeds'); + is(meta_of(200)->{ctime}, 1400000000, 'ctime of the backup is used'); + + ($err, my $replies) = run_mtunnel( + 300, + { cmd => 'config', conf => $INCOMING_CONFIG }, + { cmd => 'quit', cleanup => 0 }, + ); + is($err, '', 'tunnel exits normally') or diag(explain($replies), $task_log); + is(meta_of(300)->{ctime}, 1600000000, 'ctime of the source of a migration is kept'); + is(PVE::LXC::Config->load_config(300)->{hostname}, 'incoming', 'with its other options'); +}; + remove_tree($WORKDIR); done_testing(); -- 2.47.3