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 E76181FF0AA for ; Tue, 17 Nov 2026 23:44:45 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id A1FF621AB3; Tue, 17 Nov 2026 23:41:08 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH container v2 19/36] config: add read-only meta property with the creation time Date: Tue, 17 Nov 2026 23:34:29 +0100 Message-ID: <20261117223945.862914-20-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261117223945.862914-1-t.lamprecht@proxmox.com> References: <20261117223945.862914-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794955191904 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.196 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: N4EMTHLSTHRCU2KQL4WEKIXIYZTA2CFL X-Message-ID-Hash: N4EMTHLSTHRCU2KQL4WEKIXIYZTA2CFL 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 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 | 42 ++- src/test/Makefile | 8 +- src/test/config_tests.pl | 23 ++ src/test/lifecycle_tests.pl | 549 ++++++++++++++++++++++++++++++++++++ 6 files changed, 637 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 a61cd16..9000a7e 100644 --- a/src/PVE/API2/LXC.pm +++ b/src/PVE/API2/LXC.pm @@ -426,6 +426,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 -"; @@ -3307,6 +3308,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}; @@ -3341,6 +3344,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..2d5536a 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,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 = { @@ -1310,6 +1325,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 +1334,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 +1375,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 +1466,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 +1685,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 +1813,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..bc6cbf1 --- /dev/null +++ b/src/test/lifecycle_tests.pl @@ -0,0 +1,549 @@ +#!/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::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($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 '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