/[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 182 by dpavlin, Tue Mar 16 17:46:32 2010 UTC revision 183 by dpavlin, Tue Mar 16 19:59:41 2010 UTC
# Line 13  Line 13 
13    
14  which inotifywait >/dev/null || apt-get install inotify-tools  which inotifywait >/dev/null || apt-get install inotify-tools
15    
 lxc_status() {  
         lxc-ls -1 | sort -u | xargs -i lxc-info -n {}  
 }  
16    
17  lxc_exists() {  lxc_exists() {
18          name=$1          name=$1
# Line 33  lxc_rootfs() { Line 30  lxc_rootfs() {
30  }  }
31    
32    
33    lxc_status() {
34            lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do
35                    on_boot="       "
36                    test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"
37                    echo "$name $status $on_boot $(lxc_rootfs $name)"
38            done
39    }
40    
41    
42  cleanup_init_scripts() {  cleanup_init_scripts() {
43          rootfs=$(lxc_rootfs $1)          rootfs=$(lxc_rootfs $1)
44    
# Line 46  cleanup_init_scripts() { Line 52  cleanup_init_scripts() {
52    
53  setup_inittab() {  setup_inittab() {
54          rootfs=$(lxc_rootfs $1)          rootfs=$(lxc_rootfs $1)
55            remove=$2
56            add=$3
57    
58          # let container respond to kill -SIGPWR          # let container respond to kill -SIGPWR
59          inittab=$rootfs/etc/inittab          inittab=$rootfs/etc/inittab
60          powerfail="pw::powerfail:/sbin/init 0"          if ! grep "$add" ${inittab} >/dev/null ; then
61          if ! grep "$powerfail" ${inittab} >/dev/null ; then                  grep -v "$remove" ${inittab} > ${inittab}.new
62                  grep -v ::power ${inittab} > ${inittab}.new                  echo $add >> ${inittab}.new
                 echo $powerfail >> ${inittab}.new  
63                  mv ${inittab}.new ${inittab}                  mv ${inittab}.new ${inittab}
64                  echo "$initab modified"                  echo "$inittab modified with $add"
65          fi          fi
   
66  }  }
67    
68    
69  lxc_stop() {  lxc_kill() {
70          name=$1          name=$1
71            sig=$2
72    
73          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-`
74          if [ -z "$init_pid" ] ; then          if [ -z "$init_pid" ] ; then
75                  lxc-info -n $name                  lxc-info -n $name
76                  exit 1                  exit 1
77          fi          fi
78          echo "$name stop $init_pid"          echo "$name kill $sig $init_pid"
79          /bin/kill -SIGPWR $init_pid          /bin/kill $sig $init_pid
80          lxc-wait -n $name -s STOPPED  }
81    
82    lxc_stop() {
83            lxc_kill $name -SIGPWR
84            lxc-wait -n $name -s STOPPED
85    #       rm -f /var/lib/lxc/${name}/on_boot
86  }  }
87    
88    
# Line 83  lxc_start() { Line 94  lxc_start() {
94                  lxc-start -n $name -o /tmp/${name}.log -d                  lxc-start -n $name -o /tmp/${name}.log -d
95                  lxc-wait  -n $name -s RUNNING                  lxc-wait  -n $name -s RUNNING
96                  lxc-info  -n $name                  lxc-info  -n $name
97                    echo $name > /var/lib/lxc/${name}/on_boot
98          fi          fi
99  }  }
100    
# Line 97  while true; do Line 109  while true; do
109          if [ "$tasks" -eq 1 ]; then          if [ "$tasks" -eq 1 ]; then
110    
111                  runlevel="$(runlevel ${vps_utmp})"                  runlevel="$(runlevel ${vps_utmp})"
112                  echo "# $name runlevel $runlevel"                  echo `date +%Y-%m-%dT%H:%M:%S` "$name runlevel $runlevel"
113    
114                  case $runlevel in                  case $runlevel in
115                  N*)                  N*)
# Line 134  echo "${name} exited" Line 146  echo "${name} exited"
146  }  }
147    
148    
149  case "$1" in  command_on_lxc() {
150    command=$1
151    shift
152    
153    echo "# $command $1"
154    
155    case "$command" in
156    
157  start)  start)
158          lxc_exists $2          lxc_exists $1
159          cleanup_init_scripts $2          cleanup_init_scripts $1
160          setup_inittab $2          setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
161          lxc_start $2          setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
162          ( nohup $0 watchdog $2 >> /tmp/$2.log ) &          lxc_start $1
163          ;;          ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
164  stop)          ;;
165          lxc_exists $2  stop|halt)
166          lxc_stop $2          lxc_exists $1
167          ;;          lxc_stop $1
 status)  
         lxc_status  
168          ;;          ;;
169  reload|force-reload|restart)  reload|force-reload|restart|reboot)
170          lxc_stop $2          lxc_kill $1 -SIGINT
         lxc_start $2  
171          ;;          ;;
172  watchdog)  watchdog)
173          lxc_watchdog $2          lxc_watchdog $1
174          ;;          ;;
175  *)  *)
176          echo "Usage: $0 {start|stop|restart|status}" >&2          echo "Usage: $0 {start|stop|restart|status}" >&2
# Line 164  watchdog) Line 179  watchdog)
179    
180  esac  esac
181    
182    }
183    
184    command=$1
185    shift
186    
187    test "$command" = "status" && lxc_status && exit
188    
189    if [ -z "$1" ] ; then
190            ls /var/lib/lxc/*/on_boot | while read path ; do
191                    name=`echo $path | cut -d/ -f5`
192                    command_on_lxc $command $name
193            done
194    else
195            while [ ! -z "$1" ] ; do
196                    command_on_lxc $command $1
197                    shift
198            done
199    fi
200    

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

  ViewVC Help
Powered by ViewVC 1.1.26