xenserver: Create network.conf before running interface-reconfigure.
[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 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
22 . /etc/init.d/functions
23
24 . /etc/xensource-inventory
25 test -e /etc/sysconfig/openvswitch && . /etc/sysconfig/openvswitch
26
27 NETWORK_MODE=$(cat /etc/xensource/network.conf)
28 case $NETWORK_MODE in
29     vswitch|openvswitch)
30         ;;
31     bridge)
32         exit 0
33         ;;
34     *)
35         echo "Open vSwitch disabled (/etc/xensource/network.conf is invalid)" >&2
36         exit 0
37         ;;
38 esac
39
40 # General config variables in /etc/sysconfig/openvswitch
41 if test "$PRODUCT_VERSION" = "5.5.0"; then
42     # XenServer 5.5.0 needs ovs-brcompatd and /proc/net simulation.
43     : ${ENABLE_BRCOMPAT:=y}
44     : ${ENABLE_FAKE_PROC_NET:=y}
45 else
46     # Later versions don't need them.
47     : ${ENABLE_BRCOMPAT:=n}
48     : ${ENABLE_FAKE_PROC_NET:=n}
49 fi
50 : ${ENABLE_MONITOR:=y}
51 : ${FORCE_COREFILES:=y}
52
53 # Config variables specific to ovsdb-server
54 : ${OVSDB_SERVER_REMOTES:=punix:/var/run/openvswitch/db.sock db:Open_vSwitch,managers}
55 : ${OVSDB_SERVER_DB:=/etc/openvswitch/conf.db}
56 : ${OVSDB_SERVER_PIDFILE:=/var/run/openvswitch/ovsdb-server.pid}
57 : ${OVSDB_SERVER_RUN_DIR:=/var/xen/openvswitch}
58 : ${OVSDB_SERVER_PRIORITY:=-10}
59 : ${OVSDB_SERVER_LOGFILE:=/var/log/openvswitch/ovsdb-server.log}
60 : ${OVSDB_SERVER_FILE_LOGLEVEL:=INFO}
61 : ${OVSDB_SERVER_SYSLOG_LOGLEVEL:=ERR}
62 : ${OVSDB_SERVER_MEMLEAK_LOGFILE:=}
63 : ${OVSDB_SERVER_STRACE_LOG:=}
64 : ${OVSDB_SERVER_STRACE_OPT:=}
65 : ${OVSDB_SERVER_VALGRIND_LOG:=}
66 : ${OVSDB_SERVER_VALGRIND_OPT:=}
67
68 # Config variables specific to ovs-vswitchd
69 : ${VSWITCHD_OVSDB_SERVER:=unix:/var/run/openvswitch/db.sock}
70 : ${VSWITCHD_OVSDB_SCHEMA:=/usr/share/openvswitch/vswitch.ovsschema}
71 : ${VSWITCHD_PIDFILE:=/var/run/openvswitch/ovs-vswitchd.pid}
72 : ${VSWITCHD_RUN_DIR:=/var/xen/openvswitch}
73 : ${VSWITCHD_PRIORITY:=-10}
74 : ${VSWITCHD_MLOCKALL:=yes}
75 : ${VSWITCHD_LOGFILE:=/var/log/openvswitch/ovs-vswitchd.log}
76 : ${VSWITCHD_FILE_LOGLEVEL:=INFO}
77 : ${VSWITCHD_SYSLOG_LOGLEVEL:=ERR}
78 : ${VSWITCHD_MEMLEAK_LOGFILE:=}
79 : ${VSWITCHD_STRACE_LOG:=}
80 : ${VSWITCHD_STRACE_OPT:=}
81 : ${VSWITCHD_VALGRIND_LOG:=}
82 : ${VSWITCHD_VALGRIND_OPT:=}
83
84 # Config variables specific to ovs-brcompatd
85 : ${BRCOMPATD_PIDFILE:=/var/run/openvswitch/ovs-brcompatd.pid}
86 : ${BRCOMPATD_RUN_DIR:=/var/xen/openvswitch}
87 : ${BRCOMPATD_PRIORITY:=-10}
88 : ${BRCOMPATD_LOGFILE:=/var/log/openvswitch/ovs-brcompatd.log}
89 : ${BRCOMPATD_FILE_LOGLEVEL:=INFO}
90 : ${BRCOMPATD_SYSLOG_LOGLEVEL:=ERR}
91 : ${BRCOMPATD_MEMLEAK_LOGFILE:=}
92 : ${BRCOMPATD_STRACE_LOG:=}
93 : ${BRCOMPATD_STRACE_OPT:=}
94 : ${BRCOMPATD_VALGRIND_LOG:=}
95 : ${BRCOMPATD_VALGRIND_OPT:=}
96
97 # Full paths to executables & modules
98 ovsdb_server="/usr/sbin/ovsdb-server"
99 ovsdb_tool="/usr/bin/ovsdb-tool"
100 vswitchd="/usr/sbin/ovs-vswitchd"
101 brcompatd="/usr/sbin/ovs-brcompatd"
102 dpctl="/usr/bin/ovs-dpctl"
103 appctl="/usr/bin/ovs-appctl"
104 ofctl="/usr/bin/ovs-ofctl"
105 vsctl="/usr/bin/ovs-vsctl"
106
107 if [ "$ENABLE_FAKE_PROC_NET" = "y" ]; then
108     if [ "$ENABLE_BRCOMPAT" != "y" ]; then
109         warning "FAKE_PROC_NET required BRCOMPAT which was disabled.  Force enabling."
110         ENABLE_BRCOMPAT="y"
111     fi
112 fi
113
114 if test "$ENABLE_MONITOR" = "y"; then
115     monitor_opt="--monitor"
116 else
117     monitor_opt=
118 fi
119
120 function dp_list {
121     "$dpctl" show | grep '^dp[0-9]\+:' | cut -d':' -f 1
122 }
123
124 function turn_on_corefiles {
125     ulimit -Sc 67108864
126 }
127
128 function remove_all_dp {
129     for dp in $(dp_list); do
130         action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
131     done
132 }
133
134 function insert_modules_if_required {
135     if ! lsmod | grep -q "openvswitch_mod"; then
136         action "Inserting llc module" modprobe llc
137         action "Inserting openvswitch module" modprobe openvswitch_mod
138     fi
139     if [ "$ENABLE_BRCOMPAT" = "y" ] && [ -n "$BRCOMPATD_PIDFILE" ] && ! lsmod | grep -q "brcompat_mod"; then
140         action "Inserting brcompat module" modprobe brcompat_mod
141     fi
142 }
143
144 function remove_modules {
145     if lsmod | grep -q "brcompat_mod"; then
146         action "Removing brcompat module" rmmod brcompat_mod.ko
147     fi
148     if lsmod | grep -q "openvswitch_mod"; then
149         action "Removing openvswitch module" rmmod openvswitch_mod.ko
150     fi
151 }
152
153 function start_daemon {
154     local DAEMON=$1
155     shift
156     local BINARY=$1
157
158     # cd to daemon's run_dir so core files get dumped into a sensible place.
159     eval local run_dir=\$${DAEMON}_RUN_DIR
160     if [ ! -d "$run_dir" ]; then
161         install -d -m 755 -o root -g root "$run_dir"
162     fi
163     cd "$run_dir"
164     
165     # Configure log levels.
166     eval local syslog_loglevel=\$${DAEMON}_SYSLOG_LOGLEVEL
167     eval local file_loglevel=\$${DAEMON}_FILE_LOGLEVEL
168     eval local logfile=\$${DAEMON}_LOGFILE
169     set -- "$@" -vANY:CONSOLE:EMER -vANY:SYSLOG:"$syslog_loglevel"
170     if test -n "$file_loglevel" && test -n "$logfile"; then
171         install -d -m 755 -o root -g root `dirname "$logfile"`
172         set -- "$@" --log-file="$logfile" -vANY:FILE:"$file_loglevel"
173     fi
174
175     # Configure leak checker.
176     eval local memleak_logfile=\$${DAEMON}_MEMLEAK_LOGFILE
177     if test -n "$memleak_logfile"; then
178         set -- "$@" --check-leaks="$memleak_logfile"
179         if test -e "$memleak_logfile"; then
180             mv "$memleak_logfile" "$memleak_logfile.prev"
181         fi
182     fi
183
184     # Configure debugging wrappers.
185     eval local strace_log=\$${DAEMON}_STRACE_LOG
186     eval local strace_opt=\$${DAEMON}_STRACE_OPT
187     eval local valgrind_log=\$${DAEMON}_VALGRIND_LOG
188     eval local valgrind_opt=\$${DAEMON}_VALGRIND_OPT
189     if test -n "$strace_log" && test -n "$valgrind_log"; then
190         printf "Can not start with both VALGRIND and STRACE\n"
191         exit 1
192     elif test -n "$strace_log"; then
193         local mode=strace
194         set -- strace -o "$strace_log" $strace_opt "$@"
195     elif test -n "$valgrind_log"; then
196         local mode=valgrind
197         set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
198     else
199         local mode=production
200         eval local pidfile=\$${DAEMON}_PIDFILE
201         install -d -m 755 -o root -g root `dirname $pidfile`
202         set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
203     fi
204
205     # Configure niceness.
206     eval local priority=\$${DAEMON}_PRIORITY
207     if test -n "$priority"; then
208         set -- nice -n $priority "$@"
209     fi
210
211     if test $mode = production; then
212         action "Starting `basename $BINARY`" "$@"
213     else
214         # Start in background and force a "success" message
215         action "Starting `basename $BINARY` with $mode debugging" true
216         ("$@") &
217     fi
218 }
219
220 function start_ovsdb_server {
221     set -- "$ovsdb_server" "$OVSDB_SERVER_DB"
222     for remote in $OVSDB_SERVER_REMOTES; do
223         set -- "$@" --remote="$remote"
224     done
225     set -- "$@" --private-key=db:SSL,private_key --certificate=db:SSL,certificate --bootstrap-ca-cert=db:SSL,ca_cert
226     start_daemon OVSDB_SERVER "$@"
227 }
228
229 function start_vswitchd {
230     local fake_proc_net_opt=
231     if [ "$ENABLE_FAKE_PROC_NET" = "y" ]; then
232         fake_proc_net_opt="--fake-proc-net"
233     fi
234
235     local mlockall_opt=
236     if [ "$VSWITCHD_MLOCKALL" != "no" ]; then
237         mlockall_opt="--mlockall"
238     fi
239
240     start_daemon VSWITCHD "$vswitchd" $fake_proc_net_opt $mlockall_opt \
241                  "$VSWITCHD_OVSDB_SERVER"
242  }
243
244 function start_brcompatd {
245     start_daemon BRCOMPATD "$brcompatd" \
246                  --appctl-command="$appctl --target=/var/run/openvswitch/ovs-vswitchd.\`cat $VSWITCHD_PIDFILE\`.ctl %s" \
247                  "$VSWITCHD_OVSDB_SERVER"
248 }
249
250 function stop_daemon {
251     local DAEMON=$1
252     local BINARY=$2
253     eval local pidfile=\$${DAEMON}_PIDFILE
254     if test -f "$pidfile"; then
255         local pid=$(cat "$pidfile")
256         action "Killing `basename $BINARY` ($pid)" kill $pid
257         rm -f "$pidfile"
258     fi
259 }
260
261 function restart_approval {
262     if test ! -t 0; then
263         # Don't prompt if invoked non-interactively.
264         return 0
265     fi
266     cat <<EOF
267
268 WARNING!!!
269
270 Restarting Open vSwitch on a live server is not guaranteed to work.  It is
271 provided as a convenience for those situations in which it does work.
272
273 EOF
274     read -s -r -n 1 -p "Continue with restart (y/N): " response
275     printf "\n"
276     case "$response" in
277         y|Y)
278             return 0
279             ;;
280         *)
281             return 1
282             ;;
283     esac
284 }
285
286 function set_system_uuid {
287     if test -n "$INSTALLATION_UUID"; then
288         action "Configuring Open vSwitch system UUID" true
289         $vsctl --no-wait set Open_vSwitch . external-ids:system-uuid="$INSTALLATION_UUID"
290     else
291         action "Configuring Open vSwitch system UUID" false
292     fi
293 }
294
295 function start {
296     if [ "$FORCE_COREFILES" = "y" ]; then
297         turn_on_corefiles
298     fi
299
300     insert_modules_if_required
301
302     # Increase the limit on the number of open file descriptors since
303     # ovs-vswitchd needs a few per bridge
304     ulimit -n 4096
305
306     # Allow GRE traffic.
307     iptables -I INPUT -p gre -j ACCEPT
308
309     if [ ! -e "$OVSDB_SERVER_DB" ]; then
310         warning "$OVSDB_SERVER_DB does not exist"
311         install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
312
313         action "Creating empty database $OVSDB_SERVER_DB" true
314         $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
315     else
316         # Upgrade or downgrade schema and compact database.
317         $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
318     fi
319
320     start_ovsdb_server
321     $vsctl --no-wait init
322     if [ ! -e /var/run/openvswitch.booted ]; then
323         touch /var/run/openvswitch.booted
324         for bridge in $($vsctl list-br); do
325             $vsctl --no-wait del-br $bridge
326         done
327     fi
328
329     set_system_uuid
330
331     start_vswitchd
332     if [ "${ENABLE_BRCOMPAT}" = "y" ] ; then
333         start_brcompatd
334     fi
335     touch /var/lock/subsys/openvswitch
336 }
337
338 function stop {
339     stop_daemon BRCOMPATD "$brcompatd"
340     stop_daemon VSWITCHD "$vswitchd"
341     stop_daemon OVSDB_SERVER "$ovsdb_server"
342     rm -f /var/lock/subsys/openvswitch
343 }
344
345 function restart {
346     if restart_approval; then
347         stop
348         start
349     fi
350 }
351
352 case "$1" in
353     start)
354         start
355         ;;
356     stop)
357         stop
358         ;;
359     restart)
360         restart
361         ;;
362     strace-vswitchd)
363         shift
364         strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
365         ;;
366     strace-brcompatd)
367         shift
368         strace -p $(cat "$BRCOMPATD_PIDFILE") "$@"
369         ;;
370     status)
371         status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
372         status -p "$VSWITCHD_PIDFILE" ovs-vswitchd &&
373         (test "$ENABLE_BRCOMPAT" != "y" || 
374             status -p "$BRCOMPATD_PIDFILE" ovs-brcompatd)
375         ;;
376     version)
377         /usr/sbin/ovsdb-server -V
378         /usr/sbin/ovs-vswitchd -V
379         /usr/sbin/ovs-brcompatd -V
380         ;;
381     help)
382         printf "openvswitch [start|stop|restart|unload|status|version]\n"
383         ;;
384     *)
385         printf "Unknown command: $1\n"
386         exit 1
387         ;;
388 esac