[pve-devel] [PATCH v2 2/3] This Patch implements the capability shutdown by Qemu Guest Agent if the agent flag in the config is set to 1 and QGA is installed on VM.

Dietmar Maurer dietmar at proxmox.com
Thu Nov 27 12:01:16 CET 2014


From: Wolfgang Link <wolfgang at linksystems.org>

Important: "guest-shutdown" returns only by error a message.

Signed-off-by: Wolfgang Link <wolfgang at linksystems.org>
Signed-off-by: Dietmar Maurer <dietmar at proxmox.com>
---
 PVE/QMPClient.pm  |   46 +++++++++++++++++++++++++++++++++++++++++++---
 PVE/QemuServer.pm |   10 +++++++---
 2 files changed, 50 insertions(+), 6 deletions(-)

diff --git a/PVE/QMPClient.pm b/PVE/QMPClient.pm
index d67bc01..f4416e8 100755
--- a/PVE/QMPClient.pm
+++ b/PVE/QMPClient.pm
@@ -87,7 +87,8 @@ sub cmd {
 		 $cmd->{execute} eq 'query-block-jobs' ||
 		 $cmd->{execute} eq 'backup-cancel' ||
 		 $cmd->{execute} eq 'query-savevm' ||
-		 $cmd->{execute} eq 'delete-drive-snapshot' ||
+		 $cmd->{execute} eq 'delete-drive-snapshot' || 
+		 $cmd->{execute} eq 'guest-shutdown' ||
 		 $cmd->{execute} eq 'snapshot-drive'  ) {
 	    $timeout = 10*60; # 10 mins ?
 	} else {
@@ -261,7 +262,6 @@ sub queue_execute {
 		my $cmd = { execute => 'qmp_capabilities', arguments => {} };
 		unshift @{$self->{queue}->{$vmid}}, $cmd;
 	    }
-
 	    $self->{mux}->set_timeout($fh, $timeout);
 	};
 	if (my $err = $@) {
@@ -295,7 +295,11 @@ sub mux_close {
     my $vmid = $self->{fhs_lookup}->{$fh} || 'undef';
     return if !defined($vmid);
 
-    $self->{errors}->{$vmid} = "client closed connection\n" if !$self->{errors}->{$vmid};
+    if(!$self->{no_answer}){
+	$self->{errors}->{$vmid} = "client closed connection\n" if !$self->{errors}->{$vmid};  
+    } else {
+	delete $self->{no_anwser}; 
+    }
 }
 
 # mux_input is called when input is available on one of
@@ -401,4 +405,40 @@ sub mux_timeout {
     &$check_queue($self);
 }
 
+sub mux_eof {
+    my ($self, $mux, $fh, $input) = @_;
+ 
+    my $vmid = $self->{fhs_lookup}->{$fh};
+    if(check_no_answer($self->{current}->{$vmid}->{execute})){
+	my @jsons = split("\n", $$input);
+
+	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};
+
+	$self->{no_answer} = 1;
+      }
+}
+
+sub check_no_answer {
+    my($cmd) = @_;
+
+    if ($cmd eq 'guest-shutdown'){
+	return 1;
+    } elsif ($cmd eq 'guest-suspend-ram'){
+	return 1;
+    } elsif ($cmd eq 'guest-suspend-disk'){
+	return 1;
+    } elsif ($cmd eq 'guest-suspend-hybrid'){
+	return 1;
+    }
+    return 0;
+}
+
 1;
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b454d90..a1f973c 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -3614,11 +3614,15 @@ sub vm_stop {
 	}
 
 	$timeout = 60 if !defined($timeout);
-
+	my $config = load_config($vmid);
+	
 	eval {
 	    if ($shutdown) {
-		$nocheck ? vm_mon_cmd_nocheck($vmid, "system_powerdown") : vm_mon_cmd($vmid, "system_powerdown");
-
+		if($config->{agent}){
+		    vm_mon_cmd($vmid,"guest-shutdown");
+		} else {
+		    $nocheck ? vm_mon_cmd_nocheck($vmid, "system_powerdown") : vm_mon_cmd($vmid, "system_powerdown");
+		}
 	    } else {
 		$nocheck ? vm_mon_cmd_nocheck($vmid, "quit") : vm_mon_cmd($vmid, "quit");
 	    }
-- 
1.7.10.4




More information about the pve-devel mailing list