review confusing w/W stuff, always consolidate
[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.5 2006/04/18 15:39:34 thierry 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 }
64
65 start ()
66 {
67     mount_plc 
68
69     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start $*
70     check
71 }
72
73 umount_plc ()
74 {
75     echo -n $"Unmounting PLC: "
76
77     for dir in $PLC_ROOT/proc $PLC_ROOT/data $PLC_ROOT ; do
78         if mounted $dir ; then
79             umount $dir
80             check
81         fi
82     done
83
84     [ $ERRORS -eq 0 ] && success $"PLC unmount" || failure $"PLC unmount"       
85     echo
86 }
87
88 stop ()
89 {
90     if mounted $PLC_ROOT ; then
91         chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop $*
92         check
93     fi
94
95     umount_plc
96
97 }
98
99 mount_status ()
100 {
101   lines=$(mount | grep $PLC_ROOT)
102   if [ -z "$lines" ] ; then
103     echo "==== $PLC_ROOT is *not* mounted"
104   else
105     echo "==== The following mount points remain active"
106     echo "$lines"
107   fi
108 }
109
110 # Get command
111 shift $(($OPTIND - 1))
112 command=$1
113
114 # Get step(s)
115 shift 1
116
117 case "$command" in
118     start|stop)
119         $command $*
120         ;;
121
122     restart)
123         stop $*
124         start $*
125         ;;
126
127     mount|umount)
128         ${command}_plc $*
129         ;;
130
131     mountstatus)
132         mount_status $*
133         ;;
134   
135     *)
136         echo "Usage: $0 {start|stop|restart|mount|umount|mountstatus}"
137         RETVAL=1
138         ;;
139 esac
140
141 exit $ERRORS