[lxc-devel] startup scripts for lxc containers?

Michael H. Warfield mhw at WittsEnd.com
Sat Jan 16 19:20:14 UTC 2010


On Sat, 2010-01-16 at 18:51 +0100, Dietmar Maurer wrote: 
> > 3. Now how to detect shutdown or reboot, i had two ideas.
> 
> Seems OpenVZ simply writes a init script into /etc/rc6.d/S00vzreboot
> 
> >#define REBOOT_MARK     "/reboot"
> >#define VZREBOOT        "S00vzreboot"
> >#define RC1             "/etc/rc.d/rc6.d"
> >#define RC2             "/etc/rc6.d"
> 
> The init script simply writes the file /reboot (a simple mark - empty file).

> A cron tasks checks it a container contains such file - if so, it (re)starts those containers.

If that checking is being done in a cron job already, is there any
reason for not simply checking the terminating run level in the
containers utmp file?

This is the logic I'm currently using on the host (just the loop part of
the script)...

== 
for VM in `ls /var/lib/lxc`
do
        STATE=`lxc-info -n ${VM} | sed -e 's/.* is //'`
        LXC_PRIVATE=${LXC_HOME}/private/${VM}


        if test "$STATE" = "RUNNING"
        then
                UTSNAME=`sed -e '/^lxc.utsname/!d' -e 's/.*= *//' < /var/lib/lxc/${VM}/config`
                PROCS=`cat /var/lib/cgroup/${VM}/tasks | wc -l`
                # Checking for utmp should prevent us from tripping over
                # lxc-exec commands...  Maybe...
                if test "$PROCS" = 1 -a -e ${LXC_PRIVATE}/var/run/utmp
                then
                        RUNLEVEL=`runlevel ${LXC_PRIVATE}/var/run/utmp`
                        case $RUNLEVEL in
                        N*)
                        # Don't do anything if he's in a new boot state
                                ;;

                        ??0)
                        # He's halted...  Kill the VM.
                                echo "Halting ${VM} ${UTSNAME}"
                                lxc-stop -n ${VM}
                                ;;

                        ??6)
                        # He's rebooting...  Kill the VM and start again...
                                echo "Rebooting ${VM} ${UTSNAME}"
                                lxc-stop -n ${VM}
                                lxc-wait -n ${VM} -s STOPPED
                                # We need to add our log back in here.
                                lxc-start -d -n ${VM}
                                STAMP=`date +%F-%T`
                                lxc-start -d -o ${LXC_HOME}/logs/${VM}.${STAMP}.log -n ${VM}
                                ;;

                        *)
                        # Ignore everything else.
                        # Should never get to this state...
                                ;;
                        esac
                fi
        fi
done
=== 

> - Dietmar

Mike
-- 
Michael H. Warfield (AI4NB) | (770) 985-6132 |  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!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 482 bytes
Desc: This is a digitally signed message part
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20100116/588e70ae/attachment.pgp>


More information about the lxc-devel mailing list