/[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 181 - (hide annotations)
Tue Mar 16 17:39:28 2010 UTC (14 years, 1 month ago) by dpavlin
File MIME type: application/x-sh
File size: 2896 byte(s)
rewrite into init-like script with start/stop/status name

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 dpavlin 181 lxc_status() {
17     lxc-ls -1 | sort -u | xargs -i lxc-info -n {}
18     }
19 root 175
20 dpavlin 181 lxc_exists() {
21     name=$1
22 root 175
23 dpavlin 181 if [ ! -e /var/lib/lxc/$name/config ] ; then
24     echo "Usage: $0 name"
25     lxc_status
26     exit 1
27     fi
28     }
29 root 175
30 dpavlin 179
31 dpavlin 181 lxc_rootfs() {
32     grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
33     }
34 dpavlin 178
35 root 175
36 dpavlin 181 cleanup_init_scripts() {
37     rootfs=$(lxc_rootfs $1)
38 dpavlin 179
39 dpavlin 181 ls \
40     $rootfs/etc/rc?.d/*umountfs \
41     $rootfs/etc/rc?.d/*umountroot \
42     $rootfs/etc/rc?.d/*hwclock* \
43     2>/dev/null | xargs -i rm -v {}
44     }
45 dpavlin 179
46 dpavlin 181
47     setup_inittab() {
48     rootfs=$(lxc_rootfs $1)
49    
50     # let container respond to kill -SIGPWR
51     inittab=$rootfs/etc/inittab
52     powerfail="pw::powerfail:/sbin/init 0"
53     if ! grep "$powerfail" ${inittab} >/dev/null ; then
54     grep -v ::power ${inittab} > ${inittab}.new
55     echo $powerfail >> ${inittab}.new
56     mv ${inittab}.new ${inittab}
57     echo "$initab modified"
58     fi
59    
60     }
61    
62    
63     lxc_stop() {
64     name=$1
65    
66     init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
67     if [ -z "$init_pid" ] ; then
68     lxc-info -n $name
69     exit 1
70     fi
71     echo "$name stop $init_pid"
72     /bin/kill -SIGPWR $init_pid
73 dpavlin 179 lxc-wait -n $name -s STOPPED
74    
75 dpavlin 181 }
76 dpavlin 179
77    
78 dpavlin 181 lxc_start() {
79     name=$1
80    
81     if ! lxc-info -n $name | grep RUNNING ; then
82     echo "$name start"
83     lxc-start -n $name -o /tmp/${name}.log -d
84     lxc-wait -n $name -s RUNNING
85     lxc-info -n $name
86     fi
87     }
88    
89     lxc_watchdog() {
90     name=$1
91     rootfs=$(lxc_rootfs $1)
92    
93 root 175 while true; do
94     # time of 5 minutes on it JUST IN CASE...
95     vps_utmp=${rootfs}/var/run/utmp
96     inotifywait -qqt 300 ${vps_utmp}
97 dpavlin 181 tasks=`wc -l < /cgroup/${name}/tasks`
98     test -z "$tasks" && exit 1
99     if [ "$tasks" -eq 1 ]; then
100 root 175
101     runlevel="$(runlevel ${vps_utmp})"
102     echo "# $name runlevel $runlevel"
103    
104     case $runlevel in
105     N*)
106     # nothing for new boot state
107     ;;
108     ??0)
109     echo "$name halt"
110     lxc-stop -n "${name}"
111 dpavlin 181 lxc-wait -n ${name} -s STOPPED
112 root 175 break
113     ;;
114     ??6)
115     echo "$name reboot";
116     lxc-stop -n ${name}
117     lxc-wait -n ${name} -s STOPPED
118     lxc-start -d -n ${name} -o /tmp/${name}.log
119     ;;
120     *)
121     # make sure vps is still running
122     state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
123     [ "$state" = "RUNNING" ] || break
124     ;;
125     esac
126 dpavlin 181 else
127     echo "# $name $tasks tasks"
128 root 175 fi
129     done
130    
131 dpavlin 181 echo "${name} exited"
132    
133     }
134    
135    
136     case "$1" in
137    
138     start)
139     lxc_exists $2
140     cleanup_init_scripts $2
141     setup_inittab $2
142     lxc_start $2
143     ( nohup $0 watchdog $2 >> /tmp/$2.log ) &
144     ;;
145     stop)
146     lxc_exists $2
147     lxc_stop $2
148     ;;
149     status)
150     lxc_status
151     ;;
152     reload|force-reload|restart)
153     lxc_stop $2
154     lxc_start $2
155     ;;
156     watchdog)
157     lxc_watchdog $2
158     ;;
159     *)
160     echo "Usage: $0 {start|stop|restart|status}" >&2
161     exit 3
162     ;;
163    
164     esac
165    

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26