3 # Provides: lxc-watchdog
4 # Required-Start: $remote_fs $named $network $time
5 # Required-Stop: $remote_fs $named $network
8 # Default-Start: 2 3 4 5
10 # Short-Description: Manage Linux Containers startup/shutdown
11 # Description: Uses clever inotify hack to monitor container's
12 # halt/reboot events watching /var/run/utmp
15 # Author: Dobrica Pavlinusic <dpavlin@rot13.org>
17 # based on Tony Risinger post to lxc-users mailing list
18 # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
21 which inotifywait >/dev/null || apt-get install inotify-tools
27 if [ ! -e /var/lib/lxc/$name/config ] ; then
36 grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
41 lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do
43 test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"
44 echo "$name $status $on_boot $(lxc_rootfs $name)"
49 cleanup_init_scripts() {
50 rootfs=$(lxc_rootfs $1)
53 $rootfs/etc/rc?.d/*umountfs \
54 $rootfs/etc/rc?.d/*umountroot \
55 $rootfs/etc/rc?.d/*hwclock* \
56 2>/dev/null | xargs -i rm -v {}
61 rootfs=$(lxc_rootfs $1)
65 # let container respond to kill -SIGPWR
66 inittab=$rootfs/etc/inittab
67 if ! grep "$add" ${inittab} >/dev/null ; then
68 grep -v "$remove" ${inittab} > ${inittab}.new
69 echo $add >> ${inittab}.new
70 mv ${inittab}.new ${inittab}
71 echo "$inittab modified with $add"
80 init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
81 if [ -z "$init_pid" ] ; then
85 echo "$name kill $sig $init_pid"
86 /bin/kill $sig $init_pid
90 lxc_kill $name -SIGPWR
91 lxc-wait -n $name -s STOPPED
92 # rm -f /var/lib/lxc/${name}/on_boot
99 if ! lxc-info -n $name | grep RUNNING ; then
101 lxc-start -n $name -o /tmp/${name}.log -d
102 lxc-wait -n $name -s RUNNING
104 test -f /var/lib/lxc/${name}/on_boot || echo $name > /var/lib/lxc/${name}/on_boot
110 rootfs=$(lxc_rootfs $1)
113 vps_utmp=${rootfs}/var/run/utmp
114 tasks=`wc -l < /cgroup/${name}/tasks`
115 test -z "$tasks" && exit 1
116 if [ "$tasks" -eq 1 ]; then
118 runlevel="$(runlevel ${vps_utmp})"
119 echo `date +%Y-%m-%dT%H:%M:%S` "$name runlevel $runlevel"
123 # nothing for new boot state
127 lxc-stop -n "${name}"
128 lxc-wait -n ${name} -s STOPPED
134 lxc-wait -n ${name} -s STOPPED
135 lxc-start -d -n ${name} -o /tmp/${name}.log
138 # make sure vps is still running
139 state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
140 [ "$state" = "RUNNING" ] || break
144 echo "# $name $tasks tasks"
147 # time of 5 minutes on it JUST IN CASE...
148 inotifywait -qqt 300 ${vps_utmp}
151 echo "${name} exited"
166 cleanup_init_scripts $1
167 setup_inittab $1 ::power "p0::powerfail:/sbin/init 0"
168 setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
170 ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
176 reload|force-reload|restart|reboot)
183 echo "Usage: $0 {start|stop|restart|status}" >&2
194 test "$command" = "status" && lxc_status && exit
196 if [ -z "$1" ] ; then
197 ls /var/lib/lxc/*/on_boot | while read path ; do
198 name=`echo $path | cut -d/ -f5`
199 command_on_lxc $command $name
202 while [ ! -z "$1" ] ; do
203 command_on_lxc $command $1