supports for separate mount/umount/mountstatus operations
[myplc.git] / host.init
1 #!/bin/bash
2 #
3 # plc   Manages all PLC services on this machine
4 #
5 # chkconfig: 2345 99 5
6 #
7 # description:  Manages all PLC services on this machine
8 #
9 # $Id: host.init,v 1.4 2006/04/12 19:30:47 mlhuang Exp $
10 #
11
12 PATH=/sbin:/bin:/usr/bin:/usr/sbin
13
14 # Source function library.
15 . /etc/init.d/functions
16
17 # Source configuration
18 if [ -f /etc/sysconfig/plc -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then
19     . /etc/sysconfig/plc
20 fi
21
22 # Total number of errors
23 ERRORS=0
24
25 # Count the exit status of the last command
26 check ()
27 {
28     ERRORS=$(($ERRORS+$?))
29 }
30
31 mounted ()
32 {
33     if cut -d' ' -f2 /proc/mounts | grep -q "$1" ; then
34         return 0
35     else
36         return 1
37     fi
38 }
39
40 mount_plc ()
41 {
42     echo -n $"Mounting PLC: "
43
44     if ! mounted $PLC_ROOT ; then
45         if ! e2fsck -a $PLC_ROOT.img | logger -t "PLC" ; then
46             e2fsck $PLC_ROOT.img
47         fi
48         mount -o loop $PLC_ROOT.img $PLC_ROOT
49         check
50     fi
51     if ! mounted $PLC_ROOT/data ; then
52         mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data
53         check
54     fi
55     if ! mounted $PLC_ROOT/proc ; then
56         mount -t proc none $PLC_ROOT/proc
57         check
58     fi
59
60     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
61     echo
62
63     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $*
64     check
65 }
66
67 umount_plc ()
68 {
69     echo -n $"Unmounting PLC: "
70
71     for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do
72         if mounted $dir ; then
73             umount $dir
74             check
75         fi
76     done
77
78     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
79     echo
80 }
81
82 stop ()
83 {
84     if mounted $PLC_ROOT ; then
85         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
86         check
87     fi
88
89     umount_plc
90
91 }
92
93 mount_status ()
94 {
95   lines=$(mount | grep $PLC_ROOT)
96   if [ -z "$lines" ] ; then
97     echo "==== $PLC_ROOT is *not* mounted"
98   else
99     echo "==== The following mount points remain active"
100     echo "$lines"
101   fi
102 }
103
104 # Get command
105 shift $(($OPTIND - 1))
106 command=$1
107
108 # Get step(s)
109 shift 1
110
111 case "$command" in
112     start|stop)
113         $command $*
114         ;;
115
116     restart)
117         stop $*
118         start $*
119         ;;
120
121     mount|umount)
122         ${command}_plc $*
123         ;;
124
125     mountstatus)
126         mount_status $*
127         ;;
128   
129     *)
130         echo "Usage: $0 {start|stop|restart}"
131         RETVAL=1
132         ;;
133 esac
134
135 exit $ERRORS