#!/bin/bash # Create L3 switch based on Tun Ubuntu Server 18.04 using FRrouting and Openswitch frr_ver='6.0' frr_file='frr_6.0-1.ubuntu18.04+1_amd64.deb' user='ubuntu' ttyS0_service='/lib/systemd/system/ttyS0.service' # Check if I am root [ "$(id -u)" != 0 ] && echo "Run the script as root, exiting" && exit 1 # Update packages apt-get update [ "$?" != 0 ] && echo "System can't be updated, exiting" && exit 1 # Check if we utilities are installed - ifupdown to manage interfaces instead netplan for package in ifupdown openvswitch-switch; do dpkg --list | grep "$package" &>/dev/null [ "$?" == 1 ] && sudo apt-get -y install "$package" done # Check if ipv4_forwarding is enabled" fwd="$(sysctl net.ipv4.ip_forward | cut -d ' ' -f3)" if [ "$fwd" == 0 ]; then sysctl -w net.ipv4.ip_forward=1 sysctl -p /etc/sysctl.conf echo "I have enabled IPv4 forwarding" elif [ "$fwd" == 1 ]; then echo "IPv4 forwarding is enabled betwwen interfaces, nothing to do" else echo "IPv4 forwarding between interfaces is not enabled, exiting" exit 1 fi # Stop renaming interface e.g. eth0 to ens3 sed -i 's/GRUB_CMDLINE_LINUX=""/GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"/g' /etc/default/grub grub-mkconfig -o /boot/grub/grub.cfg # Redirect video output to serial console echo '[Unit]' > "$ttyS0_service" echo -e "Description=Serial Console Service\n" >> "$ttyS0_service" echo '[Service]' >> "$ttyS0_service" echo 'ExecStart=/sbin/getty -L 115200 ttyS0 vt102' >> "$ttyS0_service" echo -e "Restart=always\n" >> "$ttyS0_service" echo '[Install]' >> "$ttyS0_service" echo 'WantedBy=multi-user.target' >> "$ttyS0_service" systemctl daemon-reload systemctl enable ttyS0 systemctl start ttyS0 # FRRouting is not in repository we need to install it locally wget https://github.com/FRRouting/frr/releases/download/frr-"$frr_ver"/"$frr_file" apt-get -y install ./"$frr_file" if [ "$?" != 0 ]; then echo "FRrouting can't be installed, exiting" && exit 1 else rm "$frr_file" fi # Enable routing daemons, create empty config files, change owner & group to frr, set privilleges for daemon in zebra bgpd ospfd ospf6d ripd ripngd isisd eigrpd; do sed -i "s/$daemon=no/$daemon=yes/g" /etc/frr/daemons touch /etc/frr/"$daemon".conf chown frr:frr /etc/frr/"$daemon".conf chmod 640 /etc/frr/"$daemon".conf done # Add user to groups frr & frrvty sudo usermod -a -G frr "$user" sudo usermod -a -G frrvty "$user" # Restart the FRrouting service service frr restart if [ "$?" == 0 ]; then echo "FRRouting service restarted successfully" else echo "Can't restart FRRouting service" && exit 1 fi # Reduce timeout for network interfaces to 15sec during boot mkdir -p /etc/systemd/system/networking.service.d echo "[Service]" > /etc/systemd/system/networking.service.d/reduce-timeout.conf echo "TimeoutStartSec=15" >> /etc/systemd/system/networking.service.d/reduce-timeout.conf exit 0