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 && boot="boot"
44 echo "$name $status $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"
77 echo `date +%Y-%m-%dT%H:%M:%S` $*
85 init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
86 if [ -z "$init_pid" ] ; then
90 lxc_log "$name kill $sig $init_pid"
91 /bin/kill $sig $init_pid
96 lxc_kill $name -SIGPWR
97 lxc-wait -n $name -s STOPPED
98 lxc_log "$name stoped"
99 # rm -f /var/lib/lxc/${name}/on_boot
106 if ! lxc-info -n $name | grep RUNNING ; then
107 lxc_log "$name start"
108 lxc-start -n $name -o /tmp/${name}.log -d
109 lxc-wait -n $name -s RUNNING
111 test -f /var/lib/lxc/${name}/on_boot || echo $name > /var/lib/lxc/${name}/on_boot
117 rootfs=$(lxc_rootfs $1)
120 vps_utmp=${rootfs}/var/run/utmp
121 tasks=`wc -l < /cgroup/${name}/tasks`
122 test -z "$tasks" && exit 1
123 if [ "$tasks" -eq 1 ]; then
125 runlevel="$(runlevel ${vps_utmp})"
126 lxc_log "$name runlevel $runlevel"
130 # nothing for new boot state
134 lxc-stop -n "${name}"
135 lxc-wait -n ${name} -s STOPPED
139 lxc_log "$name reboot";
141 lxc-wait -n ${name} -s STOPPED
142 lxc-start -d -n ${name} -o /tmp/${name}.log
145 # make sure vps is still running
146 state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
147 [ "$state" = "RUNNING" ] || break
151 lxc_log "$name $tasks tasks"
154 # time of 5 minutes on it JUST IN CASE...
155 inotifywait -qqt 300 ${vps_utmp}
158 lxc_log "$name watchdog exited"
173 cleanup_init_scripts $1
174 setup_inittab $1 ::power "p0::powerfail:/sbin/init 0"
175 setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
177 # give container 5 seconds to start more than one process
178 ( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
184 reload|force-reload|restart|reboot)
191 echo $1 > /var/lib/lxc/$1/on_boot
194 echo -n > /var/lib/lxc/$1/on_boot
197 echo "Usage: $0 {start|stop|restart|status}" >&2
208 test "$command" = "status" && lxc_status && exit
210 if [ -z "$1" ] ; then
211 ls /var/lib/lxc/*/on_boot | while read path ; do
212 name=`echo $path | cut -d/ -f5`
213 if [ "$command" != "start" -o "$command" = "start" -a -s $path ] ; then
214 command_on_lxc $command $name
216 echo "# skip $command $name"
220 while [ ! -z "$1" ] ; do
221 command_on_lxc $command $1