X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=host.init;h=f207e008a525ab98501024070e0b4c4dafd31e21;hb=9783e5e590e91382b65f80ccd3d95bb7e976ce2e;hp=6e38c9d0e76db9939ca5a2533fcab69b3ccd193b;hpb=c8addc71030c6159f0f99fa39ab3aa2dc4eadbe6;p=myplc.git diff --git a/host.init b/host.init index 6e38c9d..f207e00 100755 --- a/host.init +++ b/host.init @@ -6,87 +6,157 @@ # # description: Manages all PLC services on this machine # -# $Id: host.init,v 1.1.1.1 2006/03/27 17:36:46 mlhuang Exp $ +# $Id: host.init,v 1.8 2006/07/06 17:43:52 mlhuang Exp $ # PATH=/sbin:/bin:/usr/bin:/usr/sbin # Source function library. -. /etc/init.d/functions +if [ -f /etc/init.d/functions ] ; then + . /etc/init.d/functions +fi + +# If success() or failure() are not defined +if ! type -type success >/dev/null || ! type -type failure >/dev/null ; then + success() { + echo -ne "[ OK ]\r" + return 0 + } + failure() { + echo -ne "[FAILED]\r" + return 1 + } +fi # Source configuration -if [ -f /etc/sysconfig/plc -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then - . /etc/sysconfig/plc +SERVICE=$(basename $0) +if [ ! -f /etc/sysconfig/$SERVICE ] ; then + SERVICE=plc +fi +if [ -f /etc/sysconfig/$SERVICE -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then + . /etc/sysconfig/$SERVICE fi -RETVAL=0 +# Total number of errors +ERRORS=0 -start () +# Count the exit status of the last command +check () +{ + ERRORS=$(($ERRORS+$?)) +} + +mounted () +{ + if cut -d' ' -f2 /proc/mounts | grep -q "$1" ; then + return 0 + else + return 1 + fi +} + +mount_plc () { echo -n $"Mounting PLC: " - if ! cut -d' ' -f2 /proc/mounts | grep -q $PLC_ROOT ; then + if ! mounted $PLC_ROOT ; then if ! e2fsck -a $PLC_ROOT.img | logger -t "PLC" ; then e2fsck $PLC_ROOT.img fi mount -o loop $PLC_ROOT.img $PLC_ROOT - RETVAL=$(($RETVAL+$?)) + check fi - if ! cut -d' ' -f2 /proc/mounts | grep -q $PLC_ROOT/data ; then + if ! mounted $PLC_ROOT/data ; then mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data - RETVAL=$(($RETVAL+$?)) + check fi - if ! cut -d' ' -f2 /proc/mounts | grep -q $PLC_ROOT/proc ; then + if ! mounted $PLC_ROOT/proc ; then mount -t proc none $PLC_ROOT/proc - RETVAL=$(($RETVAL+$?)) + check fi - if [ $RETVAL -eq 0 ]; then - success $"PLC mount" - else - failure $"PLC mount" - fi + [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount" echo - chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start - RETVAL=$? } -stop () +start () { - chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop + # Starting everything + if [ -z "$1" ] ; then + mount_plc + fi + + chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $* + check +} +umount_plc () +{ echo -n $"Unmounting PLC: " - umount $PLC_ROOT/proc - RETVAL=$(($RETVAL+$?)) - umount $PLC_ROOT/data - RETVAL=$(($RETVAL+$?)) - umount $PLC_ROOT - RETVAL=$(($RETVAL+$?)) + for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do + if mounted $dir ; then + umount $dir + check + fi + done - if [ $RETVAL -eq 0 ]; then - success $"PLC unmount" - else - failure $"PLC unmount" - fi + [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount" echo } -case "$1" in +stop () +{ + if mounted $PLC_ROOT ; then + chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $* + check + fi + + # Stopped everything + if [ -z "$1" ] ; then + umount_plc + fi +} + +mountstatus_plc () +{ + for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do + if mounted $dir ; then + echo $dir + fi + done +} + +# Get command +shift $(($OPTIND - 1)) +command=$1 + +# Get step(s) +shift 1 + +case "$command" in start|stop) - $1 + $command $* ;; restart) - stop - start + stop $* + start $* ;; + reload) + chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS reload $* + ;; + + mount|umount|mountstatus) + ${command}_plc $* + ;; + *) - echo "Usage: $0 {start|stop|restart}" + echo "Usage: $0 {start|stop|restart|reload|mount|umount|mountstatus}" RETVAL=1 ;; esac -exit $RETVAL +exit $ERRORS