From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pve-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9])
	by lore.proxmox.com (Postfix) with ESMTPS id 55A791FF17C
	for <inbox@lore.proxmox.com>; Wed, 25 Jun 2025 17:58:18 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 19E0A190B4;
	Wed, 25 Jun 2025 17:58:06 +0200 (CEST)
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed, 25 Jun 2025 17:56:34 +0200
Message-ID: <20250625155751.268047-12-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.47.2
In-Reply-To: <20250625155751.268047-1-f.ebner@proxmox.com>
References: <20250625155751.268047-1-f.ebner@proxmox.com>
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.030 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
Subject: [pve-devel] [PATCH qemu-server 11/31] agent: avoid dependency on
 QemuConfig module
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
Reply-To: Proxmox VE development discussion <pve-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pve-devel-bounces@lists.proxmox.com
Sender: "pve-devel" <pve-devel-bounces@lists.proxmox.com>

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@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



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel