Re: [pve-devel] [PATCH container 1/6] add reboot helpers to be used by containers

2019-11-13 Thread Thomas Lamprecht
On 11/12/19 5:26 PM, Oguz Bektas wrote:
> 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)


why would I want above? Pending changes wait to be applied
as soon as possible!

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


Re: [pve-devel] [PATCH container 1/6] add reboot helpers to be used by containers

2019-11-13 Thread Stefan Reiter

On 11/12/19 5:26 PM, Oguz Bektas wrote:

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.



That being so, why do we need a restart trigger file at all? You create 
it in vm_reboot and then delete it in vm_start or the error path... But 
it existing or not doesn't change behaviour of any of these?



Signed-off-by: Oguz Bektas 
---
  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) = @_;
  



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


[pve-devel] [PATCH container 1/6] add reboot helpers to be used by containers

2019-11-12 Thread Oguz Bektas
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 
---
 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