all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Daniel Kral <d.kral@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [RFC qemu-server 1/1] fix #7053: api: migrate: save and restore migration params for HA managed VMs
Date: Wed, 25 Feb 2026 15:35:05 +0100	[thread overview]
Message-ID: <20260225143514.368884-2-d.kral@proxmox.com> (raw)
In-Reply-To: <20260225143514.368884-1-d.kral@proxmox.com>

If a HA-managed VM's migrate API endpoint is called from a web API or
CLI environment, it is first relayed to the HA Manager by queueing a
'migrate' CRM command with `ha-manager migrate vm:$vmid $target_node`.
This command doesn't take any additional migration parameters though.

As soon as the HA Manager reads the CRM command in the next HA Manager
round, it passes the migration request - if valid - to the HA resource
state. This migration request is then picked up by the LRM, where the HA
resource is assigned to and calls the migrate_vm API endpoint, which
will then initial the VM migration.

As the migrate_vm API request is proxied to the node, where the HA
resource is assigned to, this allows the migrate parameters to stored
locally while the migration request is passed through the HA stack.

Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
 src/PVE/API2/Qemu.pm | 54 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 54 insertions(+)

diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 1f0864f5..027896a3 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -3,6 +3,7 @@ package PVE::API2::Qemu;
 use strict;
 use warnings;
 use Cwd 'abs_path';
+use File::stat qw();
 use Net::SSLeay;
 use IO::Socket::IP;
 use IO::Socket::UNIX;
@@ -5367,6 +5368,52 @@ __PACKAGE__->register_method({
     },
 });
 
+my sub migrate_params_filename {
+    my ($vmid) = @_;
+    return "/run/qemu-server/$vmid.migrate_params";
+}
+
+my sub save_migrate_params {
+    my ($vmid, $params) = @_;
+
+    my $migrate_params_file = migrate_params_filename($vmid);
+
+    warn "existing migration parameters file for '$vmid' will be overwritten\n"
+        if -f $migrate_params_file;
+
+    PVE::Tools::file_set_contents($migrate_params_file, encode_json($params), 0640);
+}
+
+my sub try_to_restore_migrate_params {
+    my ($vmid, $params) = @_;
+
+    my $migrate_params_file = migrate_params_filename($vmid);
+    my @migrate_params_denylist = qw(node vmid target online force with-local-disks targetstorage);
+
+    if (-f $migrate_params_file) {
+        my $stat = File::stat::lstat($migrate_params_file);
+        # prevent that non-root users could write the migrate parameters file
+        my $has_correct_perms = $stat->uid == 0 && ($stat->mode & 037) == 0;
+
+        if (PVE::HA::Config::vm_is_ha_managed($vmid) && $has_correct_perms) {
+            my $migration_params = {};
+            eval {
+                my $data = PVE::Tools::file_get_contents($migrate_params_file);
+                $migration_params = decode_json($data) // {};
+            };
+            for my $key (keys %$migration_params) {
+                next if grep { $key eq $_ } @migrate_params_denylist;
+
+                $params->{$key} = $migration_params->{$key};
+            }
+        } else {
+            warn "remove orphan migration parameters file\n";
+        }
+
+        unlink $migrate_params_file;
+    }
+}
+
 __PACKAGE__->register_method({
     name => 'migrate_vm',
     path => '{vmid}/migrate',
@@ -5464,6 +5511,13 @@ __PACKAGE__->register_method({
 
         my $vmid = extract_param($param, 'vmid');
 
+        if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') {
+            save_migrate_params($vmid, $param);
+        } else {
+            # always try to restore to remove oprhaned migration parameters files
+            try_to_restore_migrate_params($vmid, $param);
+        }
+
         raise_param_exc({ force => "Only root may use this option." })
             if $param->{force} && $authuser ne 'root@pam';
 
-- 
2.47.3





      reply	other threads:[~2026-02-25 14:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-25 14:35 [RFC PATCH-SERIES qemu-server 0/1] fix #7053: allow setting additional HA migration parameters Daniel Kral
2026-02-25 14:35 ` Daniel Kral [this message]

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=20260225143514.368884-2-d.kral@proxmox.com \
    --to=d.kral@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