[pve-devel] [PATCH qemu-server v2 3/5] api: add reboot api call

Dominik Csapak d.csapak at proxmox.com
Fri Aug 23 10:55:58 CEST 2019


this creates a reboot trigger file (inspired by pve-container)
and relies on the 'qm cleanup' call by the qmeventd to detect
and restart the vm afterwards

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
changes from rfc:
* use PVE::QemuServer:vm_stop instead of the api call, to prevent
  the ha manager to change the target state
* create the trigger only during the lock in vm_stop, to be sure we are
  actually shutting down

 PVE/API2/Qemu.pm  | 63 +++++++++++++++++++++++++++++++++++++++++++++++
 PVE/QemuServer.pm |  4 ++-
 2 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index b30931d..5094721 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -2339,6 +2339,69 @@ __PACKAGE__->register_method({
 	}
     }});
 
+__PACKAGE__->register_method({
+    name => 'vm_reboot',
+    path => '{vmid}/status/reboot',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Reboot the VM by shutting it down, and starting it again.",
+    permissions => {
+	check => ['perm', '/vms/{vmid}', [ 'VM.PowerMgmt' ]],
+    },
+    parameters => {
+	additionalProperties => 0,
+	properties => {
+	    node => get_standard_option('pve-node'),
+	    vmid => get_standard_option('pve-vmid',
+					{ completion => \&PVE::QemuServer::complete_vmid_running }),
+	    timeout => {
+		description => "Wait maximal timeout seconds for the shutdown.",
+		type => 'integer',
+		minimum => 0,
+		optional => 1,
+	    },
+	},
+    },
+    returns => {
+	type => 'string',
+    },
+    code => sub {
+	my ($param) = @_;
+
+	my $rpcenv = PVE::RPCEnvironment::get();
+	my $authuser = $rpcenv->get_user();
+
+	my $node = extract_param($param, 'node');
+	my $vmid = extract_param($param, 'vmid');
+
+	my $storecfg = PVE::Storage::config();
+
+	my $shutdown = 1;
+	my $reboot = 1;
+
+	my $qmpstatus = eval {
+	    PVE::QemuServer::vm_qmp_command($vmid, { execute => "query-status" }, 0);
+	};
+	my $err = $@ if $@;
+
+	if (!$err && $qmpstatus->{status} eq "paused") {
+	    die "VM is paused - cannot shutdown\n";
+	}
+
+	my $realcmd = sub {
+	    my $upid = shift;
+
+	    syslog('info', "shutdown VM $vmid: $upid\n");
+
+	    PVE::QemuServer::vm_stop($storecfg, $vmid, undef, 0, $param->{timeout},
+		$shutdown, undef, undef, undef, $reboot);
+	    return;
+	};
+
+	return $rpcenv->fork_worker('qmshutdown', $vmid, $authuser, $realcmd);
+    }});
+
 __PACKAGE__->register_method({
     name => 'vm_suspend',
     path => '{vmid}/status/suspend',
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index cc0c95e..ab4ee3e 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -5739,7 +5739,7 @@ sub vm_stop_cleanup {
 # We need that when migration VMs to other nodes (files already moved)
 # Note: we set $keepActive in vzdump stop mode - volumes need to stay active
 sub vm_stop {
-    my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom) = @_;
+    my ($storecfg, $vmid, $skiplock, $nocheck, $timeout, $shutdown, $force, $keepActive, $migratedfrom, $reboot) = @_;
 
     $force = 1 if !defined($force) && !$shutdown;
 
@@ -5756,6 +5756,8 @@ sub vm_stop {
 	my $pid = check_running($vmid, $nocheck);
 	return if !$pid;
 
+	create_reboot_trigger($vmid) if $reboot;
+
 	my $conf;
 	if (!$nocheck) {
 	    $conf = PVE::QemuConfig->load_config($vmid);
-- 
2.20.1




More information about the pve-devel mailing list