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