MyPLC: portable self-contained PLC installation
[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: plc.init,v 1.6 2005/04/24 19:48:11 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 ] ; then
19     . /etc/sysconfig/plc
20 fi
21
22 RETVAL=0
23
24 # Get options
25 while getopts "vh" opt ; do
26     case $opt in
27         v)
28             verbose=1
29             set -x
30             ;;
31         h|*)
32             usage
33             ;;
34     esac
35 done
36
37 start ()
38 {
39     echo -n $"Mounting PLC: "
40
41     if ! grep -q $PLC_ROOT.img /proc/mounts ; then
42         if ! e2fsck -a $PLC_ROOT.img | logger -t "PLC" ; then
43             e2fsck $PLC_ROOT.img
44         fi
45         mount -o loop $PLC_ROOT.img $PLC_ROOT
46         RETVAL=$(($RETVAL+$?))
47     fi
48     if ! grep -q $PLC_DATA /proc/mounts ; then
49         mount -t none -o bind,rw $PLC_DATA $PLC_ROOT/data
50         RETVAL=$(($RETVAL+$?))
51     fi
52     if ! grep -q $PLC_ROOT/proc /proc/mounts ; then
53         mount -t proc none $PLC_ROOT/proc
54         RETVAL=$(($RETVAL+$?))
55     fi
56
57     if [ $RETVAL -eq 0 ]; then
58         success $"PLC mount"
59     else
60         failure $"PLC mount"
61     fi
62     echo
63
64     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS start
65     RETVAL=$?
66 }
67
68 stop ()
69 {
70     chroot $PLC_ROOT /sbin/service plc $PLC_OPTIONS stop
71
72     echo -n $"Unmounting PLC: "
73
74     umount $PLC_ROOT/proc
75     RETVAL=$(($RETVAL+$?))
76     umount $PLC_ROOT/data
77     RETVAL=$(($RETVAL+$?))
78     umount $PLC_ROOT
79     RETVAL=$(($RETVAL+$?))
80
81     if [ $RETVAL -eq 0 ]; then
82         success $"PLC unmount"
83     else
84         failure $"PLC unmount"  
85     fi
86     echo
87 }
88
89 restart ()
90 {
91     stop
92     start
93 }
94
95 case "$1" in
96     start|stop|restart)
97         $1
98         ;;
99
100     *)
101         echo "Usage: $0 {start|stop|restart}"
102         RETVAL=1
103         ;;
104 esac
105
106 exit $RETVAL