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 6AB041FF0B7 for ; Mon, 30 Nov 2026 19:12:34 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 7E5CB2188E; Mon, 30 Nov 2026 19:10:55 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v3 13/18] api: migrate: check the CPU of the target before requesting HA migration Date: Mon, 30 Nov 2026 19:09:45 +0100 Message-ID: <20261130181007.596064-14-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261130181007.596064-1-j.theisen@proxmox.com> References: <20261130181007.596064-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: 1796062243347 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.062 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: XFKJJI72CJXGXFNRN2NT7TFME4ZHCGSM X-Message-ID-Hash: XFKJJI72CJXGXFNRN2NT7TFME4ZHCGSM 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 then runs it in a separate task on the LRM. If the target node's CPU cannot run the VM, that task fails, but the API call and its task have already succeeded, so the caller only learns about it from the LRM's task log or the HA status. Run the CPU compatibility check before requesting the HA migration, as done for the storage checks already, so that the API call fails with the reason directly. Options that override an incompatibility, like 'allow-missing-cpu-flags', are honored as in the migration itself, but only if they are forwarded to the HA stack, which requires pve-manager 9.2.31 or newer on this node. The warnings are not reported here, as the migration task of the LRM logs them. Migrations that the HA stack starts on its own, for example for maintenance mode or automatic rebalancing, do not use this API path and still fail in the migration task, after which the HA stack currently retries them 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 | 19 +++++++-- src/test/run_api_tests.pl | 87 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 4 deletions(-) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 9824a59c..3f520125 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -5934,7 +5934,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(); @@ -5980,15 +5981,25 @@ __PACKAGE__->register_method({ } if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { + # Only nodes with pve-manager 9.2.31 or newer have an ha-manager that accepts + # additional migration options. + my $forwards_options = + PVE::Cluster::node_pvecfg_version_at_least($localnode, 9, 2, 31); + + # 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 : {}; + PVE::QemuMigrate::Helpers::assert_target_cpu_compatibility( + $vmid, $conf, $pid, $target, $check_opts, + ) if $param->{online}; my $hacmd = sub { my $upid = shift; my $extra_opts = ''; my $cmd = ['ha-manager', 'migrate', "vm:$vmid", $target]; - # Only nodes with pve-manager 9.2.31 or newer have an ha-manager that accepts - # additional migration options. - if (PVE::Cluster::node_pvecfg_version_at_least($localnode, 9, 2, 31)) { + if ($forwards_options) { for my $prop (sort keys $ha_migrate_props->%*) { if (defined(my $value = $param->{$prop})) { push($cmd->@*, "--$prop", $value); diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl index 8e1d80ad..5eeb7e83 100755 --- a/src/test/run_api_tests.pl +++ b/src/test/run_api_tests.pl @@ -435,6 +435,93 @@ subtest 'migration preconditions of the CPU of a running VM' => sub { mock_unreachable_config(); }; +subtest 'migration of an HA-managed VM checks the CPU before requesting it' => sub { + $node_versions->{pve2} = $min_version; + # 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->%* } }; + 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', + ); + $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