From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id B0F461FF0DE for ; Thu, 19 Nov 2026 11:42:03 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id DB8F421794; Thu, 19 Nov 2026 11:37:20 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v4 16/41] fix #7846: config: add a guest identity to the 'meta' property Date: Thu, 19 Nov 2026 11:34:02 +0100 Message-ID: <20261119103551.1666964-17-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261119103551.1666964-1-t.lamprecht@proxmox.com> References: <20261119103551.1666964-1-t.lamprecht@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795084556181 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.159 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: NMS7DQFEOV3HRZHMRESVSCZLNRH4XRTV X-Message-ID-Hash: NMS7DQFEOV3HRZHMRESVSCZLNRH4XRTV X-MailFrom: t.lamprecht@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Give VMs a guest identity in the read-only 'meta' property: a random identifier, 'uid', and a registration time, 'rtime'. Tools can use it to recognize a guest across restores and migrations, independently of its VMID. Enable the identity handling of the config class, so that each config write keeps the identity a VM already has or assigns one if it has none. New VMs, clones and imports get an identity when their VMID is reserved. Existing VMs get one with their next config write. A stale config, a clone source or a snapshot rollback cannot replace the identity of a VM. The other members of 'meta' behave as before. A config without creation information thus gets a 'meta' property with only the identity. Older qemu-server versions reject the new members and drop the whole property on their next config write. Signed-off-by: Thomas Lamprecht --- changes v3 -> v4: * tests: 'create' and 'clone' call the API handlers instead of repeating their config writes, and cases that only checked the schema parser are gone * the identity parts of the fake cluster filesystem come with this patch, its base is in an independent test commit before the series * rebase onto current master (tests only) changes v2 -> v3: * group the imports of the new test script changes v1 -> v2: * rebase, adapt the tests src/PVE/QemuConfig.pm | 7 + src/PVE/QemuServer/MetaInfo.pm | 2 + src/test/Makefile | 5 +- src/test/QemuServerTest/FakeCFS.pm | 84 +++++- .../q35-windows-pinning-identity-only.conf | 6 + ...q35-windows-pinning-identity-only.conf.cmd | 26 ++ .../meta-identity-invalid.conf | 6 + .../meta-identity-invalid.conf.strict.error | 2 + .../meta-identity-invalid.conf | 7 + .../parse-config-input/meta-identity.conf | 20 ++ src/test/run_guest_identity_tests.pl | 275 ++++++++++++++++++ src/test/run_parse_config_tests.pl | 2 +- src/test/run_qemu_restore_config_tests.pl | 3 +- .../create/qemu-server/401.conf | 31 ++ .../create/qemu-server/402.conf | 31 ++ .../rollback/qemu-server/401.conf | 36 +++ .../rollback/qemu-server/402.conf | 35 +++ .../create/qemu-server/401.conf | 13 + .../create/qemu-server/402.conf | 13 + .../rollback/qemu-server/401.conf | 35 +++ .../rollback/qemu-server/402.conf | 34 +++ src/test/snapshot-test.pm | 82 +++++- 22 files changed, 738 insertions(+), 17 deletions(-) create mode 100644 src/test/cfg2cmd/q35-windows-pinning-identity-only.conf create mode 100644 src/test/cfg2cmd/q35-windows-pinning-identity-only.conf.cmd create mode 100644 src/test/parse-config-expected/meta-identity-invalid.conf create mode 100644 src/test/parse-config-expected/meta-identity-invalid.conf.strict.error create mode 100644 src/test/parse-config-input/meta-identity-invalid.conf create mode 100644 src/test/parse-config-input/meta-identity.conf create mode 100755 src/test/run_guest_identity_tests.pl create mode 100644 src/test/snapshot-expected/create/qemu-server/401.conf create mode 100644 src/test/snapshot-expected/create/qemu-server/402.conf create mode 100644 src/test/snapshot-expected/rollback/qemu-server/401.conf create mode 100644 src/test/snapshot-expected/rollback/qemu-server/402.conf create mode 100644 src/test/snapshot-input/create/qemu-server/401.conf create mode 100644 src/test/snapshot-input/create/qemu-server/402.conf create mode 100644 src/test/snapshot-input/rollback/qemu-server/401.conf create mode 100644 src/test/snapshot-input/rollback/qemu-server/402.conf diff --git a/src/PVE/QemuConfig.pm b/src/PVE/QemuConfig.pm index 6656c137..e7adfbfb 100644 --- a/src/PVE/QemuConfig.pm +++ b/src/PVE/QemuConfig.pm @@ -672,6 +672,13 @@ sub write_config { return $class->SUPER::write_config($vmid, $conf); } +# the format of the 'meta' property includes the guest identity, see PVE::QemuServer::MetaInfo +sub __identity_enabled { + my ($class) = @_; + + return 1; +} + # END implemented abstract methods from PVE::AbstractConfig sub has_cloudinit { diff --git a/src/PVE/QemuServer/MetaInfo.pm b/src/PVE/QemuServer/MetaInfo.pm index b69e5a1c..399cd920 100644 --- a/src/PVE/QemuServer/MetaInfo.pm +++ b/src/PVE/QemuServer/MetaInfo.pm @@ -3,6 +3,7 @@ package PVE::QemuServer::MetaInfo; use strict; use warnings; +use PVE::GuestIdentity; use PVE::JSONSchema; use PVE::QemuServer::Helpers; @@ -20,6 +21,7 @@ our $meta_info_fmt = { pattern => '\d+(\.\d+)+', optional => 1, }, + PVE::GuestIdentity::meta_format_properties()->%*, }; sub parse_meta_info { diff --git a/src/test/Makefile b/src/test/Makefile index 7cd88247..0d47205d 100644 --- a/src/test/Makefile +++ b/src/test/Makefile @@ -1,6 +1,6 @@ all: test -test: test_snapshot test_cfg_to_cmd test_cfg_to_cmd_aarch64 test_hotplug test_hotplug_aarch64 test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config test_memory test_agent test_raw_config_write test_api test_api_cloudinit_dump test_mtunnel +test: test_snapshot test_cfg_to_cmd test_cfg_to_cmd_aarch64 test_hotplug test_hotplug_aarch64 test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config test_memory test_agent test_raw_config_write test_api test_api_cloudinit_dump test_mtunnel test_guest_identity test_snapshot: run_snapshot_tests.pl ./run_snapshot_tests.pl @@ -60,6 +60,9 @@ test_api_cloudinit_dump: run_api_cloudinit_dump_tests.pl test_mtunnel: run_mtunnel_tests.pl QemuServerTest/FakeCFS.pm QemuServerTest/MTunnel.pm ./run_mtunnel_tests.pl +test_guest_identity: run_guest_identity_tests.pl QemuServerTest/FakeCFS.pm + ./run_guest_identity_tests.pl + .PHONY: clean clean: rm -rf MigrationTest/run diff --git a/src/test/QemuServerTest/FakeCFS.pm b/src/test/QemuServerTest/FakeCFS.pm index c5609892..a9949d63 100644 --- a/src/test/QemuServerTest/FakeCFS.pm +++ b/src/test/QemuServerTest/FakeCFS.pm @@ -1,10 +1,12 @@ package QemuServerTest::FakeCFS; # Fake cluster filesystem for VM config files, so that tests can use the real config class methods -# of PVE::QemuConfig without pmxcfs. +# of PVE::QemuConfig, including the guest identity handling of write_config, without pmxcfs. # # Configs are kept as raw text, indexed by VMID, and an empty file at the path that config_file() -# returns exists for each, for checks of the file's existence. +# returns exists for each, for checks of the file's existence. Generated guest identities are +# deterministic: the n-th generated uid is 'a' repeated 17 times followed by the base32 encoding of +# n, and rtime is $now_ms. use v5.36; @@ -15,10 +17,15 @@ use Test::MockModule; use PVE::GuestHelpers; use PVE::GuestID; +use PVE::GuestIdentity; use PVE::QemuConfig; use PVE::QemuServer; our $configs = {}; # VMID => raw config, for VMs on the local node +our $other_guests = {}; # VMID => raw 'meta' property of other guests, like on other nodes or CTs +our $now_ms = 1700000000000; +our $generated = 0; +our $lock_domain_error; our $written = {}; # VMID => the written raw configs, in the order of the writes my $tmpdir = tempdir('fake-cfs-XXXXXX', DIR => '.', CLEANUP => 1); @@ -28,6 +35,10 @@ $PVE::GuestHelpers::lockdir = "$tmpdir/lock"; sub reset() { remove_tree("$tmpdir/etc-pve"); $configs = {}; + $other_guests = {}; + $now_ms = 1700000000000; + $generated = 0; + $lock_domain_error = undef; $written = {}; } @@ -53,12 +64,55 @@ sub set_config($vmid, $raw) { sync_config_file($vmid); } +# Returns the last 'meta' property of the top-level section of the raw config $raw, like the config +# class reads the stored identity. +my sub top_level_meta($raw) { + my $meta; + for my $line (split(/\n/, $raw // '')) { + last if $line =~ m/^\[/; + # like pmxcfs, a line with nothing after the colon does not count + $meta = $1 =~ s/^\s+|\s+$//gr if $line =~ m/^meta:(.+)$/; + } + + return $meta; +} + +# Returns the identity stored in the raw config of $vmid, undef if there is none. +sub stored_identity($vmid) { + return PVE::GuestIdentity::parse_identity(top_level_meta($configs->{$vmid})); +} + +# Returns the uid that the $n-th generation of an identity yields. +sub generated_uid($n) { + my $alphabet = 'abcdefghijklmnopqrstuvwxyz234567'; + return ('a' x 17) . join('', map { substr($alphabet, ($n >> (5 * $_)) & 31, 1) } (2, 1, 0)); +} + # the list of used guest IDs is not part of the fake cluster filesystem our $guest_id_module = Test::MockModule->new('PVE::GuestID'); $guest_id_module->mock(register_used_id => sub { return; }); +our $guest_identity_module = Test::MockModule->new('PVE::GuestIdentity'); +$guest_identity_module->mock( + __random_bytes => sub { + my ($len) = @_; + die "unexpected length $len\n" if $len != 20; + $generated++; + # one byte per uid character, the last three hold 5 bits of the counter each + return ("\0" x 17) . pack('C3', map { ($generated >> $_) & 31 } (10, 5, 0)); + }, + __time_ms => sub { + return $now_ms; + }, +); + our $cluster_module = Test::MockModule->new('PVE::Cluster'); $cluster_module->mock( + get_config => sub { + my ($path) = @_; + my $vmid = vmid_of_path($path) // die "get_config (mocked) - unexpected path '$path'\n"; + return $configs->{$vmid}; + }, cfs_read_file => sub { my ($path) = @_; return {} if $path eq 'datacenter.cfg'; @@ -80,10 +134,34 @@ $cluster_module->mock( }, check_vmid_unused => sub { my ($vmid, $noerr) = @_; - return 1 if !defined($configs->{$vmid}); + return 1 if !defined($configs->{$vmid}) && !defined($other_guests->{$vmid}); return undef if $noerr; die "VM $vmid already exists\n"; }, + cfs_lock_domain => sub { + my ($domain, $timeout, $code, @param) = @_; + die "cfs_lock_domain (mocked) - unexpected domain '$domain'\n" + if $domain ne 'guest-identity'; + if ($lock_domain_error) { + $@ = $lock_domain_error; + return undef; + } + my $res = eval { $code->(@param) }; + return $res; + }, + get_guest_config_properties => sub { + my ($properties) = @_; + die "get_guest_config_properties (mocked) - unexpected properties\n" + if join(',', $properties->@*) ne 'meta'; + + my $res = {}; + for my $vmid (keys $configs->%*) { + my $meta = top_level_meta($configs->{$vmid}); + $res->{$vmid} = { meta => $meta } if defined($meta); + } + $res->{$_} = { meta => $other_guests->{$_} } for keys $other_guests->%*; + return $res; + }, ); our $qemu_config_module = Test::MockModule->new('PVE::QemuConfig'); diff --git a/src/test/cfg2cmd/q35-windows-pinning-identity-only.conf b/src/test/cfg2cmd/q35-windows-pinning-identity-only.conf new file mode 100644 index 00000000..5242ec13 --- /dev/null +++ b/src/test/cfg2cmd/q35-windows-pinning-identity-only.conf @@ -0,0 +1,6 @@ +# TEST: Windows without pinned machine and with only the guest identity as meta info stays on 5.1 +machine: q35 +meta: rtime=1762300000000,uid=abcdefghijklmnopqrst +ostype: win11 +smbios1: uuid=7b10d7af-b932-4c66-b2c3-3996152ec465 +vmgenid: c773c261-d800-4348-9f5d-167fadd53cf8 diff --git a/src/test/cfg2cmd/q35-windows-pinning-identity-only.conf.cmd b/src/test/cfg2cmd/q35-windows-pinning-identity-only.conf.cmd new file mode 100644 index 00000000..67d20b79 --- /dev/null +++ b/src/test/cfg2cmd/q35-windows-pinning-identity-only.conf.cmd @@ -0,0 +1,26 @@ +/usr/bin/kvm \ + -id 8006 \ + -name vm8006 \ + -no-shutdown \ + -chardev 'socket,id=qmp,path=/var/run/qemu-server/8006.qmp,server=on,wait=off' \ + -object 'monitor-qmp,chardev=qmp,id=monitor-qmp' \ + -chardev 'socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect-ms=5000' \ + -object 'monitor-qmp,chardev=qmp-event,id=monitor-qmp-event' \ + -pidfile /var/run/qemu-server/8006.pid \ + -daemonize \ + -smbios 'type=1,uuid=7b10d7af-b932-4c66-b2c3-3996152ec465' \ + -smp '1,sockets=1,cores=1,maxcpus=1' \ + -nodefaults \ + -boot 'menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg' \ + -vnc 'unix:/var/run/qemu-server/8006.vnc,password=on' \ + -global 'kvm-pit.lost_tick_policy=discard' \ + -cpu 'kvm64,-cet-ibt,-cet-ss,enforce,hv_ipi,hv_relaxed,hv_reset,hv_runtime,hv_spinlocks=0x1fff,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vpindex,+kvm_pv_eoi,+kvm_pv_unhalt,+lahf_lm,+sep' \ + -m 512 \ + -readconfig /usr/share/qemu-server/pve-q35-4.0.cfg \ + -device 'vmgenid,guid=c773c261-d800-4348-9f5d-167fadd53cf8' \ + -device 'usb-tablet,id=tablet,bus=ehci.0,port=1' \ + -device 'VGA,id=vga,bus=pcie.0,addr=0x1,edid=off' \ + -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3' \ + -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \ + -rtc 'driftfix=slew,base=localtime' \ + -machine 'hpet=off,type=pc-q35-5.1+pve0' diff --git a/src/test/parse-config-expected/meta-identity-invalid.conf b/src/test/parse-config-expected/meta-identity-invalid.conf new file mode 100644 index 00000000..195514fa --- /dev/null +++ b/src/test/parse-config-expected/meta-identity-invalid.conf @@ -0,0 +1,6 @@ +# VM with a guest uid that is not valid +boot: order=scsi0 +cores: 2 +memory: 4096 +name: identity +scsi0: local-lvm:vm-8006-disk-0,size=32G diff --git a/src/test/parse-config-expected/meta-identity-invalid.conf.strict.error b/src/test/parse-config-expected/meta-identity-invalid.conf.strict.error new file mode 100644 index 00000000..ae584c1f --- /dev/null +++ b/src/test/parse-config-expected/meta-identity-invalid.conf.strict.error @@ -0,0 +1,2 @@ +vm 8006 - unable to parse value of 'meta' - format error +uid: value does not match the regex pattern diff --git a/src/test/parse-config-input/meta-identity-invalid.conf b/src/test/parse-config-input/meta-identity-invalid.conf new file mode 100644 index 00000000..90e60197 --- /dev/null +++ b/src/test/parse-config-input/meta-identity-invalid.conf @@ -0,0 +1,7 @@ +# VM with a guest uid that is not valid +boot: order=scsi0 +cores: 2 +memory: 4096 +meta: creation-qemu=10.1.2,ctime=1762000000,rtime=1762000000123,uid=ABCDEFGHIJKLMNOPQRST +name: identity +scsi0: local-lvm:vm-8006-disk-0,size=32G diff --git a/src/test/parse-config-input/meta-identity.conf b/src/test/parse-config-input/meta-identity.conf new file mode 100644 index 00000000..7a1e03be --- /dev/null +++ b/src/test/parse-config-input/meta-identity.conf @@ -0,0 +1,20 @@ +# VM with guest identity +boot: order=scsi0 +cores: 2 +memory: 4096 +meta: creation-qemu=10.1.2,ctime=1762000000,rtime=1762000000123,uid=abcdefghijklmnopqrst +name: identity +scsi0: local-lvm:vm-8006-disk-0,size=32G +scsihw: virtio-scsi-single +smbios1: uuid=7b4f2a1e-3c2d-4e5f-8a9b-0c1d2e3f4a5b + +[snap] +boot: order=scsi0 +cores: 2 +memory: 4096 +meta: creation-qemu=10.1.2,ctime=1762000000,rtime=1762000000123,uid=abcdefghijklmnopqrst +name: identity +scsi0: local-lvm:vm-8006-disk-0,size=32G +scsihw: virtio-scsi-single +smbios1: uuid=7b4f2a1e-3c2d-4e5f-8a9b-0c1d2e3f4a5b +snaptime: 1762000100 diff --git a/src/test/run_guest_identity_tests.pl b/src/test/run_guest_identity_tests.pl new file mode 100755 index 00000000..3eec153f --- /dev/null +++ b/src/test/run_guest_identity_tests.pl @@ -0,0 +1,275 @@ +#!/usr/bin/perl + +# Tests the guest identity of VM configs through the real config class methods, with a fake cluster +# filesystem, see QemuServerTest::FakeCFS. + +use v5.36; + +use lib qw(.. .); + +use File::Temp qw(tempdir); +use Test::MockModule; +use Test::More; + +use PVE::JSONSchema; +use PVE::RPCEnvironment; + +use PVE::QemuConfig; +use PVE::QemuConfig::NoWrite; +use PVE::QemuServer; +use PVE::QemuServer::MetaInfo; + +use PVE::API2::Qemu; + +use QemuServerTest::FakeCFS; +use QemuServerTest::Utils qw(capture_output); + +my $UID_A = 'abcdefghijklmnopqrst'; +my $UID_B = 'bbbbbbbbbbbbbbbbbbbb'; + +my $helpers_module = Test::MockModule->new('PVE::QemuServer::Helpers'); +$helpers_module->mock(kvm_user_version => sub { return '10.1.2'; }); + +my $rpcenv = PVE::RPCEnvironment->init('cli'); +$rpcenv->set_user('root@pam'); + +my $rpcenv_module = Test::MockModule->new('PVE::RPCEnvironment'); +$rpcenv_module->mock( + check => sub { return 1; }, + check_vm_perm => sub { return 1; }, + fork_worker => sub { + my ($self, $type, $id, $user, $code) = @_; + $code->('UPID:test'); + return 'UPID:test'; + }, +); +$QemuServerTest::FakeCFS::cluster_module->mock(check_cfs_quorum => sub { return 1; }); +my $network_module = Test::MockModule->new('PVE::QemuServer::Network'); +$network_module->mock(create_ifaces_ipams_ips => sub { return; }); + +sub configs { return $QemuServerTest::FakeCFS::configs; } +sub generated_uid { return QemuServerTest::FakeCFS::generated_uid(@_); } +sub stored_identity { return QemuServerTest::FakeCFS::stored_identity(@_); } + +sub load_meta($vmid) { + return PVE::QemuServer::MetaInfo::parse_meta_info(PVE::QemuConfig->load_config($vmid)->{meta}); +} + +subtest 'meta format' => sub { + my $meta = + 'creation-qemu=10.1.2,ctime=1700000000,rtime=1700000000123,uid=abcdefghijklmnopqrst'; + + is_deeply( + PVE::QemuServer::MetaInfo::parse_meta_info($meta), + { + 'creation-qemu' => '10.1.2', + ctime => 1700000000, + rtime => 1700000000123, + uid => $UID_A, + }, + 'identity members get parsed', + ); + is( + PVE::JSONSchema::print_property_string( + PVE::QemuServer::MetaInfo::parse_meta_info($meta), + $PVE::QemuServer::MetaInfo::meta_info_fmt, + ), + $meta, + 'printing the parsed property gives the same string', + ); +}; + +subtest 'identities are enabled, but not for writing temporary configs' => sub { + QemuServerTest::FakeCFS::reset(); + + ok(PVE::QemuConfig->__identity_enabled(), 'enabled for PVE::QemuConfig'); + + my $conf = { name => 'template-cmd' }; + PVE::QemuConfig::NoWrite->mark_config($conf); + eval { PVE::QemuConfig->write_config(100, $conf) }; + is($@, "refusing to write temporary configuration\n", 'NoWrite refuses'); + eval { PVE::QemuConfig::NoWrite->write_config(100, $conf) }; + is( + $@, + "refusing to write temporary configuration\n", + 'NoWrite refuses when called directly', + ); + is_deeply(configs(), {}, 'nothing written'); + is($QemuServerTest::FakeCFS::generated, 0, 'no identity generated'); +}; + +subtest 'config writes keep the stored identity' => sub { + QemuServerTest::FakeCFS::reset(); + + PVE::QemuConfig->write_config(100, { memory => 512, meta => "rtime=1,uid=$UID_B" }); + is_deeply( + stored_identity(100), + { uid => generated_uid(1), rtime => 1700000000000 }, + 'first write ignores a carried identity and generates one', + ); + like(configs()->{100}, qr/^meta: rtime=1700000000000,uid=\w+$/m, + 'written as meta property'); + + $QemuServerTest::FakeCFS::now_ms = 1800000000000; + + my $conf = PVE::QemuConfig->load_config(100); + $conf->{meta} = "creation-qemu=9.2.0,ctime=1600000000,rtime=1,uid=$UID_B"; + PVE::QemuConfig->write_config(100, $conf); + is_deeply( + load_meta(100), + { + 'creation-qemu' => '9.2.0', + ctime => 1600000000, + rtime => 1700000000000, + uid => generated_uid(1), + }, + 'foreign identity replaced by the stored one, other members kept', + ); + + $conf = PVE::QemuConfig->load_config(100); + delete $conf->{meta}; + PVE::QemuConfig->write_config(100, $conf); + is_deeply( + load_meta(100), + { rtime => 1700000000000, uid => generated_uid(1) }, + 'identity kept when the property is gone', + ); + + configs()->{101} = "memory: 512\nmeta: creation-qemu=8.0.2,ctime=1500000000\n"; + $conf = PVE::QemuConfig->load_config(101); + $conf->{name} = 'old'; + PVE::QemuConfig->write_config(101, $conf); + is_deeply( + load_meta(101), + { + 'creation-qemu' => '8.0.2', + ctime => 1500000000, + rtime => 1800000000000, + uid => generated_uid(2), + }, + 'existing guest without identity gets one on its first write', + ); +}; + +subtest 'create' => sub { + QemuServerTest::FakeCFS::reset(); + + my $storage_module = Test::MockModule->new('PVE::Storage'); + $storage_module->mock(config => sub { return {}; }); + my $replication_state_module = Test::MockModule->new('PVE::ReplicationState'); + $replication_state_module->mock(delete_guest_states => sub { return; }); + + capture_output(sub { + PVE::API2::Qemu->create_vm({ node => 'localhost', vmid => 100, memory => 512 }); + }); + + my $reserved = $QemuServerTest::FakeCFS::written->{100}->[0]; + like( + $reserved, + qr/^meta: rtime=1700000000000,uid=\w+$/m, + 'reservation stores a new identity', + ); + my $meta = load_meta(100); + is_deeply( + [$meta->@{qw(creation-qemu uid rtime)}], + ['10.1.2', generated_uid(1), 1700000000000], + 'final config keeps the reserved identity next to the creation info', + ); + ok($meta->{ctime}, 'creation time set'); +}; + +subtest 'clone' => sub { + QemuServerTest::FakeCFS::reset(); + + my $disk = "scsi0: local:100/vm-100-disk-0.raw,size=4G\n"; + my $source = "memory: 512\nmeta: creation-qemu=9.2.0,ctime=1600000000," + . "rtime=1600000000000,uid=$UID_A\nname: source\n$disk"; + # a snapshot can carry any identity + configs()->{100} = + "$source\n[snap]\nmemory: 256\nmeta: rtime=1,uid=$UID_B\n${disk}snaptime: 1600000001\n"; + + my $storage_module = Test::MockModule->new('PVE::Storage'); + $storage_module->mock( + activate_volumes => sub { return; }, + config => sub { + return { + ids => { + local => + { type => 'dir', path => '/var/lib/vz', content => { images => 1 } }, + }, + }; + }, + deactivate_volumes => sub { return; }, + get_bandwidth_limit => sub { return undef; }, + volume_has_feature => sub { return 1; }, + ); + my $qemu_server_module = Test::MockModule->new('PVE::QemuServer'); + $qemu_server_module->mock( + check_running => sub { return 0; }, + clone_disk => sub { + my ($storecfg, $source, $dest) = @_; + return { file => "local:$dest->{vmid}/vm-$dest->{vmid}-disk-0.raw", size => '4G' }; + }, + ); + my $firewall_module = Test::MockModule->new('PVE::Firewall'); + $firewall_module->mock(clone_vmfw_conf => sub { return; }); + + my $output = capture_output(sub { + PVE::API2::Qemu->clone_vm({ node => 'localhost', vmid => 100, newid => 300 }); + PVE::API2::Qemu->clone_vm( + { node => 'localhost', vmid => 100, newid => 301, snapname => 'snap' }); + }); + + is(stored_identity(300)->{uid}, generated_uid(1), 'clone has a new uid'); + is_deeply( + load_meta(300), + { + 'creation-qemu' => '9.2.0', + ctime => 1600000000, + rtime => 1700000000000, + uid => generated_uid(1), + }, + 'clone keeps the creation info of its source', + ); + is_deeply( + load_meta(301), + { rtime => 1700000000000, uid => generated_uid(2) }, + 'clone of a snapshot has a new uid as well', + ); + is(stored_identity(100)->{uid}, $UID_A, 'source keeps its uid'); +}; + +subtest 'importovf' => sub { + QemuServerTest::FakeCFS::reset(); + + require PVE::CLI::qm; + + my $tmpdir = tempdir('importovf-XXXXXX', DIR => '.', CLEANUP => 1); + my $manifest = "$tmpdir/test.ovf"; + PVE::Tools::file_set_contents($manifest, "\n"); + + my $storage_module = Test::MockModule->new('PVE::Storage'); + $storage_module->mock( + config => sub { return {}; }, + storage_check_enabled => sub { return {}; }, + ); + my $ovf_module = Test::MockModule->new('PVE::GuestImport::OVF'); + $ovf_module->mock( + parse_ovf => sub { + return { qm => { name => 'imported', memory => 1024, cores => 2 }, disks => [] }; + }, + ); + + PVE::CLI::qm->importovf({ vmid => 100, manifest => $manifest, storage => 'local' }); + + my $conf = PVE::QemuConfig->load_config(100); + is($conf->{name}, 'imported', 'config imported'); + ok(!defined($conf->{lock}), 'create lock removed'); + is_deeply( + stored_identity(100), + { uid => generated_uid(1), rtime => 1700000000000 }, + 'imported VM got a new identity on reservation and kept it', + ); +}; + +done_testing(); diff --git a/src/test/run_parse_config_tests.pl b/src/test/run_parse_config_tests.pl index c30aad99..566eec4f 100755 --- a/src/test/run_parse_config_tests.pl +++ b/src/test/run_parse_config_tests.pl @@ -25,7 +25,7 @@ my $INPUT_DIR = './parse-config-input'; my $EXPECTED_DIR = './parse-config-expected'; # NOTE update when you add/remove tests -plan tests => 2 * 11; +plan tests => 2 * 13; sub run_tests { my ($strict) = @_; diff --git a/src/test/run_qemu_restore_config_tests.pl b/src/test/run_qemu_restore_config_tests.pl index f5f9e026..4e2bc798 100755 --- a/src/test/run_qemu_restore_config_tests.pl +++ b/src/test/run_qemu_restore_config_tests.pl @@ -312,7 +312,8 @@ sub restore_api { return ($err, $output); } -my $TARGET_CONF = "memory: 1024\nmeta: creation-qemu=10.0.0,ctime=1700000000\nname: target\n"; +my $TARGET_CONF = "memory: 1024\nmeta: creation-qemu=10.0.0,ctime=1700000000," + . "rtime=1700000000000,uid=tttttttttttttttttttt\nname: target\n"; my $NOT_CLEANED_UP = qr/VM 100 state is NOT cleaned up/; my $KEPT_LOCKED = 'WARN: keeping VM 100 locked, the cleanup left behind'; diff --git a/src/test/snapshot-expected/create/qemu-server/401.conf b/src/test/snapshot-expected/create/qemu-server/401.conf new file mode 100644 index 00000000..87aadf80 --- /dev/null +++ b/src/test/snapshot-expected/create/qemu-server/401.conf @@ -0,0 +1,31 @@ +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: creation-qemu=10.0.0,ctime=1700000000,rtime=1700000000000,uid=abcdefghijklmnopqrst +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +parent: test +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 1 +vga: qxl + +[test] +#test comment +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: creation-qemu=10.0.0,ctime=1700000000,rtime=1700000000000,uid=abcdefghijklmnopqrst +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +snaptime: 1234567890 +sockets: 1 +vga: qxl diff --git a/src/test/snapshot-expected/create/qemu-server/402.conf b/src/test/snapshot-expected/create/qemu-server/402.conf new file mode 100644 index 00000000..976c0a39 --- /dev/null +++ b/src/test/snapshot-expected/create/qemu-server/402.conf @@ -0,0 +1,31 @@ +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: creation-qemu=8.0.2,ctime=1600000000,rtime=1234567890123,uid=qrstuvwxyzbcdefghijk +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +parent: test +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 1 +vga: qxl + +[test] +#test comment +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: creation-qemu=8.0.2,ctime=1600000000 +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +snaptime: 1234567890 +sockets: 1 +vga: qxl diff --git a/src/test/snapshot-expected/rollback/qemu-server/401.conf b/src/test/snapshot-expected/rollback/qemu-server/401.conf new file mode 100644 index 00000000..6215ce26 --- /dev/null +++ b/src/test/snapshot-expected/rollback/qemu-server/401.conf @@ -0,0 +1,36 @@ +# this is a description +agent: 1 +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: creation-qemu=9.2.0,ctime=1600000000,rtime=1700000000000,uid=abcdefghijklmnopqrst +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +parent: test +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 1 +vga: qxl + +[test] +#test comment +agent: 1 +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +machine: q35 +memory: 8192 +meta: creation-qemu=9.2.0,ctime=1600000000,rtime=1600000000000,uid=bbbbbbbbbbbbbbbbbbbb +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +snaptime: 1234567890 +sockets: 1 +vga: qxl +vmstate: somestorage:state-volume diff --git a/src/test/snapshot-expected/rollback/qemu-server/402.conf b/src/test/snapshot-expected/rollback/qemu-server/402.conf new file mode 100644 index 00000000..a10bfa13 --- /dev/null +++ b/src/test/snapshot-expected/rollback/qemu-server/402.conf @@ -0,0 +1,35 @@ +# this is a description +agent: 1 +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: rtime=1700000000000,uid=abcdefghijklmnopqrst +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +parent: test +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 1 +vga: qxl + +[test] +#test comment +agent: 1 +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +machine: q35 +memory: 8192 +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +snaptime: 1234567890 +sockets: 1 +vga: qxl +vmstate: somestorage:state-volume diff --git a/src/test/snapshot-input/create/qemu-server/401.conf b/src/test/snapshot-input/create/qemu-server/401.conf new file mode 100644 index 00000000..41ccdae5 --- /dev/null +++ b/src/test/snapshot-input/create/qemu-server/401.conf @@ -0,0 +1,13 @@ +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: creation-qemu=10.0.0,ctime=1700000000,rtime=1700000000000,uid=abcdefghijklmnopqrst +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 1 +vga: qxl diff --git a/src/test/snapshot-input/create/qemu-server/402.conf b/src/test/snapshot-input/create/qemu-server/402.conf new file mode 100644 index 00000000..e03717b6 --- /dev/null +++ b/src/test/snapshot-input/create/qemu-server/402.conf @@ -0,0 +1,13 @@ +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +memory: 8192 +meta: creation-qemu=8.0.2,ctime=1600000000 +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 1 +vga: qxl diff --git a/src/test/snapshot-input/rollback/qemu-server/401.conf b/src/test/snapshot-input/rollback/qemu-server/401.conf new file mode 100644 index 00000000..7895e0df --- /dev/null +++ b/src/test/snapshot-input/rollback/qemu-server/401.conf @@ -0,0 +1,35 @@ +# this is a description +agent: 1 +bootdisk: ide2 +cores: 2 +ide0: local:snapshotable-disk-1,size=32G +ide2: none,media=cdrom +memory: 4096 +meta: creation-qemu=10.0.0,ctime=1700000000,rtime=1700000000000,uid=abcdefghijklmnopqrst +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +parent: test +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 2 + +[test] +#test comment +agent: 1 +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +machine: q35 +memory: 8192 +meta: creation-qemu=9.2.0,ctime=1600000000,rtime=1600000000000,uid=bbbbbbbbbbbbbbbbbbbb +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +snaptime: 1234567890 +sockets: 1 +vga: qxl +vmstate: somestorage:state-volume diff --git a/src/test/snapshot-input/rollback/qemu-server/402.conf b/src/test/snapshot-input/rollback/qemu-server/402.conf new file mode 100644 index 00000000..0fa7fe51 --- /dev/null +++ b/src/test/snapshot-input/rollback/qemu-server/402.conf @@ -0,0 +1,34 @@ +# this is a description +agent: 1 +bootdisk: ide2 +cores: 2 +ide0: local:snapshotable-disk-1,size=32G +ide2: none,media=cdrom +memory: 4096 +meta: creation-qemu=10.0.0,ctime=1700000000,rtime=1700000000000,uid=abcdefghijklmnopqrst +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +parent: test +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +sockets: 2 + +[test] +#test comment +agent: 1 +bootdisk: ide0 +cores: 4 +ide0: local:snapshotable-disk-1,discard=on,size=32G +ide2: none,media=cdrom +machine: q35 +memory: 8192 +name: win +net0: e1000=12:34:56:78:90:12,bridge=somebr0,firewall=1 +numa: 0 +ostype: win7 +smbios1: uuid=01234567-890a-bcde-f012-34567890abcd +snaptime: 1234567890 +sockets: 1 +vga: qxl +vmstate: somestorage:state-volume diff --git a/src/test/snapshot-test.pm b/src/test/snapshot-test.pm index b5269538..4e7fccdb 100644 --- a/src/test/snapshot-test.pm +++ b/src/test/snapshot-test.pm @@ -7,6 +7,7 @@ use lib qw(..); use JSON; +use PVE::GuestHelpers; use PVE::Storage; use PVE::Storage::Plugin; use PVE::QemuServer; @@ -33,6 +34,9 @@ my $stop_possible; my $save_vmstate_works; my $vm_mon = {}; my $vm_start_sync_time; +my $real_write_config; + +my $original_write_config = \&PVE::QemuConfig::write_config; # Mocked methods @@ -312,6 +316,13 @@ sub cfs_config_path { return "snapshot-working/$node/qemu-server/$vmid.conf"; } +# the same file that mocked_get_config reads, so that nothing looks at the host's /etc/pve +sub config_file { + my ($class, $vmid, $node) = @_; + + return $class->cfs_config_path($vmid, $node); +} + sub load_config { my ($class, $vmid, $node) = @_; @@ -326,18 +337,10 @@ sub load_config { sub write_config { my ($class, $vmid, $conf) = @_; - my $filename = $class->cfs_config_path($vmid); - - if ($conf->{snapshots}) { - foreach my $snapname (keys %{ $conf->{snapshots} }) { - $conf->{snapshots}->{$snapname}->{snaptime} = "1234567890" - if $conf->{snapshots}->{$snapname}->{snaptime}; - } - } + # the real method with the guest identity handling, which writes through mocked_cfs_write_file + return $original_write_config->($class, $vmid, $conf) if $real_write_config; - my $raw = PVE::QemuServer::write_vm_config($filename, $conf); - - PVE::Tools::file_set_contents($filename, $raw); + mocked_cfs_write_file($class->cfs_config_path($vmid), $conf); } sub has_feature { @@ -361,6 +364,31 @@ sub assert_config_exists_on_node { } # END mocked PVE::QemuConfig methods +# BEGIN mocked PVE::Cluster methods + +sub mocked_get_config { + my ($path) = @_; + + return -e $path ? PVE::Tools::file_get_contents($path) : undef; +} + +sub mocked_cfs_write_file { + my ($filename, $conf) = @_; + + if ($conf->{snapshots}) { + foreach my $snapname (keys %{ $conf->{snapshots} }) { + $conf->{snapshots}->{$snapname}->{snaptime} = "1234567890" + if $conf->{snapshots}->{$snapname}->{snaptime}; + } + } + + my $raw = PVE::QemuServer::write_vm_config($filename, $conf); + + PVE::Tools::file_set_contents($filename, $raw); +} + +# END mocked PVE::Cluster methods + # BEGIN mocked PVE::QemuServer::Helpers methods sub vm_running_locally { @@ -477,6 +505,16 @@ sub vm_stop { PVE::Tools::run_command("rm -rf snapshot-working"); PVE::Tools::run_command("cp -a snapshot-input snapshot-working"); +$PVE::GuestHelpers::lockdir = "snapshot-working/lock"; + +my $cluster_module = Test::MockModule->new('PVE::Cluster'); +$cluster_module->mock('get_config', \&mocked_get_config); +$cluster_module->mock('cfs_write_file', \&mocked_cfs_write_file); + +my $guest_identity_module = Test::MockModule->new('PVE::GuestIdentity'); +$guest_identity_module->mock('__random_bytes', sub { return '0123456789abcdefghij'; }); +$guest_identity_module->mock('__time_ms', sub { return 1234567890123; }); + my $qemu_migrate_helpers_module = Test::MockModule->new('PVE::QemuMigrate::Helpers'); $qemu_migrate_helpers_module->mock('set_migration_caps', \&set_migration_caps); @@ -489,6 +527,7 @@ $qemu_monitor_module->mock('qmp_cmd', \&qmp_cmd); my $qemu_config_module = Test::MockModule->new('PVE::QemuConfig'); $qemu_config_module->mock('config_file_lock', \&config_file_lock); $qemu_config_module->mock('cfs_config_path', \&cfs_config_path); +$qemu_config_module->mock('config_file', \&config_file); $qemu_config_module->mock('load_config', \&load_config); $qemu_config_module->mock('write_config', \&write_config); $qemu_config_module->mock('has_feature', \&has_feature); @@ -694,6 +733,16 @@ testcase_create("303", "test", 1, "test comment", "storage activation failed\n\n $activate_storage_possible = 1; +$real_write_config = 1; + +printf("Successful snapshot_create keeping the guest identity\n"); +testcase_create("401", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" }); + +printf("Successful snapshot_create assigning a guest identity to a config without one\n"); +testcase_create("402", "test", 0, "test comment", "", { "local:snapshotable-disk-1" => "test" }); + +$real_write_config = 0; + $nodename = "delete"; printf("\n"); printf("Running delete tests\n"); @@ -845,6 +894,17 @@ testcase_rollback("303", "test", "storage activation failed\n"); $activate_storage_possible = 1; +$real_write_config = 1; + +printf("Successful snapshot_rollback keeping the current guest identity, not the snapshot's\n"); +testcase_rollback("401", "test", "", { "local:snapshotable-disk-1" => "test" }); + +printf( + "Successful snapshot_rollback to a snapshot without meta info, keeping the guest identity\n"); +testcase_rollback("402", "test", "", { "local:snapshotable-disk-1" => "test" }); + +$real_write_config = 0; + done_testing(); 1; -- 2.47.3