[pve-devel] [PATCH 2/2] now if the QMP command starts with guest-+ , it will bind dynamicly to the VMID.qga socket.

Wolfgang Link wolfgang at linksystems.org
Tue Nov 25 10:21:39 CET 2014


Signed-off-by: Wolfgang Link <wolfgang at linksystems.org>
---
 PVE/QMPClient.pm  |   54 +++++++++++++++++++++++++++++++++--------------------
 PVE/QemuServer.pm |   16 +++++-----------
 2 files changed, 39 insertions(+), 31 deletions(-)

diff --git a/PVE/QMPClient.pm b/PVE/QMPClient.pm
index 9674d00..b5684b6 100755
--- a/PVE/QMPClient.pm
+++ b/PVE/QMPClient.pm
@@ -20,7 +20,7 @@ use Data::Dumper;
 # Note: kvm can onyl handle 1 connection, so we close connections asap
 
 sub new {
-    my ($class, $eventcb, $qga) = @_;
+    my ($class, $eventcb) = @_;
 
     my $mux = new IO::Multiplex;
 
@@ -34,7 +34,6 @@ sub new {
     }, $class;
 
     $self->{eventcb} = $eventcb if $eventcb;
-    $self->{qga} = $qga if $qga;
 
     $mux->set_callback_object($self);
 
@@ -106,9 +105,18 @@ sub cmd {
 };
 
 my $cmdid_seq = 0;
+my $cmdid_seq_qga = 0;
+
 my $next_cmdid = sub {
-    $cmdid_seq++;
-    return "$$"."0".$cmdid_seq;
+    my ($qga) = @_;
+    
+    if($qga){
+	$cmdid_seq++;
+	return "$$:$cmdid_seq";
+    } else {
+	$cmdid_seq_qga++;
+	return "$$"."0".$cmdid_seq_qga;
+    }
 };
 
 my $close_connection = sub {
@@ -124,9 +132,9 @@ my $close_connection = sub {
 };
 
 my $open_connection = sub {
-    my ($self, $vmid, $timeout) = @_;
+    my ($self, $vmid, $timeout, $qga) = @_;
 
-    my $sname = PVE::QemuServer::qmp_socket($vmid, $self->{qga});
+    my $sname = PVE::QemuServer::qmp_socket($vmid, $qga);
 
     $timeout = 1 if !$timeout;
 
@@ -181,7 +189,8 @@ my $check_queue = sub {
 	eval {
 
 	    my $cmd = $self->{current}->{$vmid} = shift @{$self->{queue}->{$vmid}};
-	    $cmd->{id} = &$next_cmdid();
+
+	    $cmd->{id} = &$next_cmdid($cmd->{qga});
 
 	    my $fd = -1;
 	    if ($cmd->{execute} eq 'add-fd' || $cmd->{execute} eq 'getfd') {
@@ -191,7 +200,7 @@ my $check_queue = sub {
 
 	    my $qmpcmd = undef;
 
-	    if($self->{qga}){
+	    if($self->{current}->{$vmid}->{qga}){
 
 		my $qmpcmdid =to_json({
 		    execute => 'guest-sync',
@@ -242,11 +251,15 @@ sub queue_execute {
     # open all necessary connections
     foreach my $vmid (keys %{$self->{queue}}) {
 	next if !scalar(@{$self->{queue}->{$vmid}}); # no commands for the VM
+	
+	if ($self->{queue}->{$vmid}[0]->{execute} =~ /^guest\-+/){
+	    $self->{queue}->{$vmid}[0]->{qga} = "1";
+	}
 
 	eval {
-	    my $fh = &$open_connection($self, $vmid, $timeout);
+	    my $fh = &$open_connection($self, $vmid, $timeout, $self->{queue}->{$vmid}[0]->{qga});
 	   
-	     if(!$self->{qga}){
+	     if(!$self->{queue}->{$vmid}[0]->{qga}){
 		 my $cmd = { execute => 'qmp_capabilities', arguments => {} };
 		 unshift @{$self->{queue}->{$vmid}}, $cmd;
 
@@ -290,16 +303,17 @@ sub mux_close {
 # the descriptors.
 sub mux_input {
     my ($self, $mux, $fh, $input) = @_;
-   
-    if($self->{qga}){
-	return if $$input !~ m/}\n(.+)}\n$/;
-    }else{
-	return if $$input !~ m/}\r\n$/;
-    }
 
-    my $raw = $$input;
+    my $vmid = $self->{fhs_lookup}->{$fh};    
+    my $raw;
+    if ($self->{current}->{$vmid}->{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) {
 	warn "internal error - unable to lookup vmid";
 	return;
@@ -308,7 +322,7 @@ sub mux_input {
     eval {
 	my @jsons = split("\n", $raw);
 
-	if($self->{qga}){
+	if($self->{current}->{$vmid}->{qga}){
 
 	    die "response is not complete" if @jsons != 2 ;
 
@@ -328,7 +342,7 @@ sub mux_input {
 	    $obj = from_json($jsons[1]);
 
 	    if (my $callback = $curcmd->{callback}) {
-		&$callback($vmid, $obj);
+		&$callback($vmid, $obj);	   
 	    }
 
 	    return;
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index d740564..ab02b93 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -2881,13 +2881,6 @@ sub qga_socket {
     return "${var_run_tmpdir}/$vmid.qga";
 }
 
-sub vm_qga_cmd {
-    my ($vmid, $execute, %params) = @_;
-
-    my $cmd = { execute => $execute, arguments => \%params };
-    vm_qmp_command($vmid, $cmd, undef, 1);
-}
-
 sub pidfile_name {
     my ($vmid) = @_;
     return "${var_run_tmpdir}/$vmid.pid";
@@ -3481,7 +3474,7 @@ sub vm_mon_cmd_nocheck {
 }
 
 sub vm_qmp_command {
-    my ($vmid, $cmd, $nocheck, $qga) = @_;
+    my ($vmid, $cmd, $nocheck) = @_;
 
     my $res;
 
@@ -3493,12 +3486,13 @@ sub vm_qmp_command {
 
     eval {
 	die "VM $vmid not running\n" if !check_running($vmid, $nocheck);
-	my $sname = qmp_socket($vmid, $qga);
+	my $qga = ($cmd->{execute} =~ /^guest\-+/)?1:0;
+	my $sname = qmp_socket($vmid,$qga);
 	if (-e $sname) {
-	    my $qmpclient = PVE::QMPClient->new(undef, $qga);
+	    my $qmpclient = PVE::QMPClient->new();
 
 	    $res = $qmpclient->cmd($vmid, $cmd, $timeout);
-	} elsif (-e "${var_run_tmpdir}/$vmid.mon" && !$qga) {
+	} elsif (-e "${var_run_tmpdir}/$vmid.mon") {
 	    die "can't execute complex command on old monitor - stop/start your vm to fix the problem\n"
 		if scalar(%{$cmd->{arguments}});
 	    vm_monitor_command($vmid, $cmd->{execute}, $nocheck);
-- 
1.7.10.4




More information about the pve-devel mailing list