-
Notifications
You must be signed in to change notification settings - Fork 0
/
ovs-bridge-builder.sh
executable file
·579 lines (533 loc) · 17.3 KB
/
ovs-bridge-builder.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#!/bin/bash
# Network Setup to create LXD & Libvirt OpenVswitch Networks
# This currently supports Ubuntu-Xenial only; YMMV on other OS'es
# Requires running LXD Libvirt and OpenVSwitch components
## TO-DO List
# Add logging function
# Add Verbose/Quiet run option
# Add package/dependency install function
# Add GRE IFACE Configuration as optional function
# Add function to create local LXD Firewall/Gateway Container
# Add support for classic LXD Snap "lxd.lxc commands"
# Add support for other LXD storage configuration options during profile creation
# Add Better Error handling & Detection
# Add support for Ubuntu Core OS
# Add support for LXD+MAAS Integration
# - https://github.com/lxc/lxd/blob/master/doc/containers.md (MAAS Integration)
#
# Review & research:
# - https://github.com/yeasy/easyOVS
# Enable multi-distribution detection & setting of service unit name congruence
# Check if run as root!
if [[ "$EUID" -ne "0" ]]; then
echo "ERROR: Must be run with root/sudo priviledges!"
echo "Exiting!"
exit 1
fi
# Set Output Formatting Variables:
SEP_1="------------------------------------------------------------------+"
SEP_2=" |"
SEP_3=" +"
# Set Bridge-Builder Variables
# Used unless otherwise set by flags at run-time
echo "[o00.0b]$SEP_1"
echo "$SEP_2 Setting Default Variables"
OBB_VERSION=v00.81.a
#Distribution Specific System Service Names
LXD_SERVICE="lxd.service"
OVS_SERVICE="openvswitch-switch.service"
LIBVIRT_SERVICE="libvirtd.service"
# Default Variables
DEFAULT_NETWORK_NAME="obb"
NETWORK_NAME="$DEFAULT_NETWORK_NAME"
TMP_FILE_STORE=/tmp/bridge-builder/
BRIDGE_DRIVER="openvswitch"
PURGE_NETWORK=false
SHOW_CONFIG=false
SHOW_HEALTH=false
SHOW_HELP=false
WORK_DIR=$(pwd)
# Read variables from command line
echo "$SEP_2 Enabling Command Line Options"
OPTS=`getopt -o phnsHz: --long help,name,show,health,purge: -n 'parse-options' -- "$@"`
# Fail if options are not sane
echo "$SEP_2 Checking Command Line Option Sanity"
if [ $? != 0 ] ;
then echo "$SEP_2 Failed parsing options ... Exiting!" >&2 ;
exit 1
fi
eval set -- "$OPTS"
# Parse variables
echo "$SEP_2 Parsing Command Line Options"
while true; do
case "$1" in
-h | --help ) SHOW_HELP=true; shift ;;
-n | --name ) NETWORK_NAME="$3"; shift; shift ;;
-s | --show ) SHOW_CONFIG=true; shift;;
-H | --health ) SHOW_HEALTH=true; shift ;;
-p | --purge ) PURGE_NETWORK="$3"; shift; shift ;;
-- ) shift; break ;;
* ) break ;;
esac
done
echo "[o00.0e]$SEP_1"
print_VARS () {
#Print option values
echo "[d00.0b]$SEP_1"
echo " | SHOW_HELP = $SHOW_HELP"
echo " | NETWORK_NAME = $NETWORK_NAME"
echo " | SHOW_CONFIG = $SHOW_CONFIG"
echo " | SHOW_HEALTH = $SHOW_HEALTH"
echo " | PURGE_NETWORK = $PURGE_NETWORK"
echo " | Confirmed command line options are useable .... Continuing"
echo "[d00.0e]$SEP_1"
}
#Debug & Testing break
DBG () {
echo " | NOTICE: DBG Calls Enabled"
print_VARS
# exit 0
}
DBG
end_BUILD () {
# Confirm end of setup script
echo "[f0e.0s]$SEP_1"
echo "$SEP_2 $NETWORK_NAME Build complete for LXD and KVM"
echo "[f0e.0e]$SEP_1"
}
config_LIBVIRT () {
# create virsh network xml & define new virsh network
echo "[f08.0s]$SEP_1"
echo "$SEP_2 Configuring Network Definitions for Libvirtd+KVM+QEMU"
# Set VIRSH Working Variables
VIRSH_XML_FILE=$NETWORK_NAME.xml
VIRSH_XML_PATH="/var/lib/libvirt/network-config"
VIRSH_XML_TARGET=$VIRSH_XML_PATH/$VIRSH_XML_FILE
# Create xml file path and file
echo "[f08.1r]$SEP_1"
echo "$SEP_2 Creating virsh network xml configuration file"
mkdir -p $VIRSH_XML_PATH
echo "$SEP_2 Creating virsh network xml directory"
# Write xml configuration
echo "[f08.2r]$SEP_1"
echo "$SEP_2 Writing configuration:
$SEP_2 > $VIRSH_XML_PATH/$VIRSH_XML_FILE"
cat >$VIRSH_XML_TARGET <<EOF
<network>
<name>$NETWORK_NAME</name>
<forward mode='bridge'/>
<bridge name='$NETWORK_NAME' />
<virtualport type='openvswitch'/>
</network>
EOF
echo "$SEP_2 $VIRSH_XML_FILE VIRSH XML Config Written"
# Defining libvirt network $NETWORK_NAME
echo "[f08.3r]$SEP_1"
echo "$SEP_2 Creating virsh network from $VIRSH_XML_TARGET"
echo "$SEP_3
"
virsh net-define $VIRSH_XML_TARGET
echo "$SEP_3"
echo "$SEP_2 > Defined virsh network from $VIRSH_XML_FILE"
#Starting Libvirt network
echo "$SEP_3"
echo "[f08.4r] Starting virsh $NETWORK_NAME"
echo "$SEP_3
"
virsh net-start $NETWORK_NAME
# Setting network to auto-start at boot
echo "$SEP_3"
echo "$SEP_2 Switching virsh $NETWORK_NAME to autostart"
echo "$SEP_3
"
virsh net-autostart $NETWORK_NAME
echo "$SEP_3"
echo "[f08.0e] Done Configuring Libvirt $NETWORK_NAME"
}
# Create initial bridge with OVS driver & configure LXD
config_LXD () {
# Create network via LXD API
echo "[f07.0s]$SEP_1"
echo "$SEP_2 Building LXD Network \"$NETWORK_NAME\" using \"$BRIDGE_DRIVER\" driver"
echo "$SEP_3
"
lxc network create $NETWORK_NAME
echo "
$SEP_3"
echo "$SEP_2 Created LXD Network $NETWORK_NAME"
# Setup network driver type
echo "[f07.1r]$SEP_1"
lxc network set $NETWORK_NAME \
bridge.driver $BRIDGE_DRIVER
echo "$SEP_2 Configured $NETWORK_NAME with $BRIDGE_DRIVER driver"
## DNS configuration Options
# define default domain name:
#echo "[f07.2r]$SEP_1"
#lxc network set $NETWORK_NAME \
# dns.domain $DNS_DOMAIN
# echo "$SEP_2 Configured $NETWORK_NAME with default domain name: $DNS_DOMAIN"
# define dns mode = set via hostname
#lxc network set $NETWORK_NAME \
# dns.mode dynamic
echo "[f07.3r] Disabling LXD IP Configuration"
# Set ipv4 address on bridge
lxc network set $NETWORK_NAME \
ipv4.address none
# Set ipv6 address on bridge
lxc network set $NETWORK_NAME \
ipv6.address none
# Configure ipv4 & ipv6 address on bridge [true/false]
#echo "[f07.4r] Disabling Bridge Address"
# configure ipv4 nat setting
#lxc network set $NETWORK_NAME \
# ipv4.nat $NATv4
# echo "$SEP_2 Switching ipv4 nat to $NATv4"
# configure ipv4 nat setting
#lxc network set $NETWORK_NAME \
# ipv6.nat $NATv6
# echo "$SEP_2 Switching ipv6 nat to $NATv6"
# Configure routing on bridge [enable/disable]
echo "[f07.5r] Disabling Bridge Routing function"
# set ipv4 routing
#lxc network set $NETWORK_NAME \
# ipv4.routing $ROUTEv4
# echo "$SEP_2 Switching ipv4 routing to $DHCPv4"
# Set ipv6 routing
#lxc network set $NETWORK_NAME \
# ipv6.routing $ROUTEv6
# echo "$SEP_2 Switching ipv6 routing to $DHCPv6"
# configure dhcp on bridge
# options: true false
echo "[f07.6r] Disabling LXD DHCP Function"
# set ipv4 dhcp
lxc network set $NETWORK_NAME \
ipv4.dhcp $DHCPv4
# set ipv6 dhcp
lxc network set $NETWORK_NAME \
ipv6.dhcp $DHCPv6
# Bridge nat+router+firewall settings
echo "[f07.7r] Disabling LXD NAT+Firewall Function"
# disable ipv4 firewall
lxc network set $NETWORK_NAME \
ipv4.firewall false
# disable ipv6 firewall
lxc network set $NETWORK_NAME \
ipv6.firewall false
# Create associated lxd profile with default ethernet device name and storage
# path
echo "[f07.8r] Creating LXD Profile for $NETWORK_NAME"
echo "$SEP_3
"
lxc profile create $LXD_PROFILE
lxc profile device add $NETWORK_NAME $LXD_PROFILE \
nic nictype=bridged parent=$NETWORK_NAME
lxc profile device add $LXD_PROFILE \
root disk path=/ pool=default
echo "
$SEP_3"
echo "[f07.0e] LXD Network \"$NETWORK_NAME\" Configuration Complete"
}
check_DEFAULTS () {
echo "[f06.0s]$SEP_1"
echo "[f06.1r] Validating All LXD Configuration Variables"
DEFAULT_CHECK_CONFIRM="Are you sure you want to continue building"
if [ $NETWORK_NAME == $DEFAULT_NETWORK_NAME ]
then
echo "[f06.2r] WARN: Bridge Builder run with default value for OVS network configuration!"
while true; do
read -p "$DEFAULT_CHECK_CONFIRM $NETWORK_NAME?" yn
case $yn in
[Yy]* ) echo "Continuing ...." ; break;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
else
echo "[f06.3r]----------------------------------------------------------"
echo " | Preparing to configure $NETWORK_NAME"
fi
echo "[f06.0e]$SEP_1"
}
set_LXD_DEFAULTS () {
# Define default DNS domain assignment
echo "[f05.0s]$SEP_1"
echo "$SEP_2 Setting additional LXD Network and Profile Build Variables"
#DNS_DOMAIN="braincraft.io"
#echo "Setting Default Domain Name to $DNS_DOMAIN"
# Set Working Dir for temp files such as network .xml definitions for KVM+QEMU
IFACE_CFG_DIR="/root/network/"
# Set LXD Profile name to match network name
LXD_PROFILE=$NETWORK_NAME
# Configure default LXD container interface name
LXD_ETHERNET="eth0"
# Configure DHCP function
# Valid options "true|false"
DHCPv4="false"
DHCPv6="false"
# Configure Routing function
# Valid Options "true|false"
ROUTEv4="false"
ROUTEv6="false"
echo "[f05.0e]$SEP_1"
}
BUILD () {
# Core Bridge Builder Feature
# This function calls the child functions which build the bridge and configure
# client services such as LXD and KVM+QEMU (virsh) for use with OVS
echo "[f04.0s]$SEP_1"
echo "[f04.1r] > Checking System Readiness"
check_SERVICE_HEALTH
echo "[f04.2r]$SEP_2 > Setting LXD Default Variables"
set_LXD_DEFAULTS
echo "[f04.3r]$SEP_2 > Checking LXD Variables"
check_DEFAULTS
echo "[f04.4r]$SEP_2 > purging any pre-existing $NETWORK_NAME configuration"
purge_NETWORK
echo "[f04.5r]$SEP_2 > Starting LXD Configuration"
config_LXD
config_LIBVIRT
show_CONFIG
echo "[f04.0s]$SEP_1"
}
purge_NETWORK () {
# Purge any conflicting networks ) --purge | -p
if [ $PURGE_NETWORK = "false" ]
then
PURGE_NETWORK=$NETWORK_NAME
fi
echo "[f03.0s]$SEP_1"
echo "[f03.1r] Purging $PURGE_NETWORK from LXD Network and Profile configuration"
lxc network delete $PURGE_NETWORK > /dev/null 2>&1 ;
lxc profile delete $LXD_PROFILE > /dev/null 2>&1
echo "$SEP_2 > Purged $PURGE_NETWORK from LXD configuration"
echo "[f03.2r] Purging $PURGE_NETWORK from Libvirt Network Configuration"
virsh net-undefine $PURGE_NETWORK > /dev/null 2>&1 ;
virsh net-destroy $PURGE_NETWORK > /dev/null 2>&1 ;
echo "$SEP_2 > Purged $PURGE_NETWORK from Libvirt configuration"
echo "[f03.3r] Purging OpenVswitch Configuration"
ovs-vsctl del-br $PURGE_NETWORK > /dev/null 2>&1 ;
echo "$SEP_2 > Purged $PURGE_NETWORK from OpenVswitch configuration"
echo "$SEP_2 Finished Purging $PURGE_NETWORK from system"
echo "[f03.0e]$SEP_1"
}
check_SERVICE_HEALTH () {
# Confirm OVS/LXD/Libvirtd services are all running ) --health | -H
echo "[f02.0s]$SEP_1"
echo "$SEP_2 Checking service health"
LXD_STATUS=$(systemctl is-active $LXD_SERVICE)
LXD_ENABLED=$(systemctl is-enabled $LXD_SERVICE)
if [ $LXD_STATUS != "active" ]
then
echo "$SEP_2 $LXD_SERVICE does not appear to be running"
echo "$SEP_2 Starting $LXD_SERVICE "
systemctl start $LXD_SERVICE
LXD_STATUS=$(systemctl is-active $LXD_SERVICE)
fi
if [ $LXD_ENABLED != "enabled" ] && [ $LXD_ENABLED != "indirect" ]
then
echo "$SEP_2 $LXD_SERVICE does not appear to be enabled"
echo "$SEP_2 Enabling $LXD_SERVICE"
systemctl enable $LXD_SERVICE
LXD_ENABLED=$(systemctl is-enabled $LXD_SERVICE)
fi
echo "[f02.1r]$SEP_1"
echo "$SEP_2 LXD Daemon is $LXD_STATUS & $LXD_ENABLED"
if [ $LXD_STATUS != "active" ] && [ $LXD_ENABLED != "enabled" ]
then
echo "$SEP_2 LXD Service is not running or enabled persistently"
echo "$SEP_2 Install/Enable and initialize LXD service or check configuration and try again"
exit 0
else
echo "$SEP_2 LXD Service is READY"
fi
echo "[f02.2r]$SEP_1"
LIBVIRT_STATUS=$(systemctl is-active $LIBVIRT_SERVICE)
LIBVIRT_ENABLED=$(systemctl is-enabled $LIBVIRT_SERVICE)
if [ $LIBVIRT_STATUS != "active" ]
then
systemctl start $LIBVIRT_SERVICE
LIBVIRT_STATUS=$(systemctl is-active $LIBVIRT_SERVICE)
fi
if [ $LIBVIRT_ENABLED != "enabled" ]
then
systemctl start $LIBVIRT_SERVICE
LIBVIRT_ENABLED=$(systemctl is-active $LIBVIRT_SERVICE)
fi
echo "$SEP_2 Libvirt Daemon is $LIBVIRT_STATUS & $LIBVIRT_ENABLED"
if [ $LIBVIRT_STATUS != "active" ] && [ $LIBVIRT_ENABLED != "enabled" ]
then
echo "$SEP_2 Libvirtd Service is not running or enabled persistently"
echo "$SEP_2 Install/Enable Libvirtd service or check configuration and try again"
exit 0
else
echo "$SEP_2 Libvirtd Service is READY"
fi
echo "[f02.3r]$SEP_1"
OVS_STATUS=$(systemctl is-active $OVS_SERVICE)
OVS_ENABLED=$(systemctl is-enabled $OVS_SERVICE)
if [ $OVS_STATUS != "active" ]
then
systemctl start $OVS_SERVICE
OVS_STATUS=$(systemctl is-active $OVS_SERVICE)
fi
if [ $OVS_ENABLED != "enabled" ]
then
systemctl enable $OVS_SERVICE
OVS_ENABLED=$(systemctl is-enabled $OVS_SERVICE)
fi
echo "$SEP_2 OVS Daemon is $OVS_STATUS & $OVS_ENABLED"
if [ $OVS_STATUS != "active" ] && [ $OVS_ENABLED != "enabled" ]
then
echo "$SEP_2 OVS Service is not running or enabled persistently"
echo "$SEP_2 Install/Enable OpenVswitch or check configuration and try again"
exit 0
else
echo "$SEP_2 OVS Service is READY"
fi
echo "[f02.0e]$SEP_1"
}
show_CONFIG () {
# Show current networks configured for OVS/LXD/KVM+QEMU ) --show | -s
echo "[f01.0s]$SEP_1"
echo "$SEP_2 Showing Local Bridge Configuration"
echo "[f01.0s]$SEP_1"
echo "$SEP_2"
#Checking System Service Status
OVS_SERVICE_STATUS=$(systemctl is-active $OVS_SERVICE)
LXD_SERVICE_STATUS=$(systemctl is-active $LXD_SERVICE)
LIBVIRT_SERVICE_STATUS=$(systemctl is-active $LIBVIRT_SERVICE)
# List Openvswitch Networks
echo "$SEP_2 $OVS_SERVICE = $OVS_SERVICE_STATUS"
echo "$SEP_2 $LXD_SERVICE = $LXD_SERVICE_STATUS"
echo "$SEP_2 $LIBVIRT_SERVICE = $LIBVIRT_SERVICE_STATUS"
echo "$SEP_2"
echo "$SEP_3
"
if [ "$OVS_SERVICE_STATUS" = active ]
then
echo "[f01.1r] > OpenVSwitch Configuration <"
echo "$SEP_3
"
ovs-vsctl show
echo "
$SEP_3"
else
echo "$SEP_2 ERROR: The OpenVSwitch System Service IS NOT RUNNING"
fi
# List LXD Networks
if [ "$LXD_SERVICE_STATUS" = "active" ]
then
echo "[f01.2r] > LXD Networks List <"
echo "$SEP_3
"
lxc network list
echo "
$SEP_3"
else
echo "$SEP_2 ERROR: The LXD System Service IS NOT RUNNING"
fi
# List LibVirtD Networks
if [ "$LIBVIRT_SERVICE_STATUS" = "active" ]
then
echo "[f01.3r] > LibVirtD Network Configuration < "
echo "$SEP_3
"
virsh net-list --all
echo "
$SEP_3"
else
echo "$SEP_2 ERROR: The LibVirtD System Service IS NOT RUNNING"
fi
echo "[f01.0e]$SEP_1"
}
show_HELP () {
# Show Help menu ) --help | -h
echo "[f0h.0s]$SEP_1"
echo "$SEP_3"
echo "
syntax:
command [option] [value]
This tool will create a new OVS bridge.
By default, this bridge will be ready for use with each of the following:
LXD:
\_______
\_Launch a container with the \"lxd profile\" flag to attach
| Example:
| lxc launch ubuntu: test-container -p \$NETWORK_NAME
Libvirt Guests:
\_______
\_Attach Libvirt / QEMU / KVM guests:
| Example:
| virt-manager nic configuration
| virsh xml configuration
Physical Ports:
\_______
\_Attach Physical Ports via the following
| Example with physical device name "eth0"
| ovs-vsctl add-port $NETWORK_NAME eth0
Options:
--help -h -- Print this help menu
--health-check -H -- Check OVS|LXD|Libvirtd Service Status
--show-config -c -- Show current networks configured locally
--purge -p -- Purges network when pased with a value
matching an existing network name.
--name -n -- Sets the name for building the following:
OVS Bridge
Libvirt Bridge
LXD Network & Profile Name
"
echo "$SEP_3"
echo "[f0h.0e]$SEP_1"
}
RUN () {
# Initial function that determines behavior from command line flags
if [ $SHOW_HELP != 'false' ]
then
echo "[f0h.0o]<"
show_HELP
echo "$SEP_2 OVS_BridgeBuilder_VERSION = $OBB_VERSION"
echo "[f0h.0c]$SEP_1"
exit 0
fi
if [ $SHOW_CONFIG != 'false' ]
then
echo "[f01.0o]<"
show_CONFIG
echo "[f01.0c]$SEP_1"
exit 0
fi
if [ $SHOW_HEALTH != 'false' ]
then
echo "[f02.0o]<"
check_SERVICE_HEALTH
echo "[q00.3c]$SEP_1"
exit 0
fi
if [ $PURGE_NETWORK != 'false' ]
then
echo "[f03.0o]<"
echo "$SEP_2 Purging $PURGE_NETWORK ..."
purge_NETWORK
show_CONFIG
echo "$SEP_2 Removed $PURGE_NETWORK"
echo "[f03.0c]$SEP_1"
exit 0
fi
if [ $SHOW_HELP == 'false' ] && \
[ $SHOW_CONFIG == 'false' ] && \
[ $SHOW_HEALTH == 'false' ] && \
[ $PURGE_NETWORK == 'false' ]
then
echo "[f04.0o]<"
BUILD
echo "[f04.1r]<"
end_BUILD
echo "[f04.0c]"
exit 0
else
echo "$SEP_2 ERROR: Unable to parse comand line options .. EXITING!"
exit 0
fi
}
# Start initial function that determines behavior from command line flags
RUN