]>
Commit | Line | Data |
---|---|---|
064af421 BP |
1 | #!/bin/sh |
2 | # | |
e0edde6f | 3 | # Copyright (c) 2011 Nicira, Inc. |
064af421 BP |
4 | # Copyright (c) 2007, 2009 Javier Fernandez-Sanguino <jfs@debian.org> |
5 | # | |
6 | # This is free software; you may redistribute it and/or modify | |
7 | # it under the terms of the GNU General Public License as | |
8 | # published by the Free Software Foundation; either version 2, | |
9 | # or (at your option) any later version. | |
10 | # | |
11 | # This is distributed in the hope that it will be useful, but | |
12 | # WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | # GNU General Public License for more details. | |
15 | # | |
16 | # You should have received a copy of the GNU General Public License with | |
17 | # the Debian operating system, in /usr/share/common-licenses/GPL; if | |
18 | # not, write to the Free Software Foundation, Inc., 59 Temple Place, | |
19 | # Suite 330, Boston, MA 02111-1307 USA | |
20 | # | |
21 | ### BEGIN INIT INFO | |
22 | # Provides: openvswitch-controller | |
9b094b09 JP |
23 | # Required-Start: $network $local_fs $remote_fs |
24 | # Required-Stop: $remote_fs | |
064af421 BP |
25 | # Should-Start: $named |
26 | # Should-Stop: | |
27 | # Default-Start: 2 3 4 5 | |
28 | # Default-Stop: 0 1 6 | |
29 | # Short-Description: Open vSwitch controller | |
1db6b185 TG |
30 | # Description: The Open vSwitch controller enables OpenFlow switches that connect to it |
31 | # to act as MAC-learning Ethernet switches. | |
064af421 BP |
32 | ### END INIT INFO |
33 | ||
34 | PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin | |
35 | ||
e6687b0e | 36 | DAEMON=/usr/bin/ovs-controller # Introduce the server's location here |
064af421 BP |
37 | NAME=ovs-controller # Introduce the short server's name here |
38 | DESC=ovs-controller # Introduce a short description here | |
39 | LOGDIR=/var/log/openvswitch # Log directory to use | |
40 | ||
24e81092 | 41 | PIDFILE=/var/run/openvswitch/$NAME.pid |
064af421 BP |
42 | |
43 | test -x $DAEMON || exit 0 | |
44 | ||
45 | . /lib/lsb/init-functions | |
46 | ||
47 | # Default options, these can be overriden by the information | |
366d78fd | 48 | # at /etc/default/openvswitch-controller |
064af421 BP |
49 | DAEMON_OPTS="" # Additional options given to the server |
50 | ||
51 | DODTIME=10 # Time to wait for the server to die, in seconds | |
52 | # If this value is set too low you might not | |
53 | # let some servers to die gracefully and | |
54 | # 'restart' will not work | |
55 | ||
56 | LOGFILE=$LOGDIR/$NAME.log # Server logfile | |
57 | #DAEMONUSER= # User to run the daemons as. If this value | |
58 | # is set start-stop-daemon will chuid the server | |
59 | ||
60 | # Include defaults if available | |
61 | default=/etc/default/openvswitch-controller | |
62 | if [ -f $default ] ; then | |
f20bbd7a | 63 | . $default |
064af421 BP |
64 | fi |
65 | ||
66 | # Check that the user exists (if we set a user) | |
67 | # Does the user exist? | |
68 | if [ -n "$DAEMONUSER" ] ; then | |
69 | if getent passwd | grep -q "^$DAEMONUSER:"; then | |
70 | # Obtain the uid and gid | |
71 | DAEMONUID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $3}'` | |
72 | DAEMONGID=`getent passwd |grep "^$DAEMONUSER:" | awk -F : '{print $4}'` | |
73 | else | |
74 | log_failure_msg "The user $DAEMONUSER, required to run $NAME does not exist." | |
75 | exit 1 | |
76 | fi | |
77 | fi | |
78 | ||
79 | ||
80 | set -e | |
81 | ||
82 | running_pid() { | |
83 | # Check if a given process pid's cmdline matches a given name | |
84 | pid=$1 | |
85 | name=$2 | |
86 | [ -z "$pid" ] && return 1 | |
87 | [ ! -d /proc/$pid ] && return 1 | |
88 | cmd=`cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1` | |
89 | # Is this the expected server | |
90 | [ "$cmd" != "$name" ] && return 1 | |
91 | return 0 | |
92 | } | |
93 | ||
94 | running() { | |
95 | # Check if the process is running looking at /proc | |
96 | # (works for all users) | |
97 | ||
98 | # No pidfile, probably no daemon present | |
99 | [ ! -f "$PIDFILE" ] && return 1 | |
100 | pid=`cat $PIDFILE` | |
101 | running_pid $pid $DAEMON || return 1 | |
102 | return 0 | |
103 | } | |
104 | ||
105 | start_server() { | |
106 | if [ -z "$LISTEN" ]; then | |
107 | echo "$default: No connection methods configured, controller disabled" >&2 | |
108 | exit 0 | |
109 | fi | |
110 | ||
298fd6d2 BP |
111 | if [ ! -d /var/run/openvswitch ]; then |
112 | install -d -m 755 -o root -g root /var/run/openvswitch | |
113 | fi | |
114 | ||
064af421 BP |
115 | SSL_OPTS= |
116 | case $LISTEN in | |
117 | *ssl*) | |
118 | : ${PRIVKEY:=/etc/openvswitch-controller/privkey.pem} | |
119 | : ${CERT:=/etc/openvswitch-controller/cert.pem} | |
120 | : ${CACERT:=/etc/openvswitch-controller/cacert.pem} | |
121 | if test ! -e "$PRIVKEY" || test ! -e "$CERT" || | |
122 | test ! -e "$CACERT"; then | |
123 | if test ! -e "$PRIVKEY"; then | |
124 | echo "$PRIVKEY: private key missing" >&2 | |
125 | fi | |
126 | if test ! -e "$CERT"; then | |
127 | echo "$CERT: certificate for private key missing" >&2 | |
128 | fi | |
129 | if test ! -e "$CACERT"; then | |
130 | echo "$CACERT: CA certificate missing" >&2 | |
131 | fi | |
132 | exit 1 | |
133 | fi | |
134 | SSL_OPTS="--private-key=$PRIVKEY --certificate=$CERT --ca-cert=$CACERT" | |
135 | ;; | |
136 | esac | |
137 | ||
138 | # Start the process using the wrapper | |
139 | if [ -z "$DAEMONUSER" ] ; then | |
140 | start-stop-daemon --start --pidfile $PIDFILE \ | |
141 | --exec $DAEMON -- --detach --pidfile=$PIDFILE \ | |
142 | $LISTEN $DAEMON_OPTS $SSL_OPTS | |
143 | errcode=$? | |
144 | else | |
145 | # if we are using a daemonuser then change the user id | |
146 | start-stop-daemon --start --quiet --pidfile $PIDFILE \ | |
147 | --chuid $DAEMONUSER --exec $DAEMON -- \ | |
148 | --detach --pidfile=$PIDFILE $LISTEN $DAEMON_OPTS \ | |
149 | $SSL_OPTS | |
150 | errcode=$? | |
151 | fi | |
f20bbd7a | 152 | return $errcode |
064af421 BP |
153 | } |
154 | ||
155 | stop_server() { | |
156 | # Stop the process using the wrapper | |
157 | if [ -z "$DAEMONUSER" ] ; then | |
158 | start-stop-daemon --stop --quiet --pidfile $PIDFILE \ | |
159 | --exec $DAEMON | |
160 | errcode=$? | |
161 | else | |
162 | # if we are using a daemonuser then look for process that match | |
163 | start-stop-daemon --stop --quiet --pidfile $PIDFILE \ | |
164 | --user $DAEMONUSER --exec $DAEMON | |
165 | errcode=$? | |
166 | fi | |
167 | ||
f20bbd7a | 168 | return $errcode |
064af421 BP |
169 | } |
170 | ||
171 | reload_server() { | |
172 | [ ! -f "$PIDFILE" ] && return 1 | |
173 | pid=`cat $PIDFILE` # This is the daemon's pid | |
174 | # Send a SIGHUP | |
175 | kill -1 $pid | |
176 | return $? | |
177 | } | |
178 | ||
179 | force_stop() { | |
180 | # Force the process to die killing it manually | |
f20bbd7a JP |
181 | [ ! -e "$PIDFILE" ] && return |
182 | if running ; then | |
183 | kill -15 $pid | |
184 | # Is it really dead? | |
97044604 | 185 | sleep "$DODTIME" |
f20bbd7a JP |
186 | if running ; then |
187 | kill -9 $pid | |
97044604 | 188 | sleep "$DODTIME" |
f20bbd7a JP |
189 | if running ; then |
190 | echo "Cannot kill $NAME (pid=$pid)!" | |
191 | exit 1 | |
192 | fi | |
193 | fi | |
194 | fi | |
195 | rm -f $PIDFILE | |
064af421 BP |
196 | } |
197 | ||
198 | ||
199 | case "$1" in | |
200 | start) | |
f20bbd7a | 201 | log_daemon_msg "Starting $DESC " "$NAME" |
064af421 BP |
202 | # Check if it's running first |
203 | if running ; then | |
204 | log_progress_msg "apparently already running" | |
205 | log_end_msg 0 | |
206 | exit 0 | |
207 | fi | |
208 | if start_server && running ; then | |
209 | # It's ok, the server started and is running | |
210 | log_end_msg 0 | |
211 | else | |
212 | # Either we could not start it or it is not running | |
213 | # after we did | |
214 | # NOTE: Some servers might die some time after they start, | |
215 | # this code does not try to detect this and might give | |
216 | # a false positive (use 'status' for that) | |
217 | log_end_msg 1 | |
218 | fi | |
f20bbd7a | 219 | ;; |
064af421 BP |
220 | stop) |
221 | log_daemon_msg "Stopping $DESC" "$NAME" | |
222 | if running ; then | |
223 | # Only stop the server if we see it running | |
224 | stop_server | |
225 | log_end_msg $? | |
226 | else | |
227 | # If it's not running don't do anything | |
228 | log_progress_msg "apparently not running" | |
229 | log_end_msg 0 | |
230 | exit 0 | |
231 | fi | |
232 | ;; | |
233 | force-stop) | |
234 | # First try to stop gracefully the program | |
235 | $0 stop | |
236 | if running; then | |
237 | # If it's still running try to kill it more forcefully | |
238 | log_daemon_msg "Stopping (force) $DESC" "$NAME" | |
239 | force_stop | |
240 | log_end_msg $? | |
241 | fi | |
f20bbd7a | 242 | ;; |
064af421 BP |
243 | restart|force-reload) |
244 | log_daemon_msg "Restarting $DESC" "$NAME" | |
245 | stop_server | |
246 | # Wait some sensible amount, some server need this | |
97044604 | 247 | [ -n "$DODTIME" ] && sleep $DODTIME |
064af421 BP |
248 | start_server |
249 | running | |
250 | log_end_msg $? | |
f20bbd7a | 251 | ;; |
064af421 BP |
252 | status) |
253 | ||
254 | log_daemon_msg "Checking status of $DESC" "$NAME" | |
255 | if running ; then | |
256 | log_progress_msg "running" | |
257 | log_end_msg 0 | |
258 | else | |
259 | log_progress_msg "apparently not running" | |
260 | log_end_msg 1 | |
261 | exit 1 | |
262 | fi | |
263 | ;; | |
264 | # Use this if the daemon cannot reload | |
265 | reload) | |
266 | log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon" | |
267 | log_warning_msg "cannot re-read the config file (use restart)." | |
268 | ;; | |
269 | *) | |
366d78fd | 270 | N=/etc/init.d/openvswitch-controller |
f20bbd7a JP |
271 | echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2 |
272 | exit 1 | |
273 | ;; | |
064af421 BP |
274 | esac |
275 | ||
276 | exit 0 |