62aeed4fb78eb1d7315069672d02dab74c11db2e
[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.8 2006/07/06 17:43:52 mlhuang Exp $
10 #
11
12 PATH=/sbin:/bin:/usr/bin:/usr/sbin
13
14 # Source function library.
15 if [ -f /etc/init.d/functions ] ; then
16     . /etc/init.d/functions
17 fi
18
19 # If success() or failure() are not defined
20 if ! type -type success >/dev/null || ! type -type failure >/dev/null ; then
21     success() {
22         echo -ne "[  OK  ]\r"
23         return 0
24     }
25     failure() {
26         echo -ne "[FAILED]\r"
27         return 1
28     }
29 fi
30
31 # Source configuration
32 if [ -f /etc/sysconfig/plc -a -z "${PLC_ROOT}${PLC_DATA}" ] ; then
33     . /etc/sysconfig/plc
34 fi
35
36 # Total number of errors
37 ERRORS=0
38
39 # Count the exit status of the last command
40 check ()
41 {
42     ERRORS=$(($ERRORS+$?))
43 }
44
45 mounted ()
46 {
47     if cut -d' ' -f2 /proc/mounts | grep -q "$1" ; then
48         return 0
49     else
50         return 1
51     fi
52 }
53
54 mount_plc ()
55 {
56     echo -n $"Mounting PLC: "
57
58     if ! mounted $PLC_ROOT ; then
59         if ! e2fsck -a $PLC_ROOT.img | logger -t "PLC" ; then
60             e2fsck $PLC_ROOT.img
61         fi
62         mount -o loop $PLC_ROOT.img $PLC_ROOT
63         check
64     fi
65     if ! mounted $PLC_ROOT/data ; then
66         mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data
67         check
68     fi
69     if ! mounted $PLC_ROOT/proc ; then
70         mount -t proc none $PLC_ROOT/proc
71         check
72     fi
73
74     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
75     echo
76
77 }
78
79 start ()
80 {
81     # Starting everything
82     if [ -z "$1" ] ; then
83         mount_plc 
84     fi
85
86     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $*
87     check
88 }
89
90 umount_plc ()
91 {
92     echo -n $"Unmounting PLC: "
93
94     for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do
95         if mounted $dir ; then
96             umount $dir
97             check
98         fi
99     done
100
101     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
102     echo
103 }
104
105 stop ()
106 {
107     if mounted $PLC_ROOT ; then
108         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
109         check
110     fi
111
112     # Stopped everything
113     if [ -z "$1" ] ; then
114         umount_plc
115     fi
116 }
117
118 mountstatus_plc ()
119 {
120     for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do
121         if mounted $dir ; then
122             echo $dir
123         fi
124     done
125 }
126
127 # Get command
128 shift $(($OPTIND - 1))
129 command=$1
130
131 # Get step(s)
132 shift 1
133
134 case "$command" in
135     start|stop)
136         $command $*
137         ;;
138
139     restart)
140         stop $*
141         start $*
142         ;;
143
144     mount|umount|mountstatus)
145         ${command}_plc $*
146         ;;
147   
148     *)
149         echo "Usage: $0 {start|stop|restart|mount|umount|mountstatus}"
150         RETVAL=1
151         ;;
152 esac
153
154 exit $ERRORS