]> git.proxmox.com Git - mirror_ovs.git/blob - tutorial/ovs-sandbox
treewide: Convert leading tabs to spaces.
[mirror_ovs.git] / tutorial / ovs-sandbox
1 #! /bin/sh
2 #
3 # Copyright (c) 2013, 2015, 2016 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 run_xterm() {
25 title=$1;
26 shift
27 run xterm -T "$title" -e "$@" &
28 }
29
30 rungdb() {
31 under_gdb=$1
32 gdb_run=$2
33 shift
34 shift
35
36 # Remove the --detach and to put the process under gdb control.
37 # Also remove --vconsole:off to allow error message to show up
38 # on the console.
39 # Use "DISPLAY" variable to determine out if X is supported
40 if $under_gdb && [ "$DISPLAY" ]; then
41 args=`echo $@ |sed s/--detach//g | sed s/--vconsole:off//g`
42 xterm_title=$1
43
44 gdb_cmd=""
45 if $gdb_run; then
46 gdb_cmd="-ex run"
47 fi
48
49 run_xterm $xterm_title gdb $gdb_cmd --args $args
50 else
51 run $@
52 fi
53 }
54
55 gdb_vswitchd=false
56 gdb_ovsdb=false
57 gdb_vswitchd_ex=false
58 gdb_ovsdb_ex=false
59 gdb_ovn_northd=false
60 gdb_ovn_northd_ex=false
61 gdb_ovn_controller=false
62 gdb_ovn_controller_ex=false
63 gdb_ovn_controller_vtep=false
64 gdb_ovn_controller_vtep_ex=false
65 builddir=
66 srcdir=
67 schema=
68 installed=false
69 built=false
70 ovn=false
71 ovnsb_schema=
72 ovnnb_schema=
73 ovn_rbac=true
74 n_northds=1
75 n_controllers=1
76 nbdb_model=standalone
77 nbdb_servers=3
78 sbdb_model=backup
79 sbdb_servers=3
80 dummy=override
81
82 for option; do
83 # This option-parsing mechanism borrowed from a Autoconf-generated
84 # configure script under the following license:
85
86 # Copyright (C) 1992, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
87 # 2002, 2003, 2004, 2005, 2006, 2009, 2013 Free Software Foundation, Inc.
88 # This configure script is free software; the Free Software Foundation
89 # gives unlimited permission to copy, distribute and modify it.
90
91 # If the previous option needs an argument, assign it.
92 if test -n "$prev"; then
93 eval $prev=\$option
94 prev=
95 continue
96 fi
97 case $option in
98 *=*) optarg=`expr "X$option" : '[^=]*=\(.*\)'` ;;
99 *) optarg=yes ;;
100 esac
101
102 case $dashdash$option in
103 --)
104 dashdash=yes ;;
105 -h|--help)
106 cat <<EOF
107 ovs-sandbox, for starting a sandboxed dummy Open vSwitch environment
108 usage: $0 [OPTION...]
109
110 If you run ovs-sandbox from an OVS build directory, it uses the OVS that
111 you built. Otherwise, if you have an installed Open vSwitch, it uses
112 the installed version.
113
114 These options force ovs-sandbox to use a particular OVS build:
115 -b, --builddir=DIR specify Open vSwitch build directory
116 -s, --srcdir=DIR specify Open vSwitch source directory
117 These options force ovs-sandbox to use an installed Open vSwitch:
118 -i, --installed use installed Open vSwitch
119
120 General options:
121 -g, --gdb-vswitchd run ovs-vswitchd under gdb
122 -d, --gdb-ovsdb run ovsdb-server under gdb
123 --gdb-ovn-northd run ovn-northd under gdb
124 --gdb-ovn-controller run ovn-controller under gdb
125 --gdb-ovn-controller-vtep run ovn-controller-vtep under gdb
126 --dummy=ARG pass --enable-dummy=ARG to vswitchd (default: override)
127 -R, --gdb-run automatically start running the daemon in gdb
128 for any daemon set to run under gdb
129 -S, --schema=FILE use FILE as vswitch.ovsschema
130
131 OVN options:
132 -o, --ovn enable OVN
133 --no-ovn-rbac disable role-based access control for OVN
134 --n-northds=NUMBER run NUMBER copies of northd (default: 1)
135 --nbdb-model=standalone|backup|clustered northbound database model
136 --nbdb-servers=N number of servers in nbdb cluster (default: 3)
137 --sbdb-model=standalone|backup|clustered southbound database model
138 --sbdb-servers=N number of servers in sbdb cluster (default: 3)
139
140 Other options:
141 -h, --help Print this usage message.
142 EOF
143 exit 0
144 ;;
145
146 --b*=*)
147 builddir=$optarg
148 built=:
149 ;;
150 -b|--b*)
151 prev=builddir
152 built=:
153 ;;
154 --sr*=*)
155 srcdir=$optarg
156 built=false
157 ;;
158 --dummy)
159 prev=dummy
160 ;;
161 --dummy=*)
162 dummy=$optarg
163 ;;
164 -s|--sr*)
165 prev=srcdir
166 built=false
167 ;;
168 -i|--installed)
169 installed=:
170 ;;
171 --sc*=*)
172 schema=$optarg
173 installed=:
174 ;;
175 -S|--sc*)
176 prev=schema
177 installed=:
178 ;;
179 -g|--gdb-v*)
180 gdb_vswitchd=true
181 gdb_vswitchd_ex=false
182 ;;
183 -e|--gdb-ex-v*)
184 gdb_vswitchd=true
185 gdb_vswitchd_ex=true
186 ;;
187 -d|--gdb-ovsdb)
188 gdb_ovsdb=true
189 gdb_ovsdb_ex=false
190 ;;
191 -r|--gdb-ex-o*)
192 gdb_ovsdb=true
193 gdb_ovsdb_ex=true
194 ;;
195 --gdb-ovn-northd)
196 gdb_ovn_northd=true
197 ;;
198 --gdb-ovn-controller)
199 gdb_ovn_controller=true
200 ;;
201 --gdb-ovn-controller-vtep)
202 gdb_ovn_controller_vtep=true
203 ;;
204 -o|--ovn)
205 ovn=true
206 ;;
207 --no-ovn-rbac)
208 ovn_rbac=false
209 ;;
210 --n-northd*=*)
211 n_northds=$optarg
212 ;;
213 --n-northd*)
214 prev=n_northds
215 ;;
216 --n-controller*=*)
217 n_controllers=$optarg
218 ;;
219 --n-controller*)
220 prev=n_controllers
221 ;;
222 --nbdb-s*=*)
223 nbdb_servers=$optarg
224 nbdb_model=clustered
225 ;;
226 --nbdb-s*)
227 prev=nbdb_servers
228 nbdb_model=clustered
229 ;;
230 --nbdb-m*=*)
231 nbdb_model=$optarg
232 ;;
233 --nbdb-m*)
234 prev=nbdb_model
235 ;;
236 --sbdb-s*=*)
237 sbdb_servers=$optarg
238 sbdb_model=clustered
239 ;;
240 --sbdb-s*)
241 prev=sbdb_servers
242 sbdb_model=clustered
243 ;;
244 --sbdb-m*=*)
245 sbdb_model=$optarg
246 ;;
247 --sbdb-m*)
248 prev=sbdb_model
249 ;;
250 -R|--gdb-run)
251 gdb_vswitchd_ex=true
252 gdb_ovsdb_ex=true
253 gdb_ovn_northd_ex=true
254 gdb_ovn_controller_ex=true
255 gdb_ovn_controller_vtep_ex=true
256 ;;
257 -*)
258 echo "unrecognized option $option (use --help for help)" >&2
259 exit 1
260 ;;
261 *)
262 echo "$option: non-option arguments not supported (use --help for help)" >&2
263 exit 1
264 ;;
265 esac
266 shift
267 done
268
269 if $installed && $built; then
270 echo "sorry, conflicting options (use --help for help)" >&2
271 exit 1
272 elif $installed || $built; then
273 :
274 elif test -e vswitchd/ovs-vswitchd; then
275 built=:
276 builddir=.
277 elif (ovs-vswitchd --version) >/dev/null 2>&1; then
278 installed=:
279 else
280 echo "can't find an OVS build or install (use --help for help)" >&2
281 exit 1
282 fi
283
284 if $built; then
285 if test ! -e "$builddir"/vswitchd/ovs-vswitchd; then
286 echo "$builddir does not appear to be an OVS build directory" >&2
287 exit 1
288 fi
289 builddir=`cd $builddir && pwd`
290
291 # Find srcdir.
292 case $srcdir in
293 '')
294 srcdir=$builddir
295 if test ! -e "$srcdir"/README.rst; then
296 srcdir=`cd $builddir/.. && pwd`
297 fi
298 ;;
299 /*) ;;
300 *) srcdir=`pwd`/$srcdir ;;
301 esac
302 schema=$srcdir/vswitchd/vswitch.ovsschema
303 if test ! -e "$schema"; then
304 echo >&2 'source directory not found, please use --srcdir'
305 exit 1
306 fi
307 if $ovn; then
308 ovnsb_schema=$srcdir/ovn/ovn-sb.ovsschema
309 if test ! -e "$ovnsb_schema"; then
310 echo >&2 'source directory not found, please use --srcdir'
311 exit 1
312 fi
313 ovnnb_schema=$srcdir/ovn/ovn-nb.ovsschema
314 if test ! -e "$ovnnb_schema"; then
315 echo >&2 'source directory not found, please use --srcdir'
316 exit 1
317 fi
318 vtep_schema=$srcdir/vtep/vtep.ovsschema
319 if test ! -e "$vtep_schema"; then
320 echo >&2 'source directory not found, please use --srcdir'
321 exit 1
322 fi
323 fi
324
325 # Put built tools early in $PATH.
326 if test ! -e $builddir/vswitchd/ovs-vswitchd; then
327 echo >&2 'build not found, please change set $builddir or change directory'
328 exit 1
329 fi
330 PATH=$builddir/ovsdb:$builddir/vswitchd:$builddir/utilities:$builddir/vtep:$PATH
331 if $ovn; then
332 PATH=$builddir/ovn/controller:$builddir/ovn/controller-vtep:$builddir/ovn/northd:$builddir/ovn/utilities:$PATH
333 fi
334 export PATH
335 else
336 case $schema in
337 '')
338 for schema in \
339 /usr/local/share/openvswitch/vswitch.ovsschema \
340 /usr/share/openvswitch/vswitch.ovsschema \
341 none; do
342 if test -r $schema; then
343 break
344 fi
345 done
346 ;;
347 /*) ;;
348 *) schema=`pwd`/$schema ;;
349 esac
350 if test ! -r "$schema"; then
351 echo "can't find vswitch.ovsschema, please specify --schema" >&2
352 exit 1
353 fi
354 if $ovn; then
355 echo "running with ovn is only supported from the build dir." >&2
356 exit 1
357 fi
358 fi
359
360 # Create sandbox.
361 rm -rf sandbox
362 mkdir sandbox
363 sandbox=`cd sandbox && pwd`
364
365 # Set up environment for OVS programs to sandbox themselves.
366 OVS_RUNDIR=$sandbox; export OVS_RUNDIR
367 OVS_LOGDIR=$sandbox; export OVS_LOGDIR
368 OVS_DBDIR=$sandbox; export OVS_DBDIR
369 OVS_SYSCONFDIR=$sandbox; export OVS_SYSCONFDIR
370
371 if $built; then
372 # Easy access to OVS manpages.
373 (cd "$builddir" && ${MAKE-make} install-man install-man-rst mandir="$sandbox"/man)
374 MANPATH=$sandbox/man:; export MANPATH
375 fi
376
377 # Ensure cleanup.
378 trap 'kill `cat "$sandbox"/*.pid`' 0 1 2 3 13 14 15
379
380 # Create database and start ovsdb-server.
381 touch "$sandbox"/.conf.db.~lock~
382 run ovsdb-tool create conf.db "$schema"
383 ovsdb_server_args=
384 if $ovn; then
385 touch "$sandbox"/.ovnnb.db.~lock~
386 run ovsdb-tool create ovnnb.db "$ovnnb_schema"
387 run ovsdb-tool create vtep.db "$vtep_schema"
388 ovsdb_server_args="vtep.db conf.db"
389 ovsdb_nb_server_args="ovnnb.db"
390
391 if [ "$HAVE_OPENSSL" = yes ]; then
392 OVS_PKI="run ovs-pki --dir=$sandbox/pki --log=$sandbox/ovs-pki.log"
393 $OVS_PKI -B 1024 init
394 $OVS_PKI -B 1024 req+sign ovnsb switch
395 $OVS_PKI -B 1024 req+sign ovnnb switch
396 for i in $(seq $n_controllers); do
397 $OVS_PKI -B 1024 -u req+sign chassis-$i switch
398 done
399 fi
400 fi
401 rungdb $gdb_ovsdb $gdb_ovsdb_ex ovsdb-server --detach --no-chdir --pidfile -vconsole:off --log-file -vsyslog:off \
402 --remote=punix:"$sandbox"/db.sock \
403 --remote=db:Open_vSwitch,Open_vSwitch,manager_options \
404 $ovsdb_server_args
405 if $ovn; then
406 ovn_start_db() {
407 local db=$1 model=$2 servers=$3 schema=$4
408 local DB=$(echo $db | tr a-z A-Z)
409 local schema_name=$(ovsdb-tool schema-name $schema)
410
411 case $model in
412 standalone | backup) ;;
413 clustered)
414 case $servers in
415 [1-9] | [1-9][0-9]) ;;
416 *) echo "${db}db servers must be between 1 and 99" >&2
417 exit 1
418 ;;
419 esac
420 ;;
421 *)
422 echo "unknown ${db}db model \"$model\"" >&2
423 exit 1
424 ;;
425 esac
426
427 ovn_start_ovsdb_server() {
428 local i=$1; shift
429 rungdb $gdb_ovsdb $gdb_ovsdb_ex ovsdb-server --detach --no-chdir \
430 --pidfile=$db$i.pid -vconsole:off --log-file=$db$i.log \
431 -vsyslog:off \
432 --remote=db:$schema_name,${DB}_Global,connections \
433 --private-key=db:$schema_name,SSL,private_key \
434 --certificate=db:$schema_name,SSL,certificate \
435 --ca-cert=db:$schema_name,SSL,ca_cert \
436 --ssl-protocols=db:$schema_name,SSL,ssl_protocols \
437 --ssl-ciphers=db:$schema_name,SSL,ssl_ciphers \
438 --unixctl=${db}$i --remote=punix:$db$i.ovsdb ${db}$i.db "$@"
439 }
440
441 case $model in
442 standalone)
443 run ovsdb-tool create ${db}1.db "$schema"
444 ovn_start_ovsdb_server 1
445 remote=unix:${db}1.ovsdb
446 ;;
447 backup)
448 for i in 1 2; do
449 run ovsdb-tool create $db$i.db "$schema"
450 done
451 ovn_start_ovsdb_server 1
452 ovn_start_ovsdb_server 2 --sync-from=unix:${db}1.ovsdb
453 remote=unix:${db}1.ovsdb
454 backup_note="$backup_note
455 The backup server of OVN $DB can be accessed by:
456 * ovn-${db}ctl --db=unix:`pwd`/sandbox/${db}2.ovsdb
457 * ovs-appctl -t `pwd`/sandbox/${db}2
458 The backup database file is sandbox/${db}2.db
459 "
460 ;;
461 clustered)
462 for i in $(seq $servers); do
463 if test $i = 1; then
464 run ovsdb-tool create-cluster ${db}1.db "$schema" unix:${db}1.raft;
465 else
466 run ovsdb-tool join-cluster $db$i.db $schema_name unix:$db$i.raft unix:${db}1.raft
467 fi
468 ovn_start_ovsdb_server $i
469 done
470 remote=unix:${db}1.ovsdb
471 for i in `seq 2 $n`; do
472 remote=$remote,unix:$db$i.ovsdb
473 done
474 for i in $(seq $servers); do
475 run ovsdb-client wait unix:$db$i.ovsdb $schema_name connected
476 done
477 ;;
478 esac
479 eval OVN_${DB}_DB=\$remote
480 eval export OVN_${DB}_DB
481 }
482
483 backup_note=
484 ovn_start_db nb "$nbdb_model" "$nbdb_servers" "$ovnnb_schema"
485 ovn_start_db sb "$sbdb_model" "$sbdb_servers" "$ovnsb_schema"
486 fi
487
488 #Add a small delay to allow ovsdb-server to launch.
489 sleep 0.1
490
491 #Wait for ovsdb-server to finish launching.
492 if test ! -e "$sandbox"/db.sock; then
493 printf "Waiting for ovsdb-server to start..."
494 while test ! -e "$sandbox"/db.sock; do
495 sleep 1;
496 done
497 echo " Done"
498 fi
499
500 # Initialize database.
501 run ovs-vsctl --no-wait -- init
502
503 # Start ovs-vswitchd.
504 rungdb $gdb_vswitchd $gdb_vswitchd_ex ovs-vswitchd --detach --no-chdir --pidfile -vconsole:off --log-file -vsyslog:off \
505 --enable-dummy=$dummy -vvconn -vnetdev_dummy
506
507 if $ovn; then
508 ovn-nbctl init
509 ovn-sbctl init
510
511 ovs-vsctl set open . external-ids:system-id=chassis-1
512 ovs-vsctl set open . external-ids:hostname=sandbox
513 ovs-vsctl set open . external-ids:ovn-encap-type=geneve
514 ovs-vsctl set open . external-ids:ovn-encap-ip=127.0.0.1
515
516 if [ "$HAVE_OPENSSL" = yes ]; then
517 ovn-nbctl set-ssl $sandbox/ovnnb-privkey.pem $sandbox/ovnnb-cert.pem $sandbox/pki/switchca/cacert.pem
518 ovn-nbctl set-connection pssl:6641
519 ovn-sbctl set-ssl $sandbox/ovnsb-privkey.pem $sandbox/ovnsb-cert.pem $sandbox/pki/switchca/cacert.pem
520 if $ovn_rbac; then
521 ovn-sbctl set-connection role=ovn-controller pssl:6642
522 else
523 ovn-sbctl set-connection pssl:6642
524 fi
525 ovs-vsctl set open . external-ids:ovn-remote=ssl:127.0.0.1:6642
526 OVN_CTRLR_PKI="-p $sandbox/chassis-1-privkey.pem -c $sandbox/chassis-1-cert.pem -C $sandbox/pki/switchca/cacert.pem"
527 else
528 ovs-vsctl set open . external-ids:ovn-remote=$OVN_SB_DB
529 OVN_CTRLR_PKI=""
530 fi
531 for i in $(seq $n_northds); do
532 rungdb $gdb_ovn_northd $gdb_ovn_northd_ex ovn-northd --detach \
533 --no-chdir --pidfile=ovn-northd$i.pid -vconsole:off \
534 --log-file=ovn-northd$i.log -vsyslog:off \
535 --ovnsb-db="$OVN_SB_DB" --ovnnb-db="$OVN_NB_DB"
536 done
537 for i in $(seq $n_controllers); do
538 rungdb $gdb_ovn_controller $gdb_ovn_controller_ex ovn-controller \
539 $OVN_CTRLR_PKI --detach --no-chdir -vsyslog:off \
540 --pidfile=ovn-controller$i.pid -vconsole:off --log-file
541 done
542 rungdb $gdb_ovn_controller_vtep $gdb_ovn_controller_vtep_ex \
543 ovn-controller-vtep --detach --no-chdir --pidfile -vconsole:off \
544 $OVN_CTRLR_PKI --log-file -vsyslog:off \
545 --ovnsb-db=unix:"$sandbox"/ovnsb_db.sock
546 fi
547
548 cat <<EOF
549
550
551
552 ----------------------------------------------------------------------
553 You are running in a dummy Open vSwitch environment. You can use
554 ovs-vsctl, ovs-ofctl, ovs-appctl, and other tools to work with the
555 dummy switch.
556
557 EOF
558 if $ovn; then cat << EOF
559 This environment also has the OVN daemons and databases enabled.
560 You can use ovn-nbctl and ovn-sbctl to interact with the OVN databases.
561 $backup_note
562 EOF
563 fi
564 cat <<EOF
565 Log files, pidfiles, and the configuration database are in the
566 "sandbox" subdirectory.
567
568 Exit the shell to kill the running daemons.
569 EOF
570
571 status=0; $SHELL || status=$?
572
573 cat <<EOF
574 ----------------------------------------------------------------------
575
576
577
578 EOF
579
580 exit $status