/[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 185 by dpavlin, Tue Mar 16 20:51:46 2010 UTC revision 189 by dpavlin, Tue Mar 16 23:05:31 2010 UTC
# Line 39  lxc_rootfs() { Line 39  lxc_rootfs() {
39    
40  lxc_status() {  lxc_status() {
41          lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do          lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do
42                  on_boot="       "                  boot="    "
43                  test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"                  test -s /var/lib/lxc/$name/on_boot && boot="boot"
44                  echo "$name $status $on_boot $(lxc_rootfs $name)"                  echo "$name $status $boot $(lxc_rootfs $name)"
45          done          done
46  }  }
47    
# Line 73  setup_inittab() { Line 73  setup_inittab() {
73  }  }
74    
75    
76    lxc_log() {
77            echo `date +%Y-%m-%dT%H:%M:%S` $*
78    }
79    
80    
81  lxc_kill() {  lxc_kill() {
82          name=$1          name=$1
83          sig=$2          sig=$2
# Line 82  lxc_kill() { Line 87  lxc_kill() {
87                  lxc-info -n $name                  lxc-info -n $name
88                  exit 1                  exit 1
89          fi          fi
90          echo "$name kill $sig $init_pid"          lxc_log "$name kill $sig $init_pid"
91          /bin/kill $sig $init_pid          /bin/kill $sig $init_pid
92  }  }
93    
94  lxc_stop() {  lxc_stop() {
95            lxc_log "$name stop"
96          lxc_kill $name -SIGPWR          lxc_kill $name -SIGPWR
97          lxc-wait -n $name -s STOPPED          lxc-wait -n $name -s STOPPED
98            lxc_log "$name stoped"
99  #       rm -f /var/lib/lxc/${name}/on_boot  #       rm -f /var/lib/lxc/${name}/on_boot
100  }  }
101    
# Line 97  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
# Line 116  while true; do Line 123  while true; do
123          if [ "$tasks" -eq 1 ]; then          if [ "$tasks" -eq 1 ]; then
124    
125                  runlevel="$(runlevel ${vps_utmp})"                  runlevel="$(runlevel ${vps_utmp})"
126                  echo `date +%Y-%m-%dT%H:%M:%S` "$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 141  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...          # time of 5 minutes on it JUST IN CASE...
155          inotifywait -qqt 300 ${vps_utmp}          inotifywait -qqt 300 ${vps_utmp}
156  done  done
157    
158  echo "${name} exited"  lxc_log "$name watchdog exited"
159    
160  }  }
161    
# Line 167  start) Line 174  start)
174          setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"          setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
175          setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"          setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
176          lxc_start $1          lxc_start $1
177          ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &          # give container 5 seconds to start more than one process
178            ( sleep 5 ; nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
179          ;;          ;;
180  stop|halt)  stop|halt)
181          lxc_exists $1          lxc_exists $1
# Line 179  reload|force-reload|restart|reboot) Line 187  reload|force-reload|restart|reboot)
187  watchdog)  watchdog)
188          lxc_watchdog $1          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
198          exit 3          exit 3
# Line 196  test "$command" = "status" && lxc_status Line 210  test "$command" = "status" && lxc_status
210  if [ -z "$1" ] ; then  if [ -z "$1" ] ; then
211          ls /var/lib/lxc/*/on_boot | while read path ; do          ls /var/lib/lxc/*/on_boot | while read path ; do
212                  name=`echo $path | cut -d/ -f5`                  name=`echo $path | cut -d/ -f5`
213                  command_on_lxc $command $name                  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          done
219  else  else
220          while [ ! -z "$1" ] ; do          while [ ! -z "$1" ] ; do

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

  ViewVC Help
Powered by ViewVC 1.1.26