]>
git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/Agent.pm
1 package PVE
::QemuServer
::Agent
;
7 use PVE
::QemuServer
::Monitor
;
8 use MIME
::Base64
qw(decode_base64 encode_base64);
18 sub check_agent_error
{
19 my ($result, $errmsg, $noerr) = @_;
23 if (ref($result) eq 'HASH' && $result->{error
} && $result->{error
}->{desc
}) {
24 $error = "Agent error: $result->{error}->{desc}\n";
25 } elsif (!defined($result)) {
26 $error = "Agent error: $errmsg\n";
30 die $error if !$noerr;
40 my ($vmid, $conf, $noerr) = @_;
43 die "No QEMU guest agent configured\n" if !defined($conf->{agent
});
44 die "VM $vmid is not running\n" if !PVE
::QemuServer
::check_running
($vmid);
45 die "QEMU guest agent is not running\n" if !PVE
::QemuServer
::qga_check_running
($vmid, 1);
56 # loads config, checks if available, executes command, checks for errors
58 my ($vmid, $cmd, $params, $errormsg, $noerr) = @_;
60 my $conf = PVE
::QemuConfig-
>load_config($vmid); # also checks if VM exists
61 agent_available
($vmid, $conf, $noerr);
63 my $res = PVE
::QemuServer
::Monitor
::mon_cmd
($vmid, "guest-$cmd", %$params);
64 check_agent_error
($res, $errormsg, $noerr);
70 my ($vmid, $input_data, $cmd) = @_;
73 'capture-output' => JSON
::true
,
77 $args->{path
} = shift @$cmd;
81 $args->{'input-data'} = encode_base64
($input_data, '') if defined($input_data);
83 die "command or input-data (or both) required\n"
84 if !defined($args->{'input-data'}) && !defined($args->{path
});
86 my $errmsg = "can't execute command";
88 $errmsg .= " ($args->{path} $args->{arg})";
90 if (defined($input_data)) {
91 $errmsg .= " (input-data given)";
94 my $res = agent_cmd
($vmid, "exec", $args, $errmsg);
99 sub qemu_exec_status
{
100 my ($vmid, $pid) = @_;
102 my $res = agent_cmd
($vmid, "exec-status", { pid
=> $pid }, "can't get exec status for '$pid'");
104 if ($res->{'out-data'}) {
105 my $decoded = eval { decode_base64
($res->{'out-data'}) };
107 if (defined($decoded)) {
108 $res->{'out-data'} = $decoded;
112 if ($res->{'err-data'}) {
113 my $decoded = eval { decode_base64
($res->{'err-data'}) };
115 if (defined($decoded)) {
116 $res->{'err-data'} = $decoded;
120 # convert JSON::Boolean to 1/0
121 foreach my $d (keys %$res) {
122 if (JSON
::is_bool
($res->{$d})) {
123 $res->{$d} = ($res->{$d})?
1 : 0;