/[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 184 by dpavlin, Tue Mar 16 20:42:38 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                    on_boot="       "
43                    test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"
44                    echo "$name $status $on_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_stop() {  lxc_kill() {
77          name=$1          name=$1
78            sig=$2
79    
80          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-`
81          if [ -z "$init_pid" ] ; then          if [ -z "$init_pid" ] ; then
82                  lxc-info -n $name                  lxc-info -n $name
83                  exit 1                  exit 1
84          fi          fi
85          echo "$name stop $init_pid"          echo "$name kill $sig $init_pid"
86          /bin/kill -SIGPWR $init_pid          /bin/kill $sig $init_pid
87          lxc-wait -n $name -s STOPPED  }
88    
89    lxc_stop() {
90            lxc_kill $name -SIGPWR
91            lxc-wait -n $name -s STOPPED
92    #       rm -f /var/lib/lxc/${name}/on_boot
93  }  }
94    
95    
# Line 83  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
105          fi          fi
106  }  }
107    
# Line 91  name=$1 Line 110  name=$1
110  rootfs=$(lxc_rootfs $1)  rootfs=$(lxc_rootfs $1)
111    
112  while true; do  while true; do
         # time of 5 minutes on it JUST IN CASE...  
113          vps_utmp=${rootfs}/var/run/utmp          vps_utmp=${rootfs}/var/run/utmp
         inotifywait -qqt 300 ${vps_utmp}  
114          tasks=`wc -l < /cgroup/${name}/tasks`          tasks=`wc -l < /cgroup/${name}/tasks`
115          test -z "$tasks" && exit 1          test -z "$tasks" && exit 1
116          if [ "$tasks" -eq 1 ]; then          if [ "$tasks" -eq 1 ]; then
117    
118                  runlevel="$(runlevel ${vps_utmp})"                  runlevel="$(runlevel ${vps_utmp})"
119                  echo "# $name runlevel $runlevel"                  echo `date +%Y-%m-%dT%H:%M:%S` "$name runlevel $runlevel"
120    
121                  case $runlevel in                  case $runlevel in
122                  N*)                  N*)
# Line 126  while true; do Line 143  while true; do
143          else          else
144                  echo "# $name $tasks tasks"                  echo "# $name $tasks tasks"
145          fi          fi
146    
147            # time of 5 minutes on it JUST IN CASE...
148            inotifywait -qqt 300 ${vps_utmp}
149  done  done
150    
151  echo "${name} exited"  echo "${name} exited"
# Line 133  echo "${name} exited" Line 153  echo "${name} exited"
153  }  }
154    
155    
156  case "$1" in  command_on_lxc() {
157    command=$1
158    shift
159    
160    echo "# $command $1"
161    
162    case "$command" in
163    
164  start)  start)
165          lxc_exists $2          lxc_exists $1
166          cleanup_init_scripts $2          cleanup_init_scripts $1
167          setup_inittab $2          setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
168          lxc_start $2          setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
169          ( nohup $0 watchdog $2 >> /tmp/$2.log ) &          lxc_start $1
170            ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
171            ;;
172    stop|halt)
173            lxc_exists $1
174            lxc_stop $1
175          ;;          ;;
176  stop)  reload|force-reload|restart|reboot)
177          lxc_exists $2          lxc_kill $1 -SIGINT
         lxc_stop $2  
         ;;  
 status)  
         lxc_status  
         ;;  
 reload|force-reload|restart)  
         lxc_stop $2  
         lxc_start $2  
178          ;;          ;;
179  watchdog)  watchdog)
180          lxc_watchdog $2          lxc_watchdog $1
181          ;;          ;;
182  *)  *)
183          echo "Usage: $0 {start|stop|restart|status}" >&2          echo "Usage: $0 {start|stop|restart|status}" >&2
# Line 163  watchdog) Line 186  watchdog)
186    
187  esac  esac
188    
189    }
190    
191    command=$1
192    shift
193    
194    test "$command" = "status" && lxc_status && exit
195    
196    if [ -z "$1" ] ; then
197            ls /var/lib/lxc/*/on_boot | while read path ; do
198                    name=`echo $path | cut -d/ -f5`
199                    command_on_lxc $command $name
200            done
201    else
202            while [ ! -z "$1" ] ; do
203                    command_on_lxc $command $1
204                    shift
205            done
206    fi
207    

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

  ViewVC Help
Powered by ViewVC 1.1.26