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