/[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 183 by dpavlin, Tue Mar 16 19:59:41 2010 UTC revision 187 by dpavlin, Tue Mar 16 21:36:45 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    
# Line 94  lxc_start() { Line 101  lxc_start() {
101                  lxc-start -n $name -o /tmp/${name}.log -d                  lxc-start -n $name -o /tmp/${name}.log -d
102                  lxc-wait  -n $name -s RUNNING                  lxc-wait  -n $name -s RUNNING
103                  lxc-info  -n $name                  lxc-info  -n $name
104                  echo $name > /var/lib/lxc/${name}/on_boot                  test -f /var/lib/lxc/${name}/on_boot || echo $name > /var/lib/lxc/${name}/on_boot
105          fi          fi
106  }  }
107    
108    lxc_log() {
109            echo `date +%Y-%m-%dT%H:%M:%S` $*
110    }
111    
112  lxc_watchdog() {  lxc_watchdog() {
113  name=$1  name=$1
114  rootfs=$(lxc_rootfs $1)  rootfs=$(lxc_rootfs $1)
# Line 109  while true; do Line 120  while true; do
120          if [ "$tasks" -eq 1 ]; then          if [ "$tasks" -eq 1 ]; then
121    
122                  runlevel="$(runlevel ${vps_utmp})"                  runlevel="$(runlevel ${vps_utmp})"
123                  echo `date +%Y-%m-%dT%H:%M:%S` "$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                          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
139                          lxc-start -d -n ${name} -o /tmp/${name}.log                          lxc-start -d -n ${name} -o /tmp/${name}.log
# Line 134  while true; do Line 145  while true; do
145                  ;;                  ;;
146                  esac                  esac
147          else          else
148                  echo "# $name $tasks tasks"                  lxc_log "$name $tasks tasks"
149          fi          fi
150    
151          # time of 5 minutes on it JUST IN CASE...          # time of 5 minutes on it JUST IN CASE...
152          inotifywait -qqt 300 ${vps_utmp}          inotifywait -qqt 300 ${vps_utmp}
153  done  done
154    
155  echo "${name} exited"  lxc_log "$name exited"
156    
157  }  }
158    
# Line 160  start) Line 171  start)
171          setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"          setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
172          setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"          setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
173          lxc_start $1          lxc_start $1
174          ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &          # 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)  stop|halt)
178          lxc_exists $1          lxc_exists $1

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

  ViewVC Help
Powered by ViewVC 1.1.26