[pve-devel] [PATCH container 2/2] poststop: use a reboot trigger file

Wolfgang Bumiller w.bumiller at proxmox.com
Fri Mar 10 11:25:41 CET 2017


Since the STOPPED state is set before cgroup cleanup we need
a better way to restart a container: We now install an
ExecStopPost snippet into the lxc at .service which when
encountering a reboot trigger file
(/var/lib/lxc/$vmid/reboot) performs a systemctl restart.
---
 debian/triggers            |  1 +
 src/Makefile               |  5 +++++
 src/lxc-pve-poststop-hook  |  7 +++++--
 src/lxc-pve-prestart-hook  |  3 +++
 src/lxc-pve-reboot-trigger | 10 ++++++++++
 src/pve-reboot.conf        |  2 ++
 6 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100755 src/lxc-pve-reboot-trigger
 create mode 100644 src/pve-reboot.conf

diff --git a/debian/triggers b/debian/triggers
index 59dd688..b469b72 100644
--- a/debian/triggers
+++ b/debian/triggers
@@ -1 +1,2 @@
 activate-noawait pve-api-updates
+activate-noawait /etc/init.d
diff --git a/src/Makefile b/src/Makefile
index 7ffb593..928db5e 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -2,6 +2,7 @@ PACKAGE=pve-container
 
 PREFIX=${DESTDIR}/usr
 BINDIR=${PREFIX}/bin
+LIBDIR=${PREFIX}/lib
 SBINDIR=${PREFIX}/sbin
 MANDIR=${PREFIX}/share/man
 DOCDIR=${PREFIX}/share/doc/${PACKAGE}
@@ -9,6 +10,7 @@ LXC_SCRIPT_DIR=${PREFIX}/share/lxc
 LXC_TMPL_DIR=${LXC_SCRIPT_DIR}/templates
 LXC_HOOK_DIR=${LXC_SCRIPT_DIR}/hooks
 LXC_COMMON_CONFIG_DIR=${LXC_SCRIPT_DIR}/config/common.conf.d
+SERVICEDIR=$(LIBDIR)/systemd/system
 PODDIR=${DOCDIR}/pod
 MAN1DIR=${MANDIR}/man1/
 MAN5DIR=${MANDIR}/man5/
@@ -33,6 +35,9 @@ install: pct lxc-pve.conf lxc-pve-prestart-hook lxc-pve-autodev-hook lxc-pve-pos
 	install -m 0755 pve-update-lxc-config ${SBINDIR}
 	install -d ${LXC_SCRIPT_DIR}
 	install -m 0755 lxcnetaddbr ${LXC_SCRIPT_DIR}
+	install -m 0755 lxc-pve-reboot-trigger ${LXC_SCRIPT_DIR}
+	install -d -m0755 ${SERVICEDIR}/lxc at .service.d
+	install -m0644 pve-reboot.conf ${SERVICEDIR}/lxc at .service.d/
 	install -d ${LXC_HOOK_DIR}
 	install -m 0755 lxc-pve-prestart-hook ${LXC_HOOK_DIR}
 	install -m 0755 lxc-pve-autodev-hook ${LXC_HOOK_DIR}
diff --git a/src/lxc-pve-poststop-hook b/src/lxc-pve-poststop-hook
index a4302d6..0396739 100755
--- a/src/lxc-pve-poststop-hook
+++ b/src/lxc-pve-poststop-hook
@@ -81,9 +81,12 @@ __PACKAGE__->register_method ({
 	    # that we must not block because we're part of the service cgroup
 	    # systemd waits for to die before issuing the new lxc-start command.
 	    PVE::LXC::update_lxc_config($vmid, $conf);
-	    PVE::Tools::run_command(['systemctl', '--no-block', 'restart', "lxc\@$vmid"]);
+	    # Tell the post-stop hook we want to be restarted.
+	    open(my $fh, '>', "/var/lib/lxc/$vmid/reboot")
+		or die "failed to create reboot trigger file: $!\n";
+	    close($fh);
 	    # cause lxc to stop instead of rebooting
-	    POSIX::_exit(1);
+	    exit(1);
 	}
 
 	return undef;
diff --git a/src/lxc-pve-prestart-hook b/src/lxc-pve-prestart-hook
index 9835b1c..fd29423 100755
--- a/src/lxc-pve-prestart-hook
+++ b/src/lxc-pve-prestart-hook
@@ -76,6 +76,9 @@ __PACKAGE__->register_method ({
 
 	my $rootdir = $param->{rootfs};
 
+	# Delete any leftover reboot-trigger file
+	unlink("/var/lib/lxc/$vmid/reboot");
+
 	my $devlist_file = "/var/lib/lxc/$vmid/devices";
 	unlink $devlist_file;
 	my $devices = [];
diff --git a/src/lxc-pve-reboot-trigger b/src/lxc-pve-reboot-trigger
new file mode 100755
index 0000000..95c3ac8
--- /dev/null
+++ b/src/lxc-pve-reboot-trigger
@@ -0,0 +1,10 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+my $vmid = $ARGV[0] or exit(1);
+exit(0) if $vmid !~ /^\d+$/; # check for a PVE ID
+exit(0) if !-e "/etc/pve/lxc/${vmid}.conf";
+exit(0) if !unlink("/var/lib/lxc/$vmid/reboot");
+exec {'systemctl'} 'systemctl', '--no-block', 'restart', "lxc\@${vmid}.service";
diff --git a/src/pve-reboot.conf b/src/pve-reboot.conf
new file mode 100644
index 0000000..8a0539c
--- /dev/null
+++ b/src/pve-reboot.conf
@@ -0,0 +1,2 @@
+[Service]
+ExecStopPost=/usr/share/lxc/lxc-pve-reboot-trigger %i
-- 
2.1.4





More information about the pve-devel mailing list