From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH container 18/34] config: add read-only meta property with the creation time
Date: Mon, 16 Nov 2026 01:46:30 +0100 [thread overview]
Message-ID: <20261116004703.2908294-19-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261116004703.2908294-1-t.lamprecht@proxmox.com>
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 no parameter of 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 <t.lamprecht@proxmox.com>
---
src/PVE/API2/LXC.pm | 4 +
src/PVE/API2/LXC/Config.pm | 29 +-
src/PVE/LXC/Config.pm | 55 +++-
src/test/Makefile | 8 +-
src/test/config_tests.pl | 23 ++
src/test/lifecycle_tests.pl | 564 ++++++++++++++++++++++++++++++++++++
6 files changed, 665 insertions(+), 18 deletions(-)
create mode 100755 src/test/lifecycle_tests.pl
diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index d736dac..b9a1337 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -423,6 +423,7 @@ __PACKAGE__->register_method({
PVE::LXC::Config->update_pct_config($vmid, $conf, 0, $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 -";
@@ -3277,6 +3278,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};
@@ -3311,6 +3314,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..3d7529f 100644
--- a/src/PVE/API2/LXC/Config.pm
+++ b/src/PVE/API2/LXC/Config.pm
@@ -57,20 +57,23 @@ __PACKAGE__->register_method({
},
returns => {
type => "object",
- properties => PVE::LXC::Config->json_config_properties({
- lxc => {
- description =>
- "Array of lxc low-level configurations ([[key1, value1], [key2, value2] ...]).",
- type => 'array',
- items => { type => 'array', items => { type => 'string' } },
- optional => 1,
- },
- digest => {
- type => 'string',
- description =>
- 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
+ properties => PVE::LXC::Config->json_config_properties(
+ {
+ lxc => {
+ description =>
+ "Array of lxc low-level configurations ([[key1, value1], [key2, value2] ...]).",
+ type => 'array',
+ items => { type => 'array', items => { type => 'string' } },
+ optional => 1,
+ },
+ digest => {
+ type => 'string',
+ description =>
+ 'SHA1 digest of configuration file. This can be used to prevent concurrent modifications.',
+ },
},
- }),
+ 1,
+ ),
},
code => sub {
my ($param) = @_;
diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index fadb0f7..7273cf2 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -504,6 +504,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,
@@ -716,6 +725,12 @@ 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 => "Some (read-only) meta-information about this container.",
+ },
};
my $valid_lxc_conf_keys = {
@@ -1309,6 +1324,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
}
@@ -1317,6 +1333,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;
@@ -1357,7 +1374,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);
@@ -1448,19 +1465,34 @@ sub check_type {
}
}
-# add JSON properties for create and set function
+# add JSON properties for create and set function, with $with_meta also the read-only 'meta' for
+# returning the config
sub json_config_properties {
- my ($class, $prop) = @_;
+ my ($class, $prop, $with_meta) = @_;
foreach my $opt (keys %$confdesc) {
next if $opt eq 'parent' || $opt eq 'snaptime';
+ next if $opt eq 'meta' && !$with_meta;
next if $prop->{$opt};
$prop->{$opt} = $confdesc->{$opt};
}
+ if ($with_meta) {
+ # without its format, as a config written by a newer version can have unknown members
+ $prop->{meta} = { $prop->{meta}->%* };
+ delete $prop->{meta}->{format};
+ }
+
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) = @_;
@@ -1649,9 +1681,24 @@ my $LXC_FASTPLUG_OPTIONS = {
'lock' => 1,
};
+# 'meta' is read-only, so only a manual edit can add a pending change of it. Drop such a change
+# instead of applying it, as it cannot be reverted through the API either.
+my sub drop_pending_meta {
+ my ($class, $conf) = @_;
+
+ my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
+ return if !defined($conf->{pending}->{meta}) && !$pending_delete_hash->{meta};
+
+ warn "ignoring pending change of read-only option 'meta'\n";
+ delete $conf->{pending}->{meta};
+ $class->remove_from_pending_delete($conf, 'meta');
+}
+
sub vmconfig_hotplug_pending {
my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
+ drop_pending_meta($class, $conf);
+
my $pid = PVE::LXC::find_lxc_pid($vmid);
my $rootdir = "/proc/$pid/root";
@@ -1778,6 +1825,8 @@ sub vmconfig_hotplug_pending {
sub vmconfig_apply_pending {
my ($class, $vmid, $conf, $storecfg, $selection, $errors) = @_;
+ drop_pending_meta($class, $conf);
+
my $add_apply_error = sub {
my ($opt, $msg) = @_;
my $err_msg = "unable to apply pending change $opt : $msg";
diff --git a/src/test/Makefile b/src/test/Makefile
index c1ff506..456dfb2 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -2,7 +2,8 @@ RUN_USERNS := lxc-usernsexec -m "u:0:`id -u`:1" -m "g:0:`id -g`:1" --
all: test
-test: test_setup test_snapshot test_bindmount test_idmap test_config test_raw_config_write
+test: test_setup test_snapshot test_bindmount test_idmap test_config test_raw_config_write \
+ test_lifecycle
test_setup: run_setup_tests.pl
if test -e /run/lock/sbuild; then \
@@ -30,5 +31,8 @@ test_config: config_tests.pl
test_raw_config_write: raw_config_write_test.pl
./raw_config_write_test.pl
+test_lifecycle: lifecycle_tests.pl
+ ./lifecycle_tests.pl
+
clean:
- rm -rf tmprootfs
+ rm -rf tmprootfs lifecycle-working
diff --git a/src/test/config_tests.pl b/src/test/config_tests.pl
index beb7e37..a37a9ad 100755
--- a/src/test/config_tests.pl
+++ b/src/test/config_tests.pl
@@ -175,4 +175,27 @@ 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');
+ ok(exists(PVE::LXC::Config->json_config_properties({}, 1)->{meta}), 'meta is returned');
+};
+
done_testing();
diff --git a/src/test/lifecycle_tests.pl b/src/test/lifecycle_tests.pl
new file mode 100755
index 0000000..e6083c9
--- /dev/null
+++ b/src/test/lifecycle_tests.pl
@@ -0,0 +1,564 @@
+#!/usr/bin/perl
+
+# Runs API calls that create, restore, update and migrate containers against a fake cluster
+# filesystem in a working directory, with storage and container runtime operations mocked.
+
+use strict;
+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.
+BEGIN {
+ $INC{'PVE/HA/Config.pm'} = $INC{'PVE/HA/Env/PVE2.pm'} = __FILE__;
+}
+
+use File::Path qw(make_path remove_tree);
+use JSON;
+use Socket qw(AF_UNIX SOCK_STREAM PF_UNSPEC);
+use Test::MockModule;
+use Test::More;
+
+use PVE::Cluster;
+use PVE::GuestHelpers;
+use PVE::INotify;
+use PVE::JSONSchema;
+use PVE::Tools qw(file_get_contents file_set_contents);
+
+use PVE::API2::LXC;
+use PVE::API2::LXC::Config;
+use PVE::LXC;
+use PVE::LXC::Config;
+use PVE::LXC::Create;
+
+my $WORKDIR = 'lifecycle-working';
+my $NODENAME = PVE::INotify::nodename();
+my $BACKUP_VOLID = 'local:backup/vzdump-lxc-100-2026_01_01-00_00_00.tar.zst';
+
+remove_tree($WORKDIR);
+make_path("$WORKDIR/lock");
+
+# Fake cluster filesystem
+
+my sub cfs_file {
+ my ($path) = @_;
+
+ return "$WORKDIR/cfs/$path";
+}
+
+my sub ct_config_path {
+ my ($vmid, $node) = @_;
+
+ return "nodes/" . ($node // $NODENAME) . "/lxc/$vmid.conf";
+}
+
+my sub write_raw_config {
+ my ($vmid, $raw, $node) = @_;
+
+ my $file = cfs_file(ct_config_path($vmid, $node));
+ make_path($file =~ s|/[^/]+$||r);
+ file_set_contents($file, $raw);
+}
+
+my sub read_raw_config {
+ my ($vmid, $node) = @_;
+
+ my $file = cfs_file(ct_config_path($vmid, $node));
+ return -f $file ? file_get_contents($file) : undef;
+}
+
+my sub reset_configs {
+ remove_tree("$WORKDIR/cfs", "$WORKDIR/rootfs");
+}
+
+my $storage_cfg = {
+ ids => {
+ local => {
+ type => 'dir',
+ path => '/var/lib/vz',
+ content => { rootdir => 1, images => 1, backup => 1, vztmpl => 1 },
+ },
+ },
+};
+
+my $cluster_module = Test::MockModule->new('PVE::Cluster');
+$cluster_module->mock(
+ cfs_update => sub { },
+ check_cfs_quorum => sub { },
+ check_node_exists => sub { },
+ get_config => sub {
+ my ($path) = @_;
+ my $file = cfs_file($path);
+ return -f $file ? file_get_contents($file) : undef;
+ },
+ cfs_read_file => sub {
+ my ($path) = @_;
+ die "unexpected read of '$path'\n" if $path !~ m|^nodes/[^/]+/lxc/\d+\.conf$|;
+ my $file = cfs_file($path);
+ return undef if !-f $file;
+ return PVE::LXC::Config::parse_pct_config("/etc/pve/$path", file_get_contents($file));
+ },
+ cfs_write_file => sub {
+ my ($path, $conf) = @_;
+ die "unexpected write of '$path'\n" if $path !~ m|^nodes/[^/]+/lxc/\d+\.conf$|;
+ my $raw = PVE::LXC::Config::write_pct_config("/etc/pve/$path", $conf);
+ my $file = cfs_file($path);
+ make_path($file =~ s|/[^/]+$||r);
+ file_set_contents($file, $raw);
+ },
+ check_vmid_unused => sub {
+ my ($vmid, $noerr) = @_;
+ return 1 if !-f cfs_file(ct_config_path($vmid));
+ return undef if $noerr;
+ die "CT $vmid already exists on node '$NODENAME'\n";
+ },
+);
+
+my $api_module = Test::MockModule->new('PVE::API2::LXC');
+$api_module->mock(
+ cfs_read_file => sub {
+ my ($path) = @_;
+ die "unexpected read of '$path'\n" if $path ne 'storage.cfg';
+ return $storage_cfg;
+ },
+);
+
+my $config_module = Test::MockModule->new('PVE::LXC::Config');
+$config_module->mock(
+ config_file => sub {
+ my ($class, $vmid, $node) = @_;
+ return cfs_file(ct_config_path($vmid, $node));
+ },
+ config_file_lock => sub {
+ my ($class, $vmid) = @_;
+ return "$WORKDIR/lock/pve-config-$vmid.lock";
+ },
+);
+
+my $repl_config_module = Test::MockModule->new('PVE::ReplicationConfig');
+$repl_config_module->mock(
+ new => sub { return bless {}, 'PVE::ReplicationConfig' },
+ check_for_existing_jobs => sub { return undef },
+);
+
+# Fake environment, records task output
+
+my $task_log = '';
+
+package PVE::LXC::Test::RPCEnvironment {
+
+ sub new {
+ my ($class, $user) = @_;
+ return bless { user => $user, type => 'cli' }, $class;
+ }
+
+ sub get_user { return $_[0]->{user} }
+ sub check { return 1 }
+ sub check_vm_perm { return 1 }
+ sub check_pool_exist { return 1 }
+
+ sub warn {
+ my ($self, $msg) = @_;
+ $task_log .= "WARN: $msg\n";
+ }
+
+ sub fork_worker {
+ my ($self, $type, $id, $user, $function) = @_;
+ my $upid = "UPID:$NODENAME:$type:$id";
+ $function->($upid);
+ return $upid;
+ }
+}
+
+my $rpcenv = PVE::LXC::Test::RPCEnvironment->new('root@pam');
+
+my $rpcenv_module = Test::MockModule->new('PVE::RPCEnvironment');
+$rpcenv_module->mock(get => sub { return $rpcenv });
+
+my $restenv_module = Test::MockModule->new('PVE::RESTEnvironment');
+$restenv_module->mock(
+ log_warn => sub {
+ my ($msg) = @_;
+ chomp($msg);
+ $task_log .= "WARN: $msg\n";
+ },
+);
+
+# Runs $code like a task, with its output going to the task log. Returns the error, if any.
+my sub run_task {
+ my ($code) = @_;
+
+ $task_log = '';
+ my $output = '';
+ eval {
+ local *STDOUT;
+ open(STDOUT, '>', \$output) or die "unable to capture output - $!\n";
+ local $SIG{__WARN__} = sub { $output .= "WARN: $_[0]" };
+ $code->();
+ };
+ my $err = $@;
+ $task_log = $output . $task_log;
+
+ return $err;
+}
+
+# Fake storage and container runtime
+
+my $backup_raw_config;
+
+my $storage_module = Test::MockModule->new('PVE::Storage');
+$storage_module->mock(
+ config => sub { return $storage_cfg },
+ activate_storage => sub { },
+ activate_volumes => sub { },
+ deactivate_volumes => sub { },
+ check_volume_access => sub { },
+ storage_check_enabled => sub {
+ my ($cfg, $storeid) = @_;
+ return $storage_cfg->{ids}->{$storeid};
+ },
+ storage_config => sub {
+ my ($cfg, $storeid) = @_;
+ return $storage_cfg->{ids}->{$storeid} // die "storage '$storeid' does not exist\n";
+ },
+ storage_has_feature => sub { return 0 },
+ get_bandwidth_limit => sub { return undef },
+);
+
+my $lxc_module = Test::MockModule->new('PVE::LXC');
+$lxc_module->mock(
+ check_running => sub { return 0 },
+ create_disks => sub {
+ my ($storecfg, $vmid, $settings, $conf) = @_;
+ my $vollist = [];
+ for my $ms (sort keys %$settings) {
+ my $volid = "local:$vmid/vm-$vmid-disk-$ms.raw";
+ $conf->{$ms} = "$volid,size=4G";
+ push @$vollist, $volid;
+ }
+ return $vollist;
+ },
+ destroy_disks => sub { },
+ destroy_lxc_container => sub {
+ my ($storecfg, $vmid, $conf, $replacement_conf) = @_;
+ if (defined($replacement_conf)) {
+ PVE::LXC::Config->write_config($vmid, $replacement_conf);
+ } else {
+ PVE::LXC::Config->destroy_config($vmid);
+ }
+ },
+ mount_all => sub {
+ my ($vmid) = @_;
+ my $rootdir = "$WORKDIR/rootfs/$vmid";
+ make_path($rootdir);
+ return $rootdir;
+ },
+ umount_all => sub { },
+ create_ifaces_ipams_ips => sub { },
+ delete_ifaces_ipams_ips => sub { },
+ update_lxc_config => sub { },
+);
+
+my $create_module = Test::MockModule->new('PVE::LXC::Create');
+$create_module->mock(
+ archive_is_oci_format => sub { return 0 },
+ recover_config => sub {
+ my ($storecfg, $volid, $vmid) = @_;
+ my $conf = PVE::LXC::Config::parse_pct_config("/lxc/$vmid.conf", $backup_raw_config);
+ delete $conf->{snapshots};
+ my $mp_param = {};
+ PVE::LXC::Config->foreach_volume($conf,
+ sub { $mp_param->{ $_[0] } = $conf->{ $_[0] } });
+ return wantarray ? ($conf, $mp_param) : $conf;
+ },
+ restore_archive => sub {
+ my ($storecfg, $archive, $rootdir, $conf) = @_;
+ if (defined($backup_raw_config)) {
+ make_path("$rootdir/etc/vzdump");
+ file_set_contents("$rootdir/etc/vzdump/pct.conf", $backup_raw_config);
+ }
+ },
+);
+
+my $setup_module = Test::MockModule->new('PVE::LXC::Setup');
+$setup_module->mock(new => sub { return bless {}, 'PVE::LXC::Test::Setup' });
+
+package PVE::LXC::Test::Setup {
+ sub post_create_hook { }
+ sub post_restore_hook { }
+ sub post_clone_hook { }
+}
+
+my $firewall_module = Test::MockModule->new('PVE::Firewall');
+$firewall_module->mock(remove_vmfw_conf => sub { });
+
+my $access_control_module = Test::MockModule->new('PVE::AccessControl');
+$access_control_module->mock(assemble_tunnel_ticket => sub { return 'ticket' });
+
+# API call helpers
+
+my sub create_ct {
+ my ($vmid, $param) = @_;
+
+ return run_task(sub {
+ PVE::API2::LXC->create_vm({
+ node => $NODENAME,
+ vmid => $vmid,
+ ostemplate => 'local:vztmpl/debian-13-standard_13.1-2_amd64.tar.zst',
+ hostname => "ct$vmid",
+ ($param // {})->%*,
+ });
+ });
+}
+
+my sub restore_ct {
+ my ($vmid, $param) = @_;
+
+ return run_task(sub {
+ PVE::API2::LXC->create_vm({
+ node => $NODENAME,
+ vmid => $vmid,
+ ostemplate => $BACKUP_VOLID,
+ restore => 1,
+ ($param // {})->%*,
+ });
+ });
+}
+
+my sub update_ct {
+ my ($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 {
+ my ($vmid, @commands) = @_;
+
+ socketpair(my $tunnel_end, my $client_end, AF_UNIX, SOCK_STREAM, PF_UNSPEC)
+ or die "socketpair failed - $!\n";
+ for my $command (@commands) {
+ print $client_end JSON::encode_json($command) . "\n";
+ }
+ $client_end->flush();
+ shutdown($client_end, 1);
+
+ my $socket_module = Test::MockModule->new('IO::Socket::UNIX');
+ $socket_module->mock(
+ new => sub { return bless { conn => $tunnel_end }, 'PVE::LXC::Test::Socket' });
+
+ my $err = run_task(sub { PVE::API2::LXC->mtunnel({ node => $NODENAME, vmid => $vmid }) });
+
+ close($tunnel_end);
+ my $replies = [map { JSON::decode_json($_) } <$client_end>];
+ close($client_end);
+
+ return ($err, $replies);
+}
+
+package PVE::LXC::Test::Socket {
+ sub accept { return $_[0]->{conn} }
+}
+
+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
+ EOF
+
+my sub backup_config {
+ my ($meta) = @_;
+
+ my $raw = <<~'EOF';
+ arch: amd64
+ hostname: backup
+ memory: 2048
+ ostype: debian
+ rootfs: local:100/vm-100-disk-0.raw,size=8G
+ unprivileged: 1
+ EOF
+ $raw .= "meta: $meta\n" if defined($meta);
+
+ return $raw;
+}
+
+my sub existing_config {
+ my ($meta) = @_;
+
+ my $raw = <<~'EOF';
+ arch: amd64
+ hostname: existing
+ memory: 512
+ ostype: debian
+ rootfs: local:200/vm-200-disk-0.raw,size=4G
+ unprivileged: 1
+ EOF
+ $raw .= "meta: $meta\n" if defined($meta);
+
+ return $raw;
+}
+
+# Returns the members of the 'meta' property in the config of $vmid.
+my sub meta_of {
+ my ($vmid) = @_;
+
+ my $conf = PVE::LXC::Config->load_config($vmid);
+
+ return { map { split(/=/, $_, 2) } split(/,/, $conf->{meta} // '') };
+}
+
+# Tests
+
+subtest 'meta cannot be changed through the API' => sub {
+ reset_configs();
+ write_raw_config(200, existing_config('ctime=1500000000'));
+ my $raw = read_raw_config(200);
+
+ for my $user ('root@pam', 'admin@pve') {
+ $rpcenv = PVE::LXC::Test::RPCEnvironment->new($user);
+
+ my $err = update_ct(200, { meta => 'ctime=1' });
+ like($err, qr/meta: property is not defined in schema/, "$user cannot set meta");
+
+ $err = update_ct(200, { delete => 'meta' });
+ like($err, qr/unable to delete read-only option: 'meta'/, "$user cannot delete meta");
+
+ $err = update_ct(200, { revert => 'meta' });
+ like($err, qr/unable to revert read-only option: 'meta'/, "$user cannot revert meta");
+
+ $err = create_ct(201, { meta => 'ctime=1' });
+ like($err, qr/meta: property is not defined in schema/,
+ "$user cannot create with meta");
+
+ $err = restore_ct(200, { force => 1, meta => 'ctime=1' });
+ like(
+ $err,
+ qr/meta: property is not defined in schema/,
+ "$user cannot restore with meta",
+ );
+ }
+ $rpcenv = PVE::LXC::Test::RPCEnvironment->new('root@pam');
+
+ 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_configs();
+ 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' });
+ $lxc_module->mock(find_lxc_pid => sub { return 1 });
+ {
+ local $SIG{__WARN__} = sub { };
+ PVE::LXC::Config->vmconfig_hotplug_pending(200, $conf, $storage_cfg, undef, {});
+ }
+ $lxc_module->unmock('find_lxc_pid');
+ 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_configs();
+ 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' => sub {
+ reset_configs();
+
+ 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');
+};
+
+subtest 'restore keeps the meta information of the backup' => sub {
+ reset_configs();
+
+ $backup_raw_config = backup_config('ctime=1400000000');
+ my $err = restore_ct(201);
+ is($err, '', 'restore to a free VMID succeeds');
+ is(meta_of(201)->{ctime}, 1400000000, 'backup ctime 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, 'backup ctime is used');
+};
+
+subtest 'remote migration keeps the meta information of the source' => sub {
+ reset_configs();
+
+ my ($err, $replies) = run_mtunnel(
+ 300,
+ { cmd => 'config', conf => $INCOMING_CONFIG },
+ { cmd => 'quit', cleanup => 0 },
+ );
+ is($err, '', 'tunnel exits normally') or diag(explain($replies), $task_log);
+ ok($replies->[0]->{success}, 'config command succeeds');
+
+ my $conf = PVE::LXC::Config->load_config(300);
+ is(meta_of(300)->{ctime}, 1600000000, 'source ctime is kept');
+ is($conf->{hostname}, 'incoming', 'other options are applied');
+ is($conf->{lock}, 'migrate', 'config is locked for the migration');
+};
+
+remove_tree($WORKDIR);
+
+done_testing();
--
2.47.3
next prev parent reply other threads:[~2026-11-16 0:51 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 ` [PATCH guest-common 10/34] abstract config: let the VMID reservation return the previous state Thomas Lamprecht
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 ` Thomas Lamprecht [this message]
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-19-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox