real host reboot, status now include rootfs and on_boot
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 16 Mar 2010 19:59:41 +0000 (19:59 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 16 Mar 2010 19:59:41 +0000 (19:59 +0000)
git-svn-id: svn://svn.rot13.org/sysadmin-cookbook@183 191e9f34-6774-4a6d-acfc-7664dacd4a2a

recepies/lxc/lxc-watchdog.sh

index d161aff..1188db6 100755 (executable)
@@ -13,9 +13,6 @@
 
 which inotifywait >/dev/null || apt-get install inotify-tools
 
-lxc_status() {
-       lxc-ls -1 | sort -u | xargs -i lxc-info -n {}
-}
 
 lxc_exists() {
        name=$1
@@ -33,6 +30,15 @@ lxc_rootfs() {
 }
 
 
+lxc_status() {
+       lxc-ls -1 | sort -u | xargs -i lxc-info -n {} | sed "s/'//g" | while read name is status ; do
+               on_boot="       "
+               test -s /var/lib/lxc/$name/on_boot && on_boot="on_boot"
+               echo "$name $status $on_boot $(lxc_rootfs $name)"
+       done
+}
+
+
 cleanup_init_scripts() {
        rootfs=$(lxc_rootfs $1)
 
@@ -46,32 +52,37 @@ cleanup_init_scripts() {
 
 setup_inittab() {
        rootfs=$(lxc_rootfs $1)
+       remove=$2
+       add=$3
 
        # 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
+       if ! grep "$add" ${inittab} >/dev/null ; then
+               grep -v "$remove" ${inittab} > ${inittab}.new
+               echo $add >> ${inittab}.new
                mv ${inittab}.new ${inittab}
-               echo "$initab modified"
+               echo "$inittab modified with $add"
        fi
-
 }
 
 
-lxc_stop() {
+lxc_kill() {
        name=$1
+       sig=$2
 
        init_pid=`lxc-ps -C init -o pid | grep "^$name" | cut -d" " -f2-`
        if [ -z "$init_pid" ] ; then
                lxc-info -n $name
                exit 1
        fi
-       echo "$name stop $init_pid"
-       /bin/kill -SIGPWR $init_pid
-       lxc-wait -n $name -s STOPPED
+       echo "$name kill $sig $init_pid"
+       /bin/kill $sig $init_pid
+}
 
+lxc_stop() {
+       lxc_kill $name -SIGPWR
+       lxc-wait -n $name -s STOPPED
+#      rm -f /var/lib/lxc/${name}/on_boot
 }
 
 
@@ -83,6 +94,7 @@ lxc_start() {
                lxc-start -n $name -o /tmp/${name}.log -d
                lxc-wait  -n $name -s RUNNING
                lxc-info  -n $name
+               echo $name > /var/lib/lxc/${name}/on_boot
        fi
 }
 
@@ -97,7 +109,7 @@ while true; do
        if [ "$tasks" -eq 1 ]; then
 
                runlevel="$(runlevel ${vps_utmp})"
-               echo "# $name runlevel $runlevel"
+               echo `date +%Y-%m-%dT%H:%M:%S` "$name runlevel $runlevel"
 
                case $runlevel in
                N*)
@@ -134,28 +146,31 @@ echo "${name} exited"
 }
 
 
-case "$1" in
+command_on_lxc() {
+command=$1
+shift
+
+echo "# $command $1"
+
+case "$command" in
 
 start)
-       lxc_exists $2
-       cleanup_init_scripts $2
-       setup_inittab $2
-       lxc_start $2
-       ( nohup $0 watchdog $2 >> /tmp/$2.log ) &
+       lxc_exists $1
+       cleanup_init_scripts $1
+       setup_inittab $1 ::power      "p0::powerfail:/sbin/init 0"
+       setup_inittab $1 ::ctrlaltdel "p6::ctrlaltdel:/sbin/init 6"
+       lxc_start $1
+       ( nohup $0 watchdog $1 >> /tmp/$1.log 2>/dev/null ) &
        ;;
-stop)
-       lxc_exists $2
-       lxc_stop $2
+stop|halt)
+       lxc_exists $1
+       lxc_stop $1
        ;;
-status)
-       lxc_status
-       ;;
-reload|force-reload|restart)
-       lxc_stop $2
-       lxc_start $2
+reload|force-reload|restart|reboot)
+       lxc_kill $1 -SIGINT
        ;;
 watchdog)
-       lxc_watchdog $2
+       lxc_watchdog $1
        ;;
 *)
        echo "Usage: $0 {start|stop|restart|status}" >&2
@@ -164,3 +179,22 @@ watchdog)
 
 esac
 
+}
+
+command=$1
+shift
+
+test "$command" = "status" && lxc_status && exit
+
+if [ -z "$1" ] ; then
+       ls /var/lib/lxc/*/on_boot | while read path ; do
+               name=`echo $path | cut -d/ -f5`
+               command_on_lxc $command $name
+       done
+else
+       while [ ! -z "$1" ] ; do
+               command_on_lxc $command $1
+               shift
+       done
+fi
+