5 # Dobrica Pavlinusic <dpavlin@rot13.org> 2010-03-15
7 # this script can be used to start/stop Linux containers
8 # using clever inotify hack to monitor halt/reboot from
9 # Tony Risinger posted to lxc-users mailing list
11 # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
14 which inotifywait >/dev/null || apt-get install inotify-tools
17 lxc-ls -1 | sort -u | xargs -i lxc-info -n {}
23 if [ ! -e /var/lib/lxc/$name/config ] ; then
32 grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
36 cleanup_init_scripts() {
37 rootfs=$(lxc_rootfs $1)
40 $rootfs/etc/rc?.d/*umountfs \
41 $rootfs/etc/rc?.d/*umountroot \
42 $rootfs/etc/rc?.d/*hwclock* \
43 2>/dev/null | xargs -i rm -v {}
48 rootfs=$(lxc_rootfs $1)
50 # let container respond to kill -SIGPWR
51 inittab=$rootfs/etc/inittab
52 powerfail="pw::powerfail:/sbin/init 0"
53 if ! grep "$powerfail" ${inittab} >/dev/null ; then
54 grep -v ::power ${inittab} > ${inittab}.new
55 echo $powerfail >> ${inittab}.new
56 mv ${inittab}.new ${inittab}
57 echo "$initab modified"
66 init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
67 if [ -z "$init_pid" ] ; then
71 echo "$name stop $init_pid"
72 /bin/kill -SIGPWR $init_pid
73 lxc-wait -n $name -s STOPPED
81 if ! lxc-info -n $name | grep RUNNING ; then
83 lxc-start -n $name -o /tmp/${name}.log -d
84 lxc-wait -n $name -s RUNNING
91 rootfs=$(lxc_rootfs $1)
94 vps_utmp=${rootfs}/var/run/utmp
95 tasks=`wc -l < /cgroup/${name}/tasks`
96 test -z "$tasks" && exit 1
97 if [ "$tasks" -eq 1 ]; then
99 runlevel="$(runlevel ${vps_utmp})"
100 echo "# $name runlevel $runlevel"
104 # nothing for new boot state
108 lxc-stop -n "${name}"
109 lxc-wait -n ${name} -s STOPPED
115 lxc-wait -n ${name} -s STOPPED
116 lxc-start -d -n ${name} -o /tmp/${name}.log
119 # make sure vps is still running
120 state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
121 [ "$state" = "RUNNING" ] || break
125 echo "# $name $tasks tasks"
128 # time of 5 minutes on it JUST IN CASE...
129 inotifywait -qqt 300 ${vps_utmp}
132 echo "${name} exited"
141 cleanup_init_scripts $2
144 ( nohup $0 watchdog $2 >> /tmp/$2.log ) &
153 reload|force-reload|restart)
161 echo "Usage: $0 {start|stop|restart|status}" >&2