[pve-devel] [PATCH v5 container 2/4] add new pve-container units

Fabian Grünbichler f.gruenbichler at proxmox.com
Wed Oct 11 14:13:11 CEST 2017


to replace lxc at .service.

we want our unit to only start when manually invoked (by our code), and
stop on shutdown via pve-guests or pve-ha-lrm. lxc@ units are stopped by
systemd on shutdown, because of transitive dependencies.

since all instances of template service units are by default assigned to
a new slice with DefaultDependencies=yes, we also need to introduce our
own custom slice with DefaultDependencies=no.

Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 src/Makefile                         |  3 +++
 src/pve-container-stop-wrapper       | 20 ++++++++++++++++++++
 src/pve-container at .service           | 18 ++++++++++++++++++
 "src/system-pve\\x2dcontainer.slice" |  9 +++++++++
 4 files changed, 50 insertions(+)
 create mode 100755 src/pve-container-stop-wrapper
 create mode 100644 src/pve-container at .service
 create mode 100644 "src/system-pve\\x2dcontainer.slice"

diff --git a/src/Makefile b/src/Makefile
index 9c5fbc3..f69dda2 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -40,8 +40,11 @@ install: pct lxc-pve.conf lxc-pve-prestart-hook lxc-pve-autodev-hook lxc-pve-pos
 	install -d ${LXC_SCRIPT_DIR}
 	install -m 0755 lxcnetaddbr ${LXC_SCRIPT_DIR}
 	install -m 0755 lxc-pve-reboot-trigger ${LXC_SCRIPT_DIR}
+	install -m 0755 pve-container-stop-wrapper ${LXC_SCRIPT_DIR}
 	install -d -m0755 ${SERVICEDIR}/lxc at .service.d
 	install -m0644 pve-reboot.conf ${SERVICEDIR}/lxc at .service.d/
+	install -m0644 pve-container at .service ${SERVICEDIR}/
+	install -m0644 'system-pve\x2dcontainer.slice' ${SERVICEDIR}/
 	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/pve-container-stop-wrapper b/src/pve-container-stop-wrapper
new file mode 100755
index 0000000..97eafe5
--- /dev/null
+++ b/src/pve-container-stop-wrapper
@@ -0,0 +1,20 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+# sanity checks
+my $vmid = $ARGV[0] or exit(1);
+exit(0) if $vmid !~ /^\d+$/;
+exit(0) if !-e "/etc/pve/lxc/${vmid}.conf";
+
+if (unlink("/var/lib/lxc/$vmid/reboot")) {
+    # restart if rebooted from within container
+    exec {'systemctl'} 'systemctl', '--no-block', 'restart', "pve-container\@${vmid}.service";
+} elsif (!defined($ENV{EXIT_CODE}) || $ENV{EXIT_CODE} ne "exited") {
+    # stop if called via "systemctl stop lxc at ID"
+    exec {'lxc-stop'} 'lxc-stop', '-n', $vmid;
+} else {
+    # just exit if container was shutdown from within or via lxc-stop
+    exit(0);
+}
diff --git a/src/pve-container at .service b/src/pve-container at .service
new file mode 100644
index 0000000..fb4a069
--- /dev/null
+++ b/src/pve-container at .service
@@ -0,0 +1,18 @@
+# based on lxc at .service, but without any dependencies because
+# starting and stopping should be initiated by PVE code, not
+# systemd.
+[Unit]
+Description=PVE LXC Container: %i
+DefaultDependencies=No
+Documentation=man:lxc-start man:lxc man:pct
+
+[Service]
+Type=forking
+KillMode=mixed
+TimeoutStopSec=120s
+ExecStart=/usr/bin/lxc-start -n %i
+ExecStop=/usr/share/lxc/pve-container-stop-wrapper %i
+# Environment=BOOTUP=serial
+# Environment=CONSOLETYPE=serial
+StandardOutput=syslog
+StandardError=syslog
diff --git "a/src/system-pve\\x2dcontainer.slice" "b/src/system-pve\\x2dcontainer.slice"
new file mode 100644
index 0000000..7edae2c
--- /dev/null
+++ "b/src/system-pve\\x2dcontainer.slice"
@@ -0,0 +1,9 @@
+# needed because instances of template service units automatically
+# get their own slice with DefaultDependencies=yes, regardless of
+# the DefaultDependencies value of the template unit
+#
+# see man systemd.service and pve-container at .service
+[Unit]
+Description=PVE LXC Container Slice
+DefaultDependencies=No
+Documentation=man:pct
-- 
2.14.1





More information about the pve-devel mailing list