xenserver: Be compatible with non-XenServer func libraries in init script.
[cascardo/ovs.git] / xenserver / etc_init.d_openvswitch
1 #!/bin/bash
2 #
3 # openvswitch
4 #
5 # chkconfig: 2345 09 91
6 # description: Manage Open vSwitch kernel modules and user-space daemons
7
8 # Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc.
9 #
10 # Licensed under the Apache License, Version 2.0 (the "License");
11 # you may not use this file except in compliance with the License.
12 # You may obtain a copy of the License at:
13 #
14 #     http://www.apache.org/licenses/LICENSE-2.0
15 #
16 # Unless required by applicable law or agreed to in writing, software
17 # distributed under the License is distributed on an "AS IS" BASIS,
18 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 # See the License for the specific language governing permissions and
20 # limitations under the License.
21 ### BEGIN INIT INFO
22 # Provides:          openvswitch-switch
23 # Required-Start:    $network $named $remote_fs $syslog
24 # Required-Stop:     $remote_fs
25 # Default-Start:     2 3 4 5
26 # Default-Stop:      0 1 6
27 # Short-Description: Open vSwitch switch
28 ### END INIT INFO
29
30 # source function library
31 if [ -f /etc/init.d/functions ]; then
32     . /etc/init.d/functions
33 elif [ -f /etc/rc.d/init.d/functions ]; then
34     . /etc/rc.d/init.d/functions
35 elif [ -f /lib/lsb/init-functions ]; then
36     . /lib/lsb/init-functions
37 else
38     echo "$0: missing LSB shell function library" >&2
39     exit 1
40 fi
41
42 if type action >/dev/null 2>&1; then
43     :
44 else
45     # SUSE lacks action
46     action() {
47        STRING=$1
48        shift
49        "$@"
50        rc=$?
51        if [ $rc -eq 0 ] ; then
52                log_success_msg $"$STRING "
53        else
54                log_failure_msg $"$STRING "
55        fi
56        return $rc
57     }
58 fi
59
60 . /etc/xensource-inventory
61 test -e /etc/sysconfig/openvswitch && . /etc/sysconfig/openvswitch
62
63 NETWORK_MODE=$(cat /etc/xensource/network.conf)
64 case $NETWORK_MODE in
65     vswitch|openvswitch)
66         ;;
67     bridge)
68         exit 0
69         ;;
70     *)
71         echo "Open vSwitch disabled (/etc/xensource/network.conf is invalid)" >&2
72         exit 0
73         ;;
74 esac
75
76 # General config variables in /etc/sysconfig/openvswitch
77 if test "$PRODUCT_VERSION" = "5.5.0"; then
78     # XenServer 5.5.0 needs ovs-brcompatd and /proc/net simulation.
79     : ${ENABLE_BRCOMPAT:=y}
80     : ${ENABLE_FAKE_PROC_NET:=y}
81 else
82     # Later versions don't need them.
83     : ${ENABLE_BRCOMPAT:=n}
84     : ${ENABLE_FAKE_PROC_NET:=n}
85 fi
86 : ${ENABLE_MONITOR:=y}
87 : ${FORCE_COREFILES:=y}
88
89 # Config variables specific to ovsdb-server
90 : ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,managers}
91 : ${OVSDB_SERVER_DB:=/etc/openvswitch/conf.db}
92 : ${OVSDB_SERVER_PIDFILE:=/var/run/openvswitch/ovsdb-server.pid}
93 : ${OVSDB_SERVER_RUN_DIR:=/var/xen/openvswitch}
94 : ${OVSDB_SERVER_PRIORITY:=-10}
95 : ${OVSDB_SERVER_LOGFILE:=/var/log/openvswitch/ovsdb-server.log}
96 : ${OVSDB_SERVER_FILE_LOGLEVEL:=INFO}
97 : ${OVSDB_SERVER_SYSLOG_LOGLEVEL:=ERR}
98 : ${OVSDB_SERVER_MEMLEAK_LOGFILE:=}
99 : ${OVSDB_SERVER_STRACE_LOG:=}
100 : ${OVSDB_SERVER_STRACE_OPT:=}
101 : ${OVSDB_SERVER_VALGRIND_LOG:=}
102 : ${OVSDB_SERVER_VALGRIND_OPT:=}
103
104 # Config variables specific to ovs-vswitchd
105 : ${VSWITCHD_OVSDB_SERVER:=unix:/var/run/openvswitch/db.sock}
106 : ${VSWITCHD_OVSDB_SCHEMA:=/usr/share/openvswitch/vswitch.ovsschema}
107 : ${VSWITCHD_PIDFILE:=/var/run/openvswitch/ovs-vswitchd.pid}
108 : ${VSWITCHD_RUN_DIR:=/var/xen/openvswitch}
109 : ${VSWITCHD_PRIORITY:=-10}
110 : ${VSWITCHD_MLOCKALL:=yes}
111 : ${VSWITCHD_LOGFILE:=/var/log/openvswitch/ovs-vswitchd.log}
112 : ${VSWITCHD_FILE_LOGLEVEL:=INFO}
113 : ${VSWITCHD_SYSLOG_LOGLEVEL:=ERR}
114 : ${VSWITCHD_MEMLEAK_LOGFILE:=}
115 : ${VSWITCHD_STRACE_LOG:=}
116 : ${VSWITCHD_STRACE_OPT:=}
117 : ${VSWITCHD_VALGRIND_LOG:=}
118 : ${VSWITCHD_VALGRIND_OPT:=}
119
120 # Config variables specific to ovs-brcompatd
121 : ${BRCOMPATD_PIDFILE:=/var/run/openvswitch/ovs-brcompatd.pid}
122 : ${BRCOMPATD_RUN_DIR:=/var/xen/openvswitch}
123 : ${BRCOMPATD_PRIORITY:=-10}
124 : ${BRCOMPATD_LOGFILE:=/var/log/openvswitch/ovs-brcompatd.log}
125 : ${BRCOMPATD_FILE_LOGLEVEL:=INFO}
126 : ${BRCOMPATD_SYSLOG_LOGLEVEL:=ERR}
127 : ${BRCOMPATD_MEMLEAK_LOGFILE:=}
128 : ${BRCOMPATD_STRACE_LOG:=}
129 : ${BRCOMPATD_STRACE_OPT:=}
130 : ${BRCOMPATD_VALGRIND_LOG:=}
131 : ${BRCOMPATD_VALGRIND_OPT:=}
132
133 # Full paths to executables & modules
134 ovsdb_server="/usr/sbin/ovsdb-server"
135 ovsdb_tool="/usr/bin/ovsdb-tool"
136 vswitchd="/usr/sbin/ovs-vswitchd"
137 brcompatd="/usr/sbin/ovs-brcompatd"
138 dpctl="/usr/bin/ovs-dpctl"
139 appctl="/usr/bin/ovs-appctl"
140 ofctl="/usr/bin/ovs-ofctl"
141 vsctl="/usr/bin/ovs-vsctl"
142
143 if [ "$ENABLE_FAKE_PROC_NET" = "y" ]; then
144     if [ "$ENABLE_BRCOMPAT" != "y" ]; then
145         warning "FAKE_PROC_NET required BRCOMPAT which was disabled.  Force enabling."
146         ENABLE_BRCOMPAT="y"
147     fi
148 fi
149
150 if test "$ENABLE_MONITOR" = "y"; then
151     monitor_opt="--monitor"
152 else
153     monitor_opt=
154 fi
155
156 function hup_monitor_external_ids {
157     if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
158         action "Configuring Open vSwitch external IDs" kill -HUP `cat /var/run/openvswitch/ovs-external-ids.pid`
159     fi
160 }
161
162 function dp_list {
163     "$dpctl" show | grep '^dp[0-9]\+:' | cut -d':' -f 1
164 }
165
166 function turn_on_corefiles {
167     ulimit -Sc 67108864
168 }
169
170 function remove_all_dp {
171     for dp in $(dp_list); do
172         action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
173     done
174 }
175
176 function insert_modules_if_required {
177     if ! lsmod | grep -q "openvswitch_mod"; then
178         action "Inserting llc module" modprobe llc
179         action "Inserting openvswitch module" modprobe openvswitch_mod
180     fi
181     if [ "$ENABLE_BRCOMPAT" = "y" ] && [ -n "$BRCOMPATD_PIDFILE" ] && ! lsmod | grep -q "brcompat_mod"; then
182         action "Inserting brcompat module" modprobe brcompat_mod
183     fi
184 }
185
186 function remove_modules {
187     if lsmod | grep -q "brcompat_mod"; then
188         action "Removing brcompat module" rmmod brcompat_mod.ko
189     fi
190     if lsmod | grep -q "openvswitch_mod"; then
191         action "Removing openvswitch module" rmmod openvswitch_mod.ko
192     fi
193 }
194
195 function start_daemon {
196     local DAEMON=$1
197     shift
198     local BINARY=$1
199
200     # cd to daemon's run_dir so core files get dumped into a sensible place.
201     eval local run_dir=\$${DAEMON}_RUN_DIR
202     if [ ! -d "$run_dir" ]; then
203         install -d -m 755 -o root -g root "$run_dir"
204     fi
205     cd "$run_dir"
206     
207     # Configure log levels.
208     eval local syslog_loglevel=\$${DAEMON}_SYSLOG_LOGLEVEL
209     eval local file_loglevel=\$${DAEMON}_FILE_LOGLEVEL
210     eval local logfile=\$${DAEMON}_LOGFILE
211     set -- "$@" -vANY:CONSOLE:EMER -vANY:SYSLOG:"$syslog_loglevel"
212     if test -n "$file_loglevel" && test -n "$logfile"; then
213         install -d -m 755 -o root -g root `dirname "$logfile"`
214         set -- "$@" --log-file="$logfile" -vANY:FILE:"$file_loglevel"
215     fi
216
217     # Configure leak checker.
218     eval local memleak_logfile=\$${DAEMON}_MEMLEAK_LOGFILE
219     if test -n "$memleak_logfile"; then
220         set -- "$@" --check-leaks="$memleak_logfile"
221         if test -e "$memleak_logfile"; then
222             mv "$memleak_logfile" "$memleak_logfile.prev"
223         fi
224     fi
225
226     # Configure debugging wrappers.
227     eval local strace_log=\$${DAEMON}_STRACE_LOG
228     eval local strace_opt=\$${DAEMON}_STRACE_OPT
229     eval local valgrind_log=\$${DAEMON}_VALGRIND_LOG
230     eval local valgrind_opt=\$${DAEMON}_VALGRIND_OPT
231     if test -n "$strace_log" && test -n "$valgrind_log"; then
232         printf "Can not start with both VALGRIND and STRACE\n"
233         exit 1
234     elif test -n "$strace_log"; then
235         local mode=strace
236         set -- strace -o "$strace_log" $strace_opt "$@"
237     elif test -n "$valgrind_log"; then
238         local mode=valgrind
239         set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
240     else
241         local mode=production
242         eval local pidfile=\$${DAEMON}_PIDFILE
243         install -d -m 755 -o root -g root `dirname $pidfile`
244         set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
245     fi
246
247     # Configure niceness.
248     eval local priority=\$${DAEMON}_PRIORITY
249     if test -n "$priority"; then
250         set -- nice -n $priority "$@"
251     fi
252
253     if test $mode = production; then
254         action "Starting `basename $BINARY`" "$@"
255     else
256         # Start in background and force a "success" message
257         action "Starting `basename $BINARY` with $mode debugging" true
258         ("$@") &
259     fi
260 }
261
262 function start_ovsdb_server {
263     set -- "$ovsdb_server" "$OVSDB_SERVER_DB"
264     for remote in $OVSDB_SERVER_REMOTES; do
265         set -- "$@" --remote="$remote"
266     done
267     set -- "$@" --private-key=db:SSL,private_key --certificate=db:SSL,certificate --bootstrap-ca-cert=db:SSL,ca_cert
268     start_daemon OVSDB_SERVER "$@"
269 }
270
271 function start_vswitchd {
272     local fake_proc_net_opt=
273     if [ "$ENABLE_FAKE_PROC_NET" = "y" ]; then
274         fake_proc_net_opt="--fake-proc-net"
275     fi
276
277     local mlockall_opt=
278     if [ "$VSWITCHD_MLOCKALL" != "no" ]; then
279         mlockall_opt="--mlockall"
280     fi
281
282     start_daemon VSWITCHD "$vswitchd" $fake_proc_net_opt $mlockall_opt \
283                  "$VSWITCHD_OVSDB_SERVER"
284  }
285
286 function start_brcompatd {
287     start_daemon BRCOMPATD "$brcompatd" \
288                  --appctl-command="$appctl --target=/var/run/openvswitch/ovs-vswitchd.\`cat $VSWITCHD_PIDFILE\`.ctl %s" \
289                  "$VSWITCHD_OVSDB_SERVER"
290 }
291
292 function stop_daemon {
293     local DAEMON=$1
294     local BINARY=$2
295     eval local pidfile=\$${DAEMON}_PIDFILE
296     if test -f "$pidfile"; then
297         local pid=$(cat "$pidfile")
298         action "Killing `basename $BINARY` ($pid)" kill $pid
299         for delay in .1 .25 .65 1 1 1 1; do
300             if kill -0 $pid >/dev/null 2>&1; then
301                 sleep $delay
302             else
303                 break
304             fi
305         done
306         rm -f "$pidfile"
307     fi
308 }
309
310 function restart_approval {
311     if test ! -t 0; then
312         # Don't prompt if invoked non-interactively.
313         return 0
314     fi
315     cat <<EOF
316
317 WARNING!!!
318
319 Restarting Open vSwitch on a live server is not guaranteed to work.  It is
320 provided as a convenience for those situations in which it does work.
321
322 EOF
323     read -s -r -n 1 -p "Continue with restart (y/N): " response
324     printf "\n"
325     case "$response" in
326         y|Y)
327             return 0
328             ;;
329         *)
330             return 1
331             ;;
332     esac
333 }
334
335 function set_system_ids {
336     if [ -f /etc/xensource-inventory ]; then
337         OVS_VERSION=`ovs-vswitchd --version | sed 's/.*) //;1q'`
338         action "Configuring Open vSwitch system IDs" true
339         $vsctl --no-wait --timeout=5 set Open_vSwitch . \
340             ovs-version="$OVS_VERSION" \
341             system-type="$PRODUCT_BRAND" \
342             system-version="$PRODUCT_VERSION-$BUILD_NUMBER" \
343             external-ids:system-id="$INSTALLATION_UUID" \
344             external-ids:xs-system-uuid="$INSTALLATION_UUID"
345     else
346         action "Configuring Open vSwitch system IDs" false
347     fi
348 }
349
350 function start {
351     if [ "$FORCE_COREFILES" = "y" ]; then
352         turn_on_corefiles
353     fi
354
355     insert_modules_if_required
356
357     # Increase the limit on the number of open file descriptors since
358     # ovs-vswitchd needs a few per bridge
359     ulimit -n 4096
360
361     # Allow GRE traffic.
362     iptables -I INPUT -p gre -j ACCEPT
363
364     schemaver=`$ovsdb_tool schema-version "$VSWITCHD_OVSDB_SCHEMA"`
365     if [ ! -e "$OVSDB_SERVER_DB" ]; then
366         warning "$OVSDB_SERVER_DB does not exist"
367         install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
368
369         action "Creating empty database $OVSDB_SERVER_DB" true
370         $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
371     else
372         # If schema version changed, then back up the old version.
373         oldver=`$ovsdb_tool db-version "$OVSDB_SERVER_DB"`
374         if test "X$oldver" != "X$schemaver"; then
375             backup=$OVSDB_SERVER_DB.backup$oldver
376             action "Backing up $OVSDB_SERVER_DB in $backup before converting from schema version \"$oldver\" to \"$schemaver\"" true
377             cp "$OVSDB_SERVER_DB" "$backup"
378         fi
379
380         # Upgrade or downgrade schema and compact database.
381         $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
382     fi
383
384     start_ovsdb_server
385     $vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schemaver"
386     if [ ! -e /var/run/openvswitch.booted ]; then
387         touch /var/run/openvswitch.booted
388         for bridge in $($vsctl list-br); do
389             $vsctl --no-wait --timeout=5 del-br $bridge
390         done
391     fi
392
393     set_system_ids
394
395     start_vswitchd
396     if [ "${ENABLE_BRCOMPAT}" = "y" ] ; then
397         start_brcompatd
398     fi
399
400     # Start daemon to monitor external ids
401     PYTHONPATH=/usr/share/openvswitch/python \
402                /usr/share/openvswitch/scripts/ovs-external-ids \
403                --pidfile --detach $monitor_opt "$VSWITCHD_OVSDB_SERVER"
404
405     touch /var/lock/subsys/openvswitch
406 }
407
408 function stop {
409     stop_daemon BRCOMPATD "$brcompatd"
410     stop_daemon VSWITCHD "$vswitchd"
411     stop_daemon OVSDB_SERVER "$ovsdb_server"
412     if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
413         kill `cat /var/run/openvswitch/ovs-external-ids.pid`
414     fi
415     rm -f /var/lock/subsys/openvswitch
416 }
417
418 function restart {
419     if restart_approval; then
420         stop
421         start
422     fi
423 }
424
425 case "$1" in
426     start)
427         start
428         ;;
429     stop)
430         stop
431         ;;
432     restart)
433         restart
434         ;;
435     reload|force-reload)
436         # Nothing to do to ovs-vswitchd and ovsdb-server as they keep their
437         # configuration up-to-date all the time.  HUP ovs-external-ids so it
438         # re-runs.
439         hup_monitor_external_ids
440         ;;
441     strace-vswitchd)
442         shift
443         strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
444         ;;
445     strace-brcompatd)
446         shift
447         strace -p $(cat "$BRCOMPATD_PIDFILE") "$@"
448         ;;
449     status)
450         status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
451         status -p "$VSWITCHD_PIDFILE" ovs-vswitchd &&
452         (test "$ENABLE_BRCOMPAT" != "y" || 
453             status -p "$BRCOMPATD_PIDFILE" ovs-brcompatd)
454         ;;
455     version)
456         /usr/sbin/ovsdb-server -V
457         /usr/sbin/ovs-vswitchd -V
458         /usr/sbin/ovs-brcompatd -V
459         ;;
460     help)
461         printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
462         ;;
463     *)
464         printf "Unknown command: $1\n"
465         exit 1
466         ;;
467 esac