[lxc-devel] [PATCH 2/2] add lxc-autostart support for sysv init systems

Dwight Engen dwight.engen at oracle.com
Tue Dec 31 19:21:55 UTC 2013


This change updates the way init scripts get installed so that more
than one init system can be supported. Instead of installing the
systemd service file from the spec file, it should be installed at
make install time, so that someone compiling from source also gets
the unit file installed.

Update the plamo template to use a lock file not named just
/var/lock/subsys/lxc since the presence of that file is used by
sysv init rc file to know if it should run the K01lxc script. This
also makes it consistent with the other templates which use
/var/lock/subsys/lxc-$template-name.

Signed-off-by: Dwight Engen <dwight.engen at oracle.com>
---
 configure.ac           | 45 +++++++++++++++++++++++++++++++++++
 lxc.spec.in            | 27 +++++++++++++++------
 src/lxc/Makefile.am    | 44 +++++++++++++++++++++++++++++++++-
 src/lxc/lxc.sysvinit   | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++
 templates/lxc-plamo.in |  4 ++--
 5 files changed, 174 insertions(+), 10 deletions(-)
 create mode 100755 src/lxc/lxc.sysvinit

diff --git a/configure.ac b/configure.ac
index 4c5f002..2d24937 100644
--- a/configure.ac
+++ b/configure.ac
@@ -70,6 +70,50 @@ AC_MSG_RESULT([$with_distro])
 AM_CONDITIONAL([HAVE_DEBIAN], [test x"$with_distro" = "xdebian" -o x"$with_distro" = "xubuntu"])
 AM_CONDITIONAL([DISTRO_UBUNTU], [test "x$with_distro" = "xubuntu"])
 
