]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/Agent.pm
1 package PVE
::QemuServer
::Agent
;
7 use MIME
::Base64
qw(decode_base64);
17 sub check_agent_error
{
18 my ($result, $errmsg, $noerr) = @_;
22 if (ref($result) eq 'HASH' && $result->{error
} && $result->{error
}->{desc
}) {
23 $error = "Agent error: $result->{error}->{desc}\n";
24 } elsif (!defined($result)) {
25 $error = "Agent error: $errmsg\n";
29 die $error if !$noerr;
39 my ($vmid, $conf, $noerr) = @_;
42 die "No QEMU guest agent configured\n" if !defined($conf->{agent
});
43 die "VM $vmid is not running\n" if !PVE
::QemuServer
::check_running
($vmid);
44 die "QEMU guest agent is not running\n" if !PVE
::QemuServer
::qga_check_running
($vmid, 1);
55 # loads config, checks if available, executes command, checks for errors
57 my ($vmid, $cmd, $params, $errormsg, $noerr) = @_;
59 my $conf = PVE
::QemuConfig-
>load_config($vmid); # also checks if VM exists
60 agent_available
($vmid, $conf, $noerr);
62 my $res = PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-$cmd", %$params);
63 check_agent_error
($res, $errormsg, $noerr);
69 my ($vmid, $cmd) = @_;
72 my $path = shift @$cmd;
78 'capture-output' => JSON
::true
,
80 my $res = agent_cmd
($vmid, "exec", $args, "can't execute command '$path $arguments'");
85 sub qemu_exec_status
{
86 my ($vmid, $pid) = @_;
88 my $res = agent_cmd
($vmid, "exec-status", { pid
=> $pid }, "can't get exec status for '$pid'");
90 if ($res->{'out-data'}) {
91 my $decoded = eval { decode_base64
($res->{'out-data'}) };
93 if (defined($decoded)) {
94 $res->{'out-data'} = $decoded;
98 if ($res->{'err-data'}) {
99 my $decoded = eval { decode_base64
($res->{'err-data'}) };
101 if (defined($decoded)) {
102 $res->{'err-data'} = $decoded;
106 # convert JSON::Boolean to 1/0
107 foreach my $d (keys %$res) {
108 if (JSON
::is_bool
($res->{$d})) {
109 $res->{$d} = ($res->{$d})?
1 : 0;