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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 181 by dpavlin, Tue Mar 16 17:39:28 2010 UTC revision 189 by dpavlin, Tue Mar 16 23:05:31 2010 UTC
# Line 1  Line 1 
1  #!/bin/sh  #! /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  # lxc-watchdog.sh  # Author: Dobrica Pavlinusic <dpavlin@rot13.org>
 #  
 # Dobrica Pavlinusic <dpavlin@rot13.org> 2010-03-15  
 #  
 # this script can be used to start/stop Linux containers  
 # using clever inotify hack to monitor halt/reboot from  
 # Tony Risinger posted to lxc-users mailing list  
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  # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
19    
20    
21  which inotifywait >/dev/null || apt-get install inotify-tools  which inotifywait >/dev/null || apt-get install inotify-tools
22    
 lxc_status() {  
         lxc-ls -1 | sort -u | xargs -i lxc-info -n {}  
 }  
23    
24  lxc_exists() {  lxc_exists() {
25          name=$1          name=$1
# Line 33  lxc_rootfs() { Line 37  lxc_rootfs() {
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                    boot="    "
43                    test -s /var/lib/lxc/$name/on_boot && boot="boot"
44                    echo "$name $status $boot $(lxc_rootfs $name)"
45            done
46    }
47    
48    
49  cleanup_init_scripts() {  cleanup_init_scripts() {
50          rootfs=$(lxc_rootfs $1)          rootfs=$(lxc_rootfs $1)
51    
# Line 46  cleanup_init_scripts() { Line 59  cleanup_init_scripts() {
59    
60  setup_inittab() {  setup_inittab() {
61          rootfs=$(lxc_rootfs $1)          rootfs=$(lxc_rootfs $1)
62            remove=$2
63            add=$3
64    
65          # let container respond to kill -SIGPWR          # let container respond to kill -SIGPWR
66          inittab=$rootfs/etc/inittab          inittab=$rootfs/etc/inittab
67          powerfail="pw::powerfail:/sbin/init 0"          if ! grep "$add" ${inittab} >/dev/null ; then
68          if ! grep "$powerfail" ${inittab} >/dev/null ; then                  grep -v "$remove" ${inittab} > ${inittab}.new
69                  grep -v ::power ${inittab} > ${inittab}.new                  echo $add >> ${inittab}.new
                 echo $powerfail >> ${inittab}.new  
70                  mv ${inittab}.new ${inittab}                  mv ${inittab}.new ${inittab}
71                  echo "$initab modified"                  echo "$inittab modified with $add"
72          fi          fi
73    }
74    
75    
76    lxc_log() {
77            echo `date +%Y-%m-%dT%H:%M:%S` $*
78  }  }
79    
80    
81  lxc_stop() {  lxc_kill() {
82          name=$1          name=$1
83            sig=$2
84    
85          init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`          init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
86          if [ -z "$init_pid" ] ; then          if [ -z "$init_pid" ] ; then
87                  lxc-info -n $name                  lxc-info -n $name
88                  exit 1                  exit 1
89          fi          fi
90          echo "$name stop $init_pid"          lxc_log "$name kill $sig $init_pid"
91          /bin/kill -SIGPWR $init_pid          /bin/kill $sig $init_pid
92          lxc-wait -n $name -s STOPPED  }
93    
94    lxc_stop() {
95            lxc_log "$name stop"
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
100  }  }
101    
102    
# Line 79  lxc_start() { Line 104  lxc_start() {
104          name=$1          name=$1
105    
106          if ! lxc-info -n $name | grep RUNNING ; then          if ! lxc-info -n $name | grep RUNNING ; then
107                  echo "$name start"                  lxc_log "$name start"
108                  lxc-start -n $name -o /tmp/${name}.log -d                  lxc-start -n $name -o /tmp/${name}.log -d
109                  lxc-wait  -n $name -s RUNNING                  lxc-wait  -n $name -s RUNNING
110                  lxc-info  -n $name                  lxc-info  -n $name
111                    test -f /var/lib/lxc/${name}/on_boot || echo $name > /var/lib/lxc/${name}/on_boot
112          fi          fi
113  }  }
114    
# Line 91  name=$1 Line 117  name=$1
117  rootfs=$(lxc_rootfs $1)  rootfs=$(lxc_rootfs $1)
118    
119  while true; do  while true; do
         # time of 5 minutes on it JUST IN CASE...  
120          vps_utmp=${rootfs}/var/run/utmp          vps_utmp=${rootfs}/var/run/utmp
         inotifywait -qqt 300 ${vps_utmp}  
121          tasks=`wc -l < /cgroup/${name}/tasks`          tasks=`wc -l < /cgroup/${name}/tasks`
122          test -z "$tasks" && exit 1          test -z "$tasks" && exit 1
123          if [ "$tasks" -eq 1 ]; then          if [ "$tasks" -eq 1 ]; then
124    
125                  runlevel="$(runlevel ${vps_utmp})"                  runlevel="$(runlevel ${vps_utmp})"
126                  echo "# $name runlevel $runlevel"                  lxc_log "$name runlevel $runlevel"
127    
128                  case $runlevel in                  case $runlevel in
129                  N*)                  N*)
130                          # nothing for new boot state                          # nothing for new boot state
131                  ;;                  ;;
132                  ??0)                  ??0)
133                          echo "$name halt"                          lxc_log "$name halt"
134                          lxc-stop -n "${name}"                          lxc-stop -n "${name}"
135                          lxc-wait -n ${name} -s STOPPED                          lxc-wait -n ${name} -s STOPPED
136                          break                          break
137                  ;;                  ;;
138                  ??6)                  ??6)
139                          echo "$name reboot";                          lxc_log "$name reboot";
140                          lxc-stop -n ${name}                          lxc-stop -n ${name}
141                          lxc-wait -n ${name} -s STOPPED                          lxc-wait -n ${name} -s STOPPED
142                          lxc-start -d -n ${name} -o /tmp/${name}.log                          lxc-start -d -n ${name} -o /tmp/${name}.log
# Line 124  while true; do Line 148  while true; do
148                  ;;                  ;;
149                  esac                  esac
150          else          else
151                  echo "# $name $tasks tasks"                  lxc_log "$name $tasks tasks"
152          fi          fi
153    
154            # time of 5 minutes on it JUST IN CASE...
155            inotifywait -qqt 300 ${vps_utmp}
156  done  done
157    
158  echo "${name} exited"  lxc_log "$name watchdog exited"
159    
160  }  }
161    
162    
163  case "$1" in  command_on_lxc() {
164    command=$1
165    shift
166    
167    echo "# $command $1"
168    
169    case "$command" in
170    
171  start)  start)
172          lxc_exists $2          lxc_exists $1
173          cleanup_init_scripts $2          cleanup_init_scripts $1
174          setup_inittab $2          setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
175          lxc_start $2          setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
176          ( nohup $0 watchdog $2 >> /tmp/$2.log ) &          lxc_start $1
177          ;;          # give container 5 seconds to start more than one process
178  stop)          ( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
179          lxc_exists $2          ;;
180          lxc_stop $2  stop|halt)
181          ;;          lxc_exists $1
182  status)          lxc_stop $1
183          lxc_status          ;;
184          ;;  reload|force-reload|restart|reboot)
185  reload|force-reload|restart)          lxc_kill $1 -SIGINT
         lxc_stop $2  
         lxc_start $2  
186          ;;          ;;
187  watchdog)  watchdog)
188          lxc_watchdog $2          lxc_watchdog $1
189            ;;
190    boot)
191            echo $1 > /var/lib/lxc/$1/on_boot
192            ;;
193    disable)
194            echo -n > /var/lib/lxc/$1/on_boot
195          ;;          ;;
196  *)  *)
197          echo "Usage: $0 {start|stop|restart|status}" >&2          echo "Usage: $0 {start|stop|restart|status}" >&2
# Line 163  watchdog) Line 200  watchdog)
200    
201  esac  esac
202    
203    }
204    
205    command=$1
206    shift
207    
208    test "$command" = "status" && lxc_status && exit
209    
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
215                    else
216                            echo "# skip $command $name"
217                    fi
218            done
219    else
220            while [ ! -z "$1" ] ; do
221                    command_on_lxc $command $1
222                    shift
223            done
224    fi
225    

Legend:
Removed from v.181  
changed lines
  Added in v.189

  ViewVC Help
Powered by ViewVC 1.1.26