public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Fiona Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH qemu-server 04/11] agent: avoid dependency on QemuConfig module
Date: Mon,  5 May 2025 14:57:17 +0200	[thread overview]
Message-ID: <20250505125724.75620-5-f.ebner@proxmox.com> (raw)
In-Reply-To: <20250505125724.75620-1-f.ebner@proxmox.com>

The QemuConfig module uses qga_check_running() which is planned to be
moved to the Agent module and it will use a to-be-added
guest_fsfreeze() helper from 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>
---
 PVE/API2/Qemu/Agent.pm  | 31 ++++++++++++++++++++++++-------
 PVE/CLI/qm.pm           |  6 ++++--
 PVE/QemuServer/Agent.pm | 12 ++++++------
 3 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/PVE/API2/Qemu/Agent.pm b/PVE/API2/Qemu/Agent.pm
index c9527070..5f39e149 100644
--- a/PVE/API2/Qemu/Agent.pm
+++ b/PVE/API2/Qemu/Agent.pm
@@ -250,6 +250,7 @@ __PACKAGE__->register_method({
 	my ($param) = @_;
 
 	my $vmid = $param->{vmid};
+	my $conf = PVE::QemuConfig->load_config($vmid);
 
 	my $crypted = $param->{crypted} // 0;
 	my $args = {
@@ -257,7 +258,7 @@ __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 };
     }});
@@ -305,9 +306,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;
     }});
 
@@ -374,9 +377,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;
     }});
@@ -420,8 +425,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");
+	my $qgafh = agent_cmd(
+	    $vmid, $conf, "file-open", { path => $param->{file} }, "can't open file");
 
 	my $bytes_left = $MAX_READ_SIZE;
 	my $eof = 0;
@@ -490,12 +497,22 @@ __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, "file-open",  { path => $param->{file}, mode => 'wb' }, "can't open file");
-	my $write = agent_cmd($vmid, "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");
+	my $qgafh = agent_cmd(
+	    $vmid, $conf, "file-open", { path => $param->{file}, mode => 'wb' }, "can't open file");
+
+	agent_cmd(
+	    $vmid,
+	    $conf,
+	    "file-write",
+	    { handle => $qgafh, 'buf-b64' => $buf },
+	    "can't write to file",
+	);
+
+	agent_cmd($vmid, $conf, "file-close", { handle => $qgafh }, "can't close file");
 
 	return;
     }});
diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm
index 35e85597..9427264f 100755
--- a/PVE/CLI/qm.pm
+++ b/PVE/CLI/qm.pm
@@ -884,7 +884,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};
@@ -892,7 +894,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/PVE/QemuServer/Agent.pm b/PVE/QemuServer/Agent.pm
index 264c7018..41e615aa 100644
--- a/PVE/QemuServer/Agent.pm
+++ b/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.39.5



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


  parent reply	other threads:[~2025-05-05 12:58 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-05 12:57 [pve-devel] [PATCH-SERIES qemu-server 00/11] better handle lost freeze command Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 01/11] agent: drop unused $noerr argument from helpers Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 02/11] api: agent: improve module imports Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 03/11] agent: code style: order module imports according to style guide Fiona Ebner
2025-05-05 12:57 ` Fiona Ebner [this message]
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 05/11] qmp client: remove erroneous comment Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 06/11] qmp client: add $noerr argument Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 07/11] agent: implement fsfreeze helper to better handle lost commands Fiona Ebner
2025-05-05 13:57   ` Mira Limbeck
2025-05-05 14:01     ` Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 08/11] agent: avoid use of deprecated check_running() function Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 09/11] agent: move qga_check_running() helper to agent module Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 10/11] agent: prefer usage of get_qga_key() helper Fiona Ebner
2025-05-05 12:57 ` [pve-devel] [PATCH qemu-server 11/11] agent: move guest agent format and parsing to agent module Fiona Ebner

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=20250505125724.75620-5-f.ebner@proxmox.com \
    --to=f.ebner@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal