package PVE::QMPClient;
use strict;
+use warnings;
use PVE::QemuServer;
use IO::Multiplex;
use POSIX qw(EINTR EAGAIN);
# Note: kvm can onyl handle 1 connection, so we close connections asap
sub new {
- my ($class, $eventcb) = @_;
+ my ($class, $eventcb, $qga) = @_;
my $mux = new IO::Multiplex;
}, $class;
$self->{eventcb} = $eventcb if $eventcb;
+ $self->{qga} = $qga if $qga;
$mux->set_callback_object($self);
my $cmdid_seq = 0;
my $next_cmdid = sub {
$cmdid_seq++;
- return "$$:$cmdid_seq";
+ return "$$"."0".$cmdid_seq;
};
my $close_connection = sub {
my $open_connection = sub {
my ($self, $vmid, $timeout) = @_;
- my $sname = PVE::QemuServer::qmp_socket($vmid);
+ my $sname = PVE::QemuServer::qmp_socket($vmid, $self->{qga});
$timeout = 1 if !$timeout;
delete $cmd->{arguments}->{fd};
}
- my $qmpcmd = to_json({
- execute => $cmd->{execute},
- arguments => $cmd->{arguments},
- id => $cmd->{id}});
+ my $qmpcmd = undef;
+
+ if($self->{qga}){
+
+ my $qmpcmdid =to_json({
+ execute => 'guest-sync',
+ arguments => { id => int($cmd->{id})}});
+
+ $qmpcmd = to_json({
+ execute => $cmd->{execute},
+ arguments => $cmd->{arguments}});
+
+ $qmpcmd = $qmpcmdid.$qmpcmd;
+
+ }else{
+
+ $qmpcmd = to_json({
+ execute => $cmd->{execute},
+ arguments => $cmd->{arguments},
+ id => $cmd->{id}});
+ }
if ($fd >= 0) {
my $ret = PVE::IPCC::sendfd(fileno($fh), $fd, $qmpcmd);
eval {
my $fh = &$open_connection($self, $vmid, $timeout);
- my $cmd = { execute => 'qmp_capabilities', arguments => {} };
- unshift @{$self->{queue}->{$vmid}}, $cmd;
+
+ if(!$self->{qga}){
+ my $cmd = { execute => 'qmp_capabilities', arguments => {} };
+ unshift @{$self->{queue}->{$vmid}}, $cmd;
+ }
+
$self->{mux}->set_timeout($fh, $timeout);
};
if (my $err = $@) {
sub mux_input {
my ($self, $mux, $fh, $input) = @_;
- return if $$input !~ s/^(.*})\r\n(.*)$/$2/so;
+ my $raw;
- my $raw = $1;
+ if($self->{qga}){
+ return if $$input !~ s/^([^\n]+}\n[^\n]+})\n(.*)$/$2/so;
+ $raw = $1;
+ }else{
+ return if $$input !~ s/^([^\n]+})\r?\n(.*)$/$2/so;
+ $raw = $1;
+ }
my $vmid = $self->{fhs_lookup}->{$fh};
if (!$vmid) {
eval {
my @jsons = split("\n", $raw);
+ if($self->{qga}){
+
+ die "response is not complete" if @jsons != 2 ;
+
+ my $obj = from_json($jsons[0]);
+ my $cmdid = $obj->{return};
+ die "received responsed without command id\n" if !$cmdid;
+
+ my $curcmd = $self->{current}->{$vmid};
+ die "unable to lookup current command for VM $vmid\n" if !$curcmd;
+
+ delete $self->{current}->{$vmid};
+
+ if ($curcmd->{id} ne $cmdid) {
+ die "got wrong command id '$cmdid' (expected $curcmd->{id})\n";
+ }
+
+ $obj = from_json($jsons[1]);
+
+ if (my $callback = $curcmd->{callback}) {
+ &$callback($vmid, $obj);
+ }
+
+ return;
+ }
+
foreach my $json (@jsons) {
my $obj = from_json($json);
next if defined($obj->{QMP}); # skip monitor greeting