Do the Debian package build in a _debian subdirectory.
[sliver-openvswitch.git] / debian / openflow-switch.init
1 #! /bin/sh
2 #
3 # /etc/init.d/openflow-switch
4 #
5 # Written by Miquel van Smoorenburg <miquels@cistron.nl>.
6 # Modified for Debian by Ian Murdock <imurdock@gnu.ai.mit.edu>.
7 # Further changes by Javier Fernandez-Sanguino <jfs@debian.org>
8 # Modified for openflow-switch.
9 #
10 # Version:      @(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
11 #
12 ### BEGIN INIT INFO
13 # Provides:          openflow-switch
14 # Required-Start:    $network $named $remote_fs $syslog
15 # Required-Stop:
16 # Default-Start:     2 3 4 5
17 # Default-Stop:      0 1 6
18 # Short-Description: OpenFlow switch
19 ### END INIT INFO
20
21 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
22 DAEMON=/usr/sbin/secchan
23 NAME=secchan
24 DESC=secchan
25
26 test -x $DAEMON || exit 0
27
28 LOGDIR=/var/log/openflow
29 PIDFILE=/var/run/$NAME.pid
30 DHCLIENT_PIDFILE=/var/run/dhclient.of0.pid
31 DODTIME=1                   # Time to wait for the server to die, in seconds
32                             # If this value is set too low you might not
33                             # let some servers to die gracefully and
34                             # 'restart' will not work
35
36 # Include secchan defaults if available
37 default=/etc/default/openflow-switch
38 if [ -f $default ] ; then
39         . $default
40 fi
41
42 set -e
43
44 running_pid()
45 {
46     # Check if a given process pid's cmdline matches a given name
47     pid=$1
48     name=$2
49     [ -z "$pid" ] && return 1 
50     [ ! -d /proc/$pid ] &&  return 1
51     cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1`
52     # Is this the expected child?
53     [ "$cmd" != "$name" ] &&  return 1
54     return 0
55 }
56
57 running()
58 {
59 # Check if the process is running looking at /proc
60 # (works for all users)
61
62     # No pidfile, probably no daemon present
63     [ ! -f "$PIDFILE" ] && return 1
64     # Obtain the pid and check it against the binary name
65     pid=`cat $PIDFILE`
66     running_pid $pid $NAME || return 1
67     return 0
68 }
69
70 force_stop() {
71 # Forcefully kill the process
72     [ ! -f "$PIDFILE" ] && return
73     if running ; then
74         kill -15 $pid
75         # Is it really dead?
76         [ -n "$DODTIME" ] && sleep "$DODTIME"s
77         if running ; then
78             kill -9 $pid
79             [ -n "$DODTIME" ] && sleep "$DODTIME"s
80             if running ; then
81                 echo "Cannot kill $LABEL (pid=$pid)!"
82                 exit 1
83             fi
84         fi
85     fi
86     rm -f $PIDFILE
87     return 0
88 }
89
90 must_succeed() {
91     echo -n "$1: "
92     shift
93     if "$@"; then
94         echo "success."
95     else
96         echo " ERROR."
97         exit 1
98     fi
99 }
100
101 check_op() {
102     echo -n "$1: "
103     shift
104     if "$@"; then
105         echo "success."
106     else
107         echo " ERROR."
108     fi
109 }
110
111 case "$1" in
112     start)
113         if test -z "$NETDEVS"; then
114             echo "$default: No network devices configured, switch disabled" >&2
115             echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
116             exit 0
117         fi
118         if test -z "$CONTROLLER"; then
119             echo "$default: No controller configured, switch disabled" >&2
120             echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
121             exit 0
122         fi
123         if test "$IN_BAND" != yes && test "$IN_BAND" != no; then
124             echo "$default: IN_BAND must set to 'yes' or 'no'" >&2
125             echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
126             exit 1
127         fi
128         case $CONTROLLER in
129             tcp:*)
130                 ;;
131             ssl:*)
132                 : ${PRIVKEY:=/etc/openflow-switch/of0-privkey.pem}
133                 : ${CERT:=/etc/openflow-switch/of0-cert.pem}
134                 : ${CACERT:=/etc/openflow-switch/cacert.pem}
135                 if test ! -e "$PRIVKEY" || test ! -e "$CERT" ||
136                     test ! -e "$CACERT"; then
137                     if test ! -e "$PRIVKEY"; then
138                         echo "$PRIVKEY: private key missing" >&2
139                     fi
140                     if test ! -e "$CERT"; then
141                         echo "$CERT: certificate for private key missing" >&2
142                     fi
143                     if test ! -e "$CACERT"; then
144                         echo "$CACERT: CA certificate missing" >&2
145                     fi
146                     echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
147                     exit 1
148                 fi
149                 ;;
150             *)
151                 echo "$default: CONTROLLER must be in the form 'ssl:HOST[:PORT]' or 'tcp:HOST[:PORT]'" >&2
152                 echo "Run ofp-switch-setup or edit /etc/default/openflow-switch to configure" >&2
153                 exit 1
154         esac
155
156         echo -n "Loading openflow_mod: "
157         if modprobe openflow_mod; then
158             echo "success."
159         else
160             echo " ERROR."
161             echo "openflow_mod has probably not been built for this kernel."
162             if ! test -d /usr/share/doc/openflow-datapath-source; then
163                 echo "Install the openflow-datapath-source package, then read"
164                 echo "/usr/share/doc/openflow-datapath-source/README.Debian"
165             else
166                 echo "For instructions, read"
167                 echo "/usr/share/doc/openflow-datapath-source/README.Debian"
168             fi
169             exit 1
170         fi
171
172         must_succeed "Adding datapath" dpctl adddp nl:0
173         for netdev in $NETDEVS; do
174             must_succeed "Adding $netdev to datapath" dpctl addif nl:0 $netdev
175         done
176
177         if test "$IN_BAND" = yes; then
178             if test "$SWITCH_IP" = dhcp; then
179                 must_succeed "Temporarily disabling of0" ifconfig of0 down
180             else
181                 must_succeed "Configuring of0 as $SWITCH_IP" ifconfig of0 $SWITCH_IP
182             fi
183         else
184             must_succeed "Disabling of0" ifconfig of0 down
185         fi
186
187         echo -n "Starting $DESC: "
188         start-stop-daemon --start --quiet --pidfile $PIDFILE \
189             --exec $DAEMON -- nl:0 $CONTROLLER --detach --pidfile=$PIDFILE \
190             $DAEMON_OPTS $SSL_OPTS
191         if running; then
192             echo "$NAME."
193         else
194             echo " ERROR."
195         fi
196
197         if test "$IN_BAND" = yes && test "$SWITCH_IP" = dhcp; then
198             echo -n "Starting dhclient on of0: "
199             start-stop-daemon --start --quiet --pidfile $DHCLIENT_PIDFILE \
200                 --exec /sbin/dhclient -- -q -pf $DHCLIENT_PIDFILE of0
201             if running; then
202                 echo "dhclient."
203             else
204                 echo " ERROR."
205             fi
206         fi
207         ;;
208     stop)
209         if test -e /var/run/dhclient.of0.pid; then
210             echo -n "Stopping dhclient on of0: "
211             start-stop-daemon --stop --quiet --oknodo \
212                 --pidfile $DHCLIENT_PIDFILE --exec /sbin/dhclient
213             echo "dhclient."
214         fi            
215
216         echo -n "Stopping $DESC: "
217         start-stop-daemon --stop --quiet --oknodo --pidfile $PIDFILE \
218             --exec $DAEMON
219         echo "$NAME."
220
221         for netdev in $NETDEVS; do
222             check_op "Removing $netdev from datapath" dpctl delif nl:0 $netdev
223         done
224         check_op "Deleting datapath" dpctl deldp nl:0
225         ;;
226     force-stop)
227         echo -n "Forcefully stopping $DESC: "
228         force_stop
229         if ! running; then
230             echo "$NAME."
231         else
232             echo " ERROR."
233         fi
234         ;;
235     reload)
236         ;;
237     force-reload)
238         start-stop-daemon --stop --test --quiet --pidfile \
239             $PIDFILE --exec $DAEMON \
240             && $0 restart \
241             || exit 0
242         ;;
243     restart)
244         $0 stop || true
245         $0 start
246         ;;
247     status)
248         echo -n "$LABEL is "
249         if running ;  then
250             echo "running"
251         else
252             echo " not running."
253             exit 1
254         fi
255         ;;
256     *)
257         N=/etc/init.d/$NAME
258         echo "Usage: $N {start|stop|restart|force-reload|status|force-stop}" >&2
259         exit 1
260         ;;
261 esac
262
263 exit 0