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 86B8D1FF0E1 for ; Mon, 16 Nov 2026 01:51:42 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8AD0F21A2F; Mon, 16 Nov 2026 01:48:44 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server 14/34] fix #7846: config: add a guest identity to the 'meta' property Date: Mon, 16 Nov 2026 01:46:26 +0100 Message-ID: <20261116004703.2908294-15-t.lamprecht@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261116004703.2908294-1-t.lamprecht@proxmox.com> References: <20261116004703.2908294-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: 1794790029581 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.215 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: 7EV4FTYG4MBHVZZPGE57LWPCBJYUQEJL X-Message-ID-Hash: 7EV4FTYG4MBHVZZPGE57LWPCBJYUQEJL 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 --- src/PVE/QemuConfig.pm | 7 + src/PVE/QemuServer/MetaInfo.pm | 2 + src/test/Makefile | 5 +- .../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 | 259 ++++++++++++++++++ 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 | 85 +++++- 21 files changed, 644 insertions(+), 14 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 c84f10f8..29ec7c86 100644 --- a/src/PVE/QemuConfig.pm +++ b/src/PVE/QemuConfig.pm @@ -630,6 +630,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 3916179e..3c7865e5 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_raw_config_write test_api +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_raw_config_write test_api test_guest_identity test_snapshot: run_snapshot_tests.pl ./run_snapshot_tests.pl @@ -50,6 +50,9 @@ test_raw_config_write: run_raw_config_write_tests.pl test_api: run_api_tests.pl ./run_api_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/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..e80609d9 --- /dev/null +++ b/src/test/run_guest_identity_tests.pl @@ -0,0 +1,259 @@ +#!/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 strict; +use warnings; + +use lib qw(.. .); + +use File::Temp qw(tempdir); +use Test::MockModule; +use Test::More; + +use PVE::JSONSchema; +use PVE::QemuConfig; +use PVE::QemuConfig::NoWrite; +use PVE::QemuServer; +use PVE::QemuServer::MetaInfo; + +use QemuServerTest::FakeCFS; + +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'; }); + +sub configs { return $QemuServerTest::FakeCFS::configs; } +sub generated_uid { return QemuServerTest::FakeCFS::generated_uid(@_); } +sub published_identity { return QemuServerTest::FakeCFS::published_identity(@_); } + +sub load_meta { + my ($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', + ); + + for my $invalid ( + 'uid=ABCDEFGHIJKLMNOPQRST', 'uid=abcdefghijklmnopqrs', 'rtime=-1', 'rtime=1x', + ) { + eval { + PVE::JSONSchema::parse_property_string( + $PVE::QemuServer::MetaInfo::meta_info_fmt, $invalid, + ); + }; + ok($@, "'$invalid' is rejected"); + } + + # versions before the identity members only know these two, and drop the whole property + my $old_fmt = { + ctime => $PVE::QemuServer::MetaInfo::meta_info_fmt->{ctime}, + 'creation-qemu' => $PVE::QemuServer::MetaInfo::meta_info_fmt->{'creation-qemu'}, + }; + eval { PVE::JSONSchema::parse_property_string($old_fmt, $meta) }; + like( + $@, + qr/property is not defined in schema/, + 'format without identity members rejects it', + ); +}; + +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 published identity' => sub { + QemuServerTest::FakeCFS::reset(); + + PVE::QemuConfig->write_config(100, { memory => 512, meta => "rtime=1,uid=$UID_B" }); + is_deeply( + published_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 published 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 $reservation = PVE::QemuConfig->create_and_lock_config(100); + is_deeply( + $reservation, + { + existed => 0, + identity => undef, + pending => undef, + reserved => { uid => generated_uid(1), rtime => 1700000000000 }, + }, + 'reservation publishes a new identity', + ); + + $QemuServerTest::FakeCFS::now_ms = 1700000005000; + + my $conf = { memory => 512, meta => PVE::QemuServer::MetaInfo::new_meta_info_string() }; + PVE::QemuConfig->write_config(100, $conf); + 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(); + + configs()->{100} = "memory: 512\nmeta: creation-qemu=9.2.0,ctime=1600000000," + . "rtime=1600000000000,uid=$UID_A\nname: source\n"; + + for my $snapshot (0, 1) { + my $newid = 200 + $snapshot; + my $source = PVE::QemuConfig->load_config(100); + if ($snapshot) { + # a snapshot can carry any identity, the clone must not take it over either + $source = { memory => 256, meta => "rtime=1,uid=$UID_B" }; + } + + PVE::QemuConfig->create_and_lock_config($newid, 0, 'clone'); + + # like the clone API, which copies everything but some snapshot related keys + my $newconf = { $source->%*, lock => 'clone', name => "clone-$newid" }; + delete $newconf->{digest}; + PVE::QemuConfig->write_config($newid, $newconf); + delete $newconf->{lock}; + PVE::QemuConfig->write_config($newid, $newconf); + + my $uid = generated_uid(1 + $snapshot); + is(published_identity($newid)->{uid}, $uid, "clone $newid has a new uid"); + is_deeply( + load_meta($newid), + $snapshot + ? { rtime => 1700000000000, uid => $uid } + : { + 'creation-qemu' => '9.2.0', + ctime => 1600000000, + rtime => 1700000000000, + uid => $uid, + }, + "clone $newid keeps the creation info of its source", + ); + } + is(published_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( + published_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 f68abc06..69bb96ad 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 * 10; +plan tests => 2 * 12; 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 e6739d66..dd8629f4 100755 --- a/src/test/run_qemu_restore_config_tests.pl +++ b/src/test/run_qemu_restore_config_tests.pl @@ -289,7 +289,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/; for my $type (sort keys $ARCHIVES->%*) { 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..f5bccd87 --- /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=aerukz4jvpg66ajdivty +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 44e55d6a..345accea 100644 --- a/src/test/snapshot-test.pm +++ b/src/test/snapshot-test.pm @@ -5,6 +5,7 @@ use warnings; use lib qw(..); +use PVE::GuestHelpers; use PVE::Storage; use PVE::Storage::Plugin; use PVE::QemuServer; @@ -30,6 +31,9 @@ my $freeze_possible; my $stop_possible; my $save_vmstate_works; my $vm_mon = {}; +my $real_write_config; + +my $original_write_config = \&PVE::QemuConfig::write_config; # Mocked methods @@ -296,6 +300,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) = @_; @@ -310,18 +321,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 { @@ -345,6 +348,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 { @@ -441,6 +469,19 @@ 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 pack('H26', '0123456789abcdef0123456789'); }, +); +$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); @@ -453,6 +494,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); @@ -641,6 +683,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"); @@ -777,6 +829,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