all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Jonas Theisen <j.theisen@proxmox.com>
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	[thread overview]
Message-ID: <20261130181007.596064-14-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261130181007.596064-1-j.theisen@proxmox.com>

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 <j.theisen@proxmox.com>
---
 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





  parent reply	other threads:[~2026-11-30 18:12 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 18:09 [PATCH docs/manager/qemu-server v3 00/18] check the target node's CPU before live migrating a VM Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 01/18] buildsys: ignore migration test runtime files Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 02/18] test: migration: allow checking for warnings and errors in the log Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 03/18] cpu flags: normalize underscores in all CPU flag names Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 04/18] test: api: factor out validating the migration preconditions Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 05/18] cpu flags: factor out parsing the broadcast CPU flags of a node Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 06/18] migrate: factor out whether the running virtual CPU is kept Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 07/18] cpu flags: factor out the flags of a CPU model expansion Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 08/18] migrate: block live migration of host CPU VMs between CPU vendors Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 09/18] fix #6535: migrate: warn about live migration " Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 10/18] fix #2924: migrate: block live migration of host CPU VMs to nodes lacking flags Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 11/18] migrate: warn about live migration of host CPU VMs between CPU models Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 12/18] migrate: block live migration if the target cannot run the virtual CPU Jonas Theisen
2026-11-30 18:09 ` Jonas Theisen [this message]
2026-11-30 18:09 ` [PATCH manager v3 14/18] pvestatd: broadcast host CPU information relevant for live migration Jonas Theisen
2026-11-30 18:09 ` [PATCH manager v3 15/18] ui: migrate: show why the CPU of the target node is incompatible Jonas Theisen
2026-11-30 18:09 ` [PATCH manager v3 16/18] fix #6535: ui: migrate: show CPU warnings for the target node Jonas Theisen
2026-11-30 18:09 ` [PATCH manager v3 17/18] api: nodes: migrateall: skip VMs whose CPU is incompatible with the target Jonas Theisen
2026-11-30 18:09 ` [PATCH docs v3 18/18] 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=20261130181007.596064-14-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 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.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal