Merge "master" into "ovn".
[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     gdb_run=$2
32     shift
33     shift
34
35     # Remove the --detach and to put the process under gdb control.
36     # Also remove --vconsole:off to allow error message to show up
37     # on the console.
38     # Use "DISPLAY" variable to determine out if X is supported
39     if $under_gdb && [ "$DISPLAY" ]; then
40         args=`echo $@ |sed s/--detach//g | sed s/--vconsole:off//g`
41         xterm_title=$1
42
43         gdb_cmd=""
44         if $gdb_run; then
45             gdb_cmd="-ex run"
46         fi
47
48         run_xterm $xterm_title gdb $gdb_cmd --args $args
49     else
50         run $@
51     fi
52 }
53
54 gdb_vswitchd=false
55 gdb_ovsdb=false
56 gdb_vswitchd_ex=false
57 gdb_ovsdb_ex=false
58 gdb_ovn_northd=false
59 gdb_ovn_controller=false
60 builddir=
61 srcdir=
62 schema=
63 installed=false
64 built=false
65 ovn=false
66 ovnsb_schema=
67 ovnnb_schema=
68
69 for option; do
70     # This option-parsing mechanism borrowed from a Autoconf-generated
71     # configure script under the following license:
72
73     # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
74     # 2002, 2003, 2004, 2005, 2006, 2009, 2013 Free Software Foundation, Inc.
75     # This configure script is free software; the Free Software Foundation
76     # gives unlimited permission to copy, distribute and modify it.
77
78     # If the previous option needs an argument, assign it.
79     if test -n "$prev"; then
80         eval $prev=\$option
81         prev=
82         continue
83     fi
84     case $option in
85         *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
86         *) optarg=yes ;;
87     esac
88
89     case $dashdash$option in
90         --)
91             dashdash=yes ;;
92         -h|--help)
93             cat <<EOF
94 ovs-sandbox, for starting a sandboxed dummy Open vSwitch environment
95 usage: $0 [OPTION...]
96
97 If you run ovs-sandbox from an OVS build directory, it uses the OVS that
98 you built.  Otherwise, if you have an installed Open vSwitch, it uses
99 the installed version.
100
101 These options force ovs-sandbox to use a particular OVS build:
102   -b, --builddir=DIR   specify Open vSwitch build directory
103   -s, --srcdir=DIR     specify Open vSwitch source directory
104 These options force ovs-sandbox to use an installed Open vSwitch:
105   -i, --installed      use installed Open vSwitch
106   -g, --gdb-vswitchd   run ovs-vswitchd under gdb
107   -d, --gdb-ovsdb      run ovsdb-server under gdb
108   --gdb-ovn-northd     run ovn-northd under gdb
109   --gdb-ovn-controller run ovn-controller under gdb
110   -S, --schema=FILE    use FILE as vswitch.ovsschema
111   -o, --ovn            enable OVN
112
113 Other options:
114   -h, --help           Print this usage message.
115 EOF
116             exit 0
117             ;;
118
119         --b*=*)
120             builddir=$optarg
121             built=:
122             ;;
123         -b|--b*)
124             prev=builddir
125             built=:
126             ;;
127         --sr*=*)
128             srcdir=$optarg
129             built=false
130             ;;
131         -s|--sr*)
132             prev=srcdir
133             built=false
134             ;;
135         -i|--installed)
136             installed=:
137             ;;
138         --sc*=*)
139             schema=$optarg
140             installed=:
141             ;;
142         -S|--sc*)
143             prev=schema
144             installed=:
145             ;;
146         -g|--gdb-v*)
147             gdb_vswitchd=true
148             gdb_vswitchd_ex=false
149             ;;
150         -e|--gdb-ex-v*)
151             gdb_vswitchd=true
152             gdb_vswitchd_ex=true
153             ;;
154         -d|--gdb-ovsdb)
155             gdb_ovsdb=true
156             gdb_ovsdb_ex=false
157             ;;
158         -r|--gdb-ex-o*)
159             gdb_ovsdb=true
160             gdb_ovsdb_ex=true
161             ;;
162         --gdb-ovn-northd)
163             gdb_ovn_northd=true
164             ;;
165         --gdb-ovn-controller)
166             gdb_ovn_controller=true
167             ;;
168         -o|--ovn)
169             ovn=true
170             ;;
171         -*)
172             echo "unrecognized option $option (use --help for help)" >&2
173             exit 1
174             ;;
175         *)
176             echo "$option: non-option arguments not supported (use --help for help)" >&2
177             exit 1
178             ;;
179     esac
180     shift
181 done
182
183 if $installed && $built; then
184     echo "sorry, conflicting options (use --help for help)" >&2
185     exit 1
186 elif $installed || $built; then
187     :
188 elif test -e vswitchd/ovs-vswitchd; then
189     built=:
190     builddir=.
191 elif (ovs-vswitchd --version) >/dev/null 2>&1; then
192     installed=:
193 else
194     echo "can't find an OVS build or install (use --help for help)" >&2
195     exit 1
196 fi
197
198 if $built; then
199     if test ! -e "$builddir"/vswitchd/ovs-vswitchd; then
200         echo "$builddir does not appear to be an OVS build directory" >&2
201         exit 1
202     fi
203     builddir=`cd $builddir && pwd`
204
205     # Find srcdir.
206     case $srcdir in
207         '')
208             srcdir=$builddir
209             if test ! -e "$srcdir"/WHY-OVS.md; then
210                 srcdir=`cd $builddir/.. && pwd`
211             fi
212             ;;
213         /*) ;;
214         *) srcdir=`pwd`/$srcdir ;;
215     esac
216     schema=$srcdir/vswitchd/vswitch.ovsschema
217     if test ! -e "$schema"; then
218         echo >&2 'source directory not found, please use --srcdir'
219         exit 1
220     fi
221     if $ovn; then
222         ovnsb_schema=$srcdir/ovn/ovn-sb.ovsschema
223         if test ! -e "$ovnsb_schema"; then
224             echo >&2 'source directory not found, please use --srcdir'
225             exit 1
226         fi
227         ovnnb_schema=$srcdir/ovn/ovn-nb.ovsschema
228         if test ! -e "$ovnnb_schema"; then
229             echo >&2 'source directory not found, please use --srcdir'
230             exit 1
231         fi
232     fi
233
234     # Put built tools early in $PATH.
235     if test ! -e $builddir/vswitchd/ovs-vswitchd; then
236         echo >&2 'build not found, please change set $builddir or change directory'
237         exit 1
238     fi
239     PATH=$builddir/ovsdb:$builddir/vswitchd:$builddir/utilities:$PATH
240     if $ovn; then
241         PATH=$builddir/ovn:$builddir/ovn/controller:$builddir/ovn/northd:$PATH
242     fi
243     export PATH
244 else
245     case $schema in
246         '')
247             for schema in \
248                 /usr/local/share/openvswitch/vswitch.ovsschema \
249                 /usr/share/openvswitch/vswitch.ovsschema \
250                 none; do
251                 if test -r $schema; then
252                     break
253                 fi
254             done
255             ;;
256         /*) ;;
257         *) schema=`pwd`/$schema ;;
258     esac
259     if test ! -r "$schema"; then
260         echo "can't find vswitch.ovsschema, please specify --schema" >&2
261         exit 1
262     fi
263     if $ovn; then
264         echo "running with ovn is only supported from the build dir." >&2
265         exit 1
266     fi
267 fi
268
269 # Create sandbox.
270 rm -rf sandbox
271 mkdir sandbox
272 sandbox=`cd sandbox && pwd`
273
274 # Set up environment for OVS programs to sandbox themselves.
275 OVS_RUNDIR=$sandbox; export OVS_RUNDIR
276 OVS_LOGDIR=$sandbox; export OVS_LOGDIR
277 OVS_DBDIR=$sandbox; export OVS_DBDIR
278 OVS_SYSCONFDIR=$sandbox; export OVS_SYSCONFDIR
279
280 if $built; then
281     # Easy access to OVS manpages.
282     (cd "$builddir" && ${MAKE} install-man mandir="$sandbox"/man)
283     MANPATH=$sandbox/man:; export MANPATH
284 fi
285
286 # Ensure cleanup.
287 trap 'kill `cat "$sandbox"/*.pid`' 0 1 2 3 13 14 15
288
289 # Create database and start ovsdb-server.
290 touch "$sandbox"/.conf.db.~lock~
291 run ovsdb-tool create conf.db "$schema"
292 ovsdb_server_args=
293 if $ovn; then
294     touch "$sandbox"/.ovnsb.db.~lock~
295     touch "$sandbox"/.ovnnb.db.~lock~
296     run ovsdb-tool create ovnsb.db "$ovnsb_schema"
297     run ovsdb-tool create ovnnb.db "$ovnnb_schema"
298     ovsdb_server_args="ovnsb.db ovnnb.db conf.db"
299 fi
300 rungdb $gdb_ovsdb $gdb_ovsdb_ex ovsdb-server --detach --no-chdir --pidfile -vconsole:off --log-file \
301     --remote=punix:"$sandbox"/db.sock $ovsdb_server_args
302
303 #Add a small delay to allow ovsdb-server to launch.
304 sleep 0.1
305
306 #Wait for ovsdb-server to finish launching.
307 if test ! -e "$sandbox"/db.sock; then
308     echo -n "Waiting for ovsdb-server to start..."
309     while test ! -e "$sandbox"/db.sock; do
310         sleep 1;
311     done
312     echo "  Done"
313 fi
314
315 # Initialize database.
316 run ovs-vsctl --no-wait -- init
317
318 # Start ovs-vswitchd.
319 rungdb $gdb_vswitchd $gdb_vswitchd_ex ovs-vswitchd --detach --no-chdir --pidfile -vconsole:off --log-file \
320     --enable-dummy=override -vvconn -vnetdev_dummy
321
322 if $ovn; then
323     ovs-vsctl set open . external-ids:system-id=56b18105-5706-46ef-80c4-ff20979ab068
324     ovs-vsctl set open . external-ids:ovn-remote=unix:"$sandbox"/db.sock
325     ovs-vsctl set open . external-ids:ovn-encap-type=geneve
326     ovs-vsctl set open . external-ids:ovn-encap-ip=127.0.0.1
327     ovs-vsctl add-br br-int \
328         -- set bridge br-int fail-mode=secure other-config:disable-in-band=true
329
330     rungdb $gdb_ovn_northd ovn-northd --detach --no-chdir --pidfile -vconsole:off --log-file
331     rungdb $gdb_ovn_controller ovn-controller --detach --no-chdir --pidfile -vconsole:off --log-file
332 fi
333
334 cat <<EOF
335
336
337
338 ----------------------------------------------------------------------
339 You are running in a dummy Open vSwitch environment.  You can use
340 ovs-vsctl, ovs-ofctl, ovs-appctl, and other tools to work with the
341 dummy switch.  
342
343 Log files, pidfiles, and the configuration database are in the
344 "sandbox" subdirectory.
345
346 Exit the shell to kill the running daemons.
347 EOF
348
349 status=0; $SHELL || status=$?
350
351 cat <<EOF
352 ----------------------------------------------------------------------
353
354
355
356 EOF
357
358 exit $status