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

Reply via email to