/[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 178 by dpavlin, Tue Mar 16 15:06:30 2010 UTC revision 183 by dpavlin, Tue Mar 16 19:59:41 2010 UTC
# Line 1  Line 1 
1  #!/bin/bash  #!/bin/sh
2    
3  # based on Tony Risinger code from lxc-users  # lxc-watchdog.sh
4    #
5    # Dobrica Pavlinusic <dpavlin@rot13.org> 2010-03-15
6    #
7    # this script can be used to start/stop Linux containers
8    # using clever inotify hack to monitor halt/reboot from
9    # Tony Risinger posted to lxc-users mailing list
10    #
11  # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html  # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
12    
13    
14  which inotifywait >/dev/null || apt-get install inotify-tools  which inotifywait >/dev/null || apt-get install inotify-tools
15    
 name=$1  
16    
17  if [ ! -e /var/lib/lxc/$name/config ] ; then  lxc_exists() {
18          echo "Usage: $0 name"          name=$1
19          ls /var/lib/lxc/*/config | cut -d/ -f5  
20          exit 1          if [ ! -e /var/lib/lxc/$name/config ] ; then
21  fi                  echo "Usage: $0 name"
22                    lxc_status
23                    exit 1
24            fi
25    }
26    
27    
28  rootfs=`grep lxc.rootfs /var/lib/lxc/$name/config | cut -d= -f2`  lxc_rootfs() {
29  echo "$name rootfs $rootfs"          grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
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() {
43            rootfs=$(lxc_rootfs $1)
44    
45            ls \
46                    $rootfs/etc/rc?.d/*umountfs \
47                    $rootfs/etc/rc?.d/*umountroot \
48                    $rootfs/etc/rc?.d/*hwclock* \
49            2>/dev/null | xargs -i rm -v {}
50    }
51    
52    
53    setup_inittab() {
54            rootfs=$(lxc_rootfs $1)
55            remove=$2
56            add=$3
57    
58            # let container respond to kill -SIGPWR
59            inittab=$rootfs/etc/inittab
60            if ! grep "$add" ${inittab} >/dev/null ; then
61                    grep -v "$remove" ${inittab} > ${inittab}.new
62                    echo $add >> ${inittab}.new
63                    mv ${inittab}.new ${inittab}
64                    echo "$inittab modified with $add"
65            fi
66    }
67    
68    
69    lxc_kill() {
70            name=$1
71            sig=$2
72    
73  # cleanup init scripts which don't work in containers          init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
74  ls $rootfs/etc/rc?.d/*umountfs 2>/dev/null | xargs -i rm -v {}          if [ -z "$init_pid" ] ; then
75  ls $rootfs/etc/rc?.d/*umountroot 2>/dev/null | xargs -i rm -v {}                  lxc-info -n $name
76  ls $rootfs/etc/rc?.d/*hwclock* 2>/dev/null | xargs -i rm -v {}                  exit 1
77            fi
78            echo "$name kill $sig $init_pid"
79            /bin/kill $sig $init_pid
80    }
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    
89    lxc_start() {
90            name=$1
91    
92            if ! lxc-info -n $name | grep RUNNING ; then
93                    echo "$name start"
94                    lxc-start -n $name -o /tmp/${name}.log -d
95                    lxc-wait  -n $name -s RUNNING
96                    lxc-info  -n $name
97                    echo $name > /var/lib/lxc/${name}/on_boot
98            fi
99    }
100    
101  lxc-info -n $name | grep RUNNING || lxc-start -d -n $name -o /tmp/${name}.log && echo "$name start"  lxc_watchdog() {
102    name=$1
103    rootfs=$(lxc_rootfs $1)
104    
105  while true; do  while true; do
         # time of 5 minutes on it JUST IN CASE...  
106          vps_utmp=${rootfs}/var/run/utmp          vps_utmp=${rootfs}/var/run/utmp
107          inotifywait -qqt 300 ${vps_utmp}          tasks=`wc -l < /cgroup/${name}/tasks`
108          if [ $(wc -l < /cgroup/${name}/tasks) -eq 1 ]; then          test -z "$tasks" && exit 1
109            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 39  while true; do Line 118  while true; do
118                  ??0)                  ??0)
119                          echo "$name halt"                          echo "$name halt"
120                          lxc-stop -n "${name}"                          lxc-stop -n "${name}"
121                            lxc-wait -n ${name} -s STOPPED
122                          break                          break
123                  ;;                  ;;
124                  ??6)                  ??6)
125                          echo "$name reboot";                          echo "$name reboot";
126                          lxc-stop -n ${name}                          lxc-stop -n ${name}
127                          lxc-wait -n ${name} -s STOPPED                          lxc-wait -n ${name} -s STOPPED
                         mount /mnt/llin -o remount,rw  
128                          lxc-start -d -n ${name} -o /tmp/${name}.log                          lxc-start -d -n ${name} -o /tmp/${name}.log
129                  ;;                  ;;
130                  *)                  *)
# Line 54  while true; do Line 133  while true; do
133                          [ "$state" = "RUNNING" ] || break                          [ "$state" = "RUNNING" ] || break
134                  ;;                  ;;
135                  esac                  esac
136            else
137                    echo "# $name $tasks tasks"
138          fi          fi
139    
140            # time of 5 minutes on it JUST IN CASE...
141            inotifywait -qqt 300 ${vps_utmp}
142  done  done
143    
144    echo "${name} exited"
145    
146    }
147    
148    
149    command_on_lxc() {
150    command=$1
151    shift
152    
153    echo "# $command $1"
154    
155    case "$command" in
156    
157    start)
158            lxc_exists $1
159            cleanup_init_scripts $1
160            setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
161            setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
162            lxc_start $1
163            ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
164            ;;
165    stop|halt)
166            lxc_exists $1
167            lxc_stop $1
168            ;;
169    reload|force-reload|restart|reboot)
170            lxc_kill $1 -SIGINT
171            ;;
172    watchdog)
173            lxc_watchdog $1
174            ;;
175    *)
176            echo "Usage: $0 {start|stop|restart|status}" >&2
177            exit 3
178            ;;
179    
180    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.178  
changed lines
  Added in v.183

  ViewVC Help
Powered by ViewVC 1.1.26