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