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 A91201FF162 for <inbox@lore.proxmox.com>; Mon, 5 May 2025 14:58:10 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 4C4161C3F3; Mon, 5 May 2025 14:58:02 +0200 (CEST) From: Fiona Ebner <f.ebner@proxmox.com> To: pve-devel@lists.proxmox.com Date: Mon, 5 May 2025 14:57:17 +0200 Message-Id: <20250505125724.75620-5-f.ebner@proxmox.com> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250505125724.75620-1-f.ebner@proxmox.com> References: <20250505125724.75620-1-f.ebner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.036 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 04/11] 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 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