[lxc-devel] [PATCH] have systemd service call lxc-autostart via script

Stéphane Graber stgraber at ubuntu.com
Thu May 1 21:57:47 UTC 2014


On Thu, May 01, 2014 at 05:51:15PM -0400, Michael H. Warfield wrote:
> On Thu, 2014-05-01 at 17:31 -0400, Stéphane Graber wrote:
> > On Thu, May 01, 2014 at 05:14:12PM -0400, Michael H. Warfield wrote:
> > > Related to this...  Dwight and I have been bouncing a couple of things
> > > back and forth and I noticed that lxc-autostart is being called from the
> > > sysvinit scripts without the -a parameter.  That means that any
> > > container in a non-null group will not be autostart on boot.  I feel
> > > that's the wrong behavior.  If autoboot = 1 then it should be autobooted
> > > on boot.  What you do after boot is up to you but I would expect it to
> > > be "autoboot = 1" => "autobooted on bootup", not "autoboot = 1" =>
> > > "autobooted on bootup if it's not in a group".
> 
> > I can see why you'd auto-start both the null group and say a "boot"
> > group, but I don't think we should auto-start them all.
> 
> OTOH, I wanted to group my services "WittsEnd", "WittsEnd-DNS",
> "WittsEnd-Web", "WittsEnd-DB" so they could be filtered and managed as
> groups but I want them all to start at boot, maybe by groups (boot the
> DNS first and then the DB and then the Web).  I would control if they
> autoboot at boot time with the autoboot parameter.  The order grouping
> can be handled now with some of the other options but, if autoboot does
> not mean we autoboot the container then we chose the wrong name for the
> parameter.
> 
> Perhaps we need a parameter in /etc/lxc/lxc.conf that can specify what
> groups to boot at boot time?  The fact that this is not parameterized,
> it works in a way that forces me to modify the system scripts with is
> (cough) suboptimal.  Alternatively, if a container could belong to
> multiple groups, say "WittsEnd-DB,boot" then anything belonging to the
> boot group would autoboot at boot (I'm sounding like I have a stutter
> now) would then autoboot.
> 
> > A reason is that I use those groups as a way to easily start a group of
> > interdependent containers, when I need those, I do "lxc-autostart -g
> > blah" and all containers that have lxc.group = blah and lxc.start.auto =
> > 1 will start properly sorted, with the right delays, ...). I however
> > don't necessarily want those to start at boot.
> 
> That's fine for you but it's not how I need them to work in a production
> environment when I reboot the iron.  What you do after the iron is up is
> not an "autoboot" condition but one where you are boot and controlling
> groups.  A friend of mine has "ByTheSea" and my son has "Malamaber" on
> my colo iron.  Using groups is great for me to shut down all the
> "Malamber" containers collectively but I want all of them coming up if I
> flip the BRS (Big Red Switch) on the iron.
> 
> I've suggested two options above that could accommodate both paradigms.
> Parameter in /etc/lxc/lxc.conf or multiple group memberships.  Thoughts?

Multiple group memberships is already supported, lxc.group is stored as
a list so you could do as I suggested in my previous e-mail and have the
init script call ("lxc-autostart && lxc-autostart -g boot") then have
the containers you want to start at boot time have the lxc.group = boot
on top of any existing lxc.group they may already have.

Support for a list of groups to auto-start in /etc/lxc/lxc.conf was also
part of the original specification, however this hasn't been implemented
yet, mostly because the parser for lxc.conf is rather minimal and it
seemed to much of a pain at the time :)

Regardless, patches are welcome for that bit :)


