From 165411f0c2ad08471b1313ece8000bb13ece693a Mon Sep 17 00:00:00 2001 From: Dominik Csapak Date: Wed, 11 Sep 2019 14:07:45 +0200 Subject: [PATCH] api: add reboot api call this creates a reboot request 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 --- PVE/API2/Qemu.pm | 59 +++++++++++++++++++++++++++++++++++++++++++++++ PVE/CLI/qm.pm | 2 ++ PVE/QemuServer.pm | 16 +++++++++++++ 3 files changed, 77 insertions(+) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index 9db8967f..3355c8b2 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -1910,6 +1910,7 @@ __PACKAGE__->register_method({ { subdir => 'reset' }, { subdir => 'shutdown' }, { subdir => 'suspend' }, + { subdir => 'reboot' }, ]; return $res; @@ -2333,6 +2334,64 @@ __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. Applies pending changes.", + 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 $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"; + } + + die "VM $vmid not running\n" if !PVE::QemuServer::check_running($vmid); + + my $realcmd = sub { + my $upid = shift; + + syslog('info', "requesting reboot of VM $vmid: $upid\n"); + PVE::QemuServer::vm_reboot($vmid, $param->{timeout}); + return; + }; + + return $rpcenv->fork_worker('qmreboot', $vmid, $authuser, $realcmd); + }}); + __PACKAGE__->register_method({ name => 'vm_suspend', path => '{vmid}/status/suspend', diff --git a/PVE/CLI/qm.pm b/PVE/CLI/qm.pm index 74905b2d..17935d07 100755 --- a/PVE/CLI/qm.pm +++ b/PVE/CLI/qm.pm @@ -998,6 +998,8 @@ our $cmddef = { shutdown => [ "PVE::API2::Qemu", 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit ], + reboot => [ "PVE::API2::Qemu", 'vm_reboot', ['vmid'], { node => $nodename }, $upid_exit ], + suspend => [ "PVE::API2::Qemu", 'vm_suspend', ['vmid'], { node => $nodename }, $upid_exit ], resume => [ "PVE::API2::Qemu", 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit ], diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index 8a1168f1..d5bdf7c4 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -5870,6 +5870,22 @@ sub vm_stop { }); } +sub vm_reboot { + my ($vmid, $timeout) = @_; + + PVE::QemuConfig->lock_config($vmid, sub { + + # only reboot if running, as qmeventd starts it again on a stop event + return if !check_running($vmid); + + create_reboot_request($vmid); + + my $storecfg = PVE::Storage::config(); + _do_vm_stop($storecfg, $vmid, undef, undef, $timeout, 1); + + }); +} + sub vm_suspend { my ($vmid, $skiplock, $includestate, $statestorage) = @_; -- 2.39.5