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