]> git.proxmox.com Git - mirror_ovs.git/blame - xenserver/etc_init.d_openvswitch
xenserver: modify module spec file
[mirror_ovs.git] / xenserver / etc_init.d_openvswitch
CommitLineData
064af421
BP
1#!/bin/bash
2#
bc391960 3# openvswitch
064af421
BP
4#
5# chkconfig: 2345 09 91
bc391960 6# description: Manage Open vSwitch kernel modules and user-space daemons
064af421 7
72dfb3b4 8# Copyright (C) 2009, 2010, 2011 Nicira Networks, Inc.
064af421 9#
a14bc59f
BP
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:
064af421 13#
a14bc59f 14# http://www.apache.org/licenses/LICENSE-2.0
064af421 15#
a14bc59f
BP
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.
72dfb3b4
BP
21### BEGIN INIT INFO
22# Provides: openvswitch-switch
7f9bd4e8
JP
23# Required-Start:
24# Required-Stop:
72dfb3b4
BP
25# Default-Start: 2 3 4 5
26# Default-Stop: 0 1 6
27# Short-Description: Open vSwitch switch
28### END INIT INFO
29
6f83469d
BP
30# source function library
31if [ -f /etc/init.d/functions ]; then
32 . /etc/init.d/functions
33elif [ -f /etc/rc.d/init.d/functions ]; then
34 . /etc/rc.d/init.d/functions
35elif [ -f /lib/lsb/init-functions ]; then
36 . /lib/lsb/init-functions
37else
38 echo "$0: missing LSB shell function library" >&2
39 exit 1
40fi
064af421 41
6f83469d
BP
42if type action >/dev/null 2>&1; then
43 :
44else
45 # SUSE lacks action
46 action() {
47 STRING=$1
48 shift
49 "$@"
50 rc=$?
51 if [ $rc -eq 0 ] ; then
e20d7fc6 52 log_success_msg $"$STRING "
6f83469d 53 else
e20d7fc6 54 log_failure_msg $"$STRING "
6f83469d
BP
55 fi
56 return $rc
57 }
58fi
064af421 59
77fd0ab5 60test -e /etc/xensource-inventory && . /etc/xensource-inventory
bc391960 61test -e /etc/sysconfig/openvswitch && . /etc/sysconfig/openvswitch
77fd0ab5
PM
62if test -e /etc/xensource/network.conf; then
63 NETWORK_MODE=$(cat /etc/xensource/network.conf)
64fi
064af421 65
77fd0ab5 66case ${NETWORK_MODE:=openvswitch} in
697e8aa2
JP
67 vswitch|openvswitch)
68 ;;
c6f196a0
JP
69 bridge)
70 exit 0
71 ;;
697e8aa2 72 *)
c6f196a0 73 echo "Open vSwitch disabled (/etc/xensource/network.conf is invalid)" >&2
697e8aa2
JP
74 exit 0
75 ;;
76esac
0313fb2e 77
f3ac83df 78: ${ENABLE_MONITOR:=y}
df9fd4a5 79: ${FORCE_COREFILES:=y}
064af421 80
fea28b07 81# Config variables specific to ovsdb-server
289df16d 82: ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,manager_options}
bc391960
JP
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}
fea28b07 86: ${OVSDB_SERVER_PRIORITY:=-10}
bc391960 87: ${OVSDB_SERVER_LOGFILE:=/var/log/openvswitch/ovsdb-server.log}
fea28b07
JP
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
064af421 96# Config variables specific to ovs-vswitchd
bc391960
JP
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}
44cb492f 101: ${VSWITCHD_PRIORITY:=-10}
a0bc29a5 102: ${VSWITCHD_MLOCKALL:=yes}
bc391960 103: ${VSWITCHD_LOGFILE:=/var/log/openvswitch/ovs-vswitchd.log}
39fb0881
JP
104: ${VSWITCHD_FILE_LOGLEVEL:=INFO}
105: ${VSWITCHD_SYSLOG_LOGLEVEL:=ERR}
df9fd4a5
BP
106: ${VSWITCHD_MEMLEAK_LOGFILE:=}
107: ${VSWITCHD_STRACE_LOG:=}
108: ${VSWITCHD_STRACE_OPT:=}
109: ${VSWITCHD_VALGRIND_LOG:=}
110: ${VSWITCHD_VALGRIND_OPT:=}
064af421 111
b3d5b900
BP
112: ${OVS_XAPI_SYNC_PIDFILE:=/var/run/openvswitch/ovs-xapi-sync.pid}
113
064af421 114# Full paths to executables & modules
fea28b07
JP
115ovsdb_server="/usr/sbin/ovsdb-server"
116ovsdb_tool="/usr/bin/ovsdb-tool"
eaa3c7e8 117vswitchd="/usr/sbin/ovs-vswitchd"
eaa3c7e8
BP
118dpctl="/usr/bin/ovs-dpctl"
119appctl="/usr/bin/ovs-appctl"
120ofctl="/usr/bin/ovs-ofctl"
e404c41f 121vsctl="/usr/bin/ovs-vsctl"
064af421 122
f3ac83df
BP
123if test "$ENABLE_MONITOR" = "y"; then
124 monitor_opt="--monitor"
125else
126 monitor_opt=
127fi
128
97685b90 129function hup_monitor_external_ids {
a49fe70c
JP
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`
97685b90
EJ
132 fi
133}
134
064af421 135function turn_on_corefiles {
16cd277e 136 ulimit -Sc 67108864
064af421
BP
137}
138
139function remove_all_dp {
d650378e 140 for dp in $($dpctl dump-dps); do
064af421
BP
141 action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
142 done
143}
144
145function insert_modules_if_required {
8d2662a5 146 if test -e /sys/module/bridge; then
e20d7fc6
JP
147 bridges=`echo /sys/class/net/*/bridge | sed 's,/sys/class/net/,,g;s,/bridge,,g'`
148 if test "$bridges" != "*"; then
8d2662a5
PM
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
064af421
BP
154 if ! lsmod | grep -q "openvswitch_mod"; then
155 action "Inserting llc module" modprobe llc
6867bc36 156 action "Inserting openvswitch module" modprobe openvswitch_mod
064af421 157 fi
064af421
BP
158}
159
160function remove_modules {
064af421
BP
161 if lsmod | grep -q "openvswitch_mod"; then
162 action "Removing openvswitch module" rmmod openvswitch_mod.ko
163 fi
164}
165
55e19919
BP
166function 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"
fea28b07
JP
194 fi
195 fi
55e19919
BP
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
fea28b07
JP
203 printf "Can not start with both VALGRIND and STRACE\n"
204 exit 1
55e19919
BP
205 elif test -n "$strace_log"; then
206 local mode=strace
e7eacf16 207 set -- strace -o "$strace_log" $strace_opt "$@"
55e19919
BP
208 elif test -n "$valgrind_log"; then
209 local mode=valgrind
e7eacf16 210 set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
fea28b07 211 else
55e19919
BP
212 local mode=production
213 eval local pidfile=\$${DAEMON}_PIDFILE
9cad6844 214 install -d -m 755 -o root -g root `dirname $pidfile`
e7eacf16 215 set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
fea28b07 216 fi
fea28b07 217
55e19919
BP
218 # Configure niceness.
219 eval local priority=\$${DAEMON}_PRIORITY
220 if test -n "$priority"; then
221 set -- nice -n $priority "$@"
16cd277e 222 fi
bc391960 223
55e19919
BP
224 if test $mode = production; then
225 action "Starting `basename $BINARY`" "$@"
226 else
227 # Start in background and force a "success" message
b1cd4308 228 action "Starting `basename $BINARY` with $mode debugging" true
55e19919 229 ("$@") &
064af421 230 fi
55e19919 231}
bc391960 232
55e19919
BP
233function 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
242function start_vswitchd {
55e19919 243 local mlockall_opt=
86a06318
BP
244 if [ "$VSWITCHD_MLOCKALL" != "no" ]; then
245 mlockall_opt="--mlockall"
246 fi
bc391960 247
34edeccf 248 start_daemon VSWITCHD "$vswitchd" $mlockall_opt "$VSWITCHD_OVSDB_SERVER"
55e19919 249 }
064af421 250
55e19919
BP
251function 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
2cc90641
BP
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
55e19919 265 rm -f "$pidfile"
2e8a4c31
BP
266 fi
267}
268
064af421
BP
269function restart_approval {
270 if test ! -t 0; then
271 # Don't prompt if invoked non-interactively.
272 return 0
273 fi
274 cat <<EOF
275
276WARNING!!!
277
bc391960 278Restarting Open vSwitch on a live server is not guaranteed to work. It is
064af421 279provided as a convenience for those situations in which it does work.
064af421
BP
280
281EOF
07064456 282 read -s -r -n 1 -p "Continue with restart (y/N): " response
064af421
BP
283 printf "\n"
284 case "$response" in
285 y|Y)
286 return 0
287 ;;
288 *)
289 return 1
290 ;;
291 esac
292}
293
bfe62d7a
JP
294function set_system_ids {
295 if [ -f /etc/xensource-inventory ]; then
538c6dfa 296 OVS_VERSION=`ovs-vswitchd --version | sed 's/.*) //;1q'`
bfe62d7a 297 action "Configuring Open vSwitch system IDs" true
6b7b9d34 298 $vsctl --no-wait --timeout=5 set Open_vSwitch . \
538c6dfa
BP
299 ovs-version="$OVS_VERSION" \
300 system-type="$PRODUCT_BRAND" \
301 system-version="$PRODUCT_VERSION-$BUILD_NUMBER" \
3a46ca6e
BP
302 external-ids:system-id="$INSTALLATION_UUID" \
303 external-ids:xs-system-uuid="$INSTALLATION_UUID"
f5e7ed5d 304 else
e20d7fc6 305 if test -f /etc/openvswitch/install_uuid.conf; then
2ce949ba 306 . /etc/openvswitch/install_uuid.conf
e20d7fc6
JP
307 elif INSTALLATION_UUID=`uuidgen`; then
308 echo "INSTALLATION_UUID=$INSTALLATION_UUID" > /etc/openvswitch/install_uuid.conf
309 else
310 log_failure_msg "missing uuidgen, could not generate system UUID"
311 return
2ce949ba
BP
312 fi
313 $vsctl --no-wait --timeout=5 set Open_vSwitch . \
314 external-ids:system-id="$INSTALLATION_UUID"
315 action "Configuring Open vSwitch system IDs" true
f5e7ed5d
JP
316 fi
317}
318
064af421 319function start {
ae825abb
JP
320 if [ "$FORCE_COREFILES" = "y" ]; then
321 turn_on_corefiles
322 fi
323
064af421 324 insert_modules_if_required
63920cb9
JP
325
326 # Increase the limit on the number of open file descriptors since
327 # ovs-vswitchd needs a few per bridge
328 ulimit -n 4096
329
1e50dd6c
JG
330 # Allow GRE traffic.
331 iptables -I INPUT -p gre -j ACCEPT
332
8159b984 333 schemaver=`$ovsdb_tool schema-version "$VSWITCHD_OVSDB_SCHEMA"`
fea28b07
JP
334 if [ ! -e "$OVSDB_SERVER_DB" ]; then
335 warning "$OVSDB_SERVER_DB does not exist"
bc391960 336 install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
f72e2b36
JP
337
338 action "Creating empty database $OVSDB_SERVER_DB" true
ef5e2fe5 339 $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
f8186f2c
BP
340 elif test "X`$ovsdb_tool needs-conversion "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"`" != Xno; then
341 # Back up the old version.
342 version=`$ovsdb_tool db-version "$OVSDB_SERVER_DB"`
343 cksum=`$ovsdb_tool db-cksum "$OVSDB_SERVER_DB" | awk '{print $1}'`
344 cp "$OVSDB_SERVER_DB" "$OVSDB_SERVER_DB.backup$version-$cksum"
8159b984 345
c5f341ab
BP
346 # Compact database. This is important if the old schema did not enable
347 # garbage collection (i.e. if it did not have any tables with "isRoot":
348 # true) but the new schema does. In that situation the old database
349 # may contain a transaction that creates a record followed by a
350 # transaction that creates the first use of the record. Replaying that
351 # series of transactions against the new database schema (as "convert"
352 # does) would cause the record to be dropped by the first transaction,
353 # then the second transaction would cause a referential integrity
354 # failure (for a strong reference).
355 $ovsdb_tool -vANY:console:emer compact "$OVSDB_SERVER_DB"
356
357 # Upgrade or downgrade schema.
23817ac7 358 $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
b1bdd85c
BP
359 fi
360
fea28b07 361 start_ovsdb_server
8159b984 362 $vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schemaver"
bc391960
JP
363 if [ ! -e /var/run/openvswitch.booted ]; then
364 touch /var/run/openvswitch.booted
e404c41f 365 for bridge in $($vsctl list-br); do
6b7b9d34 366 $vsctl --no-wait --timeout=5 del-br $bridge
e404c41f
BP
367 done
368 fi
369
bfe62d7a 370 set_system_ids
f5e7ed5d 371
064af421 372 start_vswitchd
df09921d 373
5badabf5
PM
374 if [ -f /etc/xensource-inventory ]; then
375 # Start daemon to monitor external ids
376 PYTHONPATH=/usr/share/openvswitch/python \
a49fe70c 377 /usr/share/openvswitch/scripts/ovs-xapi-sync \
5badabf5
PM
378 --pidfile --detach $monitor_opt "$VSWITCHD_OVSDB_SERVER"
379 fi
df09921d 380
bc391960 381 touch /var/lock/subsys/openvswitch
064af421
BP
382}
383
384function stop {
55e19919
BP
385 stop_daemon VSWITCHD "$vswitchd"
386 stop_daemon OVSDB_SERVER "$ovsdb_server"
b3d5b900 387 stop_daemon OVS_XAPI_SYNC ovs-xapi-sync
bc391960 388 rm -f /var/lock/subsys/openvswitch
064af421
BP
389}
390
391function restart {
392 if restart_approval; then
393 stop
394 start
395 fi
396}
397
f7a122fc
BP
398function internal_interfaces {
399 # Outputs a list of internal interfaces:
400 #
401 # - There is an internal interface for every bridge, whether it has
402 # an Interface record or not and whether the Interface record's
403 # 'type' is properly set.
404 #
405 # - There is an internal interface for each Interface record whose
406 # 'type' is 'internal'.
407 #
408 # But ignore interfaces that don't really exist.
409 for d in `(ovs-vsctl --bare \
410 -- --columns=name find Interface type=internal \
411 -- list-br) | sort -u`
412 do
413 if test -e "/sys/class/net/$d"; then
414 printf "%s " "$d"
415 fi
416 done
417}
418
419function force_reload_kmod {
420 ifaces=$(internal_interfaces)
421 action "Configured internal interfaces: $ifaces" true
422
423 stop
424
425 script=$(mktemp)
426 action "Save interface configuration to $script" true
427 if ! /usr/share/openvswitch/scripts/ovs-save $ifaces > $script; then
428 warning "Failed to save configuration, not replacing kernel module"
429 start
430 exit 1
431 fi
432 chmod +x $script
433
434 action "Destroy datapaths" remove_all_dp
435
436 remove_modules
437
438 start
439
440 action "Restore interface configuration from $script" $script
441}
442
064af421
BP
443case "$1" in
444 start)
064af421
BP
445 start
446 ;;
447 stop)
448 stop
449 ;;
450 restart)
451 restart
452 ;;
210374eb 453 reload|force-reload)
97685b90 454 # Nothing to do to ovs-vswitchd and ovsdb-server as they keep their
a49fe70c 455 # configuration up-to-date all the time. HUP ovs-xapi-sync so it
97685b90
EJ
456 # re-runs.
457 hup_monitor_external_ids
e20d7fc6 458 ;;
064af421
BP
459 strace-vswitchd)
460 shift
461 strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
462 ;;
064af421 463 status)
a1864c5f 464 status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
34edeccf 465 status -p "$VSWITCHD_PIDFILE" ovs-vswitchd
064af421
BP
466 ;;
467 version)
70feacd5 468 /usr/sbin/ovsdb-server -V
eaa3c7e8 469 /usr/sbin/ovs-vswitchd -V
064af421 470 ;;
f7a122fc
BP
471 force-reload-kmod)
472 force_reload_kmod
473 ;;
064af421 474 help)
210374eb 475 printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
064af421
BP
476 ;;
477 *)
478 printf "Unknown command: $1\n"
479 exit 1
480 ;;
481esac