ovs-sandbox: add delay before running ovs-vsctl
[cascardo/ovs.git] / tutorial / ovs-sandbox
1 #! /bin/sh
2 #
3 # Copyright (c) 2013, 2015 Nicira, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 set -e
18
19 run() {
20     (cd "$sandbox" && "$@") || exit 1
21 }
22
23 run_xterm() {
24     title=$1;
25     shift
26     run xterm -T "$title" -e "$@"  &
27 }
28
29 rungdb() {
30     under_gdb=$1
31     shift
32     # Remove the --detach and to put the process under gdb control.
33     # Also remove --vconsole:off to allow error message to show up
34     # on the console.
35     # Use "DISPLAY" variable to determine out if X is supported
36     if $under_gdb && [ "$DISPLAY" ]; then
37         args=`echo $@ |sed s/--detach//g | sed s/--vconsole:off//g`
38         xterm_title=$1
39         run_xterm $xterm_title gdb -ex run --args $args
40     else
41         run $@
42     fi
43 }
44
45 gdb_vswitchd=false
46 gdb_ovsdb=false
47 builddir=
48 srcdir=
49 schema=
50 installed=false
51 built=false
52
53 for option; do
54     # This option-parsing mechanism borrowed from a Autoconf-generated
55     # configure script under the following license:
56
57     # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
58     # 2002, 2003, 2004, 2005, 2006, 2009, 2013 Free Software Foundation, Inc.
59     # This configure script is free software; the Free Software Foundation
60     # gives unlimited permission to copy, distribute and modify it.
61
62     # If the previous option needs an argument, assign it.
63     if test -n "$prev"; then
64         eval $prev=\$option
65         prev=
66         continue
67     fi
68     case $option in
69         *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
70         *) optarg=yes ;;
71     esac
72
73     case $dashdash$option in
74         --)
75             dashdash=yes ;;
76         -h|--help)
77             cat <<EOF
78 ovs-sandbox, for starting a sandboxed dummy Open vSwitch environment
79 usage: $0 [OPTION...]
80
81 If you run ovs-sandbox from an OVS build directory, it uses the OVS that
82 you built.  Otherwise, if you have an installed Open vSwitch, it uses
83 the installed version.
84
85 These options force ovs-sandbox to use a particular OVS build:
86   -b, --builddir=DIR   specify Open vSwitch build directory
87   -s, --srcdir=DIR     specify Open vSwitch source directory
88 These options force ovs-sandbox to use an installed Open vSwitch:
89   -i, --installed      use installed Open vSwitch
90   -g, --gdb-vswitchd   run ovs-vswitchd under gdb
91   -d, --gdb-ovsdb      run ovsdb-server under gdb
92   -S, --schema=FILE    use FILE as vswitch.ovsschema
93
94 Other options:
95   -h, --help           Print this usage message.
96 EOF
97             exit 0
98             ;;
99
100         --b*=*)
101             builddir=$optarg
102             built=:
103             ;;
104         -b|--b*)
105             prev=builddir
106             built=:
107             ;;
108         --sr*=*)
109             srcdir=$optarg
110             built=false
111             ;;
112         -s|--sr*)
113             prev=srcdir
114             built=false
115             ;;
116         -i|--installed)
117             installed=:
118             ;;
119         --sc*=*)
120             schema=$optarg
121             installed=:
122             ;;
123         -S|--sc*)
124             prev=schema
125             installed=:
126             ;;
127         -g|--gdb-v*)
128             gdb_vswitchd=true
129             ;;
130         -d|--gdb-o*)
131             gdb_ovsdb=true
132             ;;
133         -*)
134             echo "unrecognized option $option (use --help for help)" >&2
135             exit 1
136             ;;
137         *)
138             echo "$option: non-option arguments not supported (use --help for help)" >&2
139             exit 1
140             ;;
141     esac
142     shift
143 done
144
145 if $installed && $built; then
146     echo "sorry, conflicting options (use --help for help)" >&2
147     exit 1
148 elif $installed || $built; then
149     :
150 elif test -e vswitchd/ovs-vswitchd; then
151     built=:
152     builddir=.
153 elif (ovs-vswitchd --version) >/dev/null 2>&1; then
154     installed=:
155 else
156     echo "can't find an OVS build or install (use --help for help)" >&2
157     exit 1
158 fi
159
160 if $built; then
161     if test ! -e "$builddir"/vswitchd/ovs-vswitchd; then
162         echo "$builddir does not appear to be an OVS build directory" >&2
163         exit 1
164     fi
165     builddir=`cd $builddir && pwd`
166
167     # Find srcdir.
168     case $srcdir in
169         '')
170             srcdir=$builddir
171             if test ! -e "$srcdir"/WHY-OVS.md; then
172                 srcdir=`cd $builddir/.. && pwd`
173             fi
174             ;;
175         /*) ;;
176         *) srcdir=`pwd`/$srcdir ;;
177     esac
178     schema=$srcdir/vswitchd/vswitch.ovsschema
179     if test ! -e "$schema"; then
180         echo >&2 'source directory not found, please use --srcdir'
181         exit 1
182     fi
183
184     # Put built tools early in $PATH.
185     if test ! -e $builddir/vswitchd/ovs-vswitchd; then
186         echo >&2 'build not found, please change set $builddir or change directory'
187         exit 1
188     fi
189     PATH=$builddir/ovsdb:$builddir/vswitchd:$builddir/utilities:$PATH
190     export PATH
191 else
192     case $schema in
193         '')
194             for schema in \
195                 /usr/local/share/openvswitch/vswitch.ovsschema \
196                 /usr/share/openvswitch/vswitch.ovsschema \
197                 none; do
198                 if test -r $schema; then
199                     break
200                 fi
201             done
202             ;;
203         /*) ;;
204         *) schema=`pwd`/$schema ;;
205     esac
206     if test ! -r "$schema"; then
207         echo "can't find vswitch.ovsschema, please specify --schema" >&2
208         exit 1
209     fi
210 fi
211
212 # Create sandbox.
213 rm -rf sandbox
214 mkdir sandbox
215 sandbox=`cd sandbox && pwd`
216
217 # Set up environment for OVS programs to sandbox themselves.
218 OVS_RUNDIR=$sandbox; export OVS_RUNDIR
219 OVS_LOGDIR=$sandbox; export OVS_LOGDIR
220 OVS_DBDIR=$sandbox; export OVS_DBDIR
221 OVS_SYSCONFDIR=$sandbox; export OVS_SYSCONFDIR
222
223 if $built; then
224     # Easy access to OVS manpages.
225     (cd "$builddir" && ${MAKE} install-man mandir="$sandbox"/man)
226     MANPATH=$sandbox/man:; export MANPATH
227 fi
228
229 # Ensure cleanup.
230 trap 'kill `cat "$sandbox"/*.pid`' 0 1 2 3 13 14 15
231
232 # Create database and start ovsdb-server.
233 touch "$sandbox"/.conf.db.~lock~
234 run ovsdb-tool create conf.db "$schema"
235 rungdb $gdb_ovsdb ovsdb-server --detach --no-chdir --pidfile -vconsole:off --log-file \
236     --remote=punix:"$sandbox"/db.sock
237
238 #Add a small delay to allow ovsdb-server to launch.
239 sleep 0.1
240
241 #Wait for ovsdb-server to finish launching.
242 if test ! -e "$sandbox"/db.sock; then
243     echo -n "Waiting for ovsdb-server to start..."
244     while test ! -e "$sandbox"/db.sock; do
245         sleep 1;
246     done
247     echo "  Done"
248 fi
249
250 # Initialize database.
251 run ovs-vsctl --no-wait -- init
252
253 # Start ovs-vswitchd.
254 rungdb $gdb_vswitchd ovs-vswitchd --detach --no-chdir --pidfile -vconsole:off --log-file \
255     --enable-dummy=override -vvconn -vnetdev_dummy
256
257 cat <<EOF
258
259
260
261 ----------------------------------------------------------------------
262 You are running in a dummy Open vSwitch environment.  You can use
263 ovs-vsctl, ovs-ofctl, ovs-appctl, and other tools to work with the
264 dummy switch.  
265
266 Log files, pidfiles, and the configuration database are in the
267 "sandbox" subdirectory.
268
269 Exit the shell to kill the running daemons.
270 EOF
271
272 status=0; $SHELL || status=$?
273
274 cat <<EOF
275 ----------------------------------------------------------------------
276
277
278
279 EOF
280
281 exit $status