[pve-devel] [PATCH v4 container 1/4] implement container reboot

Oguz Bektas o.bektas at proxmox.com
Wed Nov 20 15:49:38 CET 2019


* add helper function vm_reboot
* add vm_reboot api call
* add 'pct reboot'

Signed-off-by: Oguz Bektas <o.bektas at proxmox.com>
---
 src/PVE/API2/LXC/Status.pm | 52 ++++++++++++++++++++++++++++++++++++++
 src/PVE/CLI/pct.pm         |  3 ++-
 src/PVE/LXC.pm             | 13 ++++++++++
 3 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/src/PVE/API2/LXC/Status.pm b/src/PVE/API2/LXC/Status.pm
index 166c731..41f1f4f 100644
--- a/src/PVE/API2/LXC/Status.pm
+++ b/src/PVE/API2/LXC/Status.pm
@@ -65,6 +65,7 @@ __PACKAGE__->register_method({
 	    { subdir => 'start' },
 	    { subdir => 'stop' },
 	    { subdir => 'shutdown' },
+	    { subdir => 'reboot' },
 	    { subdir => 'migrate' },
 	];
 
@@ -445,4 +446,55 @@ __PACKAGE__->register_method({
 	return $upid;
     }});
 
+__PACKAGE__->register_method({
+    name => 'vm_reboot',
+    path => 'reboot',
+    method => 'POST',
+    protected => 1,
+    proxyto => 'node',
+    description => "Reboot the container 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::LXC::complete_ctid_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');
+
+	die "VM $vmid not running\n" if !PVE::LXC::check_running($vmid);
+
+	my $realcmd = sub {
+	    my $upid = shift;
+
+	    syslog('info', "requesting reboot of CT $vmid: $upid\n");
+	    PVE::LXC::vm_reboot($vmid, $param->{timeout});
+	    return;
+	};
+
+	return $rpcenv->fork_worker('vzreboot', $vmid, $authuser, $realcmd);
+    }});
+
+
+
 1;
diff --git a/src/PVE/CLI/pct.pm b/src/PVE/CLI/pct.pm
index 3a32de4..98e2c6e 100755
--- a/src/PVE/CLI/pct.pm
+++ b/src/PVE/CLI/pct.pm
@@ -836,7 +836,8 @@ our $cmddef = {
     resume => [ 'PVE::API2::LXC::Status', 'vm_resume', ['vmid'], { node => $nodename }, $upid_exit],
     shutdown => [ 'PVE::API2::LXC::Status', 'vm_shutdown', ['vmid'], { node => $nodename }, $upid_exit],
     stop => [ 'PVE::API2::LXC::Status', 'vm_stop', ['vmid'], { node => $nodename }, $upid_exit],
-    
+    reboot => [ 'PVE::API2::LXC::Status', 'vm_reboot', ['vmid'], { node => $nodename }, $upid_exit],
+
     clone => [ "PVE::API2::LXC", 'clone_vm', ['vmid', 'newid'], { node => $nodename }, $upid_exit ],
     migrate => [ "PVE::API2::LXC", 'migrate_vm', ['vmid', 'target'], { node => $nodename }, $upid_exit],
     move_volume => [ "PVE::API2::LXC", 'move_volume', ['vmid', 'volume', 'storage'], { node => $nodename }, $upid_exit ],
diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 61f9bae..c400f21 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -2009,6 +2009,19 @@ sub vm_stop {
     die "container did not stop\n";
 }
 
+sub vm_reboot {
+    my ($vmid, $timeout, $skiplock) = @_;
+
+    PVE::LXC::Config->lock_config($vmid, sub {
+	return if !check_running($vmid);
+
+	vm_stop($vmid, 0, $timeout, 1); # kill if timeout exceeds
+
+	my $conf = PVE::LXC::Config->load_config($vmid);
+	vm_start($vmid, $conf);
+    });
+}
+
 sub run_unshared {
     my ($code) = @_;
 
-- 
2.20.1




More information about the pve-devel mailing list