Implement database schema versioning.
[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 hup_monitor_external_ids {
121     if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
122         action "Configuring Open vSwitch external IDs" kill -HUP `cat /var/run/openvswitch/ovs-external-ids.pid`
123     fi
124 }
125
126 function dp_list {
127     "$dpctl" show | grep '^dp[0-9]\+:' | cut -d':' -f 1
128 }
129
130 function turn_on_corefiles {
131     ulimit -Sc 67108864
132 }
133
134 function remove_all_dp {
135     for dp in $(dp_list); do
136         action "Removing datapath: $dp" "$dpctl" del-dp "$dp"
137     done
138 }
139
140 function insert_modules_if_required {
141     if ! lsmod | grep -q "openvswitch_mod"; then
142         action "Inserting llc module" modprobe llc
143         action "Inserting openvswitch module" modprobe openvswitch_mod
144     fi
145     if [ "$ENABLE_BRCOMPAT" = "y" ] && [ -n "$BRCOMPATD_PIDFILE" ] && ! lsmod | grep -q "brcompat_mod"; then
146         action "Inserting brcompat module" modprobe brcompat_mod
147     fi
148 }
149
150 function remove_modules {
151     if lsmod | grep -q "brcompat_mod"; then
152         action "Removing brcompat module" rmmod brcompat_mod.ko
153     fi
154     if lsmod | grep -q "openvswitch_mod"; then
155         action "Removing openvswitch module" rmmod openvswitch_mod.ko
156     fi
157 }
158
159 function start_daemon {
160     local DAEMON=$1
161     shift
162     local BINARY=$1
163
164     # cd to daemon's run_dir so core files get dumped into a sensible place.
165     eval local run_dir=\$${DAEMON}_RUN_DIR
166     if [ ! -d "$run_dir" ]; then
167         install -d -m 755 -o root -g root "$run_dir"
168     fi
169     cd "$run_dir"
170     
171     # Configure log levels.
172     eval local syslog_loglevel=\$${DAEMON}_SYSLOG_LOGLEVEL
173     eval local file_loglevel=\$${DAEMON}_FILE_LOGLEVEL
174     eval local logfile=\$${DAEMON}_LOGFILE
175     set -- "$@" -vANY:CONSOLE:EMER -vANY:SYSLOG:"$syslog_loglevel"
176     if test -n "$file_loglevel" && test -n "$logfile"; then
177         install -d -m 755 -o root -g root `dirname "$logfile"`
178         set -- "$@" --log-file="$logfile" -vANY:FILE:"$file_loglevel"
179     fi
180
181     # Configure leak checker.
182     eval local memleak_logfile=\$${DAEMON}_MEMLEAK_LOGFILE
183     if test -n "$memleak_logfile"; then
184         set -- "$@" --check-leaks="$memleak_logfile"
185         if test -e "$memleak_logfile"; then
186             mv "$memleak_logfile" "$memleak_logfile.prev"
187         fi
188     fi
189
190     # Configure debugging wrappers.
191     eval local strace_log=\$${DAEMON}_STRACE_LOG
192     eval local strace_opt=\$${DAEMON}_STRACE_OPT
193     eval local valgrind_log=\$${DAEMON}_VALGRIND_LOG
194     eval local valgrind_opt=\$${DAEMON}_VALGRIND_OPT
195     if test -n "$strace_log" && test -n "$valgrind_log"; then
196         printf "Can not start with both VALGRIND and STRACE\n"
197         exit 1
198     elif test -n "$strace_log"; then
199         local mode=strace
200         set -- strace -o "$strace_log" $strace_opt "$@"
201     elif test -n "$valgrind_log"; then
202         local mode=valgrind
203         set -- valgrind --log-file="$valgrind_log" $valgrind_opt "$@"
204     else
205         local mode=production
206         eval local pidfile=\$${DAEMON}_PIDFILE
207         install -d -m 755 -o root -g root `dirname $pidfile`
208         set -- "$@" --pidfile="$pidfile" --detach $monitor_opt --no-chdir
209     fi
210
211     # Configure niceness.
212     eval local priority=\$${DAEMON}_PRIORITY
213     if test -n "$priority"; then
214         set -- nice -n $priority "$@"
215     fi
216
217     if test $mode = production; then
218         action "Starting `basename $BINARY`" "$@"
219     else
220         # Start in background and force a "success" message
221         action "Starting `basename $BINARY` with $mode debugging" true
222         ("$@") &
223     fi
224 }
225
226 function start_ovsdb_server {
227     set -- "$ovsdb_server" "$OVSDB_SERVER_DB"
228     for remote in $OVSDB_SERVER_REMOTES; do
229         set -- "$@" --remote="$remote"
230     done
231     set -- "$@" --private-key=db:SSL,private_key --certificate=db:SSL,certificate --bootstrap-ca-cert=db:SSL,ca_cert
232     start_daemon OVSDB_SERVER "$@"
233 }
234
235 function start_vswitchd {
236     local fake_proc_net_opt=
237     if [ "$ENABLE_FAKE_PROC_NET" = "y" ]; then
238         fake_proc_net_opt="--fake-proc-net"
239     fi
240
241     local mlockall_opt=
242     if [ "$VSWITCHD_MLOCKALL" != "no" ]; then
243         mlockall_opt="--mlockall"
244     fi
245
246     start_daemon VSWITCHD "$vswitchd" $fake_proc_net_opt $mlockall_opt \
247                  "$VSWITCHD_OVSDB_SERVER"
248  }
249
250 function start_brcompatd {
251     start_daemon BRCOMPATD "$brcompatd" \
252                  --appctl-command="$appctl --target=/var/run/openvswitch/ovs-vswitchd.\`cat $VSWITCHD_PIDFILE\`.ctl %s" \
253                  "$VSWITCHD_OVSDB_SERVER"
254 }
255
256 function stop_daemon {
257     local DAEMON=$1
258     local BINARY=$2
259     eval local pidfile=\$${DAEMON}_PIDFILE
260     if test -f "$pidfile"; then
261         local pid=$(cat "$pidfile")
262         action "Killing `basename $BINARY` ($pid)" kill $pid
263         for delay in .1 .25 .65 1 1 1 1; do
264             if kill -0 $pid >/dev/null 2>&1; then
265                 sleep $delay
266             else
267                 break
268             fi
269         done
270         rm -f "$pidfile"
271     fi
272 }
273
274 function restart_approval {
275     if test ! -t 0; then
276         # Don't prompt if invoked non-interactively.
277         return 0
278     fi
279     cat <<EOF
280
281 WARNING!!!
282
283 Restarting Open vSwitch on a live server is not guaranteed to work.  It is
284 provided as a convenience for those situations in which it does work.
285
286 EOF
287     read -s -r -n 1 -p "Continue with restart (y/N): " response
288     printf "\n"
289     case "$response" in
290         y|Y)
291             return 0
292             ;;
293         *)
294             return 1
295             ;;
296     esac
297 }
298
299 function set_system_ids {
300     if [ -f /etc/xensource-inventory ]; then
301         OVS_VERSION=`ovs-vswitchd --version | sed 's/.*) //;1q'`
302         action "Configuring Open vSwitch system IDs" true
303         $vsctl --no-wait --timeout=5 set Open_vSwitch . \
304             ovs-version="$OVS_VERSION" \
305             system-type="$PRODUCT_BRAND" \
306             system-version="$PRODUCT_VERSION-$BUILD_NUMBER" \
307             external-ids:system-id="$INSTALLATION_UUID" \
308             external-ids:xs-system-uuid="$INSTALLATION_UUID"
309     else
310         action "Configuring Open vSwitch system IDs" false
311     fi
312 }
313
314 function start {
315     if [ "$FORCE_COREFILES" = "y" ]; then
316         turn_on_corefiles
317     fi
318
319     insert_modules_if_required
320
321     # Increase the limit on the number of open file descriptors since
322     # ovs-vswitchd needs a few per bridge
323     ulimit -n 4096
324
325     # Allow GRE traffic.
326     iptables -I INPUT -p gre -j ACCEPT
327
328     schemaver=`$ovsdb_tool schema-version "$VSWITCHD_OVSDB_SCHEMA"`
329     if [ ! -e "$OVSDB_SERVER_DB" ]; then
330         warning "$OVSDB_SERVER_DB does not exist"
331         install -d -m 755 -o root -g root `dirname $OVSDB_SERVER_DB`
332
333         action "Creating empty database $OVSDB_SERVER_DB" true
334         $ovsdb_tool -vANY:console:emer create "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
335     else
336         # If schema version changed, then back up the old version.
337         oldver=`$ovsdb_tool db-version "$OVSDB_SERVER_DB"`
338         if test "X$oldver" != "X$schemaver"; then
339             backup=$OVSDB_SERVER_DB.backup$oldver
340             action "Backing up $OVSDB_SERVER_DB in $backup before converting from schema version \"$oldver\" to \"$schemaver\"" true
341             cp "$OVSDB_SERVER_DB" "$backup"
342         fi
343
344         # Upgrade or downgrade schema and compact database.
345         $ovsdb_tool -vANY:console:emer convert "$OVSDB_SERVER_DB" "$VSWITCHD_OVSDB_SCHEMA"
346     fi
347
348     start_ovsdb_server
349     $vsctl --no-wait --timeout=5 init -- set Open_vSwitch . db-version="$schemaver"
350     if [ ! -e /var/run/openvswitch.booted ]; then
351         touch /var/run/openvswitch.booted
352         for bridge in $($vsctl list-br); do
353             $vsctl --no-wait --timeout=5 del-br $bridge
354         done
355     fi
356
357     set_system_ids
358
359     start_vswitchd
360     if [ "${ENABLE_BRCOMPAT}" = "y" ] ; then
361         start_brcompatd
362     fi
363
364     # Start daemon to monitor external ids
365     PYTHONPATH=/usr/share/openvswitch/python \
366                /usr/share/openvswitch/scripts/ovs-external-ids \
367                --pidfile --detach $monitor_opt "$VSWITCHD_OVSDB_SERVER"
368
369     touch /var/lock/subsys/openvswitch
370 }
371
372 function stop {
373     stop_daemon BRCOMPATD "$brcompatd"
374     stop_daemon VSWITCHD "$vswitchd"
375     stop_daemon OVSDB_SERVER "$ovsdb_server"
376     if [ -e /var/run/openvswitch/ovs-external-ids.pid ]; then
377         kill `cat /var/run/openvswitch/ovs-external-ids.pid`
378     fi
379     rm -f /var/lock/subsys/openvswitch
380 }
381
382 function restart {
383     if restart_approval; then
384         stop
385         start
386     fi
387 }
388
389 case "$1" in
390     start)
391         start
392         ;;
393     stop)
394         stop
395         ;;
396     restart)
397         restart
398         ;;
399     reload|force-reload)
400         # Nothing to do to ovs-vswitchd and ovsdb-server as they keep their
401         # configuration up-to-date all the time.  HUP ovs-external-ids so it
402         # re-runs.
403         hup_monitor_external_ids
404         ;;
405     strace-vswitchd)
406         shift
407         strace -p $(cat "$VSWITCHD_PIDFILE") "$@"
408         ;;
409     strace-brcompatd)
410         shift
411         strace -p $(cat "$BRCOMPATD_PIDFILE") "$@"
412         ;;
413     status)
414         status -p "$OVSDB_SERVER_PIDFILE" ovsdb-server &&
415         status -p "$VSWITCHD_PIDFILE" ovs-vswitchd &&
416         (test "$ENABLE_BRCOMPAT" != "y" || 
417             status -p "$BRCOMPATD_PIDFILE" ovs-brcompatd)
418         ;;
419     version)
420         /usr/sbin/ovsdb-server -V
421         /usr/sbin/ovs-vswitchd -V
422         /usr/sbin/ovs-brcompatd -V
423         ;;
424     help)
425         printf "openvswitch [start|stop|restart|reload|force-reload|status|version]\n"
426         ;;
427     *)
428         printf "Unknown command: $1\n"
429         exit 1
430         ;;
431 esac