/[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 180 by dpavlin, Tue Mar 16 16:18:40 2010 UTC revision 181 by dpavlin, Tue Mar 16 17:39:28 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    
16  name=$1  lxc_status() {
17            lxc-ls -1 | sort -u | xargs -i lxc-info -n {}
18    }
19    
20    lxc_exists() {
21            name=$1
22    
23            if [ ! -e /var/lib/lxc/$name/config ] ; then
24                    echo "Usage: $0 name"
25                    lxc_status
26                    exit 1
27            fi
28    }
29    
30    
31    lxc_rootfs() {
32            grep lxc.rootfs "/var/lib/lxc/$1/config" | cut -d= -f2 | sed 's/^ *//'
33    }
34    
35    
36    cleanup_init_scripts() {
37            rootfs=$(lxc_rootfs $1)
38    
39            ls \
40                    $rootfs/etc/rc?.d/*umountfs \
41                    $rootfs/etc/rc?.d/*umountroot \
42                    $rootfs/etc/rc?.d/*hwclock* \
43            2>/dev/null | xargs -i rm -v {}
44    }
45    
46    
47    setup_inittab() {
48            rootfs=$(lxc_rootfs $1)
49    
50            # let container respond to kill -SIGPWR
51            inittab=$rootfs/etc/inittab
52            powerfail="pw::powerfail:/sbin/init 0"
53            if ! grep "$powerfail" ${inittab} >/dev/null ; then
54                    grep -v ::power ${inittab} > ${inittab}.new
55                    echo $powerfail >> ${inittab}.new
56                    mv ${inittab}.new ${inittab}
57                    echo "$initab modified"
58            fi
59    
60    }
61    
62    
63  if [ ! -e /var/lib/lxc/$name/config ] ; then  lxc_stop() {
64          echo "Usage: $0 name"          name=$1
65          ls /var/lib/lxc/*/config | cut -d/ -f5  
66          exit 1          init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
67  fi          if [ -z "$init_pid" ] ; then
68                    lxc-info -n $name
69  rootfs=`grep lxc.rootfs /var/lib/lxc/$name/config | cut -d= -f2`                  exit 1
70  echo "$name rootfs $rootfs"          fi
71            echo "$name stop $init_pid"
72            /bin/kill -SIGPWR $init_pid
 # cleanup init scripts which don't work in containers  
 ls \  
         $rootfs/etc/rc?.d/*umountfs \  
         $rootfs/etc/rc?.d/*umountroot \  
         $rootfs/etc/rc?.d/*hwclock* \  
 2>/dev/null | xargs -i rm -v {}  
   
   
 # let container respond to kill -SIGPWR  
 inittab=$rootfs/etc/inittab  
 powerfail="pw::powerfail:/sbin/init 0"  
 if ! grep "$powerfail" ${inittab} >/dev/null ; then  
         grep -v ::power ${inittab} > ${inittab}.new  
         echo $powerfail >> ${inittab}.new  
         mv ${inittab}.new ${inittab}  
         echo "$initab modified"  
 fi  
   
   
 if [ "$2" == "stop" ] ; then  
         echo "$name stop"  
         kill -SIGPWR `lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`  
73          lxc-wait -n $name -s STOPPED          lxc-wait -n $name -s STOPPED
         exit  
 fi  
74    
75    }
76    
77    
78    lxc_start() {
79            name=$1
80    
81  if ! lxc-info -n $name | grep RUNNING ; then          if ! lxc-info -n $name | grep RUNNING ; then
82          echo "$name start"                  echo "$name start"
83          lxc-start -n $name -o /tmp/${name}.log -d                  lxc-start -n $name -o /tmp/${name}.log -d
84          lxc-wait  -n $name -s RUNNING                  lxc-wait  -n $name -s RUNNING
85          lxc-info  -n $name                  lxc-info  -n $name
86  fi          fi
87    }
88    
89    lxc_watchdog() {
90    name=$1
91    rootfs=$(lxc_rootfs $1)
92    
93  while true; do  while true; do
94          # time of 5 minutes on it JUST IN CASE...          # time of 5 minutes on it JUST IN CASE...
95          vps_utmp=${rootfs}/var/run/utmp          vps_utmp=${rootfs}/var/run/utmp
96          inotifywait -qqt 300 ${vps_utmp}          inotifywait -qqt 300 ${vps_utmp}
97          if [ $(wc -l < /cgroup/${name}/tasks) -eq 1 ]; then          tasks=`wc -l < /cgroup/${name}/tasks`
98            test -z "$tasks" && exit 1
99            if [ "$tasks" -eq 1 ]; then
100    
101                  runlevel="$(runlevel ${vps_utmp})"                  runlevel="$(runlevel ${vps_utmp})"
102                  echo "# $name runlevel $runlevel"                  echo "# $name runlevel $runlevel"
# Line 67  while true; do Line 108  while true; do
108                  ??0)                  ??0)
109                          echo "$name halt"                          echo "$name halt"
110                          lxc-stop -n "${name}"                          lxc-stop -n "${name}"
111                            lxc-wait -n ${name} -s STOPPED
112                          break                          break
113                  ;;                  ;;
114                  ??6)                  ??6)
115                          echo "$name reboot";                          echo "$name reboot";
116                          lxc-stop -n ${name}                          lxc-stop -n ${name}
117                          lxc-wait -n ${name} -s STOPPED                          lxc-wait -n ${name} -s STOPPED
                         mount /mnt/llin -o remount,rw  
118                          lxc-start -d -n ${name} -o /tmp/${name}.log                          lxc-start -d -n ${name} -o /tmp/${name}.log
119                  ;;                  ;;
120                  *)                  *)
# Line 82  while true; do Line 123  while true; do
123                          [ "$state" = "RUNNING" ] || break                          [ "$state" = "RUNNING" ] || break
124                  ;;                  ;;
125                  esac                  esac
126            else
127                    echo "# $name $tasks tasks"
128          fi          fi
129  done  done
130    
131    echo "${name} exited"
132    
133    }
134    
135    
136    case "$1" in
137    
138    start)
139            lxc_exists $2
140            cleanup_init_scripts $2
141            setup_inittab $2
142            lxc_start $2
143            ( nohup $0 watchdog $2 >> /tmp/$2.log ) &
144            ;;
145    stop)
146            lxc_exists $2
147            lxc_stop $2
148            ;;
149    status)
150            lxc_status
151            ;;
152    reload|force-reload|restart)
153            lxc_stop $2
154            lxc_start $2
155            ;;
156    watchdog)
157            lxc_watchdog $2
158            ;;
159    *)
160            echo "Usage: $0 {start|stop|restart|status}" >&2
161            exit 3
162            ;;
163    
164    esac
165    

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

  ViewVC Help
Powered by ViewVC 1.1.26