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