From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server v4 09/21] migrate: block live migration of host CPU VMs between CPU vendors
Date: Wed, 2 Dec 2026 16:18:05 +0100 [thread overview]
Message-ID: <20261202151831.414110-10-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261202151831.414110-1-j.theisen@proxmox.com>
The 'host' and 'max' CPU models pass the features of the host CPU
through to the guest, including vendor-specific ones. A guest started
on an AMD CPU is likely to crash after a live migration to an Intel
CPU, or vice versa, as the CPU it detected during boot changed
underneath it.
Check this before the migration, based on the CPU information each node
broadcasts as the new 'host-cpu-info' node key-value entry, which
pvestatd of pve-manager is extended to broadcast. Refuse to migrate to
such nodes, and report them as not allowed in the migration precondition
API, so that clients can show this upfront. Besides the message, the API
reports the values it contains, here the CPU vendors, as typed details,
so that clients can present and translate them without parsing the
message.
Nodes that do not broadcast the information, for example as they run
an older version, are not blocked, but a task warning is logged and
the precondition API reports a warning. This is only done for checks
that can block the migration, so that checks that only warn do not
warn about every migration to an older node, and not if this node
lacks the information as well, like the vendor of an ARM CPU with an
unknown implementer. Warnings are skipped if the migration is refused
anyway.
The checks are defined in a list, so that further combinations of VM
CPU settings and node CPUs that cannot work, or are prone to problems,
can be added easily. They only apply to running VMs, as stopped VMs
are started with a virtual CPU based on the target node, and not to
confidential VMs, which cannot be live migrated at all. They operate
on the QEMU CPU of the running instance, so they also cover custom CPU
models, and parse it with QEMU's precedence: flags with '+' override
properties and flags with '-' override both, regardless of position.
With TCG, which VMs of a foreign architecture use by default, 'max' is
fully emulated and therefore not affected.
If the check itself fails, for example as the QEMU command line of the
VM cannot be read, the migration is not refused, but a task warning is
logged and the precondition API reports a warning for each node. The
check only guards migrations that work without it, so a failure must
not block them, for example the migrations of HA maintenance mode.
The check runs before the conntrack state helper is set up, as a
failed preparation is not cleaned up, so the helper would keep
running.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/API2/Qemu.pm | 80 ++++++-
src/PVE/QemuMigrate.pm | 10 +
src/PVE/QemuMigrate/Helpers.pm | 245 ++++++++++++++++++++++
src/PVE/QemuServer/Helpers.pm | 31 +++
src/test/MigrationTest/QemuMigrateMock.pm | 44 ++++
src/test/run_api_tests.pl | 109 ++++++++++
src/test/run_qemu_migrate_tests.pl | 149 +++++++++++++
7 files changed, 667 insertions(+), 1 deletion(-)
diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 2119e69e..a3f44ac0 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -5599,6 +5599,28 @@ __PACKAGE__->register_method({
description => 'The config key of a mapped resource',
},
},
+ 'incompatible-cpu' => {
+ description =>
+ "Reasons why the node's CPU cannot run the running VM"
+ . " after a live migration.",
+ type => 'array',
+ optional => 1,
+ items => {
+ type => 'object',
+ properties => {
+ type => {
+ type => 'string',
+ description => "The check that failed.",
+ enum => ['cpu-vendor'],
+ },
+ message => {
+ type => 'string',
+ description => "Why the node's CPU is incompatible.",
+ },
+ PVE::QemuMigrate::Helpers::cpu_issue_details_schema()->%*,
+ },
+ },
+ },
'blocking-ha-resources' => {
description => "HA resources, which are blocking the"
. " VM from being migrated to the node.",
@@ -5676,6 +5698,30 @@ __PACKAGE__->register_method({
description =>
"Object of mapped resources with additional information such if they're live migratable.",
},
+ 'cpu-warnings' => {
+ type => 'object',
+ optional => 1,
+ description => "For each node, reasons why the VM might not run reliably on its"
+ . " CPU after a live migration.",
+ additionalProperties => {
+ type => 'array',
+ items => {
+ type => 'object',
+ properties => {
+ type => {
+ type => 'string',
+ description => "The check that reported the warning.",
+ enum => ['missing-cpu-info', 'cpu-check-failed'],
+ },
+ message => {
+ type => 'string',
+ description => "Why the VM might not run reliably.",
+ },
+ PVE::QemuMigrate::Helpers::cpu_issue_details_schema()->%*,
+ },
+ },
+ },
+ },
'has-dbus-vmstate' => {
type => 'boolean',
description => 'Whether the VM host supports migrating additional VM state, '
@@ -5716,7 +5762,8 @@ __PACKAGE__->register_method({
# try to detect errors early
PVE::QemuConfig->check_lock($vmconf);
- $res->{running} = PVE::QemuServer::check_running($vmid) ? 1 : 0;
+ my $pid = PVE::QemuServer::check_running($vmid);
+ $res->{running} = $pid ? 1 : 0;
my ($local_resources, $mapped_resources, $missing_mappings_by_node) =
PVE::QemuMigrate::Helpers::check_local_resources($vmconf, $res->{running}, 1);
@@ -5746,6 +5793,24 @@ __PACKAGE__->register_method({
my $has_identity = PVE::QemuConfig->has_stored_identity($vmid);
my $nodelist = PVE::Cluster::get_nodelist();
+
+ my $targets = [grep { $_ ne $localnode } $nodelist->@*];
+ my $cpu_issues_by_node = eval {
+ PVE::QemuMigrate::Helpers::check_target_cpu_compatibility(
+ $vmid, $vmconf, $pid, $targets,
+ );
+ };
+ if (my $err = $@) {
+ chomp($err);
+ # the migration does not refuse the nodes either, but warns about the failed check
+ my $issue = {
+ type => 'cpu-check-failed',
+ severity => 'warning',
+ message => "unable to check whether the node's CPU can run the VM - $err",
+ };
+ $cpu_issues_by_node = { map { $_ => [$issue] } $targets->@* };
+ }
+
for my $node ($nodelist->@*) {
next if $node eq $localnode;
@@ -5761,6 +5826,19 @@ __PACKAGE__->register_method({
$missing_mappings;
}
+ for my $issue (($cpu_issues_by_node->{$node} // [])->@*) {
+ my $reason = {
+ type => $issue->{type},
+ message => $issue->{message},
+ ($issue->{details} // {})->%*,
+ };
+ if ($issue->{severity} eq 'error') {
+ push $res->{not_allowed_nodes}->{$node}->{'incompatible-cpu'}->@*, $reason;
+ } else {
+ push $res->{'cpu-warnings'}->{$node}->@*, $reason;
+ }
+ }
+
# extracting blocking resources for current node
if (my $blocking_ha_resources = $blocking_ha_resources_by_node->{$node}) {
$res->{not_allowed_nodes}->{$node}->{'blocking-ha-resources'} =
diff --git a/src/PVE/QemuMigrate.pm b/src/PVE/QemuMigrate.pm
index d3f6f155..d450cd23 100644
--- a/src/PVE/QemuMigrate.pm
+++ b/src/PVE/QemuMigrate.pm
@@ -18,6 +18,7 @@ use PVE::GuestHelpers qw(safe_boolean_ne safe_string_ne);
use PVE::GuestID;
use PVE::INotify;
use PVE::JSONSchema;
+use PVE::RESTEnvironment qw(log_warn);
use PVE::RPCEnvironment;
use PVE::Replication;
use PVE::ReplicationConfig;
@@ -250,6 +251,15 @@ sub prepare {
# during migration and remain paused after migration finishes.
$self->{vm_was_paused} = 1 if PVE::QemuServer::vm_is_paused($vmid, 0);
+ # Nodes of other clusters do not broadcast their CPU information to this cluster. Checked
+ # before setting up the conntrack state helper, as a failing preparation is not cleaned up.
+ if (!$self->{opts}->{remote}) {
+ my $warnings = PVE::QemuMigrate::Helpers::assert_target_cpu_compatibility(
+ $vmid, $conf, $pid, $self->{node},
+ );
+ log_warn($_) for $warnings->@*;
+ }
+
if ($self->{opts}->{'with-conntrack-state'}) {
if ($self->{opts}->{remote}) {
# shouldn't be reached in normal circumstances anyway, as we prevent it on
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index e0eec8b1..23f4d589 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -4,13 +4,18 @@ use strict;
use warnings;
use JSON;
+use Storable qw(dclone);
use PVE::Cluster;
+use PVE::INotify;
use PVE::JSONSchema qw(parse_property_string);
use PVE::Mapping::Dir;
use PVE::Mapping::PCI;
use PVE::Mapping::USB;
+use PVE::QemuServer::CPUConfig;
+use PVE::QemuServer::CPUFlags;
+use PVE::QemuServer::Helpers;
use PVE::QemuServer::Monitor qw(mon_cmd);
use PVE::QemuServer::Virtiofs;
@@ -113,6 +118,156 @@ sub check_local_resources {
return wantarray ? (\@loc_res, $mapped_res, $missing_mappings_by_node) : \@loc_res;
}
+# Returns the CPU information the node broadcasts, see get_host_cpu_info() in
+# PVE::QemuServer::Helpers. Information the node does not broadcast is missing.
+my sub get_node_cpu_info {
+ my ($node) = @_;
+
+ my $raw = PVE::Cluster::get_node_kv('host-cpu-info', $node)->{$node};
+ my $info = defined($raw) ? eval { decode_json($raw) } : undef;
+
+ return ref($info) eq 'HASH' ? $info : {};
+}
+
+# Parses a QEMU '-cpu' argument into the CPU model and its properties. Flags given without a value
+# are stored as 'on' or 'off'. As in QEMU, flags with '+' override properties, and flags with '-'
+# override both, regardless of their position.
+my sub parse_qemu_cpu_string {
+ my ($cpu_str) = @_;
+
+ my ($model, @options) = split(/,/, $cpu_str);
+
+ my ($props, $plus, $minus) = ({}, [], []);
+ for my $option (@options) {
+ my ($op, $name, $value) = $option =~ m/^([+-]?)([^=]+)(?:=(.*))?$/ or next;
+ $name = PVE::QemuServer::CPUFlags::normalize_cpu_flag($name);
+ if ($op eq '+') {
+ push $plus->@*, $name;
+ } elsif ($op eq '-') {
+ push $minus->@*, $name;
+ } else {
+ $props->{$name} = $value // 'on';
+ }
+ }
+ $props->{$_} = 'on' for $plus->@*;
+ $props->{$_} = 'off' for $minus->@*;
+
+ return { model => $model, props => $props };
+}
+
+my sub is_host_cpu_model {
+ my ($cpu) = @_;
+ # with TCG, 'max' is fully emulated instead of passing through the host CPU
+ return $cpu->{model} eq 'host' || ($cpu->{model} eq 'max' && $cpu->{kvm});
+}
+
+my sub vendors_differ {
+ my ($source, $target) = @_;
+
+ my ($source_vendor, $target_vendor) = ($source->{vendor}, $target->{vendor});
+ return defined($source_vendor) && defined($target_vendor) && $source_vendor ne $target_vendor;
+}
+
+# The values of CPU issues, so that API clients do not need to parse them from the message.
+my $cpu_issue_details = {
+ 'source-vendor' => {
+ type => 'string',
+ optional => 1,
+ description => "The CPU vendor of the source node.",
+ },
+ 'target-vendor' => {
+ type => 'string',
+ optional => 1,
+ description => "The CPU vendor of the target node.",
+ },
+};
+
+my sub vendor_details {
+ my ($source, $target) = @_;
+ return { 'source-vendor' => $source->{vendor}, 'target-vendor' => $target->{vendor} };
+}
+
+=head3 cpu_issue_details_schema
+
+ my $properties = cpu_issue_details_schema();
+
+Returns the JSON schema properties of the C<details> of CPU issues, see
+check_target_cpu_compatibility().
+
+=cut
+
+sub cpu_issue_details_schema {
+ return dclone($cpu_issue_details);
+}
+
+# Checks whether the CPU of a target node can run a running VM after a live migration. Each check
+# has:
+# - 'type', identifying the check
+# - 'severity', 'error' if an incompatibility prevents the migration, 'warning' if it is only
+# reported
+# - 'needs', listing the CPU information the check requires of the target node and, to compare
+# them, of this node, nodes lacking any of it are skipped
+# - 'applies', returning true if the check is relevant for the VM's CPU
+# - 'check', returning why the target node is incompatible, if it is, and optionally the details
+# of the incompatibility, see $cpu_issue_details
+#
+# The VM's CPU is parsed by parse_qemu_cpu_string(), with:
+# - 'kvm' set if the VM uses KVM
+#
+# The CPU information of the nodes is in the format of get_host_cpu_info().
+my $target_cpu_checks = [
+ {
+ type => 'cpu-vendor',
+ severity => 'error',
+ needs => ['vendor'],
+ applies => sub {
+ my ($cpu) = @_;
+ # these models pass through the host CPU, including vendor-specific features
+ return is_host_cpu_model($cpu);
+ },
+ check => sub {
+ my ($cpu, $source, $target) = @_;
+
+ return if !vendors_differ($source, $target);
+
+ my $message = "CPU model '$cpu->{model}' requires the same CPU vendor as on the"
+ . " source node ($source->{vendor}), but the node has $target->{vendor}";
+ return ($message, vendor_details($source, $target));
+ },
+ },
+];
+
+# Returns the issues of the target node. If it lacks CPU information needed by a check that can
+# prevent the migration, that is an issue as well, but only a warning, as the node is not refused.
+# Information this node lacks as well, for example the vendor of an unknown ARM CPU, is not
+# reported.
+my sub get_target_cpu_issues {
+ my ($cpu, $checks, $source_info, $target_info) = @_;
+
+ my ($issues, $target_complete) = ([], 1);
+ for my $check ($checks->@*) {
+ my @missing = grep { !defined($target_info->{$_}) } $check->{needs}->@*;
+ if (scalar(@missing)) {
+ $target_complete = 0
+ if $check->{severity} eq 'error' && grep { defined($source_info->{$_}) } @missing;
+ next;
+ }
+
+ my ($message, $details) = $check->{check}->($cpu, $source_info, $target_info) or next;
+ my $issue = { type => $check->{type}, severity => $check->{severity}, message => $message };
+ $issue->{details} = $details if $details;
+ push $issues->@*, $issue;
+ }
+
+ if (!$target_complete) {
+ my $message = "the node does not provide the CPU information needed to check it, for"
+ . " example as it runs an older version";
+ push $issues->@*,
+ { type => 'missing-cpu-info', severity => 'warning', message => $message };
+ }
+ return $issues;
+}
+
=head3 is_running_cpu_kept
my $kept = is_running_cpu_kept($conf);
@@ -129,6 +284,96 @@ sub is_running_cpu_kept {
return $conf->{cpu} && PVE::QemuServer::CPUConfig::is_abstracted($conf->{cpu}) ? 1 : 0;
}
+=head3 check_target_cpu_compatibility
+
+ my $issues_by_node = check_target_cpu_compatibility($vmid, $conf, $pid, $nodes);
+
+Checks whether the VM C<$vmid> with the configuration C<$conf>, running with the process ID
+C<$pid>, can run with its current virtual CPU on each node of C<$nodes>, based on the CPU
+information the nodes broadcast. Only applies to running VMs, as stopped VMs are started with a
+virtual CPU matching the target node, and not to confidential VMs, which cannot be live migrated
+at all.
+
+Returns a hash reference with the list of issues for each node that has any. Each issue is a hash
+with a C<type> identifying the check, a C<severity> of C<error> if the VM cannot run on the node
+or C<warning> if it might not, a human-readable C<message>, and optionally C<details> with the
+values the message contains, see cpu_issue_details_schema(). Nodes that lack CPU information
+needed by a check that can prevent the migration, for example as they run an older version, have
+an issue of type C<missing-cpu-info> with severity C<warning>, unless this node lacks that
+information as well.
+
+=cut
+
+sub check_target_cpu_compatibility {
+ my ($vmid, $conf, $pid, $nodes) = @_;
+
+ # confidential VMs cannot be live migrated at all
+ return {} if !$pid || PVE::QemuServer::CPUConfig::get_cvm_type($conf);
+
+ my $cpu = parse_qemu_cpu_string(PVE::QemuServer::CPUConfig::get_cpu_from_running_vm($pid));
+ my $arch = PVE::QemuServer::Helpers::get_vm_arch($conf);
+ $cpu->{kvm} = $conf->{kvm} // PVE::QemuServer::CPUConfig::is_native_arch($arch);
+
+ my $checks = [grep { $_->{applies}->($cpu) } $target_cpu_checks->@*];
+ return {} if !scalar($checks->@*);
+
+ # Use what this node broadcasts, as for the target nodes. The local pvestatd might not
+ # broadcast it yet, for example if it was not restarted after an upgrade.
+ my $source_info = get_node_cpu_info(PVE::INotify::nodename());
+ $source_info = PVE::QemuServer::Helpers::get_host_cpu_info() if !scalar(keys $source_info->%*);
+
+ my $issues_by_node = {};
+ for my $node ($nodes->@*) {
+ my $target_info = get_node_cpu_info($node);
+
+ my $issues = get_target_cpu_issues($cpu, $checks, $source_info, $target_info);
+ $issues_by_node->{$node} = $issues if scalar($issues->@*);
+ }
+
+ return $issues_by_node;
+}
+
+=head3 assert_target_cpu_compatibility
+
+ my $warnings = assert_target_cpu_compatibility($vmid, $conf, $pid, $node);
+
+Checks whether the running VM can run with its current virtual CPU on the node C<$node>, see
+check_target_cpu_compatibility(). Dies if the node is incompatible.
+
+Returns an array reference with the warnings to log, about possible incompatibilities, including
+missing CPU information of the node. If the check itself fails, the node is not refused, but a
+warning about the failure is returned, as the check only guards migrations that work without it.
+
+=cut
+
+sub assert_target_cpu_compatibility {
+ my ($vmid, $conf, $pid, $node) = @_;
+
+ my $issues_by_node = eval { check_target_cpu_compatibility($vmid, $conf, $pid, [$node]) };
+ if (my $err = $@) {
+ chomp($err);
+ return ["unable to check whether the CPU of node '$node' can run the VM, migrating anyway"
+ . " - $err"
+ ];
+ }
+
+ my ($errors, $warnings) = ([], []);
+ for my $issue (($issues_by_node->{$node} // [])->@*) {
+ if ($issue->{severity} eq 'warning') {
+ push $warnings->@*, "CPU of node '$node' might be incompatible: $issue->{message}";
+ } else {
+ push $errors->@*, $issue;
+ }
+ }
+
+ if (scalar($errors->@*)) {
+ die "can't migrate VM to '$node' - incompatible CPU: "
+ . join('; ', map { $_->{message} } $errors->@*) . "\n";
+ }
+
+ return $warnings;
+}
+
sub set_migration_caps {
my ($vmid, $savevm) = @_;
diff --git a/src/PVE/QemuServer/Helpers.pm b/src/PVE/QemuServer/Helpers.pm
index 644287bc..b15599bb 100644
--- a/src/PVE/QemuServer/Helpers.pm
+++ b/src/PVE/QemuServer/Helpers.pm
@@ -364,4 +364,35 @@ sub get_host_phys_address_bits {
return; # undef, cannot really do anything..
}
+=head3 get_host_cpu_info
+
+ my $info = get_host_cpu_info();
+
+Returns the properties of this host's CPU that decide whether a running VM can be live-migrated
+to it. pvestatd broadcasts them to the cluster as the C<host-cpu-info> node key-value entry,
+encoded as JSON. The returned hash contains:
+
+=over
+
+=item C<vendor>: the CPU vendor ID, for example C<GenuineIntel> or C<AuthenticAMD>
+
+=back
+
+Properties that cannot be determined are missing. Consumers of the broadcast information must
+also handle properties that nodes running an older version do not provide yet.
+
+=cut
+
+sub get_host_cpu_info {
+ my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
+
+ # read_cpuinfo() reports values it cannot determine as 'unknown'
+ my $known = sub { $_[0] ne 'unknown' };
+
+ my $info = {};
+ $info->{vendor} = $cpuinfo->{vendor} if $known->($cpuinfo->{vendor});
+
+ return $info;
+}
+
1;
diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm
index 1d895c32..bfccef5a 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -31,6 +31,7 @@ my $migrate_params = decode_json(file_get_contents("${RUN_DIR_PATH}/migrate_para
my $test_vmid = $migrate_params->{vmid};
my $test_target = $migrate_params->{target};
my $test_opts = $migrate_params->{opts};
+my $test_cpu_info = $migrate_params->{cpu_info} // {};
my $current_log = '';
my $vm_stop_executed = 0;
@@ -116,6 +117,10 @@ $qemu_migrate_module->mock(
my ($self, $level, $message) = @_;
$current_log .= "$level: $message\n";
},
+ log_warn => sub {
+ my ($message) = @_;
+ $current_log .= "warn: $message\n";
+ },
mon_cmd => sub {
my ($vmid, $command, %params) = @_;
@@ -225,13 +230,52 @@ $qemu_server_cpuconfig_module->mock(
},
);
+$MigrationTest::Shared::qemu_migrate_helpers_module->mock(
+ check_target_cpu_compatibility => sub {
+ my ($vmid, $conf, $pid, $nodes) = @_;
+ # checking requires the running CPU, which most tests do not need to specify
+ return {} if $pid && !defined($vm_status->{runningcpu});
+ die $test_cpu_info->{'check-error'} if defined($test_cpu_info->{'check-error'});
+ return $MigrationTest::Shared::qemu_migrate_helpers_module->original(
+ 'check_target_cpu_compatibility')->(@_);
+ },
+);
+
+$MigrationTest::Shared::cluster_module->mock(
+ get_node_kv => sub {
+ my ($key, $node) = @_;
+ if ($key eq 'host-cpu-info') {
+ # the source node is 'pve0', without 'source' get_host_cpu_info() is used
+ my $info =
+ $node eq 'pve0' ? $test_cpu_info->{source} : $test_cpu_info->{nodes}->{$node};
+ return {} if !$info;
+ return { $node => encode_json($info) };
+ }
+ return {};
+ },
+);
+
my $qemu_server_helpers_module = Test::MockModule->new("PVE::QemuServer::Helpers");
$qemu_server_helpers_module->mock(
+ get_host_cpu_info => sub {
+ # the source node's broadcast reflects what it reads locally
+ return { ($test_cpu_info->{source} // { vendor => 'GenuineIntel' })->%* };
+ },
vm_running_locally => sub {
return $vm_status->{running} && !$vm_stop_executed;
},
);
+my $dbus_vmstate_module = Test::MockModule->new("PVE::QemuServer::DBusVMState");
+$dbus_vmstate_module->mock(
+ # only migrations that are refused before starting the helper are tested
+ qemu_add_dbus_vmstate => sub { die "conntrack state helper started\n"; },
+);
+
+# the tests assume an x86_64 host, also when run on another architecture
+my $tools_module = Test::MockModule->new("PVE::Tools");
+$tools_module->mock(get_host_arch => sub { return 'x86_64'; });
+
my $qemu_server_machine_module = Test::MockModule->new("PVE::QemuServer::Machine");
$qemu_server_machine_module->mock(
qemu_machine_pxe => sub {
diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl
index f2916346..22a0533b 100755
--- a/src/test/run_api_tests.pl
+++ b/src/test/run_api_tests.pl
@@ -8,6 +8,7 @@ use warnings;
use lib qw(..);
+use JSON;
use Storable qw(dclone);
use Test::MockModule;
use Test::More;
@@ -333,6 +334,114 @@ subtest 'migration preconditions' => sub {
mock_unreachable_config();
};
+subtest 'migration preconditions of the CPU of a running VM' => sub {
+ # the node key-value entries of the source node 'localhost' and of the target nodes, which
+ # differ from the source as their names say, while 'no-cpu-info' broadcasts none
+ my $entries = sub {
+ my ($info) = @_;
+ return { 'host-cpu-info' => encode_json($info) };
+ };
+ my $source = { vendor => 'GenuineIntel' };
+ my $node_kv = {
+ localhost => $entries->($source),
+ 'other-vendor' => $entries->({ $source->%*, vendor => 'AuthenticAMD' }),
+ };
+ my $nodes = ['localhost', 'other-vendor', 'no-cpu-info'];
+
+ my $inotify_module = Test::MockModule->new('PVE::INotify');
+ $inotify_module->mock(nodename => sub { return 'localhost'; });
+ my $ha_config_module = Test::MockModule->new('PVE::HA::Config');
+ $ha_config_module->mock(vm_is_ha_managed => sub { return 0; });
+ my $migrate_helpers_module = Test::MockModule->new('PVE::QemuMigrate::Helpers');
+ $migrate_helpers_module->mock(
+ check_local_resources => sub {
+ return ([], {}, { map { $_ => [] } $nodes->@* });
+ },
+ );
+ $qemu_config_module->mock(load_config => sub { return { memory => 512 }; });
+ $qemu_server_module->mock(check_running => sub { return 1234; });
+ my $running_cpu;
+ my $cpu_config_module = Test::MockModule->new('PVE::QemuServer::CPUConfig');
+ $cpu_config_module->mock(get_cpu_from_running_vm => sub { return $running_cpu; });
+ $cluster_module->mock(
+ get_nodelist => sub { return $nodes; },
+ get_config => sub { return $raw_without_identity; },
+ get_node_kv => sub {
+ my ($key, $node) = @_;
+ my $value = $node_kv->{$node}->{$key} // return {};
+ return { $node => $value };
+ },
+ );
+
+ # Returns the CPU reasons of each node for the VM running with the QEMU CPU $running_cpu, the
+ # types of the incompatibilities with their override option, and of the warnings, prefixed.
+ my $cpu_reasons = sub {
+ ($running_cpu) = @_;
+
+ my $res =
+ PVE::API2::Qemu->migrate_vm_precondition({ node => 'localhost', vmid => 100 });
+ is(validate_migration_preconditions($res), '', "$running_cpu: reasons are documented");
+
+ my $reasons = {};
+ for my $node (keys $res->{not_allowed_nodes}->%*) {
+ for my $error (
+ ($res->{not_allowed_nodes}->{$node}->{'incompatible-cpu'} // [])->@*
+ ) {
+ my $override = defined($error->{override}) ? " ($error->{override})" : '';
+ push $reasons->{$node}->@*, "$error->{type}$override";
+ }
+ }
+ for my $node (keys(($res->{'cpu-warnings'} // {})->%*)) {
+ push $reasons->{$node}->@*,
+ map { "warning: $_->{type}" } $res->{'cpu-warnings'}->{$node}->@*;
+ }
+ return $reasons;
+ };
+
+ is_deeply(
+ $cpu_reasons->('host,+kvm_pv_eoi,+kvm_pv_unhalt'),
+ {
+ 'other-vendor' => ['cpu-vendor'],
+ 'no-cpu-info' => ['warning: missing-cpu-info'],
+ },
+ "reasons for 'host'",
+ );
+
+ # confidential VMs cannot be live migrated at all
+ $qemu_config_module->mock(
+ load_config => sub { return { memory => 512, 'intel-tdx' => 'type=tdx' }; });
+ is_deeply(
+ $cpu_reasons->('host,+kvm_pv_eoi,+kvm_pv_unhalt'),
+ {},
+ 'confidential VM not checked',
+ );
+ $qemu_config_module->mock(load_config => sub { return { memory => 512 }; });
+
+ my $res = PVE::API2::Qemu->migrate_vm_precondition({ node => 'localhost', vmid => 100 });
+ my $vendor_reason = $res->{not_allowed_nodes}->{'other-vendor'}->{'incompatible-cpu'}->[0];
+ is_deeply(
+ [$vendor_reason->@{qw(source-vendor target-vendor)}],
+ ['GenuineIntel', 'AuthenticAMD'],
+ 'reasons include their values as details',
+ );
+
+ # the migration only warns about a failed check as well
+ $cpu_config_module->mock(
+ get_cpu_from_running_vm =>
+ sub { die "could not read commandline of running machine\n"; },
+ );
+ is_deeply(
+ $cpu_reasons->('host'),
+ { map { $_ => ['warning: cpu-check-failed'] } grep { $_ ne 'localhost' } $nodes->@* },
+ 'failed check reported as warning for each node',
+ );
+ $cpu_config_module->mock(get_cpu_from_running_vm => sub { return $running_cpu; });
+
+ $qemu_server_module->mock(check_running => sub { return 0; });
+ $cluster_module->unmock('get_nodelist', 'get_config', 'get_node_kv');
+ mock_unreachable_config();
+};
+
subtest 'clone target nodes' => sub {
my $inotify_module = Test::MockModule->new('PVE::INotify');
$inotify_module->mock(nodename => sub { return 'pve1'; });
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index d22c64c1..b2b4d954 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -520,6 +520,70 @@ sub local_volids_for_vm {
return $res;
}
+# Returns a copy of $base, with the values of $override set recursively in nested hashes. An
+# undefined value removes the key instead.
+sub merge_hash {
+ my ($base, $override) = @_;
+
+ my $merged = { $base->%* };
+ for my $key (keys $override->%*) {
+ my $value = $override->{$key};
+ if (!defined($value)) {
+ delete $merged->{$key};
+ } elsif (ref($value) eq 'HASH' && ref($merged->{$key}) eq 'HASH') {
+ $merged->{$key} = merge_hash($merged->{$key}, $value);
+ } else {
+ $merged->{$key} = $value;
+ }
+ }
+
+ return $merged;
+}
+
+# CPU information of the source node 'pve0' and the target node 'pve1' in the CPU migration tests,
+# with nothing preventing the migration. The tests override what they check.
+my $default_cpu_info = {
+ source => { vendor => 'GenuineIntel' },
+ nodes => {
+ pve1 => { vendor => 'GenuineIntel' },
+ },
+};
+
+# Returns a test of the CPU checks for a live migration of VM 1033 to 'pve1'. The VM is configured
+# with the CPU 'cpu' and further settings in 'config', and runs with the QEMU CPU 'runningcpu'.
+# 'cpu_info' overrides $default_cpu_info. Without 'expect_die', the migration must succeed.
+# Warnings and errors about the CPU must be expected. Other keys are passed through.
+sub cpu_migration_test {
+ my (%test) = @_;
+
+ my $runningcpu = delete $test{runningcpu};
+ my $config_patch = { (delete($test{config}) // {})->%* };
+ my $cpu = delete $test{cpu};
+ $config_patch->{cpu} = $cpu if defined($cpu);
+
+ my $expected_status = { running => 1, runningmachine => 'pc-q35-5.0+pve0' };
+ $expected_status->{runningcpu} = $runningcpu if $test{expect_die};
+
+ return {
+ target => 'pve1',
+ vmid => 1033,
+ vm_status =>
+ { running => 1, runningmachine => 'pc-q35-5.0+pve0', runningcpu => $runningcpu },
+ opts => { online => 1, (delete($test{opts}) // {})->%* },
+ config_patch => $config_patch,
+ cpu_info => merge_hash($default_cpu_info, delete($test{cpu_info}) // {}),
+ expected_calls => $test{expect_die} ? {} : $default_expected_calls_online,
+ unexpected_log => ['CPU'],
+ expected => {
+ source_volids => {},
+ target_volids => {},
+ vm_config => get_patched_config(1033, $config_patch),
+ vm_status => $expected_status,
+ },
+ %test,
+ };
+}
+
my $tests = [
# each test consists of the following:
# name - unique name for the test which also serves as a dir name.
@@ -530,6 +594,9 @@ my $tests = [
# opts - options for the migrate() call
# target_volids - hash of volids on the target at the beginning
# vm_status - hash with running, runningmachine and optionally runningcpu
+ # cpu_info - hash with the CPU information of the 'source' node and of each
+ # other node in 'nodes'. With 'check-error', the CPU check fails
+ # with it. See cpu_migration_test() for tests of the CPU checks.
# expected_calls - hash whose keys are calls which are required
# to be made if the migration gets far enough
# expect_die - expect the migration call to fail, and an error message
@@ -1479,6 +1546,87 @@ my $tests = [
},
},
},
+ cpu_migration_test(
+ name => '1033_running_host_cpu_other_vendor',
+ cpu => 'host',
+ runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ source => { vendor => 'AuthenticAMD' },
+ },
+ expect_die => "can't migrate VM to 'pve1' - incompatible CPU: CPU model 'host' requires"
+ . " the same CPU vendor as on the source node (AuthenticAMD), but the node has"
+ . " GenuineIntel",
+ ),
+ cpu_migration_test(
+ # refused before starting the conntrack state helper, as a failed preparation is not
+ # cleaned up
+ name => '1033_running_host_cpu_other_vendor_conntrack',
+ cpu => 'host',
+ runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt',
+ opts => { 'with-conntrack-state' => 1 },
+ cpu_info => {
+ source => { vendor => 'AuthenticAMD' },
+ },
+ expect_die => "can't migrate VM to 'pve1' - incompatible CPU: CPU model 'host' requires"
+ . " the same CPU vendor as on the source node (AuthenticAMD), but the node has"
+ . " GenuineIntel",
+ ),
+ cpu_migration_test(
+ # the check only guards migrations that work without it
+ name => '1033_running_cpu_check_failed',
+ cpu => 'host',
+ runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ source => { vendor => 'AuthenticAMD' },
+ 'check-error' => "could not read commandline of running machine\n",
+ },
+ expect_log => [
+ "unable to check whether the CPU of node 'pve1' can run the VM, migrating anyway -"
+ . " could not read commandline of running machine",
+ ],
+ ),
+ cpu_migration_test(
+ name => '1033_running_host_cpu_target_without_cpu_info',
+ cpu => 'host',
+ runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ nodes => { pve1 => undef },
+ },
+ expect_log => [
+ "CPU of node 'pve1' might be incompatible: the node does not provide the CPU"
+ . " information needed to check it",
+ ],
+ ),
+ cpu_migration_test(
+ # with TCG, 'max' is fully emulated instead of passing through the host CPU
+ name => '1033_running_max_cpu_tcg_other_vendor',
+ cpu => 'max',
+ config => { kvm => 0 },
+ runningcpu => 'max',
+ cpu_info => {
+ source => { vendor => 'AuthenticAMD' },
+ },
+ ),
+ cpu_migration_test(
+ # VMs of another architecture than the host's run with TCG by default
+ name => '1033_running_max_cpu_emulated_other_vendor',
+ cpu => 'max',
+ config => { arch => 'aarch64' },
+ runningcpu => 'max',
+ cpu_info => {
+ source => { vendor => 'AuthenticAMD' },
+ },
+ ),
+ cpu_migration_test(
+ # for example with ARM CPUs of an unknown implementer, no node knows its CPU vendor
+ name => '1033_running_host_cpu_vendor_unknown',
+ cpu => 'host',
+ runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ source => { vendor => undef },
+ nodes => { pve1 => { vendor => undef } },
+ },
+ ),
{
name => '105_replicated_to_non_replication_target',
target => 'pve1',
@@ -1900,6 +2048,7 @@ foreach my $test (@{$tests}) {
target => $test->{target},
vmid => $test->{vmid},
opts => $test->{opts},
+ cpu_info => $test->{cpu_info},
};
file_set_contents("${run_dir}/nbd_info", to_json({}));
--
2.47.3
next prev parent reply other threads:[~2026-12-02 15:21 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-02 15:17 [PATCH docs/manager/qemu-server v4 00/21] check the target node's CPU before live migrating a VM Jonas Theisen
2026-12-02 15:17 ` [PATCH qemu-server v4 01/21] buildsys: ignore migration test runtime files Jonas Theisen
2026-12-02 15:17 ` [PATCH qemu-server v4 02/21] test: migration: allow checking for warnings and errors in the log Jonas Theisen
2026-12-02 15:17 ` [PATCH qemu-server v4 03/21] cpu flags: normalize underscores in all CPU flag names Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 04/21] test: api: factor out validating the migration preconditions Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 05/21] cpu flags: factor out parsing the broadcast CPU flags of a node Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 06/21] migrate: factor out whether the running virtual CPU is kept Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 07/21] cpu flags: factor out the flags of a CPU model expansion Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 08/21] cpu config: factor out whether hv-evmcs is enabled for a CPU vendor Jonas Theisen
2026-12-02 15:18 ` Jonas Theisen [this message]
2026-12-02 15:18 ` [PATCH qemu-server v4 10/21] fix #6535: migrate: warn about live migration between CPU vendors Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 11/21] fix #2924: migrate: block live migration of host CPU VMs to nodes lacking flags Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 12/21] migrate: warn about live migration of host CPU VMs between CPU models Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 13/21] migrate: block live migration if the target cannot run the virtual CPU Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 14/21] api: migrate: check the CPU of the target before requesting HA migration Jonas Theisen
2026-12-02 15:18 ` [PATCH manager v4 15/21] pvestatd: broadcast the supported CPU flags again if they got lost Jonas Theisen
2026-12-02 15:18 ` [PATCH manager v4 16/21] pvestatd: broadcast host CPU information relevant for live migration Jonas Theisen
2026-12-02 15:18 ` [PATCH manager v4 17/21] ui: migrate: show why the CPU of the target node is incompatible Jonas Theisen
2026-12-02 15:18 ` [PATCH manager v4 18/21] fix #6535: ui: migrate: show CPU warnings for the target node Jonas Theisen
2026-12-02 15:18 ` [PATCH manager v4 19/21] api: nodes: migrateall: skip VMs whose CPU is incompatible with the target Jonas Theisen
2026-12-02 15:18 ` [PATCH manager v4 20/21] api: cluster: bulk migrate: " Jonas Theisen
2026-12-02 15:18 ` [PATCH docs v4 21/21] qm: document the CPU checks for live migration Jonas Theisen
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=20261202151831.414110-10-j.theisen@proxmox.com \
--to=j.theisen@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox