/[sysadmin-cookbook]/recepies/lxc/lxc-watchdog.sh
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Contents of /recepies/lxc/lxc-watchdog.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 186 - (show annotations)
Tue Mar 16 20:53:59 2010 UTC (14 years, 1 month ago) by dpavlin
File MIME type: application/x-sh
File size: 4117 byte(s)
give container 5 seconds to start more than one process
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides: lxc-watchdog
4 # Required-Start: $remote_fs $named $network $time
5 # Required-Stop: $remote_fs $named $network
6 # Required-Start:
7 # Required-Stop:
8 # Default-Start: 2 3 4 5
9 # Default-Stop: 0 1 6
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
13 ### END INIT INFO
14
15 # Author: Dobrica Pavlinusic <dpavlin@rot13.org>
16 #
17 # based on Tony Risinger post to lxc-users mailing list
18 # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
19
20
21 which inotifywait >/dev/null || apt-get install inotify-tools
22
23
24 lxc_exists() {
25 name=$1
26
27 if [ ! -e /var/lib/lxc/$name/config ] ; then
28 echo "Usage: $0 name"
29 lxc_status
30 exit 1
31 fi
32 }
33
34
35 lxc_rootfs() {
36 grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
37 }
38
39
40 lxc_status() {
41 lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do
42 on_boot=" "
43 test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"
44 echo "$name $status $on_boot $(lxc_rootfs $name)"
45 done
46 }
47
48
49 cleanup_init_scripts() {
50 rootfs=$(lxc_rootfs $1)
51
52 ls \
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 {}
57 }
58
59
60 setup_inittab() {
61 rootfs=$(lxc_rootfs $1)
62 remove=$2
63 add=$3
64
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"
72 fi
73 }
74
75
76 lxc_kill() {
77 name=$1
78 sig=$2
79
80 init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
81 if [ -z "$init_pid" ] ; then
82 lxc-info -n $name
83 exit 1
84 fi
85 echo "$name kill $sig $init_pid"
86 /bin/kill $sig $init_pid
87 }
88
89 lxc_stop() {
90 lxc_kill $name -SIGPWR
91 lxc-wait -n $name -s STOPPED
92 # rm -f /var/lib/lxc/${name}/on_boot
93 }
94
95
96 lxc_start() {
97 name=$1
98
99 if ! lxc-info -n $name | grep RUNNING ; then
100 echo "$name start"
101 lxc-start -n $name -o /tmp/${name}.log -d
102 lxc-wait -n $name -s RUNNING
103 lxc-info -n $name
104 test -f /var/lib/lxc/${name}/on_boot || echo $name > /var/lib/lxc/${name}/on_boot
105 fi
106 }
107
108 lxc_watchdog() {
109 name=$1
110 rootfs=$(lxc_rootfs $1)
111
112 while true; do
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
117
118 runlevel="$(runlevel ${vps_utmp})"
119 echo `date +%Y-%m-%dT%H:%M:%S` "$name runlevel $runlevel"
120
121 case $runlevel in
122 N*)
123 # nothing for new boot state
124 ;;
125 ??0)
126 echo "$name halt"
127 lxc-stop -n "${name}"
128 lxc-wait -n ${name} -s STOPPED
129 break
130 ;;
131 ??6)
132 echo "$name reboot";
133 lxc-stop -n ${name}
134 lxc-wait -n ${name} -s STOPPED
135 lxc-start -d -n ${name} -o /tmp/${name}.log
136 ;;
137 *)
138 # make sure vps is still running
139 state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
140 [ "$state" = "RUNNING" ] || break
141 ;;
142 esac
143 else
144 echo "# $name $tasks tasks"
145 fi
146
147 # time of 5 minutes on it JUST IN CASE...
148 inotifywait -qqt 300 ${vps_utmp}
149 done
150
151 echo "${name} exited"
152
153 }
154
155
156 command_on_lxc() {
157 command=$1
158 shift
159
160 echo "# $command $1"
161
162 case "$command" in
163
164 start)
165 lxc_exists $1
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"
169 lxc_start $1
170 # give container 5 seconds to start more than one process
171 ( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
172 ;;
173 stop|halt)
174 lxc_exists $1
175 lxc_stop $1
176 ;;
177 reload|force-reload|restart|reboot)
178 lxc_kill $1 -SIGINT
179 ;;
180 watchdog)
181 lxc_watchdog $1
182 ;;
183 *)
184 echo "Usage: $0 {start|stop|restart|status}" >&2
185 exit 3
186 ;;
187
188 esac
189
190 }
191
192 command=$1
193 shift
194
195 test "$command" = "status" && lxc_status && exit
196
197 if [ -z "$1" ] ; then
198 ls /var/lib/lxc/*/on_boot | while read path ; do
199 name=`echo $path | cut -d/ -f5`
200 command_on_lxc $command $name
201 done
202 else
203 while [ ! -z "$1" ] ; do
204 command_on_lxc $command $1
205 shift
206 done
207 fi
208

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26