#
# description: Manages all PLC services on this machine
#
-# $Id: plc.init,v 1.6 2005/04/24 19:48:11 mlhuang Exp $
+# $Id: host.init,v 1.5 2006/04/18 15:39:34 thierry Exp $
#
PATH=/sbin:/bin:/usr/bin:/usr/sbin
. /etc/init.d/functions
# Source configuration
-if [ -f /etc/sysconfig/plc ] ; then
+if [ -f /etc/sysconfig/plc -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then
. /etc/sysconfig/plc
fi
-RETVAL=0
-
-# Get options
-while getopts "vh" opt ; do
- case $opt in
- v)
- verbose=1
- set -x
- ;;
- h|*)
- usage
- ;;
- esac
-done
+# 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 ! grep -q $PLC_ROOT.img /proc/mounts ; 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 ! grep -q $PLC_DATA /proc/mounts ; then
+ if ! mounted $PLC_ROOT/data ; then
mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data
- RETVAL=$(($RETVAL+$?))
+ check
fi
- if ! grep -q $PLC_ROOT/proc /proc/mounts ; 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
+ mount_plc
+ 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
}
-restart ()
+stop ()
+{
+ if mounted $PLC_ROOT ; then
+ chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
+ check
+ fi
+
+ umount_plc
+
+}
+
+mount_status ()
{
- stop
- start
+ lines=$(mount | grep $PLC_ROOT)
+ if [ -z "$lines" ] ; then
+ echo "==== $PLC_ROOT is *not* mounted"
+ else
+ echo "==== The following mount points remain active"
+ echo "$lines"
+ fi
}
-case "$1" in
- start|stop|restart)
- $1
+# Get command
+shift $(($OPTIND - 1))
+command=$1
+
+# Get step(s)
+shift 1
+
+case "$command" in
+ start|stop)
+ $command $*
+ ;;
+
+ restart)
+ stop $*
+ start $*
+ ;;
+
+ mount|umount)
+ ${command}_plc $*
;;
+ mountstatus)
+ mount_status $*
+ ;;
+
*)
- echo "Usage: $0 {start|stop|restart}"
+ echo "Usage: $0 {start|stop|restart|mount|umount|mountstatus}"
RETVAL=1
;;
esac
-exit $RETVAL
+exit $ERRORS