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 F31581FF0E1 for ; Mon, 16 Nov 2026 01:52:45 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 5F69721B28; Mon, 16 Nov 2026 01:48:48 +0100 (CET) From: Thomas Lamprecht To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server 15/34] restore: take over the guest uid from the backup where possible Date: Mon, 16 Nov 2026 01:46:27 +0100 Message-ID: <20261116004703.2908294-16-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: 1794790029649 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.214 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: CSQKCFMKJK5ZEO4TXGAQVXIWCR7JQ3LJ X-Message-ID-Hash: CSQKCFMKJK5ZEO4TXGAQVXIWCR7JQ3LJ 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: A restore to an unused VMID, or over a VM without an identity, should keep the uid of the backup if no other guest in the cluster uses it. Take over that uid through the cluster-wide check of pve-guest-common after the data is restored and the final config is written, while holding the config lock. A VM that already had an identity keeps it, and 'unique' prevents taking over the uid. Log the outcome, and only warn if taking over the uid fails, as the restored VM has an identity of its own. Refuse to restore a backup with a different uid over an existing VM unless 'allow-identity-mismatch' is set in the create API or qmrestore. This is independent of 'force', which permits overwriting the VM. With the override, the VM keeps its identity, unless it has the uid of the backup as pending uid from an unfinished restore. For PBS, backup providers and VMA, compare the uids before resetting the replication state, restoring the firewall config or replacing the data of the target. For VMA this also works for standard input, through the header callback. Read the uid from the config that the restore itself uses, not from the best-effort extraction of the API, and ignore unknown members of 'meta', so that the uid of a backup from a newer version is not lost when the property fails to parse. Take over the uid only after the live phase, the conversion to a template and, with 'unique', the registration of the new MAC addresses. A failed restore must not take the uid and so keep a retry at another VMID from getting it. For a retry over a VM left behind by an unfinished restore to work, record the uid of the backup as a pending change of 'meta'. Record it after the uid check if the restore could take the uid over, also with 'unique', and keep it in all configs that the restore writes. A VM with that pending uid can be restored over without the override, and without 'unique' the retry takes the uid over if no other guest uses it. A pending uid does not count as in use. Taking over the uid removes the pending uid, whatever the outcome. If the cluster-wide lock cannot be taken, it stays for a retry. Starting the VM for a live restore keeps it while the 'create' lock is held. Otherwise, applying pending changes drops it, and a clone does not get it. Legacy tar archives predate guest identities and get no uid check. Their config is only available after the data is restored, so only the restored config can carry a pending uid. Signed-off-by: Thomas Lamprecht --- src/PVE/API2/Qemu.pm | 18 + src/PVE/CLI/qmrestore.pm | 7 + src/PVE/QemuServer.pm | 142 +++++- src/test/QemuServerTest/FakeCFS.pm | 3 + src/test/restore-config-expected/150.conf | 9 + src/test/restore-config-input/150.conf | 10 + src/test/run_api_tests.pl | 13 + src/test/run_guest_identity_tests.pl | 77 +++ src/test/run_qemu_restore_config_tests.pl | 559 +++++++++++++++++++++- 9 files changed, 828 insertions(+), 10 deletions(-) create mode 100644 src/test/restore-config-expected/150.conf create mode 100644 src/test/restore-config-input/150.conf diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 28e8389a..395dc334 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -1241,6 +1241,15 @@ __PACKAGE__->register_method({ description => "Assign a unique random ethernet address.", requires => 'archive', }, + 'allow-identity-mismatch' => { + optional => 1, + type => 'boolean', + description => "Allow to overwrite an existing VM with a backup of another" + . " guest, as detected by a different 'uid' in 'meta'. Unless the VM is" + . " left over from an unfinished restore of the same backup, it keeps its" + . " own uid.", + requires => 'archive', + }, 'live-restore' => { optional => 1, type => 'boolean', @@ -1309,6 +1318,7 @@ __PACKAGE__->register_method({ my $ha_managed = extract_param($param, 'ha-managed'); my $storage = extract_param($param, 'storage'); my $unique = extract_param($param, 'unique'); + my $allow_identity_mismatch = extract_param($param, 'allow-identity-mismatch'); my $live_restore = extract_param($param, 'live-restore'); my $extraction_storage = extract_param($param, 'import-working-storage'); @@ -1436,6 +1446,8 @@ __PACKAGE__->register_method({ live => $live_restore, override_conf => $param, progress => $restore_progress, + reservation => $reservation, + 'allow-identity-mismatch' => $allow_identity_mismatch, }; if (my $volid = $archive->{volid}) { # best effort, real check is after restoring! @@ -1491,6 +1503,8 @@ __PACKAGE__->register_method({ PVE::QemuServer::Network::create_ifaces_ipams_ips($restored_conf, $vmid) if $unique; + + PVE::QemuServer::restore_adopt_identity($vmid, $restore_options); }; my $restore_locked = sub { @@ -4652,6 +4666,10 @@ __PACKAGE__->register_method({ next if PVE::QemuServer::drive_is_cloudinit($drive); push @$vollist, $volid; } + } elsif ($opt eq 'pending') { + # a pending uid only belongs to the source, see PVE::GuestIdentity + $newconf->{pending} = { $value->%* }; + PVE::GuestIdentity::set_pending_uid($newconf, undef); } else { # copy everything else $newconf->{$opt} = $value; diff --git a/src/PVE/CLI/qmrestore.pm b/src/PVE/CLI/qmrestore.pm index abfa2de7..d49e8332 100755 --- a/src/PVE/CLI/qmrestore.pm +++ b/src/PVE/CLI/qmrestore.pm @@ -54,6 +54,13 @@ __PACKAGE__->register_method({ type => 'boolean', description => "Assign a unique random ethernet address.", }, + 'allow-identity-mismatch' => { + optional => 1, + type => 'boolean', + description => "Allow to overwrite an existing VM with a backup of another" + . " guest, as detected by a different 'uid' in 'meta'. Unless the VM is left" + . " over from an unfinished restore of the same backup, it keeps its own uid.", + }, pool => { optional => 1, type => 'string', diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm index 9ffc11e8..86fc4b68 100644 --- a/src/PVE/QemuServer.pm +++ b/src/PVE/QemuServer.pm @@ -32,6 +32,7 @@ use PVE::DataCenterConfig; use PVE::Exception qw(raise raise_param_exc); use PVE::Format qw(render_duration render_bytes); use PVE::GuestHelpers qw(safe_string_ne safe_num_ne safe_boolean_ne); +use PVE::GuestIdentity; use PVE::Mapping::Dir; use PVE::Mapping::PCI; use PVE::Mapping::USB; @@ -4427,8 +4428,8 @@ for my $opt (keys %$confdesc_cloudinit) { $fast_plug_option->{$opt} = 1; } -# 'meta' is read-only, so only a manual edit or an older version can have queued a change of it. -# Drop such a change instead of applying it. +# 'meta' is read-only, so only a manual edit, an older version, or an unfinished restore, see +# restore_pending_uid, can have queued a change of it. Drop such a change instead of applying it. my sub drop_pending_meta { my ($conf) = @_; @@ -4847,6 +4848,12 @@ sub vmconfig_apply_pending { return if !scalar(keys %{ $conf->{pending} }); + # a restore keeps its pending uid while it holds the 'create' lock, also when it starts the VM + # for a live restore, and removes it itself, see restore_pending_uid + my $restore_uid = + ($conf->{lock} // '') eq 'create' ? PVE::GuestIdentity::get_pending_uid($conf) : undef; + PVE::GuestIdentity::set_pending_uid($conf, undef) if defined($restore_uid); + drop_pending_meta($conf); my $add_apply_error = sub { @@ -4956,6 +4963,8 @@ sub vmconfig_apply_pending { } } + PVE::GuestIdentity::set_pending_uid($conf, $restore_uid) if defined($restore_uid); + # write all changes at once to avoid unnecessary i/o PVE::QemuConfig->write_config($vmid, $conf); if ($generate_cloudinit) { @@ -6718,6 +6727,107 @@ sub restore_merge_config { return $backup_conf; } +# Returns the guest identity in the raw config of a backup, undef if it has none. Only looks at the +# 'meta' property of the top-level section, so that members unknown to this version, which make the +# config parser drop the whole property, do not hide the identity. +my sub restore_backup_identity { + my ($backup_conf_raw) = @_; + + my $meta; + for my $line (split(/\n/, $backup_conf_raw)) { + last if $line =~ m/^\[/; + $meta = $1 if $line =~ m/^meta:\s*(.*?)\s*$/; + } + + return PVE::GuestIdentity::parse_identity($meta); +} + +# Refuses to restore a backup over a VM that has another guest identity, as that is most likely a +# backup of another guest, unless 'allow-identity-mismatch' is set. A VM with the uid of the backup +# as pending uid is a leftover of an earlier restore of it, see restore_pending_uid. Must run before +# anything of the target VM changes. Returns the guest identity of the backup. +sub restore_check_identity { + my ($vmid, $backup_conf_raw, $opts) = @_; + + my $backup = restore_backup_identity($backup_conf_raw); + my $reservation = $opts->{reservation}; + my $target = $reservation && $reservation->{identity}; + + if ( + $backup + && $target + && !PVE::GuestIdentity::uid_matches_target($reservation, $backup->{uid}) + ) { + die "backup belongs to another guest: its uid '$backup->{uid}' differs from uid" + . " '$target->{uid}' of the existing VM $vmid - use 'allow-identity-mismatch' to" + . " overwrite the VM anyway\n" + if !$opts->{'allow-identity-mismatch'}; + + print "guest identity: ignoring backup uid $backup->{uid}, VM $vmid keeps uid" + . " $target->{uid}\n"; + } + + return $backup; +} + +# Keeps the guest identity of the backup in $opts for restore_adopt_identity. Returns its uid if the +# restore could take it over at its end, see PVE::GuestIdentity::adoptable_uid. Until then, the +# restore keeps it as pending uid of the VM, so that a VM left behind by a failure or an abort of +# the restore still has it for a retry, see restore_write_config. That holds with 'unique' too, +# which only keeps the restore itself from taking the uid over. +my sub restore_pending_uid { + my ($backup_identity, $opts) = @_; + + my $uid = $backup_identity && $backup_identity->{uid}; + $opts->{'backup-identity'} = $backup_identity; + $opts->{'pending-uid'} = PVE::GuestIdentity::adoptable_uid($opts->{reservation}, $uid); + + return $opts->{'pending-uid'}; +} + +# Writes the pending uid of the restore into the config of the VM, if there is one, see +# restore_pending_uid. Must run right after restore_check_identity. +my sub restore_record_pending_uid { + my ($vmid, $backup_identity, $opts) = @_; + + my $uid = restore_pending_uid($backup_identity, $opts); + PVE::QemuConfig->record_pending_uid($vmid, $uid) if defined($uid); +} + +# Writes $conf, a config that the restore assembled itself, like a temporary or the restored one, +# with the pending uid of the restore, see restore_pending_uid. +my sub restore_write_config { + my ($vmid, $conf, $opts) = @_; + + PVE::GuestIdentity::set_pending_uid($conf, $opts->{'pending-uid'}); + PVE::QemuConfig->write_config($vmid, $conf); +} + +# Takes over the uid of the backup for the restored VM where the identity rules allow it, see +# PVE::GuestIdentity::adopt, which also removes the pending uid. Must be the last step of a +# successful restore, with the config lock still held, so that a failure before leaves the pending +# uid behind. A failure only warns, as the restored VM keeps the identity of its reservation. +sub restore_adopt_identity { + my ($vmid, $opts) = @_; + + my $backup_identity = $opts->{'backup-identity'}; + my $uid = $opts->{unique} ? undef : $backup_identity && $backup_identity->{uid}; + + my $res = + eval { PVE::GuestIdentity::adopt('PVE::QemuConfig', $vmid, $uid, $opts->{reservation}); }; + if (my $err = $@) { + my $kept = $opts->{reservation}->{reserved}->{uid} // 'none'; + my $msg = + defined($uid) + ? "unable to adopt backup uid $uid, keeping uid $kept" + : "keeping uid $kept"; + log_warn("guest identity: $msg - $err"); + return; + } + + print PVE::GuestIdentity::adopt_result_message($res, 'backup') . "\n"; +} + sub scan_volids { my ($cfg, $vmid) = @_; @@ -6948,6 +7058,10 @@ sub restore_proxmox_backup_archive { $param = [$pbs_backup_name, "qemu-server.conf", $cfgfn]; PVE::Storage::PBSPlugin::run_raw_client_cmd($scfg, $storeid, $cmd, $param); + my $backup_identity = + restore_check_identity($vmid, PVE::Tools::file_get_contents($cfgfn), $options); + restore_record_pending_uid($vmid, $backup_identity, $options); + $options->{progress}->{'target-changed'} = 1; PVE::ReplicationState::delete_guest_states($vmid); @@ -6969,7 +7083,7 @@ sub restore_proxmox_backup_archive { # fixme: rate limit? # create empty/temp config - PVE::QemuConfig->write_config($vmid, { memory => 128, lock => 'create' }); + restore_write_config($vmid, { memory => 128, lock => 'create' }, $options); $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash) if $oldconf; @@ -7052,7 +7166,7 @@ sub restore_proxmox_backup_archive { my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); check_restore_permissions($rpcenv, $user, $new_conf); - PVE::QemuConfig->write_config($vmid, $new_conf); + restore_write_config($vmid, $new_conf, $options); eval { rescan($vmid, 1); }; warn $@ if $@; @@ -7131,6 +7245,9 @@ sub restore_external_archive { my $data = $backup_provider->archive_get_guest_config($volname) or die "backup provider failed to extract guest configuration\n"; + my $backup_identity = restore_check_identity($vmid, $data, $options); + restore_record_pending_uid($vmid, $backup_identity, $options); + $options->{progress}->{'target-changed'} = 1; PVE::ReplicationState::delete_guest_states($vmid); @@ -7149,7 +7266,7 @@ sub restore_external_archive { $parse_backup_hints->($rpcenv, $user, $storecfg, $fh, $devinfo, $options); # create empty/temp config - PVE::QemuConfig->write_config($vmid, { memory => 128, lock => 'create' }); + restore_write_config($vmid, { memory => 128, lock => 'create' }, $options); $restore_cleanup_oldconf->($storecfg, $vmid, $oldconf, $virtdev_hash) if $oldconf; @@ -7225,7 +7342,7 @@ sub restore_external_archive { my $new_conf = restore_merge_config($conffile, $new_conf_raw, $options->{override_conf}); check_restore_permissions($rpcenv, $user, $new_conf); - PVE::QemuConfig->write_config($vmid, $new_conf); + restore_write_config($vmid, $new_conf, $options); eval { rescan($vmid, 1); }; warn $@ if $@; @@ -7547,6 +7664,10 @@ sub restore_vma_archive { my $print_devmap = sub { my $cfgfn = "$tmpdir/qemu-server.conf"; + my $backup_identity = + restore_check_identity($vmid, PVE::Tools::file_get_contents($cfgfn), $opts); + restore_record_pending_uid($vmid, $backup_identity, $opts); + $opts->{progress}->{'target-changed'} = 1; PVE::ReplicationState::delete_guest_states($vmid); @@ -7579,7 +7700,7 @@ sub restore_vma_archive { # create empty/temp config if ($oldconf) { - PVE::QemuConfig->write_config($vmid, { memory => 128 }); + restore_write_config($vmid, { memory => 128 }, $opts); $restore_cleanup_oldconf->($cfg, $vmid, $oldconf, $virtdev_hash); } @@ -7685,7 +7806,7 @@ sub restore_vma_archive { my $new_conf = restore_merge_config($conffile, $new_conf_raw, $opts->{override_conf}); check_restore_permissions($rpcenv, $user, $new_conf); - PVE::QemuConfig->write_config($vmid, $new_conf); + restore_write_config($vmid, $new_conf, $opts); eval { rescan($vmid, 1); }; warn $@ if $@; @@ -7798,7 +7919,10 @@ sub restore_tar_archive { my $new_conf = restore_merge_config($conffile, $new_conf_raw, {}); check_restore_permissions(PVE::RPCEnvironment::get(), $user, $new_conf); - PVE::QemuConfig->write_config($vmid, $new_conf); + # the config of the backup is only known now, so the restored config is the first with the + # pending uid + restore_pending_uid(restore_backup_identity($new_conf_raw), $opts); + restore_write_config($vmid, $new_conf, $opts); eval { rescan($vmid, 1); }; warn $@ if $@; diff --git a/src/test/QemuServerTest/FakeCFS.pm b/src/test/QemuServerTest/FakeCFS.pm index dd9dddec..5b1501a1 100644 --- a/src/test/QemuServerTest/FakeCFS.pm +++ b/src/test/QemuServerTest/FakeCFS.pm @@ -27,6 +27,7 @@ our $now_ms = 1700000000000; our $generated = 0; our $lock_domain_error; our $writes = []; +our $written = {}; # VMID => the written raw configs, in the order of the writes my $tmpdir = tempdir('fake-cfs-XXXXXX', DIR => '.', CLEANUP => 1); make_path("$tmpdir/lock"); @@ -40,6 +41,7 @@ sub reset { $generated = 0; $lock_domain_error = undef; $writes = []; + $written = {}; } my sub vmid_of_path { @@ -135,6 +137,7 @@ $cluster_module->mock( $configs->{$vmid} = PVE::QemuServer::write_vm_config($path, $conf); sync_config_file($vmid); push $writes->@*, $vmid; + push $written->{$vmid}->@*, $configs->{$vmid}; return; }, cfs_update => sub { diff --git a/src/test/restore-config-expected/150.conf b/src/test/restore-config-expected/150.conf new file mode 100644 index 00000000..a8e8909c --- /dev/null +++ b/src/test/restore-config-expected/150.conf @@ -0,0 +1,9 @@ +# VM with guest identity +boot: order=scsi0 +cores: 1 +memory: 512 +meta: creation-qemu=10.1.2,ctime=1762000000,rtime=1762000000123,uid=abcdefghijklmnopqrst +name: identity +scsi0: target:150/vm-150-disk-0.qcow2,size=4G +scsihw: virtio-scsi-pci +vmgenid: 0 diff --git a/src/test/restore-config-input/150.conf b/src/test/restore-config-input/150.conf new file mode 100644 index 00000000..927be6dc --- /dev/null +++ b/src/test/restore-config-input/150.conf @@ -0,0 +1,10 @@ +# VM with guest identity +boot: order=scsi0 +cores: 1 +memory: 512 +meta: creation-qemu=10.1.2,ctime=1762000000,rtime=1762000000123,uid=abcdefghijklmnopqrst +name: identity +scsi0: mydir:150/vm-150-disk-0.qcow2,size=4G +scsihw: virtio-scsi-pci +vmgenid: 0 +#qmdump#map:scsi0:drive-scsi0:mydir:qcow2: diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl index 31498fd4..334980f0 100755 --- a/src/test/run_api_tests.pl +++ b/src/test/run_api_tests.pl @@ -13,6 +13,7 @@ use Test::MockModule; use Test::More; use PVE::API2::Qemu; +use PVE::CLI::qmrestore; use PVE::GuestIdentity; use PVE::RPCEnvironment; @@ -132,6 +133,18 @@ is( 'update_vm: reverting other options passes the parameter checks', ); +is_deeply( + api_error('create_vm', { 'allow-identity-mismatch' => 1 }), + { archive => "missing property required by 'allow-identity-mismatch'" }, + "create_vm: 'allow-identity-mismatch' only for restores", +); +my $qmrestore_param = PVE::CLI::qmrestore->map_method_by_name('qmrestore')->{parameters}; +is( + $qmrestore_param->{properties}->{'allow-identity-mismatch'}->{type}, + 'boolean', + "qmrestore has 'allow-identity-mismatch'", +); + subtest 'pending changes of meta are dropped' => sub { my $meta = 'creation-qemu=10.1.2,ctime=1762000000'; my $stored; diff --git a/src/test/run_guest_identity_tests.pl b/src/test/run_guest_identity_tests.pl index e80609d9..3429fca6 100755 --- a/src/test/run_guest_identity_tests.pl +++ b/src/test/run_guest_identity_tests.pl @@ -12,11 +12,13 @@ use File::Temp qw(tempdir); use Test::MockModule; use Test::More; +use PVE::API2::Qemu; use PVE::JSONSchema; use PVE::QemuConfig; use PVE::QemuConfig::NoWrite; use PVE::QemuServer; use PVE::QemuServer::MetaInfo; +use PVE::RPCEnvironment; use QemuServerTest::FakeCFS; @@ -26,6 +28,9 @@ 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'); + sub configs { return $QemuServerTest::FakeCFS::configs; } sub generated_uid { return QemuServerTest::FakeCFS::generated_uid(@_); } sub published_identity { return QemuServerTest::FakeCFS::published_identity(@_); } @@ -223,6 +228,78 @@ subtest 'clone' => sub { is(published_identity(100)->{uid}, $UID_A, 'source keeps its uid'); }; +subtest 'clone does not carry the pending uid of its source' => sub { + QemuServerTest::FakeCFS::reset(); + + # a VM left behind by a restore of a backup with uid B, with another pending change + configs()->{100} = + "memory: 512\nmeta: rtime=1600000000000,uid=$UID_A\nname: source\n" + . "scsi0: local:100/vm-100-disk-0.raw,size=4G\n\n[PENDING]\nmemory: 1024\n" + . "meta: uid=$UID_B\n"; + + 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 $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 $network_module = Test::MockModule->new('PVE::QemuServer::Network'); + $network_module->mock(create_ifaces_ipams_ips => sub { return; }); + + my $warnings = ''; + { + local $SIG{__WARN__} = sub { $warnings .= $_[0]; }; + open(my $out_fh, '>', \my $output); + my $oldfh = select($out_fh); + eval { PVE::API2::Qemu->clone_vm({ node => 'localhost', vmid => 100, newid => 300 }) }; + select($oldfh); + is($@, '', 'clone succeeds'); + } + + my $writes = $QemuServerTest::FakeCFS::written->{300}; + ok(scalar(grep { m/^scsi0: local:300\//m } $writes->@*) >= 2, + 'clone writes the disk first'); + ok(!scalar(grep { m/uid=$UID_B/ } $writes->@*), + 'no write of the clone has the pending uid'); + like( + $warnings, + qr/pending changes for 'memory', discarding for clone/, + 'other pending changes are discarded as before', + ); + unlike(configs()->{300}, qr/^\[PENDING\]/m, 'clone has no pending section'); + like(configs()->{100}, qr/^meta: uid=$UID_B$/m, 'source keeps its pending uid'); +}; + subtest 'importovf' => sub { QemuServerTest::FakeCFS::reset(); diff --git a/src/test/run_qemu_restore_config_tests.pl b/src/test/run_qemu_restore_config_tests.pl index dd8629f4..f179635d 100755 --- a/src/test/run_qemu_restore_config_tests.pl +++ b/src/test/run_qemu_restore_config_tests.pl @@ -15,9 +15,11 @@ use Test::More; use File::Basename; use PVE::API2::Qemu; +use PVE::GuestIdentity; use PVE::QemuConfig; use PVE::QemuServer; use PVE::QemuServer::Helpers; +use PVE::QemuServer::MetaInfo; use PVE::RPCEnvironment; use PVE::Tools qw(dir_glob_foreach file_get_contents file_set_contents); @@ -92,7 +94,8 @@ my $ARCHIVES = { }; our $current_backup; # raw config of the backup -our $fail_at = ''; # where the restore fails: 'read-config', 'data', 'after-restore' or 'live' +# where the restore fails: 'read-config', 'firewall', 'data', 'after-restore' or 'live' +our $fail_at = ''; my $calls = {}; # calls with effects on the target VM that tests check sub backup_config { @@ -133,6 +136,7 @@ $tools_module->mock( file_copy => sub { my ($src, $dst) = @_; die "file_copy (mocked) - unexpected destination '$dst'\n" if $dst !~ m|/firewall/|; + die "unable to restore the firewall config\n" if $fail_at eq 'firewall'; $calls->{firewall}++; }, ); @@ -202,6 +206,10 @@ my $qemu_server_module = Test::MockModule->new('PVE::QemuServer'); $qemu_server_module->mock( check_running => sub { return 0; }, pbs_live_restore => sub { + my ($vmid, $conf, $storecfg) = @_; + # like the start of the VM for the live phase + PVE::QemuServer::vmconfig_apply_pending($vmid, $conf, $storecfg) + if scalar(keys(($conf->{pending} // {})->%*)); die "live restore failed\n" if $fail_at eq 'live'; $calls->{live_restore}++; }, @@ -401,4 +409,553 @@ subtest 'failure after the data was restored keeps the VM' => sub { $fail_at = ''; +# Guest identity of restored VMs + +my $UID_BACKUP = 'bbbbbbbbbbbbbbbbbbbb'; +my $UID_TARGET = 'tttttttttttttttttttt'; + +my $TARGET_CONF_NO_UID = "memory: 1024\nmeta: creation-qemu=8.0.2,ctime=1500000000\nname: target\n"; + +my $NOW = 1800000000000; +my $BACKUP_META = 'creation-qemu=9.2.0,ctime=1600000000'; +my $REFUSED = + "backup belongs to another guest: its uid '$UID_BACKUP' differs from uid '$UID_TARGET' of the" + . " existing VM 100 - use 'allow-identity-mismatch' to overwrite the VM anyway\n"; +my $IGNORED = "guest identity: ignoring backup uid $UID_BACKUP, VM 100 keeps uid $UID_TARGET"; +# captures the uid that the VM keeps +my $ADOPT_FAILED = "WARN: guest identity: unable to adopt backup uid $UID_BACKUP, keeping uid" + . " ([a-z2-7]{20}) - cfs-lock 'domain-guest-identity' error: no quorum!"; +$ADOPT_FAILED = qr/^$ADOPT_FAILED$/m; +my $KEPT_TARGET = qr/^guest identity: kept uid $UID_TARGET$/m; + +# legacy tar archives get no check, 'tar' gives the result for them where it differs +my $cases = [ + { + name => 'free VMID, backup uid unused', + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + result => { uid => $UID_BACKUP, rtime => $NOW }, + output => qr/^guest identity: adopted uid $UID_BACKUP from the backup$/m, + }, + { + name => 'free VMID, backup uid in use by another guest', + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + other => "rtime=1,uid=$UID_BACKUP", + result => { uid => 'generated', rtime => $NOW }, + output => qr/: backup uid $UID_BACKUP is in use by guest 300, keeping uid \w{20}$/m, + }, + { + name => 'free VMID, backup without uid', + backup => $BACKUP_META, + result => { uid => 'generated', rtime => $NOW }, + output => qr/^guest identity: new uid aaaaaaaaaaaaaaaaaaab$/m, + }, + { + name => 'free VMID, unique', + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + param => { unique => 1 }, + result => { uid => 'generated', rtime => $NOW }, + output => qr/^guest identity: new uid aaaaaaaaaaaaaaaaaaab$/m, + }, + { + name => 'free VMID, identity lock fails', + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + lock_error => "cfs-lock 'domain-guest-identity' error: no quorum!\n", + result => { uid => 'generated', rtime => $NOW }, + output => $ADOPT_FAILED, + }, + { + name => 'free VMID, backup meta with a member unknown to this version', + backup => "$BACKUP_META,future=1,rtime=1600000000000,uid=$UID_BACKUP", + result => { uid => $UID_BACKUP, rtime => $NOW }, + # the parser drops the whole property of the backup, as it does for other members + meta => { rtime => $NOW, uid => $UID_BACKUP }, + output => qr/^guest identity: adopted uid $UID_BACKUP from the backup$/m, + }, + { + name => 'in place, backup of another guest', + target => $TARGET_CONF, + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + error => $REFUSED, + tar => + { result => { uid => $UID_TARGET, rtime => 1700000000000 }, output => $KEPT_TARGET }, + }, + { + name => 'in place, backup of another guest, unique', + target => $TARGET_CONF, + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + param => { unique => 1 }, + error => $REFUSED, + tar => + { result => { uid => $UID_TARGET, rtime => 1700000000000 }, output => $KEPT_TARGET }, + }, + { + name => 'in place, backup of another guest, override', + target => $TARGET_CONF, + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + param => { 'allow-identity-mismatch' => 1 }, + result => { uid => $UID_TARGET, rtime => 1700000000000 }, + output => qr/^\Q$IGNORED\E\n(?:.*\n)*guest identity: kept uid $UID_TARGET$/m, + tar => + { result => { uid => $UID_TARGET, rtime => 1700000000000 }, output => $KEPT_TARGET }, + }, + { + name => 'in place, backup of the same guest', + target => $TARGET_CONF, + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_TARGET", + result => { uid => $UID_TARGET, rtime => 1700000000000 }, + output => $KEPT_TARGET, + }, + { + name => 'in place, backup without uid', + target => $TARGET_CONF, + backup => $BACKUP_META, + result => { uid => $UID_TARGET, rtime => 1700000000000 }, + output => $KEPT_TARGET, + }, + { + name => 'in place, target without uid', + target => $TARGET_CONF_NO_UID, + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + result => { uid => $UID_BACKUP, rtime => $NOW }, + output => qr/^guest identity: adopted uid $UID_BACKUP from the backup$/m, + }, + { + name => 'in place, target without uid, unique', + target => $TARGET_CONF_NO_UID, + backup => "$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP", + param => { unique => 1 }, + result => { uid => 'generated', rtime => $NOW }, + output => qr/^guest identity: new uid aaaaaaaaaaaaaaaaaaab$/m, + }, +]; + +sub meta_of { + my ($vmid) = @_; + + my $conf = PVE::QemuConfig->load_config($vmid); + return PVE::QemuServer::MetaInfo::parse_meta_info($conf->{meta}); +} + +for my $type (sort keys $ARCHIVES->%*) { + for my $test ($cases->@*) { + my $case = { $test->%*, ($type eq 'tar' ? $test->{tar} // {} : {})->%* }; + delete $case->{error} if $type eq 'tar'; + + subtest "$type: $case->{name}" => sub { + QemuServerTest::FakeCFS::reset(); + $QemuServerTest::FakeCFS::now_ms = $NOW; + QemuServerTest::FakeCFS::set_config(100, $case->{target}) if $case->{target}; + $QemuServerTest::FakeCFS::other_guests->{300} = $case->{other} if $case->{other}; + $QemuServerTest::FakeCFS::lock_domain_error = $case->{lock_error}; + + my $param = { ($case->{param} // {})->%* }; + $param->{force} = 1 if $case->{target}; + my ($err, $output) = restore_api($type, backup_config($case->{backup}), $param); + + if (my $expected_err = $case->{error}) { + is($err, $expected_err, 'restore refused'); + is( + $QemuServerTest::FakeCFS::configs->{100}, + $case->{target}, + 'config unchanged', + ); + is_deeply($calls, {}, 'replication state and firewall config untouched'); + unlike($output, $NOT_CLEANED_UP, 'no warning about a changed VM'); + return; + } + + is($err, '', 'restore succeeded') or diag($output); + like($output, $case->{output}, 'task log reports the identity'); + + my $expected = { $case->{result}->%* }; + $expected->{uid} = QemuServerTest::FakeCFS::generated_uid(1) + if $expected->{uid} eq 'generated'; + is_deeply( + QemuServerTest::FakeCFS::published_identity(100), + $expected, + 'published identity', + ); + is_deeply( + meta_of(100), + $case->{meta} + // { 'creation-qemu' => '9.2.0', ctime => 1600000000, $expected->%* }, + 'other members of meta come from the backup', + ); + }; + } +} + +subtest 'identity check on raw backup configs' => sub { + my $opts = { reservation => { identity => { uid => $UID_TARGET, rtime => 1 } } }; + + my $raw = "memory: 512\nmeta: uid=$UID_TARGET\nmeta: rtime=1,uid=$UID_BACKUP\n"; + is_deeply( + PVE::QemuServer::restore_check_identity(100, $raw, { 'allow-identity-mismatch' => 1 }), + { uid => $UID_BACKUP, rtime => 1 }, + 'last top-level meta property counts', + ); + eval { PVE::QemuServer::restore_check_identity(100, $raw, $opts) }; + is($@, $REFUSED, 'refused'); + + $raw = "memory: 512\nmeta: rtime=1,uid=$UID_TARGET\n\n[snap]\nmeta: uid=$UID_BACKUP\n"; + is_deeply( + PVE::QemuServer::restore_check_identity(100, $raw, $opts), + { uid => $UID_TARGET, rtime => 1 }, + 'snapshot sections are ignored', + ); + + is( + PVE::QemuServer::restore_check_identity(100, "memory: 512\n", $opts), + undef, + 'backup without meta has no identity', + ); + is_deeply( + PVE::QemuServer::restore_check_identity( + 100, + "meta: uid=$UID_BACKUP\n", + { reservation => { identity => undef } }, + ), + { uid => $UID_BACKUP, rtime => undef }, + 'no check without identity of the target', + ); +}; + +subtest 'retry of a failed restore to a free VMID keeps the lineage' => sub { + QemuServerTest::FakeCFS::reset(); + my $backup = backup_config("$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP"); + + $fail_at = 'data'; + my ($err) = restore_api('pbs', $backup, {}); + is($err, "restoring the data failed\n", 'first attempt failed'); + + # the web interface sets 'force' for a retry + $fail_at = ''; + ($err) = restore_api('pbs', $backup, { force => 1 }); + is($err, '', 'retry succeeds'); + is(QemuServerTest::FakeCFS::published_identity(100)->{uid}, $UID_BACKUP, 'uid adopted'); +}; + +subtest 'live restore adopts the uid of the backup only when complete' => sub { + my $backup = backup_config("$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP"); + my $template = "$backup" . "template: 1\n"; + + for my $failure ('before the live phase', 'during the live phase') { + QemuServerTest::FakeCFS::reset(); + + $fail_at = $failure eq 'during the live phase' ? 'live' : ''; + my ($err, $output) = restore_api( + 'pbs', + $failure eq 'before the live phase' ? $template : $backup, + { 'live-restore' => 1 }, + ); + ok($err, "restore fails $failure"); + isnt( + QemuServerTest::FakeCFS::published_identity(100)->{uid}, + $UID_BACKUP, + "leftover VM does not have the uid of the backup ($failure)", + ); + unlike($output, qr/^guest identity:/m, "no identity reported ($failure)"); + + $fail_at = ''; + ($err, $output) = restore_api('pbs', $backup, { vmid => 101, 'live-restore' => 1 }); + is($err, '', "retry at another VMID succeeds ($failure)") or diag($output); + is($calls->{live_restore}, 1, 'live phase ran'); + is( + QemuServerTest::FakeCFS::published_identity(101)->{uid}, + $UID_BACKUP, + "retry adopts the uid of the backup ($failure)", + ); + like( + $output, + qr/^guest identity: adopted uid $UID_BACKUP from the backup$/m, + "adoption reported ($failure)", + ); + } +}; + +# A VM that a restore leaves behind keeps the uid of the backup as pending uid, so that retrying +# the restore over it takes the uid over. + +my $BACKUP_WITH_UID = backup_config("$BACKUP_META,rtime=1600000000000,uid=$UID_BACKUP"); +my $ADOPTED = qr/^guest identity: adopted uid $UID_BACKUP from the backup$/m; + +sub pending_uid_of { + my ($vmid) = @_; + + return PVE::GuestIdentity::get_pending_uid(PVE::QemuConfig->load_config($vmid)); +} + +sub published_uid_of { + my ($vmid) = @_; + + my $identity = QemuServerTest::FakeCFS::published_identity($vmid); + return $identity && $identity->{uid}; +} + +# Fails the live restore of a backup with uid to the free VMID 100 in its live phase. +sub failed_live_restore { + QemuServerTest::FakeCFS::reset(); + $QemuServerTest::FakeCFS::now_ms = $NOW; + + local $fail_at = 'live'; + my ($err, $output) = restore_api('pbs', $BACKUP_WITH_UID, { 'live-restore' => 1 }); + is($err, "live restore failed\n", 'live restore failed in its live phase'); + unlike($output, qr/ignoring pending change/, 'starting the VM keeps the pending uid'); + isnt(published_uid_of(100), $UID_BACKUP, 'leftover VM has a uid of its own'); + is(pending_uid_of(100), $UID_BACKUP, 'leftover VM has the uid of the backup as pending uid'); + unlike($QemuServerTest::FakeCFS::configs->{100}, qr/^lock:/m, 'leftover VM is not locked'); +} + +subtest 'retry in place after a failed live restore' => sub { + for my $retry ({ 'live-restore' => 1 }, {}) { + my $what = $retry->{'live-restore'} ? 'live' : 'regular'; + + failed_live_restore(); + + # the web interface restores from the backup list of the VM with 'force' + my ($err, $output) = restore_api('pbs', $BACKUP_WITH_UID, { force => 1, $retry->%* }); + is($err, '', "$what retry succeeds") or diag($output); + like($output, $ADOPTED, "$what retry adopts the uid of the backup"); + is(published_uid_of(100), $UID_BACKUP, "$what retry: published"); + is(pending_uid_of(100), undef, "$what retry: pending uid is removed"); + } +}; + +subtest 'retry in place after a failure once the restored volumes stay' => sub { + QemuServerTest::FakeCFS::reset(); + my $backup = $BACKUP_WITH_UID . "hostpci0: 0000:01:00.0\n"; + + # the permission check needs the restored config, so it runs once the data is restored + $rpcenv->set_user('user@pve'); + my ($err, $output) = restore_api('vma file', $backup, {}); + $rpcenv->set_user('root@pam'); + is( + $err, + "only root can set 'hostpci0' config for non-mapped devices\n", + 'restore refused after restoring the data', + ); + ok(defined($QemuServerTest::FakeCFS::configs->{100}), 'VM is left behind'); + is(pending_uid_of(100), $UID_BACKUP, + 'leftover VM has the uid of the backup as pending uid'); + + ($err, $output) = restore_api('vma file', $backup, { force => 1 }); + is($err, '', 'retry succeeds') or diag($output); + is(published_uid_of(100), $UID_BACKUP, 'retry adopts the uid of the backup'); + is(pending_uid_of(100), undef, 'retry: pending uid is removed'); +}; + +my $FAILURES = { + # right after the check, before the restore writes a config itself + firewall => "unable to restore the firewall config\n", + data => "restoring the data failed\n", +}; + +for my $type ('pbs', 'provider', 'vma file', 'vma stdin') { + for my $failure (sort keys $FAILURES->%*) { + my $what = "$type: retry in place after a failed restore over a VM without uid"; + + subtest "$what ($failure)" => sub { + QemuServerTest::FakeCFS::reset(); + QemuServerTest::FakeCFS::set_config(100, $TARGET_CONF_NO_UID); + + local $fail_at = $failure; + my ($err) = restore_api($type, $BACKUP_WITH_UID, { force => 1 }); + is($err, $FAILURES->{$failure}, 'restore failed'); + ok(published_uid_of(100), 'VM got a uid of its own'); + isnt(published_uid_of(100), $UID_BACKUP, 'not the one of the backup'); + is(pending_uid_of(100), $UID_BACKUP, 'VM has the uid of the backup as pending uid'); + + $fail_at = ''; + ($err, my $output) = restore_api($type, $BACKUP_WITH_UID, { force => 1 }); + is($err, '', 'retry succeeds') or diag($output); + is(published_uid_of(100), $UID_BACKUP, 'retry adopts the uid of the backup'); + is(pending_uid_of(100), undef, 'retry: pending uid is removed'); + }; + } +} + +subtest 'retry elsewhere, then over the VM left behind' => sub { + failed_live_restore(); + my $leftover = published_uid_of(100); + + my ($err, $output) = restore_api('pbs', $BACKUP_WITH_UID, { vmid => 101 }); + is($err, '', 'restore to another VMID succeeds') or diag($output); + is(published_uid_of(101), $UID_BACKUP, 'pending uid of the leftover VM is not in use'); + is(pending_uid_of(100), $UID_BACKUP, 'leftover VM keeps its pending uid'); + + ($err, $output) = restore_api('pbs', $BACKUP_WITH_UID, { force => 1 }); + is($err, '', 'restore over the leftover VM is not refused') or diag($output); + like( + $output, + qr/^guest identity: backup uid $UID_BACKUP is in use by guest 101, keeping uid $leftover$/m, + 'uid of the backup is in use by now', + ); + is(published_uid_of(100), $leftover, 'leftover VM keeps its uid'); + is(pending_uid_of(100), undef, 'pending uid is removed'); +}; + +subtest 'using the VM left behind drops its pending uid' => sub { + failed_live_restore(); + my $leftover = published_uid_of(100); + + # like a start, or a config change of the stopped VM + my $warnings = ''; + { + local $SIG{__WARN__} = sub { $warnings .= $_[0]; }; + my $conf = PVE::QemuConfig->load_config(100); + PVE::QemuServer::vmconfig_apply_pending(100, $conf, {}); + } + like($warnings, qr/^ignoring pending change of read-only option 'meta'$/m, 'dropped'); + is(pending_uid_of(100), undef, 'pending uid is gone'); + + my ($err) = restore_api('pbs', $BACKUP_WITH_UID, { force => 1 }); + like( + $err, + qr/^backup belongs to another guest: its uid .$UID_BACKUP. differs/, + 'retry is refused', + ); + is(published_uid_of(100), $leftover, 'VM stays its own guest'); +}; + +subtest 'a restore with unique keeps a pending uid, but never takes it over' => sub { + for my $retry ({ unique => 1 }, {}) { + my $what = $retry->{unique} ? 'retry with unique' : 'retry without unique'; + QemuServerTest::FakeCFS::reset(); + + # registering the addresses of the new MACs is the last step before the adoption + local $fail_at = 'after-restore'; + my ($err) = restore_api('pbs', $BACKUP_WITH_UID, { unique => 1 }); + is($err, "unable to register IPs\n", 'restore with unique failed at its end'); + my $leftover = published_uid_of(100); + is(pending_uid_of(100), $UID_BACKUP, 'VM left behind has the pending uid'); + + $fail_at = ''; + my $output; + ($err, $output) = restore_api('pbs', $BACKUP_WITH_UID, { force => 1, $retry->%* }); + is($err, '', "$what succeeds") or diag($output); + is(pending_uid_of(100), undef, "$what: pending uid is removed"); + if ($retry->{unique}) { + is(published_uid_of(100), $leftover, "$what: uid of the backup is not taken over"); + } else { + is(published_uid_of(100), $UID_BACKUP, "$what: uid of the backup is taken over"); + } + } +}; + +subtest 'a failed adoption keeps the pending uid' => sub { + QemuServerTest::FakeCFS::reset(); + + $QemuServerTest::FakeCFS::lock_domain_error = + "cfs-lock 'domain-guest-identity' error: no quorum!\n"; + my ($err, $output) = restore_api('pbs', $BACKUP_WITH_UID, {}); + $QemuServerTest::FakeCFS::lock_domain_error = undef; + is($err, '', 'restore succeeds') or diag($output); + my ($kept) = $output =~ $ADOPT_FAILED; + ok(defined($kept), 'failed adoption is a warning') or diag($output); + isnt(published_uid_of(100), $UID_BACKUP, 'VM has a uid of its own'); + is($kept, published_uid_of(100), 'warning names the uid that the VM keeps'); + is(pending_uid_of(100), $UID_BACKUP, 'nothing decided, pending uid stays'); + + ($err, $output) = restore_api('pbs', $BACKUP_WITH_UID, { force => 1 }); + is($err, '', 'restore of the backup again is not refused') or diag($output); + is(published_uid_of(100), $UID_BACKUP, 'uid of the backup is taken over'); +}; + +subtest 'config of a VM left behind shows its published identity' => sub { + failed_live_restore(); + my $meta = PVE::QemuConfig->load_config(100)->{meta}; + + my $conf = PVE::API2::Qemu->vm_config({ node => 'localhost', vmid => 100 }); + is($conf->{meta}, $meta, 'config with pending changes applied: published identity'); + $conf = PVE::API2::Qemu->vm_config({ node => 'localhost', vmid => 100, current => 1 }); + is($conf->{meta}, $meta, 'current config: published identity'); + + my $pending = PVE::API2::Qemu->vm_pending({ node => 'localhost', vmid => 100 }); + my ($item) = grep { $_->{key} eq 'meta' } $pending->@*; + is($item->{value}, $meta, 'pending view: current value'); + is($item->{pending}, "uid=$UID_BACKUP", 'pending view: pending uid'); +}; + +subtest 'removing the pending uid keeps an identity that the parser cannot read' => sub { + QemuServerTest::FakeCFS::reset(); + + # a member of a newer version makes the parser drop the whole property + my $target_meta = "creation-qemu=10.0.0,ctime=1700000000,future-member=1," + . "rtime=1700000000000,uid=$UID_TARGET"; + QemuServerTest::FakeCFS::set_config( + 100, + "memory: 512\nmeta: $target_meta\n\n[PENDING]\nmeta: uid=$UID_BACKUP\n", + ); + my $reservation = { + existed => 1, + identity => { uid => $UID_TARGET, rtime => 1700000000000 }, + pending => $UID_BACKUP, + reserved => { uid => $UID_TARGET, rtime => 1700000000000 }, + }; + my $res; + { + local $SIG{__WARN__} = sub { }; + $res = + PVE::GuestIdentity::adopt('PVE::QemuConfig', 100, 'cccccccccccccccccccc', $reservation); + } + is($res->{result}, 'kept', 'identity of another backup is not taken over'); + is(published_uid_of(100), $UID_TARGET, 'published identity is kept'); + is(pending_uid_of(100), undef, 'pending uid is removed'); +}; + +# vzdump never archives pending changes, a crafted backup must not bring any along +for my $type (sort keys $ARCHIVES->%*) { + subtest "$type: pending changes of the backup are dropped" => sub { + QemuServerTest::FakeCFS::reset(); + + my $crafted = $BACKUP_WITH_UID + . "\n[PENDING]\nargs: -device foo\nhostpci0: 0000:01:00.0\nmeta: uid=$UID_TARGET\n"; + my ($err, $output) = restore_api($type, $crafted, {}); + is($err, '', 'restore succeeds') or diag($output); + my @writes = $QemuServerTest::FakeCFS::written->{100}->@*; + ok(!grep({ m/^(args|hostpci0):|uid=$UID_TARGET/m } @writes), 'no write has them'); + unlike( + $QemuServerTest::FakeCFS::configs->{100}, + qr/^\[PENDING\]/m, + 'no pending section', + ); + }; +} + +# Once a write of the config of VMID 100 has the pending uid of the backup, all later writes of the +# restore have it, so that the VM keeps it whenever the restore stops. Only the adoption removes it. +for my $type (sort keys $ARCHIVES->%*, 'pbs live') { + for my $target (undef, $TARGET_CONF_NO_UID) { + my $what = $type . ($target ? ', in place over a VM without uid' : ', free VMID'); + + subtest "$what: every write of the restore keeps the pending uid" => sub { + QemuServerTest::FakeCFS::reset(); + QemuServerTest::FakeCFS::set_config(100, $target) if $target; + + my $param = { $target ? (force => 1) : () }; + $param->{'live-restore'} = 1 if $type eq 'pbs live'; + my ($err, $output) = restore_api($type =~ s/ live$//r, $BACKUP_WITH_UID, $param); + is($err, '', 'restore succeeds') or diag($output); + like($output, $ADOPTED, 'uid of the backup is adopted'); + + my @writes = $QemuServerTest::FakeCFS::written->{100}->@*; + my $last = pop(@writes); + my $first = 0; + $first++ while $first < @writes && $writes[$first] !~ m/^\[PENDING\]/m; + ok($first < @writes, 'pending uid is written') or return; + is( + scalar( + grep { m/^\[PENDING\]\nmeta: uid=$UID_BACKUP$/m } + @writes[$first .. $#writes] + ), + scalar(@writes) - $first, + 'all later writes keep it', + ); + unlike($last, qr/^\[PENDING\]/m, 'adoption removes it'); + }; + } +} + +$fail_at = ''; + done_testing(); -- 2.47.3