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