/[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

Contents of /recepies/lxc/lxc-watchdog.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 180 - (show annotations)
Tue Mar 16 16:18:40 2010 UTC (14 years, 1 month ago) by dpavlin
File MIME type: application/x-sh
File size: 1946 byte(s)
use lxc-ps to find container's init
1 #!/bin/bash
2
3 # based on Tony Risinger code from lxc-users
4 # http://www.mail-archive.com/lxc-users@lists.sourceforge.net/msg00074.html
5
6 which inotifywait >/dev/null || apt-get install inotify-tools
7
8 name=$1
9
10 if [ ! -e /var/lib/lxc/$name/config ] ; then
11 echo "Usage: $0 name"
12 ls /var/lib/lxc/*/config | cut -d/ -f5
13 exit 1
14 fi
15
16 rootfs=`grep lxc.rootfs /var/lib/lxc/$name/config | cut -d= -f2`
17 echo "$name rootfs $rootfs"
18
19
20 # cleanup init scripts which don't work in containers
21 ls \
22 $rootfs/etc/rc?.d/*umountfs \
23 $rootfs/etc/rc?.d/*umountroot \
24 $rootfs/etc/rc?.d/*hwclock* \
25 2>/dev/null | xargs -i rm -v {}
26
27
28 # let container respond to kill -SIGPWR
29 inittab=$rootfs/etc/inittab
30 powerfail="pw::powerfail:/sbin/init 0"
31 if ! grep "$powerfail" ${inittab} >/dev/null ; then
32 grep -v ::power ${inittab} > ${inittab}.new
33 echo $powerfail >> ${inittab}.new
34 mv ${inittab}.new ${inittab}
35 echo "$initab modified"
36 fi
37
38
39 if [ "$2" == "stop" ] ; then
40 echo "$name stop"
41 kill -SIGPWR `lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
42 lxc-wait -n $name -s STOPPED
43 exit
44 fi
45
46
47 if ! lxc-info -n $name | grep RUNNING ; then
48 echo "$name start"
49 lxc-start -n $name -o /tmp/${name}.log -d
50 lxc-wait -n $name -s RUNNING
51 lxc-info -n $name
52 fi
53
54 while true; do
55 # time of 5 minutes on it JUST IN CASE...
56 vps_utmp=${rootfs}/var/run/utmp
57 inotifywait -qqt 300 ${vps_utmp}
58 if [ $(wc -l < /cgroup/${name}/tasks) -eq 1 ]; then
59
60 runlevel="$(runlevel ${vps_utmp})"
61 echo "# $name runlevel $runlevel"
62
63 case $runlevel in
64 N*)
65 # nothing for new boot state
66 ;;
67 ??0)
68 echo "$name halt"
69 lxc-stop -n "${name}"
70 break
71 ;;
72 ??6)
73 echo "$name reboot";
74 lxc-stop -n ${name}
75 lxc-wait -n ${name} -s STOPPED
76 mount /mnt/llin -o remount,rw
77 lxc-start -d -n ${name} -o /tmp/${name}.log
78 ;;
79 *)
80 # make sure vps is still running
81 state="$(lxc-info -n "${name}" | sed -e 's/.* is //')"
82 [ "$state" = "RUNNING" ] || break
83 ;;
84 esac
85 fi
86 done
87

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.26