From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH container v4 25/41] restore: refuse overwriting a container with another guest's backup
Date: Thu, 19 Nov 2026 11:34:11 +0100 [thread overview]
Message-ID: <20261119103551.1666964-26-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261119103551.1666964-1-t.lamprecht@proxmox.com>
Restoring the backup of another guest over an existing container
results in the data of the backup under the identity of the target.
Tools that follow that identity would take the result for the original
guest.
Refuse a restore if the uids differ, unless 'allow-identity-mismatch'
is set through the create API or 'pct restore'. The existing 'force'
option permits overwriting a container but does not override this
check. With the override, a container that already had an identity
keeps it.
A container that has the uid of the backup as pending uid was left
behind by an unfinished restore of that backup, so a retry may proceed.
Without 'unique', the retry takes that uid over if no other guest uses
it.
For standard input, the config of the backup is only known after the
old volumes were destroyed and the archive got extracted. So require
the override whenever the target already has an identity. A target
without identity can still be overwritten without it.
Compare the uids before the restore destroys anything. The option is a
standard option of pve-guest-common, shared with the restore of VMs.
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
changes v3 -> v4:
* applying the network options only once the old container is removed
moved to an independent fix before the series, which does so for
create as well
* 'allow-identity-mismatch' is the standard option of pve-guest-common,
with a sentence about standard input added for containers
* use prepare_restore of pve-guest-common for the check
* tests: the cases for a container left behind by a restore are one
subtest
* a comment: plainer wording
changes v2 -> v3:
* apply the network options after the old container is removed, so that
removing it does not drop the IPAM mapping of a restored interface
with the same MAC address
* description of 'allow-identity-mismatch': any backup with the pending
uid counts
changes v1 -> v2:
* use the shared check of pve-guest-common
* 'Allow overwriting' instead of 'Allow to overwrite' and a default for
'allow-identity-mismatch' (Dominik)
src/PVE/API2/LXC.pm | 30 +++++++++-
src/test/lifecycle_tests.pl | 111 ++++++++++++++++++++++++++++++++++++
2 files changed, 138 insertions(+), 3 deletions(-)
diff --git a/src/PVE/API2/LXC.pm b/src/PVE/API2/LXC.pm
index 99f3d01..ae46b70 100644
--- a/src/PVE/API2/LXC.pm
+++ b/src/PVE/API2/LXC.pm
@@ -121,6 +121,11 @@ my sub assert_not_restore_from_external {
die "refusing to restore privileged container backup from external source\n";
}
+my $allow_identity_mismatch_option =
+ get_standard_option('pve-guest-allow-identity-mismatch', { requires => 'restore' });
+$allow_identity_mismatch_option->{description} .= " A restore from standard input over a container"
+ . " that has a uid always needs this option, as the backup cannot be checked beforehand.";
+
my $check_storage_access_migrate = sub {
my ($rpcenv, $authuser, $storecfg, $storage, $node) = @_;
@@ -261,6 +266,7 @@ __PACKAGE__->register_method({
. " the guest uid of the backup.",
requires => 'restore',
},
+ 'allow-identity-mismatch' => $allow_identity_mismatch_option,
pool => {
optional => 1,
type => 'string',
@@ -323,6 +329,7 @@ __PACKAGE__->register_method({
my $unprivileged = extract_param($param, 'unprivileged');
my $restore = extract_param($param, 'restore');
my $unique = extract_param($param, 'unique');
+ my $allow_identity_mismatch = extract_param($param, 'allow-identity-mismatch');
$param->{cpuunits} = PVE::CGroup::clamp_cpu_shares($param->{cpuunits})
if defined($param->{cpuunits}); # clamp value depending on cgroup version
@@ -533,9 +540,14 @@ __PACKAGE__->register_method({
($orig_conf, $orig_mp_param, $archive_fh) =
PVE::LXC::Create::recover_config($storage_cfg, $archive, $vmid);
$backup_uid = (PVE::LXC::Config->get_identity($orig_conf) // {})->{uid};
- # a container with another uid keeps it, which only gets logged
+ # Restoring over an existing container must not turn it into another
+ # guest, so this check comes before the restore changes anything.
$pending_uid = PVE::GuestIdentity::prepare_restore(
- 'PVE::LXC::Config', $vmid, $reservation, $backup_uid, 1,
+ 'PVE::LXC::Config',
+ $vmid,
+ $reservation,
+ $backup_uid,
+ $allow_identity_mismatch,
);
for my $opt (keys %$orig_conf) {
@@ -569,6 +581,12 @@ __PACKAGE__->register_method({
) {
$rpcenv->check($authuser, '/', ['Sys.Modify']);
}
+ } elsif ($reservation->{previous} && !$allow_identity_mismatch) {
+ # the uid of such a backup is only known once the container got overwritten
+ my $uid = $reservation->{previous}->{uid};
+ die "cannot verify the guest identity of a backup from standard input"
+ . " before overwriting CT $vmid (uid '$uid') - use"
+ . " 'allow-identity-mismatch' to overwrite the CT anyway\n";
}
}
if ($storage_only_mode) {
@@ -690,8 +708,14 @@ __PACKAGE__->register_method({
my $restored_uid = (PVE::LXC::Config->get_identity($conf) // {})->{uid};
if ($archive eq '-') {
$backup_uid = $restored_uid;
+ # A mismatch only gets logged here, as a restore from standard input
+ # over a container with an identity required the override already.
$pending_uid = PVE::GuestIdentity::prepare_restore(
- 'PVE::LXC::Config', $vmid, $reservation, $backup_uid, 1,
+ 'PVE::LXC::Config',
+ $vmid,
+ $reservation,
+ $backup_uid,
+ $allow_identity_mismatch,
);
} else {
# the merge read the config of the backup again, it must have the uid
diff --git a/src/test/lifecycle_tests.pl b/src/test/lifecycle_tests.pl
index a172393..c77cea6 100755
--- a/src/test/lifecycle_tests.pl
+++ b/src/test/lifecycle_tests.pl
@@ -28,6 +28,7 @@ use PVE::INotify;
use PVE::JSONSchema;
use PVE::Tools qw(file_get_contents file_set_contents);
+use PVE::CLI::pct;
use PVE::LXC;
use PVE::LXC::Config;
use PVE::LXC::Create;
@@ -340,6 +341,7 @@ my $backup_raw_config; # the config in the backup, undef for none
my $backup_read_error; # set to let reading the config of the backup fail
my $backup_fw_config; # the firewall config in the backup, if any
my $hook_error; # set to let the setup hooks at the end of a create or restore fail
+my $volumes_touched = 0;
my $storage_module = Test::MockModule->new('PVE::Storage');
$storage_module->mock(
@@ -378,6 +380,7 @@ $lxc_module->mock(
create_disks => sub {
my ($storecfg, $vmid, $settings, $conf, $pending, $allocated) = @_;
my $vollist = $allocated // [];
+ $volumes_touched++;
for my $ms (sort keys %$settings) {
my $volid = "local:$vmid/vm-$vmid-disk-$ms.raw";
$conf->{$ms} = "$volid,size=4G";
@@ -388,6 +391,7 @@ $lxc_module->mock(
destroy_disks => sub { return [] },
destroy_lxc_container => sub {
my ($storecfg, $vmid, $conf, $replacement_conf) = @_;
+ $volumes_touched++;
if (defined($replacement_conf)) {
PVE::LXC::Config->write_config($vmid, $replacement_conf);
} else {
@@ -529,6 +533,7 @@ my sub reset_state {
$backup_fw_config = undef;
$now_ms = 1_700_000_000_000;
$domain_lock_error = undef;
+ $volumes_touched = 0;
}
# API call helpers
@@ -1993,6 +1998,9 @@ subtest 'using the container left behind drops its pending uid' => sub {
. " restore - a retry now needs 'allow-identity-mismatch'";
like($task_log, qr/^WARN: \Q$dropped\E$/m, 'dropped');
is(pending_uid_of(200), undef, 'pending uid is gone');
+
+ $err = restore_ct(200, { force => 1 });
+ like($err, qr/backup belongs to another guest/, 'container stays a guest of its own');
};
subtest 'restore with unique keeps a pending uid, but never takes it over' => sub {
@@ -2177,6 +2185,109 @@ subtest 'restore fails if the backup changed after its uid was read' => sub {
}
};
+subtest 'restore over a container of another guest needs an override' => sub {
+ my $existing_raw = existing_config($EXISTING_META);
+ my $existing_conf = PVE::LXC::Config::parse_pct_config('/lxc/200.conf', $existing_raw);
+ delete $existing_conf->{digest};
+
+ my $setup = sub {
+ reset_state();
+ $backup_raw_config = backup_config($BACKUP_META);
+ write_raw_config(200, $existing_raw);
+ };
+ my $refused = sub($name, $param, $expected_err) {
+ $setup->();
+ my $err = restore_ct(200, { force => 1, $param->%* });
+ is($err, "unable to restore CT 200 - $expected_err\n", "$name: restore is refused");
+ is($volumes_touched, 0, "$name: volumes are not touched");
+ my $conf = PVE::LXC::Config->load_config(200);
+ delete $conf->{digest};
+ is_deeply($conf, $existing_conf, "$name: config is unchanged");
+ };
+
+ my $mismatch_err =
+ "backup belongs to another guest: its uid 'backupbackupbackupba' differs from uid"
+ . " 'existingexistingexis' of the existing CT 200 - use 'allow-identity-mismatch' to"
+ . " overwrite the CT anyway";
+ my $stdin_err =
+ "cannot verify the guest identity of a backup from standard input before overwriting"
+ . " CT 200 (uid 'existingexistingexis') - use 'allow-identity-mismatch' to overwrite the"
+ . " CT anyway";
+ my $ignored = "guest identity: ignoring backup uid backupbackupbackupba, CT 200 keeps uid"
+ . " existingexistingexis";
+ my $stdin = { ostemplate => '-', rootfs => 'local:4' };
+ $refused->('different uid', {}, $mismatch_err);
+ $refused->('different uid, unique', { unique => 1 }, $mismatch_err);
+ $refused->('from STDIN', $stdin, $stdin_err);
+
+ for my $param ({}, $stdin) {
+ my $name = $param->{ostemplate} ? 'from STDIN' : 'from file';
+ $setup->();
+ my $err = restore_ct(200, { force => 1, 'allow-identity-mismatch' => 1, $param->%* });
+ is($err, '', "$name: restore with override succeeds") or diag($task_log);
+ is(
+ PVE::LXC::Config->load_config(200)->{hostname},
+ 'backup',
+ "$name: backup is restored",
+ );
+ is_deeply(
+ identity_of(200),
+ { uid => 'existingexistingexis', rtime => 1500000000000 },
+ "$name: identity of the container is kept",
+ );
+ like($task_log, qr/^\Q$ignored\E$/m, "$name: override is logged");
+ like(
+ $task_log,
+ qr/^guest identity: kept uid existingexistingexis$/m,
+ "$name: result is logged",
+ );
+ }
+
+ reset_state();
+ $backup_raw_config = backup_config($BACKUP_META);
+ write_raw_config(200, existing_config('ctime=1500000000'));
+ my $err = restore_ct(200, { force => 1, $stdin->%* });
+ is($err, '', 'restore from STDIN over a container without identity succeeds');
+ is(identity_of(200)->{uid}, 'backupbackupbackupba', 'uid of the backup is taken over');
+
+ $err = create_ct(201, { 'allow-identity-mismatch' => 1 });
+ like($err, qr/allow-identity-mismatch/, 'override is only allowed for restores');
+
+ my ($class, $method, undef, $fixed) = $PVE::CLI::pct::cmddef->{restore}->@*;
+ is("$class->$method", 'PVE::API2::LXC->create_vm', 'pct restore uses the create API call');
+ ok($fixed->{restore}, 'pct restore sets restore, so it accepts the override');
+};
+
+subtest 'restore over the container left behind by a restore of a backup' => sub {
+ failed_restore_in_place();
+ $backup_raw_config = backup_config($BACKUP_META =~ s/uid=.*/uid=anotheranotheranothe/r);
+ my $err = restore_ct(200, { force => 1 });
+ like(
+ $err,
+ qr/backup belongs to another guest: its uid .anotheranotheranothe. differs/,
+ 'another backup is refused',
+ );
+ is(pending_uid_of(200), 'backupbackupbackupba', 'pending uid is unchanged');
+
+ $backup_raw_config = backup_config($BACKUP_META);
+ my $param = { force => 1, ostemplate => '-', rootfs => 'local:4' };
+ $err = restore_ct(200, $param);
+ like(
+ $err,
+ qr/cannot verify the guest identity of a backup from standard input/,
+ 'the same backup from STDIN is refused',
+ );
+
+ $err = restore_ct(200, { $param->%*, 'allow-identity-mismatch' => 1 });
+ is($err, '', 'from STDIN with override succeeds') or diag($task_log);
+ is(
+ identity_of(200)->{uid},
+ 'backupbackupbackupba',
+ 'its uid is the pending uid: taken over',
+ );
+ is(pending_uid_of(200), undef, 'pending uid is removed');
+};
+
remove_tree($WORKDIR);
done_testing();
--
2.47.3
next prev parent reply other threads:[~2026-11-19 10:40 UTC|newest]
Thread overview: 46+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 10:33 [PATCH cluster/guest-common/qemu-server/container/manager/docs v4 00/41] fix #7846: guest identity (uid) and cluster ID Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 01/41] pmxcfs: reject changes to the cluster ID file from user space Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 02/41] pmxcfs: generate a cluster ID on standalone nodes Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 03/41] pmxcfs: dfsm: add a callback that runs once all members are in sync Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 04/41] pmxcfs: quorum: record the corosync membership Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 05/41] pmxcfs: dcdb: generate the cluster ID once the cluster is in sync Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 06/41] cluster: add helper to read the cluster ID Thomas Lamprecht
2026-11-19 10:33 ` [PATCH cluster v4 07/41] pmxcfs: allow removing the cluster ID file in forced local mode Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 08/41] guest identity: add helpers for the guest uid and registration time Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 09/41] abstract config: assign a guest identity and keep it on config writes Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 10/41] abstract config: let the VMID reservation return the previous state Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 11/41] guest identity: allow adopting a uid that is unused in the cluster Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 12/41] migrate: add shared helpers for the identity command of the migration tunnel Thomas Lamprecht
2026-11-19 10:33 ` [PATCH guest-common v4 13/41] vzdump: notes template: list the uid variable Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 14/41] api: restore: release the VMID if a restore to an unused VMID fails Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 15/41] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 16/41] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 17/41] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 18/41] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-19 10:34 ` [PATCH qemu-server v4 19/41] api: status: return the guest uid Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 20/41] restore: decide by the reservation whether the container is new Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 21/41] config: add read-only meta property with the creation time Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 22/41] fix #7846: config: add a guest identity to the 'meta' property Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 23/41] api: migrate, clone: refuse targets that would drop the guest identity Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 24/41] restore: take over the guest uid from the backup where possible Thomas Lamprecht
2026-11-19 10:34 ` Thomas Lamprecht [this message]
2026-11-19 10:34 ` [PATCH container v4 26/41] api: remote migration: take over the guest uid from the source Thomas Lamprecht
2026-11-19 10:34 ` [PATCH container v4 27/41] api: status: return the guest uid Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 28/41] api: cluster resources: add guest uid and registration time Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 29/41] api: cluster status: add cluster ID Thomas Lamprecht
2026-11-19 12:32 ` Lukas Wagner
2026-11-19 12:51 ` Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 30/41] ui: datacenter summary: show the " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 31/41] ui: restore: allow overwriting a guest with a backup of another guest Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 32/41] ui: migrate: refuse target nodes that would drop the guest identity Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 33/41] ui: guest summary: show " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 34/41] api: bulk migrate: refuse targets that would drop " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH manager v4 35/41] vzdump: notes template: add the guest uid as variable Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 36/41] vzdump: restore: describe what a failed restore can leave behind Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 37/41] vzdump: describe the guest identity of VMs and containers Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 38/41] pvecm: document the cluster ID and how to get a new one Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 39/41] qm: configuration: describe the read-only meta property Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 40/41] pct: " Thomas Lamprecht
2026-11-19 10:34 ` [PATCH docs v4 41/41] vzdump: notes: document the uid template variable Thomas Lamprecht
2026-11-19 14:46 ` [PATCH cluster/guest-common/qemu-server/container/manager/docs v4 00/41] fix #7846: guest identity (uid) and cluster ID Dominik Csapak
2026-11-19 19:49 ` superseded: " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261119103551.1666964-26-t.lamprecht@proxmox.com \
--to=t.lamprecht@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.