/[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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 183 - (hide 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 dpavlin 181 #!/bin/sh
2 root 175
3 dpavlin 181 # 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 root 175 # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
12    
13 dpavlin 181
14 dpavlin 177 which inotifywait >/dev/null || apt-get install inotify-tools
15 root 175
16    
17 dpavlin 181 lxc_exists() {
18     name=$1
19 root 175
20 dpavlin 181 if [ ! -e /var/lib/lxc/$name/config ] ; then
21     echo "Usage: $0 name"
22     lxc_status
23     exit 1
24     fi
25     }
26 root 175
27 dpavlin 179
28 dpavlin 181 lxc_rootfs() {
29     grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
30     }
31 dpavlin 178
32 root 175
33 dpavlin 183 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 dpavlin 181 cleanup_init_scripts() {
43     rootfs=$(lxc_rootfs $1)
44 dpavlin 179
45 dpavlin 181 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 dpavlin 179
52 dpavlin 181
53     setup_inittab() {
54     rootfs=$(lxc_rootfs $1)
55 dpavlin 183 remove=$2
56     add=$3
57 dpavlin 181
58     # let container respond to kill -SIGPWR
59     inittab=$rootfs/etc/inittab
60 dpavlin 183 if ! grep "$add" ${inittab} >/dev/null ; then
61     grep -v "$remove" ${inittab} > ${inittab}.new
62     echo $add >> ${inittab}.new
63 dpavlin 181 mv ${inittab}.new ${inittab}
64 dpavlin 183 echo "$inittab modified with $add"
65 dpavlin 181 fi
66     }
67    
68    
69 dpavlin 183 lxc_kill() {
70 dpavlin 181 name=$1
71 dpavlin 183 sig=$2
72 dpavlin 181
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 dpavlin 183 echo "$name kill $sig $init_pid"
79     /bin/kill $sig $init_pid
80     }
81    
82     lxc_stop() {
83     lxc_kill $name -SIGPWR
84 dpavlin 179 lxc-wait -n $name -s STOPPED
85 dpavlin 183 # rm -f /var/lib/lxc/${name}/on_boot
86 dpavlin 181 }
87 dpavlin 179
88    
89 dpavlin 181 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 dpavlin 183 echo $name > /var/lib/lxc/${name}/on_boot
98 dpavlin 181 fi
99     }
100    
101     lxc_watchdog() {
102     name=$1
103     rootfs=$(lxc_rootfs $1)
104    
105 root 175 while true; do
106     vps_utmp=${rootfs}/var/run/utmp
107 dpavlin 181 tasks=`wc -l < /cgroup/${name}/tasks`
108     test -z "$tasks" && exit 1
109     if [ "$tasks" -eq 1 ]; then
110 root 175
111     runlevel="$(runlevel ${vps_utmp})"
112 dpavlin 183 echo `date +%Y-%m-%dT%H:%M:%S` "$name runlevel $runlevel"
113 root 175
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 dpavlin 181 lxc-wait -n ${name} -s STOPPED
122 root 175 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 dpavlin 181 else
137     echo "# $name $tasks tasks"
138 root 175 fi
139 dpavlin 182
140     # time of 5 minutes on it JUST IN CASE...
141     inotifywait -qqt 300 ${vps_utmp}
142 root 175 done
143    
144 dpavlin 181 echo "${name} exited"
145    
146     }
147    
148    
149 dpavlin 183 command_on_lxc() {
150     command=$1
151     shift
152 dpavlin 181
153 dpavlin 183 echo "# $command $1"
154    
155     case "$command" in
156    
157 dpavlin 181 start)
158 dpavlin 183 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 dpavlin 181 ;;
165 dpavlin 183 stop|halt)
166     lxc_exists $1
167     lxc_stop $1
168 dpavlin 181 ;;
169 dpavlin 183 reload|force-reload|restart|reboot)
170     lxc_kill $1 -SIGINT
171 dpavlin 181 ;;
172     watchdog)
173 dpavlin 183 lxc_watchdog $1
174 dpavlin 181 ;;
175     *)
176     echo "Usage: $0 {start|stop|restart|status}" >&2
177     exit 3
178     ;;
179    
180     esac
181    
182 dpavlin 183 }
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