]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer/Agent.pm
d/control: remove unused dependency
[qemu-server.git] / PVE / QemuServer / Agent.pm
index 8fad1091b298d594bf5e676b32c1588f22a4465b..586ac3a0eb2d153212054752a5c7dccf7c5dfbd1 100644 (file)
@@ -2,7 +2,10 @@ package PVE::QemuServer::Agent;
 
 use strict;
 use warnings;
+
 use PVE::QemuServer;
+use MIME::Base64 qw(decode_base64);
+use JSON;
 use base 'Exporter';
 
 our @EXPORT_OK = qw(
@@ -17,9 +20,9 @@ sub check_agent_error {
     $errmsg //= '';
     my $error = '';
     if (ref($result) eq 'HASH' && $result->{error} && $result->{error}->{desc}) {
-       $error = "Agent Error: $result->{error}->{desc}\n";
+       $error = "Agent error: $result->{error}->{desc}\n";
     } elsif (!defined($result)) {
-       $error = "Agent Error: $errmsg\n";
+       $error = "Agent error: $errmsg\n";
     }
 
     if ($error) {
@@ -36,9 +39,9 @@ sub agent_available {
     my ($vmid, $conf, $noerr) = @_;
 
     eval {
-       die "No Qemu Guest Agent\n" if !defined($conf->{agent});
+       die "No QEMU guest agent configured\n" if !defined($conf->{agent});
        die "VM $vmid is not running\n" if !PVE::QemuServer::check_running($vmid);
-       die "Qemu Guest Agent is not running\n" if !PVE::QemuServer::qga_check_running($vmid, 1);
+       die "QEMU guest agent is not running\n" if !PVE::QemuServer::qga_check_running($vmid, 1);
     };
 
     if (my $err = $@) {
@@ -62,4 +65,52 @@ sub agent_cmd {
     return $res;
 }
 
+sub qemu_exec {
+    my ($vmid, $cmd) = @_;
+
+
+    my $path = shift @$cmd;
+    my $arguments = $cmd;
+
+    my $args = {
+       path => $path,
+       arg => $arguments,
+       'capture-output' => JSON::true,
+    };
+    my $res = agent_cmd($vmid, "exec", $args, "can't execute command '$path $arguments'");
+
+    return $res;
+}
+
+sub qemu_exec_status {
+    my ($vmid, $pid) = @_;
+
+    my $res = agent_cmd($vmid, "exec-status", { pid => $pid }, "can't get exec status for '$pid'");
+
+    if ($res->{'out-data'}) {
+       my $decoded = eval { decode_base64($res->{'out-data'}) };
+       warn $@ if $@;
+       if (defined($decoded)) {
+           $res->{'out-data'} = $decoded;
+       }
+    }
+
+    if ($res->{'err-data'}) {
+       my $decoded = eval { decode_base64($res->{'err-data'}) };
+       warn $@ if $@;
+       if (defined($decoded)) {
+           $res->{'err-data'} = $decoded;
+       }
+    }
+
+    # convert JSON::Boolean to 1/0
+    foreach my $d (keys %$res) {
+       if (JSON::is_bool($res->{$d})) {
+           $res->{$d} = ($res->{$d})? 1 : 0;
+       }
+    }
+
+    return $res;
+}
+
 1;