[pve-devel] [PATCH qemu-server 11/31] agent: avoid dependency on QemuConfig module

Fiona Ebner f.ebner at proxmox.com
Wed Jun 25 17:56:34 CEST 2025


The QemuConfig module uses qga_check_running() which is planned to be
moved to the Agent module. Loading the config on the call-sites of
agent_cmd(), qemu_exec() and qemu_exec_status() makes it possible to
avoid introducing that cyclic dependency.

Note that the import for the QemuConfig module was already missing.

Also drops unused variables $write and $res from the 'file-write' API
endpoint implementation.

Signed-off-by: Fiona Ebner <f.ebner at proxmox.com>
---
 src/PVE/API2/Qemu/Agent.pm  | 24 ++++++++++++++++++------
 src/PVE/CLI/qm.pm           |  6 ++++--
 src/PVE/QemuServer/Agent.pm | 12 ++++++------
 3 files changed, 28 insertions(+), 14 deletions(-)

diff --git a/src/PVE/API2/Qemu/Agent.pm b/src/PVE/API2/Qemu/Agent.pm
index 3d1952a6..8a9b9264 100644
--- a/src/PVE/API2/Qemu/Agent.pm
+++ b/src/PVE/API2/Qemu/Agent.pm
@@ -257,6 +257,7 @@ __PACKAGE__->register_method({
         my ($param) = @_;
 
         my $vmid = $param->{vmid};
+        my $conf = PVE::QemuConfig->load_config($vmid);
 
         my $crypted = $param->{crypted} // 0;
         my $args = {
@@ -264,7 +265,8 @@ __PACKAGE__->register_method({
             password => encode_base64($param->{password}),
             crypted => $crypted ? JSON::true : JSON::false,
         };
-        my $res = agent_cmd($vmid, "set-user-password", $args, 'cannot set user password');
+        my $res =
+            agent_cmd($vmid, $conf, "set-user-password", $args, 'cannot set user password');
 
         return { result => $res };
     },
@@ -317,9 +319,11 @@ __PACKAGE__->register_method({
         my ($param) = @_;
 
         my $vmid = $param->{vmid};
+        my $conf = PVE::QemuConfig->load_config($vmid);
+
         my $cmd = $param->{command};
 
-        my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $param->{'input-data'}, $cmd);
+        my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $conf, $param->{'input-data'}, $cmd);
         return $res;
     },
 });
@@ -390,9 +394,11 @@ __PACKAGE__->register_method({
         my ($param) = @_;
 
         my $vmid = $param->{vmid};
+        my $conf = PVE::QemuConfig->load_config($vmid);
+
         my $pid = int($param->{pid});
 
-        my $res = PVE::QemuServer::Agent::qemu_exec_status($vmid, $pid);
+        my $res = PVE::QemuServer::Agent::qemu_exec_status($vmid, $conf, $pid);
 
         return $res;
     },
@@ -439,9 +445,10 @@ __PACKAGE__->register_method({
         my ($param) = @_;
 
         my $vmid = $param->{vmid};
+        my $conf = PVE::QemuConfig->load_config($vmid);
 
         my $qgafh =
-            agent_cmd($vmid, "file-open", { path => $param->{file} }, "can't open file");
+            agent_cmd($vmid, $conf, "file-open", { path => $param->{file} }, "can't open file");
 
         my $bytes_left = $MAX_READ_SIZE;
         my $eof = 0;
@@ -516,23 +523,28 @@ __PACKAGE__->register_method({
         my ($param) = @_;
 
         my $vmid = $param->{vmid};
+        my $conf = PVE::QemuConfig->load_config($vmid);
 
         my $buf =
             ($param->{encode} // 1) ? encode_base64($param->{content}) : $param->{content};
 
         my $qgafh = agent_cmd(
             $vmid,
+            $conf,
             "file-open",
             { path => $param->{file}, mode => 'wb' },
             "can't open file",
         );
-        my $write = agent_cmd(
+
+        agent_cmd(
             $vmid,
+            $conf,
             "file-write",
             { handle => $qgafh, 'buf-b64' => $buf },
             "can't write to file",
         );
-        my $res = agent_cmd($vmid, "file-close", { handle => $qgafh }, "can't close file");
+
+        agent_cmd($vmid, $conf, "file-close", { handle => $qgafh }, "can't close file");
 
         return;
     },
diff --git a/src/PVE/CLI/qm.pm b/src/PVE/CLI/qm.pm
index a7f08cc2..23f71ab0 100755
--- a/src/PVE/CLI/qm.pm
+++ b/src/PVE/CLI/qm.pm
@@ -962,7 +962,9 @@ __PACKAGE__->register_method({
         my $args = $param->{'extra-args'};
         $args = undef if !$args || !@$args;
 
-        my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $input_data, $args);
+        my $conf = PVE::QemuConfig->load_config($vmid);
+
+        my $res = PVE::QemuServer::Agent::qemu_exec($vmid, $conf, $input_data, $args);
 
         if ($sync) {
             my $pid = $res->{pid};
@@ -970,7 +972,7 @@ __PACKAGE__->register_method({
             my $starttime = time();
 
             while ($timeout == 0 || (time() - $starttime) < $timeout) {
-                my $out = PVE::QemuServer::Agent::qemu_exec_status($vmid, $pid);
+                my $out = PVE::QemuServer::Agent::qemu_exec_status($vmid, $conf, $pid);
                 if ($out->{exited}) {
                     $res = $out;
                     last;
diff --git a/src/PVE/QemuServer/Agent.pm b/src/PVE/QemuServer/Agent.pm
index 5bace202..a81b87fb 100644
--- a/src/PVE/QemuServer/Agent.pm
+++ b/src/PVE/QemuServer/Agent.pm
@@ -47,9 +47,8 @@ sub assert_agent_available {
 
 # loads config, checks if available, executes command, checks for errors
 sub agent_cmd {
-    my ($vmid, $cmd, $params, $errormsg) = @_;
+    my ($vmid, $conf, $cmd, $params, $errormsg) = @_;
 
-    my $conf = PVE::QemuConfig->load_config($vmid); # also checks if VM exists
     assert_agent_available($vmid, $conf);
 
     my $res = PVE::QemuServer::Monitor::mon_cmd($vmid, "guest-$cmd", %$params);
@@ -59,7 +58,7 @@ sub agent_cmd {
 }
 
 sub qemu_exec {
-    my ($vmid, $input_data, $cmd) = @_;
+    my ($vmid, $conf, $input_data, $cmd) = @_;
 
     my $args = {
         'capture-output' => JSON::true,
@@ -83,15 +82,16 @@ sub qemu_exec {
         $errmsg .= " (input-data given)";
     }
 
-    my $res = agent_cmd($vmid, "exec", $args, $errmsg);
+    my $res = agent_cmd($vmid, $conf, "exec", $args, $errmsg);
 
     return $res;
 }
 
 sub qemu_exec_status {
-    my ($vmid, $pid) = @_;
+    my ($vmid, $conf, $pid) = @_;
 
-    my $res = agent_cmd($vmid, "exec-status", { pid => $pid }, "can't get exec status for '$pid'");
+    my $res =
+        agent_cmd($vmid, $conf, "exec-status", { pid => $pid }, "can't get exec status for '$pid'");
 
     if ($res->{'out-data'}) {
         my $decoded = eval { decode_base64($res->{'out-data'}) };
-- 
2.47.2





More information about the pve-devel mailing list