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