[Lxc-users] On clean reboot of Ubuntu 10.04 containers

Trent W. Buck twb at cybersource.com.au
Mon Dec 6 08:01:24 UTC 2010


Trent W. Buck writes:

> This post describes my attempts to get "clean" shutdown of Ubuntu 10.04
> containers.  The goal here is that a "shutdown -h now" of the dom0
> should not result in a potentially inconsistent domU postgres database,
> cf. a naive lxc-stop.

As this is largely working for me, I have turned my attention to getting
clean rebooting of containers.  That is, a user should be able to ssh
into a container, issue a "sudo reboot", and have sshd hang up.
Reconnecting over ssh should indicate the container rebooted (e.g. new
processes and tmpfses).

I am unreliably informed that there is no native way to handle this in
lxc, and that OpenVZ handles this by having clunky cron job that checks
every minute for containers that need rebooting.  So I set about
implementing something similar.

I changed my halt/reboot/poweroff wrapper thusly:

      chroot $template_dir tee >/dev/null /sbin/reboot <<-EOF
      	#!/bin/bash
      	while getopts nwdfiph opt
      	do [[ f = \$opt ]] && exec kill -SEGV 1
      	done
+     	[[ '-w' = "\$*" ]] || # /etc/init.d/umountnfs.sh ALWAYS calls halt -w.
+     	touch "/force\${0##*/}"
      	exec -a "$0" "\$0.distrib" "\$@"
      	EOF

And I have my container's event-spoofing /etc/init/lxc.conf remove them
on startup:

    rm -f /forcereboot /forcehalt /forcepoweroff

Then, an /etc/cron.d/lxc of:

    # Every minute, start all containers that contain /forcereboot.
    * * * * * root LXC_START_ONLY=reboot /etc/init.d/lxc start >/dev/null

And an /etc/init.d/lxc start rule of:

    case "$1" in
      start)
        log_daemon_msg "Starting $DESC"
        shopt -s nullglob
        for name in $ROOT_DIR/*/force${LXC_START_ONLY:-boot}
        do  name=${name%/force*}
            name=${name#$ROOT_DIR/}
            # Skip it if it doesn't have an RFC952 hostname (e.g. no fooserv.~1~'s)
            [[ $name =~ ^[[:alnum:]]([[:alnum:]-]{0,61}[[:alnum:]])?$ ]] || continue
            lxc-start -n $name -f $CONF_DIR/$name.conf -d
            log_progress_msg $name
        done
        log_end_msg 0
        ;;

      ...
    esac

Comments?

PS: I saw that "telinit u" should make upstart re-exec itself, which I
hoped meant it would issue a "startup" event, allowing me to "reboot" at
the init level, without lxc-start noticing or caring -- no such luck.





More information about the lxc-users mailing list