/[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 180 by dpavlin, Tue Mar 16 16:18:40 2010 UTC revision 187 by dpavlin, Tue Mar 16 21:36:45 2010 UTC
# Line 1  Line 1 
1  #!/bin/bash  #! /bin/sh
2    ### BEGIN INIT INFO
3  # based on Tony Risinger code from lxc-users  # 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  # 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    
 name=$1  
23    
24  if [ ! -e /var/lib/lxc/$name/config ] ; then  lxc_exists() {
25          echo "Usage: $0 name"          name=$1
         ls /var/lib/lxc/*/config | cut -d/ -f5  
         exit 1  
 fi  
26    
27  rootfs=`grep lxc.rootfs /var/lib/lxc/$name/config | cut -d= -f2`          if [ ! -e /var/lib/lxc/$name/config ] ; then
28  echo "$name rootfs $rootfs"                  echo "Usage: $0 name"
29                    lxc_status
30                    exit 1
31            fi
32    }
33    
34    
35  # cleanup init scripts which don't work in containers  lxc_rootfs() {
36  ls \          grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
37          $rootfs/etc/rc?.d/*umountfs \  }
38          $rootfs/etc/rc?.d/*umountroot \  
39          $rootfs/etc/rc?.d/*hwclock* \  
40  2>/dev/null | xargs -i rm -v {}  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  # let container respond to kill -SIGPWR                  test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"
44  inittab=$rootfs/etc/inittab                  echo "$name $status $on_boot $(lxc_rootfs $name)"
45  powerfail="pw::powerfail:/sbin/init 0"          done
46  if ! grep "$powerfail" ${inittab} >/dev/null ; then  }
47          grep -v ::power ${inittab} > ${inittab}.new  
48          echo $powerfail >> ${inittab}.new  
49          mv ${inittab}.new ${inittab}  cleanup_init_scripts() {
50          echo "$initab modified"          rootfs=$(lxc_rootfs $1)
51  fi  
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  if [ "$2" == "stop" ] ; then  lxc_kill() {
77          echo "$name stop"          name=$1
78          kill -SIGPWR `lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`          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          lxc-wait -n $name -s STOPPED
92          exit  #       rm -f /var/lib/lxc/${name}/on_boot
93  fi  }
94    
95    
96  if ! lxc-info -n $name | grep RUNNING ; then  lxc_start() {
97          echo "$name start"          name=$1
98          lxc-start -n $name -o /tmp/${name}.log -d  
99          lxc-wait  -n $name -s RUNNING          if ! lxc-info -n $name | grep RUNNING ; then
100          lxc-info  -n $name                  echo "$name start"
101  fi                  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_log() {
109            echo `date +%Y-%m-%dT%H:%M:%S` $*
110    }
111    
112    lxc_watchdog() {
113    name=$1
114    rootfs=$(lxc_rootfs $1)
115    
116  while true; do  while true; do
         # time of 5 minutes on it JUST IN CASE...  
117          vps_utmp=${rootfs}/var/run/utmp          vps_utmp=${rootfs}/var/run/utmp
118          inotifywait -qqt 300 ${vps_utmp}          tasks=`wc -l < /cgroup/${name}/tasks`
119          if [ $(wc -l < /cgroup/${name}/tasks) -eq 1 ]; then          test -z "$tasks" && exit 1
120            if [ "$tasks" -eq 1 ]; then
121    
122                  runlevel="$(runlevel ${vps_utmp})"                  runlevel="$(runlevel ${vps_utmp})"
123                  echo "# $name runlevel $runlevel"                  lxc_log "$name runlevel $runlevel"
124    
125                  case $runlevel in                  case $runlevel in
126                  N*)                  N*)
127                          # nothing for new boot state                          # nothing for new boot state
128                  ;;                  ;;
129                  ??0)                  ??0)
130                          echo "$name halt"                          lxc_log "$name halt"
131                          lxc-stop -n "${name}"                          lxc-stop -n "${name}"
132                            lxc-wait -n ${name} -s STOPPED
133                          break                          break
134                  ;;                  ;;
135                  ??6)                  ??6)
136                          echo "$name reboot";                          lxc_log "$name reboot";
137                          lxc-stop -n ${name}                          lxc-stop -n ${name}
138                          lxc-wait -n ${name} -s STOPPED                          lxc-wait -n ${name} -s STOPPED
                         mount /mnt/llin -o remount,rw  
139                          lxc-start -d -n ${name} -o /tmp/${name}.log                          lxc-start -d -n ${name} -o /tmp/${name}.log
140                  ;;                  ;;
141                  *)                  *)
# Line 82  while true; do Line 144  while true; do
144                          [ "$state" = "RUNNING" ] || break                          [ "$state" = "RUNNING" ] || break
145                  ;;                  ;;
146                  esac                  esac
147            else
148                    lxc_log "$name $tasks tasks"
149          fi          fi
150    
151            # time of 5 minutes on it JUST IN CASE...
152            inotifywait -qqt 300 ${vps_utmp}
153  done  done
154    
155    lxc_log "$name exited"
156    
157    }
158    
159    
160    command_on_lxc() {
161    command=$1
162    shift
163    
164    echo "# $command $1"
165    
166    case "$command" in
167    
168    start)
169            lxc_exists $1
170            cleanup_init_scripts $1
171            setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
172            setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
173            lxc_start $1
174            # give container 5 seconds to start more than one process
175            ( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
176            ;;
177    stop|halt)
178            lxc_exists $1
179            lxc_stop $1
180            ;;
181    reload|force-reload|restart|reboot)
182            lxc_kill $1 -SIGINT
183            ;;
184    watchdog)
185            lxc_watchdog $1
186            ;;
187    *)
188            echo "Usage: $0 {start|stop|restart|status}" >&2
189            exit 3
190            ;;
191    
192    esac
193    
194    }
195    
196    command=$1
197    shift
198    
199    test "$command" = "status" && lxc_status && exit
200    
201    if [ -z "$1" ] ; then
202            ls /var/lib/lxc/*/on_boot | while read path ; do
203                    name=`echo $path | cut -d/ -f5`
204                    command_on_lxc $command $name
205            done
206    else
207            while [ ! -z "$1" ] ; do
208                    command_on_lxc $command $1
209                    shift
210            done
211    fi
212    

Legend:
Removed from v.180  
changed lines
  Added in v.187

  ViewVC Help
Powered by ViewVC 1.1.26