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