Package: debhelper
Severity: wishlist
Tags: patch

Hello,

attached you can find a patch which adds support for systemd packaging
in dh_installinit, just like upstart is already supported.

The patch installs systemd files to /etc/systemd/system/ (see the patch
for rationale) and tmpfiles to /usr/lib/tmpfiles.d. Additionally it
calls systemctl daemon-reload (to pick up service file changes) and
systemd-tmpfiles --create (to apply tmpfiles changes) on systems where
systemd is running.

-- System Information:
Debian Release: wheezy/sid
  APT prefers testing
  APT policy: (990, 'testing')
Architecture: amd64 (x86_64)
Foreign Architectures: armel
i386

Kernel: Linux 3.5.0 (SMP w/8 CPU cores)
Locale: LANG=de_DE.UTF-8, LC_CTYPE=de_DE.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/bash

Versions of packages debhelper depends on:
ii  binutils    2.22-6.1
ii  dpkg        1.16.4.3
ii  dpkg-dev    1.16.4.3
ii  file        5.11-1
ii  html2text   1.3.2a-15
ii  man-db      2.6.2-1
ii  perl        5.14.2-12
ii  po-debconf  1.0.16+nmu2

debhelper recommends no packages.

Versions of packages debhelper suggests:
ii  dh-make  0.60

-- no debconf information
>From b544e0542f8663498f7b89c0b23f8e5fadff4a47 Mon Sep 17 00:00:00 2001
From: Michael Stapelberg <stapelb...@debian.org>
Date: Sun, 29 Jul 2012 19:46:53 +0200
Subject: [PATCH] Support systemd in dh_installinit and postinst/postrm

---
 autoscripts/postinst-init         |    6 +++++
 autoscripts/postinst-init-nostart |    6 +++++
 autoscripts/postinst-init-restart |    6 +++++
 autoscripts/postrm-init           |    7 +++++
 dh_installinit                    |   51 +++++++++++++++++++++++++++++++++++--
 5 files changed, 74 insertions(+), 2 deletions(-)

diff --git a/autoscripts/postinst-init b/autoscripts/postinst-init
index 2430b2c..f5371ce 100644
--- a/autoscripts/postinst-init
+++ b/autoscripts/postinst-init
@@ -1,4 +1,10 @@
 if [ -x "/etc/init.d/#SCRIPT#" ]; then
+	# In case this system is running systemd, we need to ensure that all
+	# necessary tmpfiles (if any) are created before starting.
+	if [ -d /sys/fs/cgroup/systemd ] ; then
+		systemd-tmpfiles --create >/dev/null || true
+	fi
+
 	update-rc.d #SCRIPT# #INITPARMS# >/dev/null
 	invoke-rc.d #SCRIPT# start || #ERROR_HANDLER#
 fi
diff --git a/autoscripts/postinst-init-nostart b/autoscripts/postinst-init-nostart
index 7a1bd5e..ec6bdbd 100644
--- a/autoscripts/postinst-init-nostart
+++ b/autoscripts/postinst-init-nostart
@@ -1,3 +1,9 @@
 if [ -x "/etc/init.d/#SCRIPT#" ]; then
+	# In case this system is running systemd, we need to ensure that all
+	# necessary tmpfiles (if any) are created before starting.
+	if [ -d /sys/fs/cgroup/systemd ] ; then
+		systemd-tmpfiles --create >/dev/null || true
+	fi
+
 	update-rc.d #SCRIPT# #INITPARMS# >/dev/null || #ERROR_HANDLER#
 fi
diff --git a/autoscripts/postinst-init-restart b/autoscripts/postinst-init-restart
index 35bba20..862f9cc 100644
--- a/autoscripts/postinst-init-restart
+++ b/autoscripts/postinst-init-restart
@@ -1,4 +1,10 @@
 if [ -x "/etc/init.d/#SCRIPT#" ]; then
+	# In case this system is running systemd, we need to ensure that all
+	# necessary tmpfiles (if any) are created before starting.
+	if [ -d /sys/fs/cgroup/systemd ] ; then
+		systemd-tmpfiles --create >/dev/null || true
+	fi
+
 	update-rc.d #SCRIPT# #INITPARMS# >/dev/null
 	if [ -n "$2" ]; then
 		_dh_action=restart
diff --git a/autoscripts/postrm-init b/autoscripts/postrm-init
index 1c29298..61dcfc5 100644
--- a/autoscripts/postrm-init
+++ b/autoscripts/postrm-init
@@ -1,3 +1,10 @@
 if [ "$1" = "purge" ] ; then
 	update-rc.d #SCRIPT# remove >/dev/null
 fi
