]> git.proxmox.com Git - pve-container.git/commitdiff
implement container reboot
authorOguz Bektas <o.bektas@proxmox.com>
Wed, 20 Nov 2019 14:49:38 +0000 (15:49 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 20 Nov 2019 18:18:37 +0000 (19:18 +0100)
* add helper function vm_reboot
* add vm_reboot api call
* add 'pct reboot'

Signed-off-by: Oguz Bektas <o.bektas@proxmox.com>
src/PVE/API2/LXC/Status.pm
src/PVE/CLI/pct.pm
src/PVE/LXC.pm

index 166c731b131354f93247530ba6fad5e8b3183f59..41f1f4ff9529b75c17d860f7778e1ac1901c1218 100644 (file)
@@ -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;
index 3a32de4d7faeeef45c478583725c1b05f98e8a8b..98e2c6ef855562855d99adb3f5bab43e4d8418b1 100755 (executable)
@@ -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 ],
index 4efc8df9e2b74315c8593a12cb75855a484f87c4..51091890db5c2444f0182bc4f188b0a804549b70 100644 (file)
@@ -2220,6 +2220,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) = @_;