+# Check for init system type
+AC_MSG_CHECKING([for init system type])
+AC_ARG_WITH([init-script],
+	    [AC_HELP_STRING([--with-init-script@<:@=TYPE@:>@],
+			    [Type of init script to install: sysv, systemd, upstart,
+			     distro, none @<:@default=distro@:>@])],[],[with_init_script=distro])
+case "$with_init_script" in
+	sysv)
+		init_script=sysv
+		;;
+	systemd)
+		init_script=systemd
+		;;
+	upstart)
+		init_script=upstart
+		;;
+	none)
+		;;
+	distro)
+		case $with_distro in
+			fedora)
+				init_script=systemd
+				;;
+			redhat|centos|oracle|oracleserver)
+				init_script=sysv
+				;;
+			ubuntu)
+				init_script=upstart
+				;;
+			*)
+				echo -n "Linux distribution init system unknown, defaulting to sysv"
+				init_script=sysv
+				;;
+		esac
+		;;
+	*)
+		AC_MSG_ERROR([Unknown init system type $with_init_script])
+		;;
+esac
+AM_CONDITIONAL([INIT_SCRIPT_SYSV], test "$init_script" = "sysv")
+AM_CONDITIONAL([INIT_SCRIPT_SYSTEMD], test "$init_script" = "systemd")
+AM_CONDITIONAL([INIT_SCRIPT_UPSTART], test "$init_script" = "upstart")
+AC_MSG_RESULT($init_script)
+
 # Allow disabling rpath
 AC_ARG_ENABLE([rpath],
 	[AC_HELP_STRING([--enable-rpath], [set rpath in executables [default=no]])],
@@ -610,6 +654,7 @@ cat << EOF
 Environment:
  - compiler: $CC
  - distribution: $with_distro
+ - init script type: $init_script
  - rpath: $enable_rpath
  - GnuTLS: $enable_gnutls
 
diff --git a/lxc.spec.in b/lxc.spec.in
index 6814ad7..b977c8c 100644
--- a/lxc.spec.in
+++ b/lxc.spec.in
@@ -23,6 +23,17 @@
 %global with_python %{?_with_python: 1} %{?!_with_python: 0}
 %global with_lua %{?_with_lua: 1} %{?!_with_lua: 0}
 
+# Set with_systemd on distros that use it, so we can install the service
+# file, otherwise the sysvinit script will be installed
+%if 0%{?fedora} >= 14 || 0%{?rhel} >= 7 || 0%{?suse_version} >= 1210
+%global with_systemd 1
+%define init_script systemd
+BuildRequires: systemd-units
+%else
+%global with_systemd 0
+%define init_script sysv
+%endif
+
 # RPM needs alpha/beta/rc in Release: not Version: to ensure smooth
 # package upgrades from alpha->beta->rc->release. For more info see:
 # http://fedoraproject.org/wiki/Packaging%3aNamingGuidelines#NonNumericRelease
@@ -94,7 +105,8 @@ PATH=$PATH:/usr/sbin:/sbin %configure $args \
 %if %{with_python}
   --enable-python \
 %endif
-  --disable-rpath
+  --disable-rpath \
+  --with-init-script=%{init_script}
 make %{?_smp_mflags}
 
 %install
@@ -102,11 +114,6 @@ rm -rf %{buildroot}
 make install DESTDIR=%{buildroot}
 find %{buildroot} -type f -name '*.la' -exec rm -f {} ';'
 
-# Install some of our systemd stuff...
-install -d -m 755 %{buildroot}/lib/systemd/system
-install -c -m 644 src/lxc/lxc.service %{buildroot}/lib/systemd/system
-install -c -m 755 src/lxc/lxc-devsetup %{buildroot}/%{_libexecdir}/%{name}
-
 %clean
 rm -rf %{buildroot}
 
@@ -136,7 +143,11 @@ rm -rf %{buildroot}
 %{_datadir}/lxc/*
 %config(noreplace) %{_sysconfdir}/lxc/*
 
-/lib/systemd/system/*
+%if %{with_systemd}
+%{_unitdir}/lxc.service
+%else
+%{_sysconfdir}/rc.d/init.d/lxc
+%endif
 
 %files libs
 %defattr(-,root,root)
@@ -147,7 +158,9 @@ rm -rf %{buildroot}
 %endif
 %{_localstatedir}/*
 %attr(4555,root,root) %{_libexecdir}/%{name}/lxc-init
+%if %{with_systemd}
 %attr(555,root,root) %{_libexecdir}/%{name}/lxc-devsetup
+%endif
 
 %if %{with_python}
 %{_libdir}/python3.3/site-packages/_lxc*
diff --git a/src/lxc/Makefile.am b/src/lxc/Makefile.am
index 74b38e2..c54120a 100644
--- a/src/lxc/Makefile.am
+++ b/src/lxc/Makefile.am
@@ -154,7 +154,8 @@ EXTRA_DIST = \
 	lxc-devsetup \
 	lxc-ls \
 	lxc-top \
-	lxc.service
+	lxc.service \
+	lxc.sysvinit
 
 if ENABLE_PYTHON
 bin_SCRIPTS += lxc-device
@@ -197,6 +198,43 @@ bin_PROGRAMS = \
 pkglibexec_PROGRAMS = \
 	lxc-init
 
+if INIT_SCRIPT_SYSV
+install-init: lxc.sysvinit
+	$(MKDIR_P) $(DESTDIR)$(sysconfdir)/rc.d/init.d
+	$(INSTALL_SCRIPT) lxc.sysvinit $(DESTDIR)$(sysconfdir)/rc.d/init.d/lxc
+
+uninstall-init:
+	rm -f $(DESTDIR)$(sysconfdir)/rc.d/init.d/lxc
+	rmdir $(DESTDIR)$(sysconfdir)/rc.d/init.d || :
+else
+install-init:
+uninstall-init:
+endif
+
+if INIT_SCRIPT_SYSTEMD
+SYSTEMD_UNIT_DIR = /usr/lib/systemd/system
+install-systemd: lxc.service lxc-devsetup
+	$(MKDIR_P) $(DESTDIR)$(SYSTEMD_UNIT_DIR)
+	$(INSTALL_DATA) lxc.service $(DESTDIR)$(SYSTEMD_UNIT_DIR)/
+
+uninstall-systemd:
+	rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc.service
+	rmdir $(DESTDIR)$(SYSTEMD_UNIT_DIR) || :
+pkglibexec_SCRIPTS = lxc-devsetup
+else
+install-systemd:
+uninstall-systemd:
+endif
+
+if INIT_SCRIPT_UPSTART
+# FIXME: install/uninstall upstart script here
+install-upstart:
+uninstall-upstart:
+else
+install-upstart:
+uninstall-upstart:
+endif
+
 AM_LDFLAGS = -Wl,-E
 if ENABLE_RPATH
 AM_LDFLAGS += -Wl,-rpath -Wl,$(libdir)
@@ -229,6 +267,10 @@ lxc_snapshot_SOURCES = lxc_snapshot.c
 lxc_usernsexec_SOURCES = lxc_usernsexec.c
 lxc_user_nic_SOURCES = lxc_user_nic.c network.c network.h
 
+install-data-local: install-init install-systemd install-upstart
+
+uninstall-local: uninstall-init uninstall-systemd uninstall-upstart
+
 install-exec-local: install-soPROGRAMS
 	mkdir -p $(DESTDIR)$(datadir)/lxc
 	install -c -m 644 lxc.functions $(DESTDIR)$(datadir)/lxc
diff --git a/src/lxc/lxc.sysvinit b/src/lxc/lxc.sysvinit
new file mode 100755
index 0000000..9128187
--- /dev/null
+++ b/src/lxc/lxc.sysvinit
@@ -0,0 +1,64 @@
+#!/bin/sh
+#
+# lxc Start/Stop LXC autoboot containers
+#
+# chkconfig: 345 99 01
+# description: Starts/Stops all LXC containers configured for autostart.
+#
+### BEGIN INIT INFO
+# Provides: lxc
+# Default-Start: 3 4 5
+# Default-Stop: 0 1 6
+# Short-Description: Bring up/down LXC autostart containers
+# Description: Bring up/down LXC autostart containers
+### END INIT INFO
+
+# Source function library.
+. /etc/init.d/functions
+
+# Check for needed utility program
+[ -x /usr/bin/lxc-autostart ] || exit 1
+
+# If libvirtd is providing the bridge, it might not be
+# immediately available, so wait a bit for it before starting
+# up the containers or else any that use the bridge will fail
+# to start
+wait_for_bridge()
+{
+    [ -f /etc/lxc/default.conf ] || { return 0; }
+
+    BRNAME=`grep lxc.network.link /etc/lxc/default.conf |awk '{print $3}'`
+    [ -n $BRNAME ] || { return 0; }
+
+    for try in `seq 1 30`; do
+	ifconfig -a |grep "^$BRNAME" >/dev/null 2>&1
+	if [ $? = 0 ]; then
+	    return
+	fi
+	sleep 1
+    done
+}
+
+# See how we were called.
+case "$1" in
+  start)
+	[ ! -f /var/lock/subsys/lxc ] || { exit 0; }
+
+	# Start containers
+	wait_for_bridge
+	action $"Starting LXC containers: " /usr/bin/lxc-autostart
+	touch /var/lock/subsys/lxc
+	;;
+  stop)
+	action $"Stopping LXC containers: " /usr/bin/lxc-autostart -s
+	rm -f /var/lock/subsys/lxc
+	;;
+  restart|reload|force-reload)
+	$0 stop
+	$0 start
+	;;
+  *)
+	echo $"Usage: $0 {start|stop|restart|reload|force-reload}"
+	exit 2
+esac
+exit $?
diff --git a/templates/lxc-plamo.in b/templates/lxc-plamo.in
index 485f363..8705a05 100644
--- a/templates/lxc-plamo.in
+++ b/templates/lxc-plamo.in
@@ -145,7 +145,7 @@ install_plamo() {
       return 1
     fi
     return 0
-  ) 200> @LOCALSTATEDIR@/lock/subsys/lxc
+  ) 200> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
 }
 
 configure_plamo() {
@@ -306,7 +306,7 @@ cleanup() {
     rm -rf --one-file-system $dlcache $rtcache || return 1
     echo "Done."
     return 0
-  ) 200> @LOCALSTATEDIR@/lock/subsys/lxc
+  ) 200> @LOCALSTATEDIR@/lock/subsys/lxc-plamo
 }
 
 usage() {
-- 
1.8.3.1



More information about the lxc-devel mailing list