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