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