Prepare Open vSwitch 1.1.2 release.
[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:
24 # Required-Stop:
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 : ${ENABLE_MONITOR:=y}
79 : ${FORCE_COREFILES:=y}
80
81 # Config variables specific to ovsdb-server
82 : ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,manager_options}
83 : ${OVSDB_SERVER_DB:=/etc/openvswitch/conf.db}
84 : ${OVSDB_SERVER_PIDFILE:=/var/run/openvswitch/ovsdb-server.pid}
85 : ${OVSDB_SERVER_RUN_DIR:=/var/xen/openvswitch}
86 : ${OVSDB_SERVER_PRIORITY:=-10}
87 : ${OVSDB_SERVER_LOGFILE:=/var/log/openvswitch/ovsdb-server.log}
88 : ${OVSDB_SERVER_FILE_LOGLEVEL:=INFO}
89 : ${OVSDB_SERVER_SYSLOG_LOGLEVEL:=ERR}
90 : ${OVSDB_SERVER_MEMLEAK_LOGFILE:=}
91 : ${OVSDB_SERVER_STRACE_LOG:=}
92 : ${OVSDB_SERVER_STRACE_OPT:=}
93 : ${OVSDB_SERVER_VALGRIND_LOG:=}
94 : ${OVSDB_SERVER_VALGRIND_OPT:=}
95
96 # Config variables specific to ovs-vswitchd
97 : ${VSWITCHD_OVSDB_SERVER:=unix:/var/run/openvswitch/db.sock}
98 : ${VSWITCHD_OVSDB_SCHEMA:=/usr/share/openvswitch/vswitch.ovsschema}
99 : ${VSWITCHD_PIDFILE:=/var/run/openvswitch/ovs-vswitchd.pid}
100 : ${VSWITCHD_RUN_DIR:=/var/xen/openvswitch}
101 : ${VSWITCHD_PRIORITY:=-10}
102 : ${VSWITCHD_MLOCKALL:=yes}
103 : ${VSWITCHD_LOGFILE:=/var/log/openvswitch/ovs-vswitchd.log}
104 : ${VSWITCHD_FILE_LOGLEVEL:=INFO}
105 : ${VSWITCHD_SYSLOG_LOGLEVEL:=ERR}
106 : ${VSWITCHD_MEMLEAK_LOGFILE:=}
107 : ${VSWITCHD_STRACE_LOG:=}
108 : ${VSWITCHD_STRACE_OPT:=}
109 : ${VSWITCHD_VALGRIND_LOG:=}
110 : ${VSWITCHD_VALGRIND_OPT:=}
111
112 : ${OVS_XAPI_SYNC_PIDFILE:=/var/run/openvswitch/ovs-xapi-sync.pid}
113
114 # Full paths to executables & modules
115 ovsdb_server="/usr/sbin/ovsdb-server"
116 ovsdb_tool="/usr/bin/ovsdb-tool"
117 vswitchd="/usr/sbin/ovs-vswitchd"
118 dpctl="/usr/bin/ovs-dpctl"
119 appctl="/usr/bin/ovs-appctl"
120 ofctl="/usr/bin/ovs-ofctl"
121 vsctl="/usr/bin/ovs-vsctl"
122
123 if test "$ENABLE_MONITOR" = "y"; then
124     monitor_opt="--monitor"
125 else
126     monitor_opt=
127 fi
128
129 function hup_monitor_external_ids {
130     if [ -e /var/run/openvswitch/ovs-xapi-sync.pid ]; then
131         action "Configuring Open vSwitch external IDs" kill -HUP `cat /var/run/openvswitch/ovs-xapi-sync.pid`
132     fi
133 }
134
135 function turn_on_corefiles {
136     ulimit -Sc 67108864
137 }
138
139 function remove_all_dp {
140     for dp in $($dpctl dump-dps); do
141         action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
142     done
143 }
144
145 function insert_modules_if_required {
146     if test -e /sys/module/bridge; then
147         bridges=`echo /sys/class/net/*/bridge | sed 's,/sys/class/net/,,g;s,/bridge,,g'`
148         if test "$bridges" != "*"; then
149             log_warning_msg "not removing bridge module because bridges exist ($bridges)"
150         else
151             action "removing bridge module" rmmod bridge
152         fi
153     fi
154     if ! lsmod | grep -q "openvswitch_mod"; then
155         action "Inserting llc module" modprobe llc
156         action "Inserting openvswitch module" modprobe openvswitch_mod
157     fi
158 }
159
160 function remove_modules {
161     if lsmod | grep -q "openvswitch_mod"; then
162         action "Removing openvswitch module" rmmod openvswitch_mod.ko
163     fi
164 }
165
166 function start_daemon {
167     local DAEMON=$1
168     shift
169     local BINARY=$1
170
171     # cd to daemon's run_dir so core files get dumped into a sensible place.
172     eval local run_dir=\$${DAEMON}_RUN_DIR
173     if [ ! -d "$run_dir" ]; then
174         install -d -m 755 -o root -g root "$run_dir"
175     fi
176     cd "$run_dir"
177     
178     # Configure log levels.
179     eval local syslog_loglevel=\$${DAEMON}_SYSLOG_LOGLEVEL
180     eval local file_loglevel=\$${DAEMON}_FILE_LOGLEVEL
181     eval local logfile=\$${DAEMON}_LOGFILE
182     set -- "$@" -vANY:CONSOLE:EMER -vANY:SYSLOG:"$syslog_loglevel"
183     if test -n "$file_loglevel" && test -n "$logfile"; then
184         install -d -m 755 -o root -g root `dirname "$logfile"`
185         set -- "$@" --log-file="$logfile" -vANY:FILE:"$file_loglevel"
186     fi
187
188     # Configure leak checker.
189     eval local memleak_logfile=\$${DAEMON}_MEMLEAK_LOGFILE
190     if test -n "$memleak_logfile"; then
191         set -- "$@" --check-leaks="$memleak_logfile"
192         if test -e "$memleak_logfile"; then
193             mv "$memleak_logfile" "$memleak_logfile.prev"
194         fi
195     fi
196
197     # Configure debugging wrappers.
198     eval local strace_log=\$${DAEMON}_STRACE_LOG
199     eval local strace_opt=\$${DAEMON}_STRACE_OPT
200     eval local valgrind_log=\$${DAEMON}_VALGRIND_LOG
201     eval local valgrind_opt=\$${DAEMON}_VALGRIND_OPT
202     if test -n "$strace_log" && test -n "$valgrind_log"; then
203         printf "Can not start with both VALGRIND and STRACE\n"
204         exit 1
205     elif test -n "$strace_log"; then
206         local mode=strace
207         set -- strace -o "$strace_log" $strace_opt "$@"
208     elif test -n "$valgrind_log"; then
209         local mode=valgrind
210         set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
211     else
212         local mode=production
213         eval local pidfile=\$${DAEMON}_PIDFILE
214         install -d -m 755 -o root -g root `dirname $pidfile`
215         set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
216     fi
217
218     # Configure niceness.
219     eval local priority=\$${DAEMON}_PRIORITY
220     if test -n "$priority"; then
221         set -- nice -n $priority "$@"
222     fi
223
224     if test $mode = production; then
225         action "Starting `basename $BINARY`" "$@"
226     else
227         # Start in background and force a "success" message
228         action "Starting `basename $BINARY` with $mode debugging" true
229         ("$@") &
230     fi
231 }
232
233 function start_ovsdb_server {
234     set -- "$ovsdb_server" "$OVSDB_SERVER_DB"
235     for remote in $OVSDB_SERVER_REMOTES; do
236         set -- "$@" --remote="$remote"
237     done
238     set -- "$@" --private-key=db:SSL,private_key --certificate=db:SSL,certificate --bootstrap-ca-cert=db:SSL,ca_cert
239     start_daemon OVSDB_SERVER "$@"
240 }
241
242 function start_vswitchd {
243     local mlockall_opt=
244     if [ "$VSWITCHD_MLOCKALL" != "no" ]; then
245         mlockall_opt="--mlockall"
246     fi
247
248     start_daemon VSWITCHD "$vswitchd" $mlockall_opt "$VSWITCHD_OVSDB_SERVER"
249  }
250
251 function stop_daemon {
252     local DAEMON=$1
253     local BINARY=$2
254     eval local pidfile=\$${DAEMON}_PIDFILE
255     if test -f "$pidfile"; then
256         local pid=$(cat "$pidfile")
257         action "Killing `basename $BINARY` ($pid)" kill $pid
258         for delay in .1 .25 .65 1 1 1 1; do
259             if kill -0 $pid >/dev/null 2>&1; then
260                 sleep $delay
261             else
262                 break
263             fi
264         done
265     fi
266 }
267
268 function restart_approval {
269     if test ! -t 0; then
270         # Don't prompt if invoked non-interactively.
271         return 0
272     fi
273     cat <<EOF
274
275 WARNING!!!
276
277 Restarting Open vSwitch on a live server is not guaranteed to work.  It is
278 provided as a convenience for those situations in which it does work.
279
280 EOF
281     read -s -r -n 1 -p "Continue with restart (y/N): " response
282     printf "\n"
283     case "$response" in
284         y|Y)
285             return 0
286             ;;
287         *)
288             return 1
289             ;;
290     esac
291 }
292
293 function set_system_ids {
294     if [ -f /etc/xensource-inventory ]; then
295         OVS_VERSION=`ovs-vswitchd --version | sed 's/.*) //;1q'`
296         action "Configuring Open vSwitch system IDs" true
297         $vsctl --no-wait --timeout=5 set Open_vSwitch . \
298             ovs-version="$OVS_VERSION" \
299             system-type="$PRODUCT_BRAND" \
300             system-version="$PRODUCT_VERSION-$BUILD_NUMBER" \
301             external-ids:system-id="$INSTALLATION_UUID" \
302             external-ids:xs-system-uuid="$INSTALLATION_UUID"
303     else
304         if test -f /etc/openvswitch/install_uuid.conf; then
305             . /etc/openvswitch/install_uuid.conf
306         elif INSTALLATION_UUID=`uuidgen`; then
307             echo "INSTALLATION_UUID=$INSTALLATION_UUID" > /etc/openvswitch/install_uuid.conf
308         else
309             log_failure_msg "missing uuidgen, could not generate system UUID"
310             return
311         fi
312         $vsctl --no-wait --timeout=5 set Open_vSwitch . \
313             external-ids:system-id="$INSTALLATION_UUID"
314         action "Configuring Open vSwitch system IDs" true
315     fi
316 }
317
318 function start {
319     if [ "$FORCE_COREFILES" = "y" ]; then
320         turn_on_corefiles
321     fi
322
323     insert_modules_if_required
324
325     # Increase the limit on the number of open file descriptors since
326     # ovs-vswitchd needs a few per bridge
327     ulimit -n 4096
328
329     # Allow GRE traffic.
330     iptables -I INPUT -p gre -j ACCEPT
331
332     schemaver=`$ovsdb_tool schema-version "$VSWITCHD_OVSDB_SCHEMA"`
333     if [ ! -e "$OVSDB_SERVER_DB" ]; then
334         warning "$OVSDB_SERVER_DB does not exist"
335         install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
336
337         action "Creating empty database $OVSDB_SERVER_DB" true
338         $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
339     elif test "X`$ovsdb_tool needs-conversion "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"`" != Xno; then
340         # Back up the old version.
341         version=`$ovsdb_tool db-version "$OVSDB_SERVER_DB"`
342         cksum=`$ovsdb_tool db-cksum "$OVSDB_SERVER_DB" | awk '{print $1}'`
343         cp "$OVSDB_SERVER_DB" "$OVSDB_SERVER_DB.backup$version-$cksum"
344
345         # Compact database.  This is important if the old schema did not enable
346         # garbage collection (i.e. if it did not have any tables with "isRoot":
347         # true) but the new schema does.  In that situation the old database
348         # may contain a transaction that creates a record followed by a
349         # transaction that creates the first use of the record.  Replaying that
350         # series of transactions against the new database schema (as "convert"
351         # does) would cause the record to be dropped by the first transaction,
352         # then the second transaction would cause a referential integrity
353         # failure (for a strong reference).
354         $ovsdb_tool -vANY:console:emer compact "$OVSDB_SERVER_DB"
355
356         # Upgrade or downgrade schema.
357         $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
358     fi
359
360     start_ovsdb_server
361     $vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schemaver"
362     if [ ! -e /var/run/openvswitch.booted ]; then
363         touch /var/run/openvswitch.booted
364         for bridge in $($vsctl list-br); do
365             $vsctl --no-wait --timeout=5 del-br $bridge
366         done
367     fi
368
369     set_system_ids
370
371     start_vswitchd
372
373     if [ -f /etc/xensource-inventory ]; then
374         # Start daemon to monitor external ids
375         PYTHONPATH=/usr/share/openvswitch/python \
376                    /usr/share/openvswitch/scripts/ovs-xapi-sync \
377                    --pidfile --detach $monitor_opt "$VSWITCHD_OVSDB_SERVER"
378     fi
379
380     touch /var/lock/subsys/openvswitch
381 }
382
383 function stop {
384     stop_daemon VSWITCHD "$vswitchd"
385     stop_daemon OVSDB_SERVER "$ovsdb_server"
386     stop_daemon OVS_XAPI_SYNC ovs-xapi-sync
387     rm -f /var/lock/subsys/openvswitch
388 }
389
390 function restart {
391     if restart_approval; then
392         stop
393         start
394     fi
395 }
396
397 function internal_interfaces {
398     # Outputs a list of internal interfaces:
399     #
400     #   - There is an internal interface for every bridge, whether it has
401     #     an Interface record or not and whether the Interface record's
402     #     'type' is properly set.
403     #
404     #   - There is an internal interface for each Interface record whose
405     #     'type' is 'internal'.
406     #
407     # But ignore interfaces that don't really exist.
408     for d in `(ovs-vsctl --bare \
409                 -- --columns=name find Interface type=internal \
410                 -- list-br) | sort -u`
411     do
412         if test -e "/sys/class/net/$d"; then
413             printf "%s " "$d"
414         fi
415     done
416 }
417
418 function force_reload_kmod {
419     ifaces=$(internal_interfaces)
420     action "Configured internal interfaces: $ifaces" true
421
422     stop
423
424     script=$(mktemp)
425     action "Save interface configuration to $script" true
426     if ! /usr/share/openvswitch/scripts/ovs-save $ifaces > $script; then
427         warning "Failed to save configuration, not replacing kernel module"
428         start
429         exit 1
430     fi
431     chmod +x $script
432
433     action "Destroy datapaths" remove_all_dp
434
435     remove_modules
436
437     start
438
439     action "Restore interface configuration from $script" $script
440
441     # Restart the high-availability daemon if it is running.  Otherwise
442     # it loses its heartbeat and reboots the system after a few minutes.
443     if pidof xhad >/dev/null && test -e /etc/xensource/xhad.conf; then
444         PATH=$PATH:/opt/xensource/xha
445         action "Stopping HA daemon" ha_stop_daemon
446         action "Starting HA daemon" ha_start_daemon
447     fi
448 }
449
450 case "$1" in
451     start)
452         start
453         ;;
454     stop)
455         stop
456         ;;
457     restart)
458         restart
459         ;;
460     reload|force-reload)
461         # Nothing to do to ovs-vswitchd and ovsdb-server as they keep their
462         # configuration up-to-date all the time.  HUP ovs-xapi-sync so it
463         # re-runs.
464         hup_monitor_external_ids
465         ;;
466     strace-vswitchd)
467         shift
468         strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
469         ;;
470     status)
471         status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
472         status -p "$VSWITCHD_PIDFILE" ovs-vswitchd
473         ;;
474     version)
475         /usr/sbin/ovsdb-server -V
476         /usr/sbin/ovs-vswitchd -V
477         ;;
478     force-reload-kmod)
479         force_reload_kmod
480         ;;
481     help)
482         printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
483         ;;
484     *)
485         printf "Unknown command: $1\n"
486         exit 1
487         ;;
488 esac