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