1 # This is a shell function library sourced by some Open vSwitch scripts.
2 # It is not intended to be invoked on its own.
4 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at:
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
18 ## ----------------- ##
19 ## configure options ##
20 ## ----------------- ##
22 # All of these should be substituted by the Makefile at build time.
23 logdir=${OVS_LOGDIR-'@LOGDIR@'} # /var/log/openvswitch
24 rundir=${OVS_RUNDIR-'@RUNDIR@'} # /var/run/openvswitch
25 sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'} # /etc
26 etcdir=$sysconfdir/openvswitch # /etc/openvswitch
27 datadir=${OVS_PKGDATADIR-'@pkgdatadir@'} # /usr/share/openvswitch
28 bindir=${OVS_BINDIR-'@bindir@'} # /usr/bin
29 sbindir=${OVS_SBINDIR-'@sbindir@'} # /usr/sbin
31 # /etc/openvswitch or /var/lib/openvswitch
32 if test X"$OVS_DBDIR" != X; then
34 elif test X"$OVS_SYSCONFDIR" != X; then
35 dbdir=$OVS_SYSCONFDIR/openvswitch
41 echo "$@" >> "${logdir}/ovs-ctl.log"
47 # In case of running the daemon with strace, piping the o/p causes
48 # the script to block (strace probably does not close the inherited
49 # pipe). So, do not log the o/p to ovs-ctl.log.
50 "${datadir}/scripts/ovs-ctl" "$@"
53 # In case of the command 'status', we should return the exit status
54 # of ovs-ctl. It is also useful to document the o/p in ovs-ctl.log.
55 display=`"${datadir}/scripts/ovs-ctl" "$@" 2>&1`
57 if test -w "${logdir}/ovs-ctl.log"; then
58 echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
65 echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
66 "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
75 LC_ALL=C; export LC_ALL
81 # Use the system's own implementations if it has any.
82 if test -e /etc/init.d/functions; then
83 . /etc/init.d/functions
84 elif test -e /etc/rc.d/init.d/functions; then
85 . /etc/rc.d/init.d/functions
86 elif test -e /lib/lsb/init-functions; then
87 . /lib/lsb/init-functions
90 # Implement missing functions (e.g. OpenSUSE lacks 'action').
91 if type log_success_msg >/dev/null 2>&1; then :; else
96 if type log_failure_msg >/dev/null 2>&1; then :; else
98 printf '%s ... failed!\n' "$*"
101 if type log_warning_msg >/dev/null 2>&1; then :; else
103 printf '%s ... (warning).\n' "$*"
106 if type action >/dev/null 2>&1; then :; else
112 if test $rc = 0; then
113 log_success_msg "$STRING"
115 log_failure_msg "$STRING"
126 # This is better than "kill -0" because it doesn't require permission to
127 # send a signal (so daemon_status in particular works as non-root).
132 [ "$1" = "`cat /proc/$2/comm`" ]
135 # version_geq version_a version_b
137 # Compare (dot separated) version numbers. Returns true (exit code 0) if
138 # version_a is greater or equal than version_b, otherwise false (exit code 1).
141 n1 = split($1, a, ".");
142 n2 = split($2, b, ".");
143 n = (n1 > n2) ? n1 : n2;
144 for (i = 1; i <= n; i++) {
145 if (a[i]+0 < b[i]+0) exit 1
146 if (a[i]+0 > b[i]+0) exit 0
158 # drop core files in a sensible place
159 test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
164 test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
165 set "$@" --log-file="$logdir/$daemon.log"
167 # pidfile and monitoring
168 test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
169 set "$@" --pidfile="$rundir/$daemon.pid"
170 set "$@" --detach --monitor
175 if (valgrind --version) > /dev/null 2>&1; then
176 set valgrind -q --leak-check=full --time-stamp=yes \
177 --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
179 log_failure_msg "valgrind not installed, running $daemon without it"
183 if (strace -V) > /dev/null 2>&1; then
184 strace="strace -tt -T -s 256 -ff"
185 if (strace -DV) > /dev/null 2>&1; then
187 set $strace -D -o "$logdir/$daemon.strace.log" "$@"
191 log_failure_msg "strace not installed, running $daemon without it"
195 set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
200 log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
205 if test X"$priority" != X; then
206 set nice -n "$priority" "$@"
209 action "Starting $daemon" "$@"
211 if test X"$strace" != X; then
212 # Strace doesn't have the -D option so we attach after the fact.
213 setsid $strace -o "$logdir/$daemon.strace.log" \
214 -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
219 if test -e "$rundir/$1.pid"; then
220 if pid=`cat "$rundir/$1.pid"`; then
222 graceful="EXIT .1 .25 .65 1"
223 actions="TERM .1 .25 .65 1 1 1 1 \
224 KILL 1 1 1 2 10 15 30 \
226 version=`ovs-appctl -T 1 -t $rundir/$1.$pid.ctl version \
227 | awk 'NR==1{print $NF}'`
229 # Use `ovs-appctl exit` only if the running daemon version
230 # is >= 2.5.90. This script might be used during upgrade to
231 # stop older versions of daemons which do not behave correctly
232 # with `ovs-appctl exit` (e.g. ovs-vswitchd <= 2.5.0 deletes
234 if version_geq "$version" "2.5.90"; then
235 actions="$graceful $actions"
237 for action in $actions; do
238 if pid_exists "$pid" >/dev/null 2>&1; then :; else
243 action "Exiting $1 ($pid)" \
244 ${bindir}/ovs-appctl -T 1 -t $rundir/$1.$pid.ctl exit
247 action "Killing $1 ($pid)" kill $pid
250 action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
253 log_failure_msg "Killing $1 ($pid) failed"
263 log_success_msg "$1 is not running"
267 pidfile=$rundir/$1.pid
268 if test -e "$pidfile"; then
269 if pid=`cat "$pidfile"`; then
270 if pid_exists "$pid"; then
271 echo "$1 is running with pid $pid"
274 echo "Pidfile for $1 ($pidfile) is stale"
277 echo "Pidfile for $1 ($pidfile) exists but cannot be read"
280 echo "$1 is not running"
285 daemon_is_running () {
286 pidfile=$rundir/$1.pid
287 test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
290 # Prints commands needed to move the ip address from interface $1 to interface
293 if [ -z "$1" ] || [ -z "$2" ]; then
299 # IP addresses (including IPv6).
300 echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
301 ip addr show dev $dev | while read addr; do
304 # Check and trim family.
312 # Trim device off the end--"ip" insists on having "dev" precede it.
314 while test $# != 0; do
317 # Omit kernel-maintained route.
321 if test "$2" = link -a "$family" != inet6; then
322 # Omit route derived from IP address, e.g.
323 # 172.16.0.0/16 derived from 172.16.12.34,
324 # but preserve IPv6 link-local address.
329 # Address label string
330 label=`echo $1 | sed "s/$dev/$dst/"`
331 addrcmd="$addrcmd label $label"
336 addrcmd="$addrcmd $1"
339 if test "$1" != "$dev"; then
340 addrcmd="$addrcmd $1"
343 echo ip -f $family addr add $addrcmd dev $dst
347 # Prints commands needed to move the ip route of interface $1 to interface $2
349 if [ -z "$1" ] || [ -z "$2" ]; then
354 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
355 ip route show dev $dev | while read route; do
356 # "proto kernel" routes are installed by the kernel automatically.
358 *" proto kernel "*) continue ;;
361 echo "ip route add $route dev $dst"
366 ovsdb-tool -vconsole:off "$@"
372 action "Creating empty database $DB_FILE" ovsdb_tool create "$DB_FILE" "$DB_SCHEMA"
379 schemaver=`ovsdb_tool schema-version "$DB_SCHEMA"`
380 if test ! -e "$DB_FILE"; then
381 log_warning_msg "$DB_FILE does not exist"
382 install -d -m 755 -o root -g root `dirname $DB_FILE`
383 create_db "$DB_FILE" "$DB_SCHEMA"
384 elif test X"`ovsdb_tool needs-conversion "$DB_FILE" "$DB_SCHEMA"`" != Xno; then
385 # Back up the old version.
386 version=`ovsdb_tool db-version "$DB_FILE"`
387 cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
388 backup=$DB_FILE.backup$version-$cksum
389 action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
391 # Compact database. This is important if the old schema did not enable
392 # garbage collection (i.e. if it did not have any tables with "isRoot":
393 # true) but the new schema does. In that situation the old database
394 # may contain a transaction that creates a record followed by a
395 # transaction that creates the first use of the record. Replaying that
396 # series of transactions against the new database schema (as "convert"
397 # does) would cause the record to be dropped by the first transaction,
398 # then the second transaction would cause a referential integrity
399 # failure (for a strong reference).
401 # Errors might occur on an Open vSwitch downgrade if ovsdb-tool doesn't
402 # understand some feature of the schema used in the OVSDB version that
403 # we're downgrading from, so we don't give up on error.
404 action "Compacting database" ovsdb_tool compact "$DB_FILE"
406 # Upgrade or downgrade schema.
407 if action "Converting database schema" ovsdb_tool convert "$DB_FILE" "$DB_SCHEMA"; then
410 log_warning_msg "Schema conversion failed, using empty database instead"
412 create_db "$DB_FILE" "$DB_SCHEMA"