code for create_reboot_request and clear_reboot_request is from qemu, the only
difference is that we use /run/lxc/$vmid.reboot path instead of
/run/qemu-server.

there _is_ actually reboot triggers for lxc which are used by the
prestart hook and similar, however i think it's better if we can
differentiate between reboot requests from inside the container and from
API. (inadvertently this also allows to reboot container from inside without
applying pending changes)

since we don't have to clean up like in qemu, we can simply run vm_stop
and vm_start for a reboot operation.

Signed-off-by: Oguz Bektas <o.bek...@proxmox.com>
---
 src/PVE/LXC.pm | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index cdf6d64..c77ee01 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -2013,6 +2013,47 @@ sub vm_stop {
     die "container did not stop\n";
 }
 
+sub create_reboot_request {
+    my ($vmid) = @_;
+    open(my $fh, '>', "/run/lxc/$vmid.reboot")
+       or die "failed to create reboot trigger file: $!\n";
+    close($fh);
+}
+
+sub clear_reboot_request {
+    my ($vmid) = @_;
+    my $path = "/run/lxc/$vmid.reboot";
+    my $res = 0;
+
+    if (-e $path) {
+       $res = unlink($path);
+       die "could not remove reboot request for $vmid: $!\n" if !$res;
+    }
+
+    return $res;
+}
+
+sub vm_reboot {
+    my ($vmid, $timeout, $skiplock) = @_;
+
+    PVE::LXC::Config->lock_config($vmid, sub {
+       eval {
+           return if !check_running($vmid);
+
+           create_reboot_request($vmid);
+           vm_stop($vmid, 0, $timeout, 1); # kill if timeout exceeds
+
+           my $conf = PVE::LXC::Config->load_config($vmid);
+           vm_start($vmid, $conf);
+       };
+       if (my $err = $@) {
+           # avoid that the next normal shutdown will be confused for a reboot
+           clear_reboot_request($vmid);
+           die $err;
+       }
+    });
+}
+
 sub run_unshared {
     my ($code) = @_;
 
-- 
2.20.1

_______________________________________________
pve-devel mailing list
pve-devel@pve.proxmox.com
https://pve.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to