]> git.proxmox.com Git - mirror_ovs.git/blob - utilities/ovs-lib.in
ovs-lib: Fix SELinux contexts for created dirs.
[mirror_ovs.git] / utilities / ovs-lib.in
1 # This is a shell function library sourced by some Open vSwitch scripts.
2 # It is not intended to be invoked on its own.
3
4 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
5 #
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:
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
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.
17
18 ## ----------------- ##
19 ## configure options ##
20 ## ----------------- ##
21
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
30
31 # /etc/openvswitch or /var/lib/openvswitch
32 if test X"$OVS_DBDIR" != X; then
33 dbdir=$OVS_DBDIR
34 elif test X"$OVS_SYSCONFDIR" != X; then
35 dbdir=$OVS_SYSCONFDIR/openvswitch
36 else
37 dbdir='@DBDIR@'
38 fi
39
40 ovs_ctl_log () {
41 echo "$@" >> "${logdir}/ovs-ctl.log"
42 }
43
44 ovs_ctl () {
45 case "$@" in
46 *"=strace"*)
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" "$@"
51 ;;
52 "status")
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`
56 rc=$?
57 if test -w "${logdir}/ovs-ctl.log"; then
58 echo "${display}" | tee -a "${logdir}/ovs-ctl.log"
59 else
60 echo "${display}"
61 fi
62 return ${rc}
63 ;;
64 *)
65 echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
66 "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
67 ;;
68 esac
69 }
70
71 VERSION='@VERSION@'
72
73 DAEMON_CWD=/
74
75 LC_ALL=C; export LC_ALL
76
77 ## ------------- ##
78 ## LSB functions ##
79 ## ------------- ##
80
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
88 fi
89
90 # Implement missing functions (e.g. OpenSUSE lacks 'action').
91 if type log_success_msg >/dev/null 2>&1; then :; else
92 log_success_msg () {
93 printf '%s.\n' "$*"
94 }
95 fi
96 if type log_failure_msg >/dev/null 2>&1; then :; else
97 log_failure_msg () {
98 printf '%s ... failed!\n' "$*"
99 }
100 fi
101 if type log_warning_msg >/dev/null 2>&1; then :; else
102 log_warning_msg () {
103 printf '%s ... (warning).\n' "$*"
104 }
105 fi
106 if type action >/dev/null 2>&1; then :; else
107 action () {
108 STRING=$1
109 shift
110 "$@"
111 rc=$?
112 if test $rc = 0; then
113 log_success_msg "$STRING"
114 else
115 log_failure_msg "$STRING"
116 fi
117 return $rc
118 }
119 fi
120
121 ## ------- ##
122 ## Daemons ##
123 ## ------- ##
124
125 pid_exists () {
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).
128 test -d /proc/"$1"
129 }
130
131 pid_comm_check () {
132 [ "$1" = "`cat /proc/$2/comm`" ]
133 }
134
135 # version_geq version_a version_b
136 #
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).
139 version_geq() {
140 echo $1 $2 | awk '{
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
147 }
148 }'
149 }
150
151 install_dir () {
152 DIR="$1"
153 if test ! -d "$DIR"; then
154 install -d -m 755 -o root -g root "$DIR"
155 restorecon "$DIR" >/dev/null 2>&1
156 fi
157 }
158
159 start_daemon () {
160 priority=$1
161 wrapper=$2
162 shift; shift
163 daemon=$1
164 strace=""
165
166 # drop core files in a sensible place
167 install_dir "$DAEMON_CWD"
168 set "$@" --no-chdir
169 cd "$DAEMON_CWD"
170
171 # log file
172 install_dir "$logdir"
173 set "$@" --log-file="$logdir/$daemon.log"
174
175 # pidfile and monitoring
176 install_dir "$rundir"
177 set "$@" --pidfile="$rundir/$daemon.pid"
178 set "$@" --detach
179 test X"$MONITOR" = Xno || set "$@" --monitor
180
181 # wrapper
182 case $wrapper in
183 valgrind)
184 if (valgrind --version) > /dev/null 2>&1; then
185 set valgrind -q --leak-check=full --time-stamp=yes \
186 --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
187 else
188 log_failure_msg "valgrind not installed, running $daemon without it"
189 fi
190 ;;
191 strace)
192 if (strace -V) > /dev/null 2>&1; then
193 strace="strace -tt -T -s 256 -ff"
194 if (strace -DV) > /dev/null 2>&1; then
195 # Has the -D option.
196 set $strace -D -o "$logdir/$daemon.strace.log" "$@"
197 strace=""
198 fi
199 else
200 log_failure_msg "strace not installed, running $daemon without it"
201 fi
202 ;;
203 glibc)
204 set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
205 ;;
206 '')
207 ;;
208 *)
209 log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
210 ;;
211 esac
212
213 # priority
214 if test X"$priority" != X; then
215 set nice -n "$priority" "$@"
216 fi
217
218 action "Starting $daemon" "$@"
219
220 if test X"$strace" != X; then
221 # Strace doesn't have the -D option so we attach after the fact.
222 setsid $strace -o "$logdir/$daemon.strace.log" \
223 -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
224 fi
225 }
226
227 stop_daemon () {
228 if test -e "$rundir/$1.pid"; then
229 if pid=`cat "$rundir/$1.pid"`; then
230
231 graceful="EXIT .1 .25 .65 1"
232 actions="TERM .1 .25 .65 1 1 1 1 \
233 KILL 1 1 1 2 10 15 30 \
234 FAIL"
235 version=`ovs-appctl -T 1 -t $rundir/$1.$pid.ctl version \
236 | awk 'NR==1{print $NF}'`
237
238 # Use `ovs-appctl exit` only if the running daemon version
239 # is >= 2.5.90. This script might be used during upgrade to
240 # stop older versions of daemons which do not behave correctly
241 # with `ovs-appctl exit` (e.g. ovs-vswitchd <= 2.5.0 deletes
242 # internal ports).
243 if version_geq "$version" "2.5.90"; then
244 actions="$graceful $actions"
245 fi
246 for action in $actions; do
247 if pid_exists "$pid" >/dev/null 2>&1; then :; else
248 return 0
249 fi
250 case $action in
251 EXIT)
252 action "Exiting $1 ($pid)" \
253 ${bindir}/ovs-appctl -T 1 -t $rundir/$1.$pid.ctl exit
254 ;;
255 TERM)
256 action "Killing $1 ($pid)" kill $pid
257 ;;
258 KILL)
259 action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
260 ;;
261 FAIL)
262 log_failure_msg "Killing $1 ($pid) failed"
263 return 1
264 ;;
265 *)
266 sleep $action
267 ;;
268 esac
269 done
270 fi
271 fi
272 log_success_msg "$1 is not running"
273 }
274
275 daemon_status () {
276 pidfile=$rundir/$1.pid
277 if test -e "$pidfile"; then
278 if pid=`cat "$pidfile"`; then
279 if pid_exists "$pid"; then
280 echo "$1 is running with pid $pid"
281 return 0
282 else
283 echo "Pidfile for $1 ($pidfile) is stale"
284 fi
285 else
286 echo "Pidfile for $1 ($pidfile) exists but cannot be read"
287 fi
288 else
289 echo "$1 is not running"
290 fi
291 return 1
292 }
293
294 daemon_is_running () {
295 pidfile=$rundir/$1.pid
296 test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid" && pid_comm_check $1 $pid
297 } >/dev/null 2>&1
298
299 # Prints commands needed to move the ip address from interface $1 to interface
300 # $2
301 move_ip_address () {
302 if [ -z "$1" ] || [ -z "$2" ]; then
303 return
304 fi
305 dev="$1"
306 dst="$2"
307
308 # IP addresses (including IPv6).
309 echo "ip addr flush dev $dev 2>/dev/null" # Suppresses "Nothing to flush".
310 ip addr show dev $dev | while read addr; do
311 set -- $addr
312
313 # Check and trim family.
314 family=$1
315 shift
316 case $family in
317 inet | inet6) ;;
318 *) continue ;;
319 esac
320
321 # Trim device off the end--"ip" insists on having "dev" precede it.
322 addrcmd=
323 while test $# != 0; do
324 case $1 in
325 dynamic)
326 # Omit kernel-maintained route.
327 continue 2
328 ;;
329 scope)
330 if test "$2" = link -a "$family" != inet6; then
331 # Omit route derived from IP address, e.g.
332 # 172.16.0.0/16 derived from 172.16.12.34,
333 # but preserve IPv6 link-local address.
334 continue 2
335 fi
336 ;;
337 "$dev"|"$dev:"*)
338 # Address label string
339 label=`echo $1 | sed "s/$dev/$dst/"`
340 addrcmd="$addrcmd label $label"
341 shift
342 continue
343 ;;
344 esac
345 addrcmd="$addrcmd $1"
346 shift
347 done
348 if test "$1" != "$dev"; then
349 addrcmd="$addrcmd $1"
350 fi
351
352 echo ip -f $family addr add $addrcmd dev $dst
353 done
354 }
355
356 # Prints commands needed to move the ip route of interface $1 to interface $2
357 move_ip_routes () {
358 if [ -z "$1" ] || [ -z "$2" ]; then
359 return
360 fi
361 dev="$1"
362 dst="$2"
363 echo "ip route flush dev $dev proto boot 2>/dev/null" # Suppresses "Nothing to flush".
364 ip route show dev $dev | while read route; do
365 # "proto kernel" routes are installed by the kernel automatically.
366 case $route in
367 *" proto kernel "*) continue ;;
368 esac
369
370 echo "ip route add $route dev $dst"
371 done
372 }
373
374 ovsdb_tool () {
375 ovsdb-tool -vconsole:off "$@"
376 }
377
378 create_db () {
379 DB_FILE="$1"
380 DB_SCHEMA="$2"
381 action "Creating empty database $DB_FILE" ovsdb_tool create "$DB_FILE" "$DB_SCHEMA"
382 }
383
384 upgrade_db () {
385 DB_FILE="$1"
386 DB_SCHEMA="$2"
387
388 schemaver=`ovsdb_tool schema-version "$DB_SCHEMA"`
389 if test ! -e "$DB_FILE"; then
390 log_warning_msg "$DB_FILE does not exist"
391 install_dir `dirname $DB_FILE`
392 create_db "$DB_FILE" "$DB_SCHEMA"
393 elif test X"`ovsdb_tool needs-conversion "$DB_FILE" "$DB_SCHEMA"`" != Xno; then
394 # Back up the old version.
395 version=`ovsdb_tool db-version "$DB_FILE"`
396 cksum=`ovsdb_tool db-cksum "$DB_FILE" | awk '{print $1}'`
397 backup=$DB_FILE.backup$version-$cksum
398 action "Backing up database to $backup" cp "$DB_FILE" "$backup" || return 1
399
400 # Compact database. This is important if the old schema did not enable
401 # garbage collection (i.e. if it did not have any tables with "isRoot":
402 # true) but the new schema does. In that situation the old database
403 # may contain a transaction that creates a record followed by a
404 # transaction that creates the first use of the record. Replaying that
405 # series of transactions against the new database schema (as "convert"
406 # does) would cause the record to be dropped by the first transaction,
407 # then the second transaction would cause a referential integrity
408 # failure (for a strong reference).
409 #
410 # Errors might occur on an Open vSwitch downgrade if ovsdb-tool doesn't
411 # understand some feature of the schema used in the OVSDB version that
412 # we're downgrading from, so we don't give up on error.
413 action "Compacting database" ovsdb_tool compact "$DB_FILE"
414
415 # Upgrade or downgrade schema.
416 if action "Converting database schema" ovsdb_tool convert "$DB_FILE" "$DB_SCHEMA"; then
417 :
418 else
419 log_warning_msg "Schema conversion failed, using empty database instead"
420 rm -f "$DB_FILE"
421 create_db "$DB_FILE" "$DB_SCHEMA"
422 fi
423 fi
424 }