> 
> Regards,
> Mike
> 
> > > On Thu, 2014-05-01 at 16:06 -0400, Stéphane Graber wrote:
> > > > On Thu, May 01, 2014 at 03:33:58PM -0400, Dwight Engen wrote:
> > > > > - reuse the sysvinit script to ensure that if the lxc is configured to use
> > > > >   a bridge setup by libvirt, the bridge will be available before starting
> > > > >   the container
> > > > > 
> > > > > - made the sysvinit script check for the existance of ifconfig, and fall
> > > > >   back to ip link list if available
> > > > > 
> > > > > - made the lxc service also dependant on the network.target
> > > > > 
> > > > > - autoconfized the paths in the service file and sysvinit script
> > > > > 
> > > > > Reported-by: CDR <venefax at gmail.com>
> > > > > Signed-off-by: Dwight Engen <dwight.engen at oracle.com>
> > > > > ---
> > > > >  .gitignore                         |  3 ++
> > > > >  config/init/systemd/Makefile.am    | 14 ++++++-
> > > > >  config/init/systemd/lxc.service    | 17 --------
> > > > >  config/init/systemd/lxc.service.in | 17 ++++++++
> > > > >  config/init/sysvinit/lxc           | 66 ------------------------------
> > > > >  config/init/sysvinit/lxc.in        | 82 ++++++++++++++++++++++++++++++++++++++
> > > > >  configure.ac                       |  2 +
> > > > >  lxc.spec.in                        |  1 +
> > > > >  8 files changed, 117 insertions(+), 85 deletions(-)
> > > > >  delete mode 100644 config/init/systemd/lxc.service
> > > > >  create mode 100644 config/init/systemd/lxc.service.in
> > > > >  delete mode 100755 config/init/sysvinit/lxc
> > > > >  create mode 100755 config/init/sysvinit/lxc.in
> > > > > 
> > > > > diff --git a/.gitignore b/.gitignore
> > > > > index 8145f81..77b1ceb 100644
> > > > > --- a/.gitignore
> > > > > +++ b/.gitignore
> > > > > @@ -111,6 +111,9 @@ config/missing
> > > > >  config/libtool.m4
> > > > >  config/lt*.m4
> > > > >  config/bash/lxc
> > > > > +config/init/systemd/lxc-autostart
> > > > > +config/init/systemd/lxc.service
> > > > > +config/init/sysvinit/lxc
> > > > >  
> > > > >  doc/*.1
> > > > >  doc/*.5
> > > > > diff --git a/config/init/systemd/Makefile.am b/config/init/systemd/Makefile.am
> > > > > index de5ee50..bbc7baa 100644
> > > > > --- a/config/init/systemd/Makefile.am
> > > > > +++ b/config/init/systemd/Makefile.am
> > > > > @@ -5,7 +5,17 @@ EXTRA_DIST = \
> > > > >  if INIT_SCRIPT_SYSTEMD
> > > > >  SYSTEMD_UNIT_DIR = $(prefix)/lib/systemd/system
> > > > >  
> > > > > -install-systemd: lxc.service lxc-devsetup
> > > > > +lxc-autostart: ../sysvinit/lxc.in $(top_builddir)/config.status
> > > > > +	$(AM_V_GEN)sed                                          \
> > > > > +	    -e 's|[@]SYSCONFDIR[@]|$(sysconfdir)|g'             \
> > > > > +	    -e 's|[@]LOCALSTATEDIR[@]|$(localstatedir)|g'       \
> > > > > +	    -e 's|[@]BINDIR[@]|$(bindir)|g'                     \
> > > > > +	    < $< > $@-t &&                                      \
> > > > > +	    chmod a+x $@-t &&                                   \
> > > > > +	    mv $@-t $@
> > > > > +BUILT_SOURCES = lxc-autostart
> > > > > +
> > > > > +install-systemd: lxc.service lxc-devsetup lxc-autostart
> > > > >  	$(MKDIR_P) $(DESTDIR)$(SYSTEMD_UNIT_DIR)
> > > > >  	$(INSTALL_DATA) lxc.service $(DESTDIR)$(SYSTEMD_UNIT_DIR)/
> > > > >  
> > > > > @@ -13,7 +23,7 @@ uninstall-systemd:
> > > > >  	rm -f $(DESTDIR)$(SYSTEMD_UNIT_DIR)/lxc.service
> > > > >  	rmdir $(DESTDIR)$(SYSTEMD_UNIT_DIR) || :
> > > > >  
> > > > > -pkglibexec_SCRIPTS = lxc-devsetup
> > > > > +pkglibexec_SCRIPTS = lxc-devsetup lxc-autostart
> > > > >  
> > > > >  install-data-local: install-systemd
> > > > >  uninstall-local: uninstall-systemd
> > > > > diff --git a/config/init/systemd/lxc.service b/config/init/systemd/lxc.service
> > > > > deleted file mode 100644
> > > > > index aa20b91..0000000
> > > > > --- a/config/init/systemd/lxc.service
> > > > > +++ /dev/null
> > > > > @@ -1,17 +0,0 @@
> > > > > -[Unit]
> > > > > -Description=LXC Container Initialization and Autoboot Code
> > > > > -After=syslog.target
> > > > > -
> > > > > -[Service]
> > > > > -Type=oneshot
> > > > > -RemainAfterExit=yes
> > > > > -ExecStartPre=/usr/libexec/lxc/lxc-devsetup
> > > > > -ExecStart=/usr/libexec/lxc/lxc-startup start
> > > > > -ExecStop=/usr/libexec/lxc/lxc-startup stop
> > > > > -# Environment=BOOTUP=serial
> > > > > -# Environment=CONSOLETYPE=serial
> > > > > -StandardOutput=syslog
> > > > > -StandardError=syslog
> > > > > -
> > > > > -[Install]
> > > > > -WantedBy=multi-user.target
> > > > > diff --git a/config/init/systemd/lxc.service.in b/config/init/systemd/lxc.service.in
> > > > > new file mode 100644
> > > > > index 0000000..2f293eb
> > > > > --- /dev/null
> > > > > +++ b/config/init/systemd/lxc.service.in
> > > > > @@ -0,0 +1,17 @@
> > > > > +[Unit]
> > > > > +Description=LXC Container Initialization and Autoboot Code
> > > > > +After=syslog.target network.target
> > > > > +
> > > > > +[Service]
> > > > > +Type=oneshot
> > > > > +RemainAfterExit=yes
> > > > > +ExecStartPre=@libexecdir@/lxc/lxc-devsetup
> > > > > +ExecStart=@libexecdir@/lxc/lxc-autostart start
> > > > > +ExecStop=@libexecdir@/lxc/lxc-autostart stop
> > > > 
> > > > ^ That still seems wrong to me, lxc-autostart is a standard binary in BINDIR.
> > > > 
> > > > > +# Environment=BOOTUP=serial
> > > > > +# Environment=CONSOLETYPE=serial
> > > > > +StandardOutput=syslog
> > > > > +StandardError=syslog
> > > > > +
> > > > > +[Install]
> > > > > +WantedBy=multi-user.target
> > > > > diff --git a/config/init/sysvinit/lxc b/config/init/sysvinit/lxc
> > > > > deleted file mode 100755
> > > > > index 5ab3c46..0000000
> > > > > --- a/config/init/sysvinit/lxc
> > > > > +++ /dev/null
> > > > > @@ -1,66 +0,0 @@
> > > > > -#!/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 | sed 's/^.*=[ 	]*//'`
> > > > > -    if [ -z "$BRNAME" ]; then
> > > > > -	return 0
> > > > > -    fi
> > > > > -
> > > > > -    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 -a -A -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/config/init/sysvinit/lxc.in b/config/init/sysvinit/lxc.in
> > > > > new file mode 100755
> > > > > index 0000000..b1a725c
> > > > > --- /dev/null
> > > > > +++ b/config/init/sysvinit/lxc.in
> > > > > @@ -0,0 +1,82 @@
> > > > > +#!/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
> > > > > +
> > > > > +sysconfdir="@SYSCONFDIR@"
> > > > > +bindir="@BINDIR@"
> > > > > +localstatedir="@LOCALSTATEDIR@"
> > > > > +
> > > > > +# Source function library.
> > > > > +test ! -r "$sysconfdir"/rc.d/init.d/functions ||
> > > > > +        . "$sysconfdir"/rc.d/init.d/functions
> > > > > +
> > > > > +# Check for needed utility program
> > > > > +[ -x "$bindir"/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 "$sysconfdir"/lxc/default.conf ] || { return 0; }
> > > > > +
> > > > > +    which ifconfig >/dev/null 2>&1
> > > > > +    if [ $? = 0 ]; then
> > > > > +        cmd="ifconfig -a"
> > > > > +    else
> > > > > +        which ip >/dev/null 2>&1
> > > > > +        if [ $? = 0 ]; then
> > > > > +            cmd="ip link list"
> > > > > +        fi
> > > > > +    fi
> > > > > +    [ -n cmd ] || { return 0; }
> > > > > +
> > > > > +    BRNAME=`grep '^[ 	]*lxc.network.link' "$sysconfdir"/lxc/default.conf | sed 's/^.*=[ 	]*//'`
> > > > > +    if [ -z "$BRNAME" ]; then
> > > > > +	return 0
> > > > > +    fi
> > > > > +
> > > > > +    for try in `seq 1 30`; do
> > > > > +	eval $cmd |grep "^$BRNAME" >/dev/null 2>&1
> > > > > +	if [ $? = 0 ]; then
> > > > > +	    return
> > > > > +	fi
> > > > > +	sleep 1
> > > > > +    done
> > > > > +}
> > > > > +
> > > > > +# See how we were called.
> > > > > +case "$1" in
> > > > > +  start)
> > > > > +	[ ! -f "$localstatedir"/lock/subsys/lxc ] || { exit 0; }
> > > > > +
> > > > > +	# Start containers
> > > > > +	wait_for_bridge
> > > > > +	action $"Starting LXC containers: " "$bindir"/lxc-autostart
> > > > > +	touch "$localstatedir"/lock/subsys/lxc
> > > > > +	;;
> > > > > +  stop)
> > > > > +	action $"Stopping LXC containers: " "$bindir"/lxc-autostart -a -A -s
> > > > > +	rm -f "$localstatedir"/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/configure.ac b/configure.ac
> > > > > index 474e354..6e0d8df 100644
> > > > > --- a/configure.ac
> > > > > +++ b/configure.ac
> > > > > @@ -565,7 +565,9 @@ AC_CONFIG_FILES([
> > > > >  	config/bash/lxc
> > > > >  	config/init/Makefile
> > > > >  	config/init/sysvinit/Makefile
> > > > > +	config/init/sysvinit/lxc
> > > > >  	config/init/systemd/Makefile
> > > > > +	config/init/systemd/lxc.service
> > > > >  	config/init/upstart/Makefile
> > > > >  	config/etc/Makefile
> > > > >  	config/templates/Makefile
> > > > > diff --git a/lxc.spec.in b/lxc.spec.in
> > > > > index 5c9fdc8..f4286c2 100644
> > > > > --- a/lxc.spec.in
> > > > > +++ b/lxc.spec.in
> > > > > @@ -156,6 +156,7 @@ rm -rf %{buildroot}
> > > > >  %attr(4111,root,root) %{_libexecdir}/%{name}/lxc-user-nic
> > > > >  %if %{with_systemd}
> > > > >  %attr(555,root,root) %{_libexecdir}/%{name}/lxc-devsetup
> > > > > +%attr(555,root,root) %{_libexecdir}/%{name}/lxc-autostart
> > > > >  %endif
> > > > >  
> > > > >  %if %{with_python}
> > > > > -- 
> > > > > 1.9.0
> > > > > 
> > > > > _______________________________________________
> > > > > lxc-devel mailing list
> > > > > lxc-devel at lists.linuxcontainers.org
> > > > > http://lists.linuxcontainers.org/listinfo/lxc-devel
> > > > 
> > > > _______________________________________________
> > > > lxc-devel mailing list
> > > > lxc-devel at lists.linuxcontainers.org
> > > > http://lists.linuxcontainers.org/listinfo/lxc-devel
> > > 
> > > -- 
> > > Michael H. Warfield (AI4NB) | (770) 978-7061 |  mhw at WittsEnd.com
> > >    /\/\|=mhw=|\/\/          | (678) 463-0932 |  http://www.wittsend.com/mhw/
> > >    NIC whois: MHW9          | An optimist believes we live in the best of all
> > >  PGP Key: 0x674627FF        | possible worlds.  A pessimist is sure of it!
> > > 
> > 
> > 
> > 
> > > _______________________________________________
> > > lxc-devel mailing list
> > > lxc-devel at lists.linuxcontainers.org
> > > http://lists.linuxcontainers.org/listinfo/lxc-devel
> > 
> > 
> > _______________________________________________
> > lxc-devel mailing list
> > lxc-devel at lists.linuxcontainers.org
> > http://lists.linuxcontainers.org/listinfo/lxc-devel
> 
> -- 
> Michael H. Warfield (AI4NB) | (770) 978-7061 |  mhw at WittsEnd.com
>    /\/\|=mhw=|\/\/          | (678) 463-0932 |  http://www.wittsend.com/mhw/
>    NIC whois: MHW9          | An optimist believes we live in the best of all
>  PGP Key: 0x674627FF        | possible worlds.  A pessimist is sure of it!
> 



> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/listinfo/lxc-devel


-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20140501/e93f5988/attachment-0001.sig>


More information about the lxc-devel mailing list