Network setup (creates br0 using ovs-switch)
Edit as appropriate to use nmcli ![]()
[chuck@lizum plexqa.2003]$ cat Network-setup
#!/bin/bash
# If I am not root, exit
if [ "$(whoami)" != "root" ]; then
echo ERROR: MUST run as root user
exit
fi
# Confirm required Openvswitch-switch installed
if [ -z "$(dpkg -l | grep openvswitch-switch)" ]; then
echo INSTALLING openvswitch-switch
apt -y install openvswitch-switch
fi
# Get hostname
Hostname=$(hostname)
# Determine which adapter to use
AdapterName="$(ip link show | grep UP | grep -v UNKNOWN | awk '{print $2}' | tr -d ':' | head -1 )"
if [ "$AdapterName" == "" ]; then
echo ERROR: No usable adapters found. Please verify basic network configuration
exit 1
fi
# Inform:
echo Creating network configuration for: $AdapterName on $Hostname
# Archive all YAML files
cd /etc/netplan
if [ ! -z "$(find . -name \*.yaml -print)" ]; then
tar cf /etc/netplan/netplan-archive."$(date +%Y-%m-%d-%T)".tar *.yaml
rm -f *.yaml
fi
# Setup network.
# will use 'br0' as bridge for LXD/LXC
# Write basic template then EDIT based on $(ip link) results above.
cat > /etc/netplan/$Hostname-config.yaml <<EOF
# This is the network config based on 'subiquity'
# Customized for HOSTNAME LXD/LXC use
network:
ethernets:
# Adapter will come up without IP
ADAPTERNAME: {}
# br0 will get and have IP
br0:
dhcp4: true
dhcp6: true
version: 2
EOF
# Edit the host and adapter name to use what we found , writing into /etc/netplan
sed -i s/HOSTNAME/$Hostname/ /etc/netplan/$(hostname)-config.yaml
sed -i s/ADAPTERNAME/$AdapterName/ /etc/netplan/$(hostname)-config.yaml
netplan generate
netplan apply
#
echo Network configuration generated and applied
echo Creating bridge and adding port
# Now, if there is an existing ovs bridge br0, clean up and apply fresh
if [ ! -z "$(ovs-vsctl list-br)" ]; then
# remove bridge (free up adapter)
echo Removing existing bridge: br0
ovs-vsctl del-br br0
fi
# Add br0 to ovs-vsctl and connect to physical adapter
echo Creating br0 and adding $AdapterName
ovs-vsctl add-br br0 -- add-port br0 $AdapterName
# Confirm
Bridges="$(ovs-vsctl list-br)"
if [ -z "$Bridges" ]; then
echo ERROR: Unable to create bridge 'br0'. Setup manually and add '$AdapterName' as a port on the bridge.
exit 1
fi
# Did adapter get attached?
Adapters="$(ovs-vsctl list-ports br0 | grep -v veth )"
if [ "$Adapters" != "$AdapterName" ]; then
echo ERROR: Bridge 'br0' does not have $AdapterName attached. It has: $Adapters.
echo " Fix manually"
exit 1
fi
# Now restart the host and proceed to step 2
echo Now reboot the host.
echo CONFIRM: 'br0' is the default adapter and has IP address previously assigned this adapter
[chuck@lizum plexqa.2004]$