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