+
+
+# In case this system is running systemd, we make systemd reload the unit files
+# to pick up changes.
+if [ -d /sys/fs/cgroup/systemd ] ; then
+	systemctl --system daemon-reload >/dev/null || true
+fi
diff --git a/dh_installinit b/dh_installinit
index 9c8c155..60277f6 100755
--- a/dh_installinit
+++ b/dh_installinit
@@ -2,7 +2,7 @@
 
 =head1 NAME
 
-dh_installinit - install init scripts and/or upstart jobs into package build directories
+dh_installinit - install init scripts and/or upstart jobs and/or systemd services into package build directories
 
 =cut
 
@@ -42,6 +42,16 @@ build directory.
 If this exists, it is installed into etc/init/I<package>.conf in the package
 build directory.
 
+=item debian/I<package>.service
+
+If this exists, it is installed into etc/systemd/system/I<package>.service in
+the package build directory.
+
+=item debian/I<package>.tmpfile
+
+If this exists, it is installed into usr/lib/tmpfiles.d/I<package>.conf in the
+package build directory.
+
 =back
 
 =head1 OPTIONS
@@ -88,6 +98,13 @@ is installed as in F<etc/default/> . This may be useful for daemons with names
 ending in B<d>. (Note: this takes precedence over the B<--init-script> parameter
 described below.)
 
+=item B<--systemd-lib>
+
+Install the system service files to lib/systemd/system instead of
+etc/systemd/system. This flag should B<only> be used when there is absolutely
+no reason whatsoever for any user to ever customize the service file (e.g. for
+service files like debian-fixup.service, udev.service or munin-node.service).
+
 =item B<-u>I<params> B<--update-rcd-params=>I<params>
 
 =item B<--> I<params>
@@ -141,6 +158,7 @@ init(options => {
 	"init-script=s" => \$dh{INIT_SCRIPT},
 	"update-rcd-params=s", => \$dh{U_PARAMS},
 	"remove-d" => \$dh{D_FLAG},
+	"systemd-lib" => \$dh{SYSTEMD_LIB},
 });
 
 foreach my $package (@{$dh{DOPACKAGES}}) {
@@ -171,6 +189,29 @@ foreach my $package (@{$dh{DOPACKAGES}}) {
 		$script=$package;
 	}       
 
+	my $service=pkgfile($package,"service");
+	if ($service ne '' && ! $dh{ONLYSCRIPTS}) {
+		my $path="$tmp/etc/systemd/system";
+		if ($dh{SYSTEMD_LIB}) {
+			$path="$tmp/lib/systemd/system";
+		}
+		if (! -d "$path") {
+			doit("install","-d","$path");
+		}
+
+		doit("install","-p","-m644",$service,"$path/$script.service");
+	}
+
+	my $tmpfile=pkgfile($package,"tmpfile");
+	if ($tmpfile ne '' && ! $dh{ONLYSCRIPTS}) {
+		my $path="$tmp/usr/lib/tmpfiles.d";
+		if (! -d "$path") {
+			doit("install","-d","$path");
+		}
+
+		doit("install","-p","-m644",$tmpfile,"$path/$script.conf");
+	}
+
 	my $job=pkgfile($package,"upstart");
 
 	if ($job ne '' && ! $dh{ONLYSCRIPTS}) {
@@ -203,7 +244,11 @@ foreach my $package (@{$dh{DOPACKAGES}}) {
 		error("Can't use --init-script with an upstart job");
 	}
 
-	if ($job ne '' || $init ne '' || $dh{ONLYSCRIPTS}) {
+	# NB: The case that only $tmpfile is set makes no sense. The
+	# tmpfiles.d(5) mechanism is only available when using systemd (at
+	# least currently), so there has to be an init script which does the
+	# same thing for sysvinit.
+	if ($service ne '' || $job ne '' || $init ne '' || $dh{ONLYSCRIPTS}) {
 		# This is set by the -u "foo" command line switch, it's
 		# the parameters to pass to update-rc.d. If not set,
 		# we have to say "defaults".
@@ -265,4 +310,6 @@ Joey Hess <jo...@debian.org>
 
 Steve Langasek <steve.langa...@canonical.com>
 
+Michael Stapelberg <stapelb...@debian.org>
+
 =cut
-- 
1.7.10.4

Reply via email to