/[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 183 - (show annotations)
Tue Mar 16 19:59:41 2010 UTC (14 years, 1 month ago) by dpavlin
File MIME type: application/x-sh
File size: 3671 byte(s)
real host reboot, status now include rootfs and on_boot

1 #!/bin/sh
2
3 # lxc-watchdog.sh
4 #
5 # Dobrica Pavlinusic <dpavlin@rot13.org> 2010-03-15
6 #
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
10 #
11 # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
12
13
14 which inotifywait >/dev/null || apt-get install inotify-tools
15
16
17 lxc_exists() {
18 name=$1
19
20 if [ ! -e /var/lib/lxc/$name/config ] ; then
21 echo "Usage: $0 name"
22 lxc_status
23 exit 1
24 fi
25 }
26
27
28 lxc_rootfs() {
29 grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
30 }
31
32
33 lxc_status() {
34 lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do
35 on_boot=" "
36 test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"
37 echo "$name $status $on_boot $(lxc_rootfs $name)"
38 done
39 }
40
41
42 cleanup_init_scripts() {
43 rootfs=$(lxc_rootfs $1)
44
45 ls \
46 $rootfs/etc/rc?.d/*umountfs \
47 $rootfs/etc/rc?.d/*umountroot \
48 $rootfs/etc/rc?.d/*hwclock* \
49 2>/dev/null | xargs -i rm -v {}
50 }
51
52
53 setup_inittab() {
54 rootfs=$(lxc_rootfs $1)
55 remove=$2
56 add=$3
57
58 # let container respond to kill -SIGPWR
59 inittab=$rootfs/etc/inittab
60 if ! grep "$add" ${inittab} >/dev/null ; then
61 grep -v "$remove" ${inittab} > ${inittab}.new
62 echo $add >> ${inittab}.new
63 mv ${inittab}.new ${inittab}
64 echo "$inittab modified with $add"
65 fi
66 }
67
68
69 lxc_kill() {
70 name=$1
71 sig=$2
72
73 init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
74 if [ -z "$init_pid" ] ; then
75 lxc-info -n $name
76 exit 1
77 fi
78 echo "$name kill $sig $init_pid"
79 /bin/kill $sig $init_pid
80 }
81
82 lxc_stop() {
83 lxc_kill $name -SIGPWR
84 lxc-wait -n $name -s STOPPED
85 # rm -f /var/lib/lxc/${name}/on_boot
86 }
87
88
89 lxc_start() {
90 name=$1
91
92 if ! lxc-info -n $name | grep RUNNING ; then
93 echo "$name start"
94 lxc-start -n $name -o /tmp/${name}.log -d
95 lxc-wait -n $name -s RUNNING
96 lxc-info -n $name
97 echo $name > /var/lib/lxc/${name}/on_boot
98 fi
99 }
100
101 lxc_watchdog() {
102 name=$1
103 rootfs=$(lxc_rootfs $1)
104
105 while true; do
106 vps_utmp=${rootfs}/var/run/utmp
107 tasks=`wc -l < /cgroup/${name}/tasks`
108 test -z "$tasks" && exit 1
109 if [ "$tasks" -eq 1 ]; then
110
111 runlevel="$(runlevel ${vps_utmp})"
112 echo `date +%Y-%m-%dT%H:%M:%S` "$name runlevel $runlevel"
113
114 case $runlevel in
115 N*)
116 # nothing for new boot state
117 ;;
118 ??0)
119 echo "$name halt"
120 lxc-stop -n "${name}"
121 lxc-wait -n ${name} -s STOPPED
122 break
123 ;;
124 ??6)
125 echo "$name reboot";
126 lxc-stop -n ${name}
127 lxc-wait -n ${name} -s STOPPED
128 lxc-start -d -n ${name} -o /tmp/${name}.log
129 ;;
130 *)
131 # make sure vps is still running
132 state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
133 [ "$state" = "RUNNING" ] || break
134 ;;
135 esac
136 else
137 echo "# $name $tasks tasks"
138 fi
139
140 # time of 5 minutes on it JUST IN CASE...
141 inotifywait -qqt 300 ${vps_utmp}
142 done
143
144 echo "${name} exited"
145
146 }
147
148
149 command_on_lxc() {
150 command=$1
151 shift
152
153 echo "# $command $1"
154
155 case "$command" in
156
157 start)
158 lxc_exists $1
159 cleanup_init_scripts $1
160 setup_inittab $1 ::power "p0::powerfail:/sbin/init 0"
161 setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
162 lxc_start $1
163 ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
164 ;;
165 stop|halt)
166 lxc_exists $1
167 lxc_stop $1
168 ;;
169 reload|force-reload|restart|reboot)
170 lxc_kill $1 -SIGINT
171 ;;
172 watchdog)
173 lxc_watchdog $1
174 ;;
175 *)
176 echo "Usage: $0 {start|stop|restart|status}" >&2
177 exit 3
178 ;;
179
180 esac
181
182 }
183
184 command=$1
185 shift
186
187 test "$command" = "status" && lxc_status && exit
188
189 if [ -z "$1" ] ; then
190 ls /var/lib/lxc/*/on_boot | while read path ; do
191 name=`echo $path | cut -d/ -f5`
192 command_on_lxc $command $name
193 done
194 else
195 while [ ! -z "$1" ] ; do
196 command_on_lxc $command $1
197 shift
198 done
199 fi
200

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26