From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server v4 14/21] api: migrate: check the CPU of the target before requesting HA migration
Date: Wed, 2 Dec 2026 16:18:10 +0100 [thread overview]
Message-ID: <20261202151831.414110-15-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261202151831.414110-1-j.theisen@proxmox.com>
For HA-managed VMs, the migrate API only requests the migration from
the HA stack, which runs it in a separate task on the LRM. If the
target CPU cannot run the VM, only that task fails, and the caller
only learns about it from its task log or the HA status.
Run the CPU compatibility check before requesting the HA migration, as
done for the storage checks, so that the API call fails with the
reason directly. Overrides like 'allow-missing-cpu-flags' are only
honored, and suggested by the error, if they are forwarded to the HA
stack. Warnings are left to the LRM's migration task.
Only the requested VM is checked, VMs in positive affinity are only
checked by their own migration task. Migrations the HA stack starts on
its own, for example for maintenance mode or rebalancing, still fail
only in the migration task, which the HA stack currently retries
without backoff, see bug #8059.
Link: https://bugzilla.proxmox.com/show_bug.cgi?id=8059
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/API2/Qemu.pm | 11 ++-
src/PVE/QemuMigrate/Helpers.pm | 7 +-
src/test/run_api_tests.pl | 118 ++++++++++++++++++++++++++++++++-
3 files changed, 131 insertions(+), 5 deletions(-)
diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 68f85afd..326a1e44 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -5963,7 +5963,8 @@ __PACKAGE__->register_method({
PVE::QemuConfig->check_lock($conf);
- if (PVE::QemuServer::check_running($vmid)) {
+ my $pid = PVE::QemuServer::check_running($vmid);
+ if ($pid) {
die "can't migrate running VM without --online\n" if !$param->{online};
my $repl_conf = PVE::ReplicationConfig->new();
@@ -6011,6 +6012,14 @@ __PACKAGE__->register_method({
if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
my $forwards_options = ha_forwards_migrate_options($localnode);
+ # The HA stack migrates the VM in a separate task later, so refuse an incompatible
+ # target here, where the caller sees why. The task logs the warnings. Options that
+ # override an incompatibility only count if they reach the HA stack.
+ my $check_opts = $forwards_options ? $param : undef;
+ PVE::QemuMigrate::Helpers::assert_target_cpu_compatibility(
+ $vmid, $conf, $pid, $target, $check_opts,
+ ) if $param->{online};
+
my $hacmd = sub {
my $upid = shift;
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index 20534b71..22ed96d7 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -665,7 +665,8 @@ sub check_target_cpu_compatibility {
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, unless the migration options
-C<$opts> override all the incompatibilities.
+C<$opts> override all the incompatibilities. If C<$opts> is undefined, as no option can override
+them, the error does not suggest any.
Returns an array reference with the warnings to log, about possible incompatibilities, including
missing CPU information of the node, and about overridden incompatibilities. If the check itself
@@ -690,7 +691,7 @@ sub assert_target_cpu_compatibility {
my $override = $issue->{override};
if ($issue->{severity} eq 'warning') {
push $warnings->@*, "CPU of node '$node' might be incompatible: $issue->{message}";
- } elsif (defined($override) && $opts->{$override}) {
+ } elsif (defined($override) && $opts && $opts->{$override}) {
push $warnings->@*,
"ignoring incompatible CPU of node '$node' due to '$override': $issue->{message}";
} else {
@@ -700,7 +701,7 @@ sub assert_target_cpu_compatibility {
if (scalar($errors->@*)) {
my $hint = '';
- if (!grep { !defined($_->{override}) } $errors->@*) {
+ if ($opts && !grep { !defined($_->{override}) } $errors->@*) {
my %overrides = map { $_->{override} => 1 } $errors->@*;
$hint = " - use " . join(' and ', map { "'$_'" } sort keys %overrides) . " to override";
}
diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl
index 195e889c..36eddd33 100755
--- a/src/test/run_api_tests.pl
+++ b/src/test/run_api_tests.pl
@@ -533,8 +533,10 @@ subtest 'override for HA-managed VMs requires VM.Migrate on VMs in positive affi
vm_is_ha_managed => sub { return 1; },
get_resource_motion_info => sub { return (['ct:101', 'vm:102'], {}); },
);
- # nodes with this version forward the option to the HA stack
+ # nodes with this version forward the option, the CPU check before that is tested separately
$node_versions->{localhost} = '9.2.31';
+ my $migrate_helpers_module = Test::MockModule->new('PVE::QemuMigrate::Helpers');
+ $migrate_helpers_module->mock(assert_target_cpu_compatibility => sub { return []; });
my $migrate_error = sub {
my ($param) = @_;
@@ -573,6 +575,120 @@ subtest 'override for HA-managed VMs requires VM.Migrate on VMs in positive affi
mock_unreachable_config();
};
+subtest 'migration of an HA-managed VM checks the CPU before requesting it' => sub {
+ # the version of the local node decides whether migration options are forwarded to the HA stack
+ my $inotify_module = Test::MockModule->new('PVE::INotify');
+ $inotify_module->mock(nodename => sub { return 'localhost'; });
+ $node_versions->{localhost} = '9.2.31';
+ $cluster_module->mock(get_config => sub { return $raw_without_identity; });
+ $qemu_config_module->mock(
+ load_config => sub { return { memory => 512 }; },
+ check_lock => sub { return; },
+ );
+ $replication_config_module->mock(find_local_replication_job => sub { return; });
+ $qemu_server_module->mock(check_storage_availability => sub { return; });
+ my $rpcenv_module = Test::MockModule->new('PVE::RPCEnvironment');
+ $rpcenv_module->mock(fork_worker => sub { die "reached fork_worker\n"; });
+
+ my $ha_managed;
+ my $ha_config_module = Test::MockModule->new('PVE::HA::Config');
+ $ha_config_module->mock(vm_is_ha_managed => sub { return $ha_managed; });
+
+ # the arguments of the last CPU check, and its error
+ my ($checked, $cpu_error);
+ my $migrate_helpers_module = Test::MockModule->new('PVE::QemuMigrate::Helpers');
+ $migrate_helpers_module->mock(
+ assert_target_cpu_compatibility => sub {
+ my ($vmid, $conf, $pid, $node, $opts) = @_;
+ $checked =
+ { vmid => $vmid, pid => $pid, node => $node, opts => $opts && { $opts->%* } };
+ die $cpu_error if defined($cpu_error);
+ return [];
+ },
+ );
+
+ # stopped VMs are migrated offline, which is expected
+ local $SIG{__WARN__} = sub { warn $_[0] if $_[0] !~ m/^VM isn't running/ };
+
+ my $refused = "can't migrate VM to 'pve2' - incompatible CPU: some reason\n";
+ for my $running (1234, 0) {
+ $qemu_server_module->mock(check_running => sub { return $running; });
+ for my $managed (1, 0) {
+ $ha_managed = $managed;
+ my $what = ($running ? 'running' : 'stopped') . ($managed ? ', HA-managed' : '');
+ my $checks = $running && $managed;
+
+ ($checked, $cpu_error) = (undef, $refused);
+ is(
+ api_error(
+ 'migrate_vm',
+ { target => 'pve2', online => 1, 'allow-missing-cpu-flags' => 1 },
+ ),
+ $checks ? $refused : "reached fork_worker\n",
+ "VM $what: " . ($checks ? 'incompatible CPU refused' : 'CPU not checked'),
+ );
+ next if !$checks;
+
+ is_deeply(
+ [$checked->@{qw(vmid pid node)}, $checked->{opts}->{'allow-missing-cpu-flags'}],
+ [100, 1234, 'pve2', 1],
+ "VM $what: the running VM is checked with the migration options",
+ );
+
+ $cpu_error = undef;
+ is(
+ api_error('migrate_vm', { target => 'pve2', online => 1 }),
+ "reached fork_worker\n",
+ "VM $what: compatible CPU, HA migration requested",
+ );
+ }
+ }
+
+ $qemu_server_module->mock(check_running => sub { return 1234; });
+ ($ha_managed, $cpu_error, $node_versions->{localhost}) = (1, undef, '9.2.30');
+ api_error('migrate_vm', { target => 'pve2', online => 1, 'allow-missing-cpu-flags' => 1 });
+ is(
+ $checked->{opts}->{'allow-missing-cpu-flags'},
+ undef,
+ 'options the HA stack of an older node does not get do not override the check',
+ );
+
+ # the error only suggests options that reach the HA stack
+ $migrate_helpers_module->unmock('assert_target_cpu_compatibility');
+ $migrate_helpers_module->mock(
+ check_target_cpu_compatibility => sub {
+ my $issue = {
+ type => 'host-cpu-flags',
+ severity => 'error',
+ message => 'missing flags',
+ override => 'allow-missing-cpu-flags',
+ };
+ return { pve2 => [$issue] };
+ },
+ );
+ my $refusal = "can't migrate VM to 'pve2' - incompatible CPU: missing flags";
+ is(
+ api_error('migrate_vm', { target => 'pve2', online => 1 }),
+ "$refusal\n",
+ 'older node: no override suggested',
+ );
+ $node_versions->{localhost} = '9.2.31';
+ is(
+ api_error('migrate_vm', { target => 'pve2', online => 1 }),
+ "$refusal - use 'allow-missing-cpu-flags' to override\n",
+ 'override suggested if forwarded',
+ );
+
+ $node_versions->{localhost} = undef;
+
+ $qemu_server_module->mock(check_running => sub { return 0; });
+ $qemu_server_module->unmock('check_storage_availability');
+ $replication_config_module->unmock('find_local_replication_job');
+ $qemu_config_module->unmock('check_lock');
+ $cluster_module->unmock('get_config');
+ mock_unreachable_config();
+};
+
subtest 'clone target nodes' => sub {
my $inotify_module = Test::MockModule->new('PVE::INotify');
$inotify_module->mock(nodename => sub { return 'pve1'; });
--
2.47.3
next prev parent reply other threads:[~2026-12-02 15:20 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 ` [PATCH qemu-server v4 09/21] migrate: block live migration of host CPU VMs between CPU vendors Jonas Theisen
2026-12-02 15:18 ` [PATCH qemu-server v4 10/21] fix #6535: migrate: warn about live migration " 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 ` Jonas Theisen [this message]
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-15-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