all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH container v3 21/38] config: add read-only meta property with the creation time
Date: Wed, 18 Nov 2026 05:37:33 +0100	[thread overview]
Message-ID: <20261118043817.1659212-22-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261118043817.1659212-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 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 <t.lamprecht@proxmox.com>
---

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  |  29 +-
 src/PVE/LXC/Config.pm       |  43 ++-
 src/test/Makefile           |   8 +-
 src/test/config_tests.pl    |  23 ++
 src/test/lifecycle_tests.pl | 613 ++++++++++++++++++++++++++++++++++++
 6 files changed, 702 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 be7d64e..753988c 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -427,6 +427,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 -";
 
@@ -3317,6 +3318,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};
@@ -3351,6 +3354,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 b6eaf0b..c274e93 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);
@@ -1449,19 +1467,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) = @_;
 
@@ -1653,6 +1686,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 +1814,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/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..01e0106
--- /dev/null
+++ b/src/test/lifecycle_tests.pl
@@ -0,0 +1,613 @@
+#!/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 v5.36;
+
+use lib qw(..);
+
+# The API modules load modules of pve-ha-manager, which is not a 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::LXC;
+use PVE::LXC::Config;
+use PVE::LXC::Create;
+
+use PVE::API2::LXC;
+use PVE::API2::LXC::Config;
+
+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($path) {
+    return "$WORKDIR/cfs/$path";
+}
+
+my sub ct_config_path($vmid, $node = undef) {
+    return "nodes/" . ($node // $NODENAME) . "/lxc/$vmid.conf";
+}
+
+my sub write_raw_config($vmid, $raw, $node = undef) {
+    my $file = cfs_file(ct_config_path($vmid, $node));
+    make_path($file =~ s|/[^/]+$||r);
+    file_set_contents($file, $raw);
+}
+
+my sub read_raw_config($vmid, $node = undef) {
+    my $file = cfs_file(ct_config_path($vmid, $node));
+    return -f $file ? file_get_contents($file) : undef;
+}
+
+# VMIDs whose container config still existed when their firewall config got removed
+my $vmfw_removed_reserved = {};
+
+my sub reset_configs {
+    remove_tree("$WORKDIR/cfs", "$WORKDIR/rootfs");
+    $vmfw_removed_reserved = {};
+}
+
+my $storage_cfg = {
+    ids => {
+        local => {
+            type => 'dir',
+            path => '/var/lib/vz',
+            content => { rootdir => 1, images => 1, backup => 1, vztmpl => 1 },
+        },
+    },
+};
+
+# recording used guest IDs needs the cluster filesystem and is not what gets tested here
+my $guest_id_module = Test::MockModule->new('PVE::GuestID');
+$guest_id_module->mock(register_used_id => sub { return; });
+
+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($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($self, $msg) {
+        $task_log .= "WARN: $msg\n";
+    }
+
+    sub fork_worker($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($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 ($vmid) = @_;
+        $vmfw_removed_reserved->{$vmid}++ if defined(read_raw_config($vmid));
+    },
+);
+
+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($vmid, $param = undef) {
+    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($vmid, $param = undef) {
+    return run_task(sub {
+        PVE::API2::LXC->create_vm({
+            node => $NODENAME,
+            vmid => $vmid,
+            ostemplate => $BACKUP_VOLID,
+            restore => 1,
+            ($param // {})->%*,
+        });
+    });
+}
+
+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) {
+    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($meta = undef) {
+    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($meta = undef) {
+    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($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 'restore over a container keeps it if replacing it fails early' => sub {
+    $backup_raw_config = backup_config('ctime=1400000000');
+
+    my $kept = sub {
+        my ($name, $expected_err) = @_;
+
+        my $err = restore_ct(200, { force => 1 });
+        like($err, $expected_err, "$name: restore fails");
+        ok(!$vmfw_removed_reserved->{200}, "$name: firewall config is kept");
+        ok(defined(read_raw_config(200)), "$name: config is kept") or return;
+        my $conf = PVE::LXC::Config->load_config(200);
+        is($conf->{hostname}, 'existing', "$name: config is the one of the container");
+        is($conf->{rootfs}, 'local:200/vm-200-disk-0.raw,size=4G', "$name: volume is kept");
+        ok(!defined($conf->{lock}), "$name: lock is removed");
+    };
+
+    my $mocked_create_disks = \&PVE::LXC::create_disks;
+    $lxc_module->mock(create_disks => sub { die "unable to allocate volume - storage full\n" });
+    reset_configs();
+    write_raw_config(200, existing_config('ctime=1500000000'));
+    $kept->('allocation fails', qr/storage full/);
+    $lxc_module->mock(create_disks => $mocked_create_disks);
+
+    my $mocked_destroy = \&PVE::LXC::destroy_lxc_container;
+    $lxc_module->mock(
+        destroy_lxc_container => sub {
+            my ($storecfg, $vmid, $conf) = @_;
+            # like the real function, which checks this before it removes anything
+            die "base volume 'local:200/base-200-disk-0.raw' is still in use by linked clone\n"
+                if $conf->{template};
+            return $mocked_destroy->(@_);
+        },
+    );
+    reset_configs();
+    write_raw_config(200, existing_config('ctime=1500000000') . "template: 1\n");
+    $kept->('template with linked clones', qr/still in use by linked clone/);
+    $lxc_module->mock(destroy_lxc_container => $mocked_destroy);
+};
+
+subtest 'create refuses a container created after the call started' => sub {
+    my $raw = existing_config('ctime=1500000000') =~ s/^hostname: existing$/hostname: other/mr;
+    reset_configs();
+
+    my $mocked_activate = \&PVE::Storage::activate_storage;
+    $storage_module->mock(
+        activate_storage => sub {
+            # another task creates CT 200 between the checks and the reservation
+            write_raw_config(200, $raw) if !defined(read_raw_config(200));
+            return $mocked_activate->(@_);
+        },
+    );
+    # like the guest list of PVE::Cluster, which only changes with cfs_update at the call start
+    my $mocked_unused = \&PVE::Cluster::check_vmid_unused;
+    $cluster_module->mock(check_vmid_unused => sub { return 1 });
+
+    my $err = create_ct(200);
+    $cluster_module->mock(check_vmid_unused => $mocked_unused);
+    $storage_module->mock(activate_storage => $mocked_activate);
+
+    like($err, qr/CT 200 already exists/, 'create is refused');
+    is(read_raw_config(200), $raw, 'container is unchanged');
+};
+
+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





  parent reply	other threads:[~2026-11-18  4:44 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-18  4:37 [PATCH cluster/guest-common/qemu-server/container/manager/docs v3 00/38] fix #7846: guest identity (uid) and cluster ID Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 01/38] pmxcfs: reject changes to the cluster ID file from user space Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 02/38] pmxcfs: generate a cluster ID on standalone nodes Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 03/38] pmxcfs: dfsm: add a callback that runs once all members are in sync Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 04/38] pmxcfs: quorum: pass the corosync membership to an optional listener Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 05/38] pmxcfs: confdb: call an optional listener when the nodes get published Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 06/38] pmxcfs: dcdb: generate the cluster ID once the cluster is in sync Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 07/38] cluster: add helper to read the cluster ID Thomas Lamprecht
2026-11-18  4:37 ` [PATCH cluster v3 08/38] pmxcfs: allow removing the cluster ID file in forced local mode Thomas Lamprecht
2026-11-18  4:37 ` [PATCH guest-common v3 09/38] guest identity: add helpers for the guest uid and registration time Thomas Lamprecht
2026-11-18  4:37 ` [PATCH guest-common v3 10/38] abstract config: assign a guest identity and keep it on config writes Thomas Lamprecht
2026-11-18  4:37 ` [PATCH guest-common v3 11/38] abstract config: let the VMID reservation return the previous state Thomas Lamprecht
2026-11-18  4:37 ` [PATCH guest-common v3 12/38] guest identity: allow taking over a uid that is unused in the cluster Thomas Lamprecht
2026-11-18  4:37 ` [PATCH guest-common v3 13/38] migrate: add shared helpers for the identity command of the migration tunnel Thomas Lamprecht
2026-11-18  4:37 ` [PATCH qemu-server v3 14/38] api: restore: release the VMID if a restore to an unused VMID fails Thomas Lamprecht
2026-11-18  4:37 ` [PATCH qemu-server v3 15/38] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-18  4:37 ` [PATCH qemu-server v3 16/38] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-18  4:37 ` [PATCH qemu-server v3 17/38] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-18  4:37 ` [PATCH qemu-server v3 18/38] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-18  4:37 ` [PATCH qemu-server v3 19/38] api: status: return the guest uid Thomas Lamprecht
2026-11-18  4:37 ` [PATCH qemu-server v3 20/38] tests: remote migration: check a failed command and an early tunnel end Thomas Lamprecht
2026-11-18  4:37 ` Thomas Lamprecht [this message]
2026-11-18  4:37 ` [PATCH container v3 22/38] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-18  4:37 ` [PATCH container v3 23/38] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-18  4:37 ` [PATCH container v3 24/38] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-18  4:37 ` [PATCH container v3 25/38] restore: refuse overwriting a container with another guest's backup Thomas Lamprecht
2026-11-18  4:37 ` [PATCH container v3 26/38] test: remote migration: check the cleanup of the target after failures Thomas Lamprecht
2026-11-18  4:37 ` [PATCH container v3 27/38] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-18  4:37 ` [PATCH container v3 28/38] api: status: return the guest uid Thomas Lamprecht
2026-11-18  4:37 ` [PATCH manager v3 29/38] api: cluster resources: add guest uid and registration time Thomas Lamprecht
2026-11-18  4:37 ` [PATCH manager v3 30/38] api: cluster status: add cluster ID Thomas Lamprecht
2026-11-18  4:37 ` [PATCH manager v3 31/38] ui: restore: allow overwriting a guest with a backup of another guest Thomas Lamprecht
2026-11-18  4:37 ` [PATCH manager v3 32/38] ui: migrate: refuse target nodes that would drop the guest identity Thomas Lamprecht
2026-11-18  4:37 ` [PATCH manager v3 33/38] ui: guest summary: show " Thomas Lamprecht
2026-11-18  4:37 ` [PATCH manager v3 34/38] api: bulk migrate: refuse targets that would drop " Thomas Lamprecht
2026-11-18  4:37 ` [PATCH docs v3 35/38] vzdump: describe the guest identity of VMs and containers Thomas Lamprecht
2026-11-18  4:37 ` [PATCH docs v3 36/38] pvecm: document the cluster ID and how to get a new one Thomas Lamprecht
2026-11-18  4:37 ` [PATCH docs v3 37/38] qm: configuration: describe the read-only meta property Thomas Lamprecht
2026-11-18  4:37 ` [PATCH docs v3 38/38] pct: " Thomas Lamprecht
2026-11-18 13:49 ` [PATCH cluster/guest-common/qemu-server/container/manager/docs v3 00/38] fix #7846: guest identity (uid) and cluster ID Dominik Csapak
2026-11-18 14:13   ` Thomas Lamprecht
2026-11-19 10:38 ` 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=20261118043817.1659212-22-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.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal