From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 5A5141FF0BC for ; Wed, 02 Dec 2026 16:20:58 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8465F21955; Wed, 02 Dec 2026 16:19:50 +0100 (CET) From: Jonas Theisen 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 Message-ID: <20261202151831.414110-15-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261202151831.414110-1-j.theisen@proxmox.com> References: <20261202151831.414110-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796224747414 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.055 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: DXK34PUXXD7LXWZ2UYZBGKS265NPJKJW X-Message-ID-Hash: DXK34PUXXD7LXWZ2UYZBGKS265NPJKJW X-MailFrom: j.theisen@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: 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 --- 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