# - bof - #



<<'comment'
bash <(curl -s https://www.u150.isdevelopment.us/ISs_Arch.Ins/50-VManager-NetworkConfiguration_Bridge_Arch.sh)
comment



# -  - #
# - Network configuration files - #
iP_=114;

# -  - #
# - Overwrite checkComputer - #
 overwriteCc_="No";
#overwriteCc_="Yes";



# -  - #
# - Header_ - #
function Header_(){
  clear;
  echo "";
  figlet -f small "Network" | lolcat;
  figlet -f small "Configuration" | lolcat;
}



# -  - #
# - Greetings_ - #
function Greetings_(){
  Header_;
  echo -e "\n\nby Djalmar Enrique Rayo (2025) - GNU General Public License (GPL)";
  echo "------------------- Light version: $lightVersion_ -------------------";
  echo "                   Virtualization: $sVM_";
  echo -e "\n  Warning: This script is going to install an static IP address.";
  echo -e "       New IP address:  192.168.1.$iP_";
  echo -e "         Network name:  $nName_\n";

cat <<"EOF"
  bash <(curl -s https://www.u150.isdevelopment.us/ISs_Arch.Ins/50-VManager-NetworkConfiguration_Bridge_Arch.sh)
EOF

  echo -e "\n\n";

  if [[ "$sVM_" != "Yes" ]]; then
    echo "      1. Statis IP installation & Bridge for Virtual machines";
    echo "      2. Statis IP installation only";
    echo "      3. Exit";
  fi;

  echo -e "\n\n";

  while true; do
    if [[ "$sVM_" != "Yes" ]]; then
      read -p "Plese select the installation you want (1,2,3): " yn
      case $yn in
          [12]* )
            echo -e "\n\n\nInstallation starting...\n\n"
            break;;
          [3]* )
            exit;
            break;;
          * ) echo "Please answer (1)One, (2)Two or (3)Three to Exit.";;
      esac
    else
      read -p "Do you want to start the installation now? (Yy/Nn): " yn
      case $yn in
          [Yy]* )
            echo -e "\n\n\nInstallation starting...\n\n"
            break;;
          [Nn]* )
            exit;
            break;;
          * ) echo "Please answer (y)es or (n)o.";;
      esac
    fi;
done
}



# :A
# -  - #
# - Arch - #

# -  - #
# - createSystemdNetworkdBridge_ - #
function createSystemdNetworkdBridge_(){
  local bNetdev_=/etc/systemd/network/bridge-0.netdev \
        bNetwork_=/etc/systemd/network/bridge-0.network \
        uNetwork_=/etc/systemd/network/uplink.network \
        tmp_=~/tmp.txt;

  echo -e "\n\nCreating network bridge Arch...";

<<'comment'
  # -  - #
  # - Documentation - #
  https://major.io/p/creating-a-bridge-for-virtual-machines-using-systemd-networkd/
comment



  # -  - #
  # - bridge-0.netdev - #
  rm -rf $tmp_;
cat <<EOT >> $tmp_;
[NetDev]
Name=bridge-0
Kind=bridge
EOT
  sudo mv $tmp_ $bNetdev_;
  sudo chown -R root:root $bNetdev_;



  # -  - #
  # - bridge-0.network - #
  rm -rf $tmp_;
cat <<EOT >> $tmp_;
[Match]
Name=bridge-0

[Network]
Address=192.168.1.$iP_/24
Gateway=192.168.1.1
DNS=8.8.8.8
DNS=8.8.4.4
IPForward=yes
IPMasquerade=yes
EOT
  sudo mv $tmp_ $bNetwork_;
  sudo chown -R root:root $bNetwork_;



  # -  - #
  # - uplink.network - #
  rm -rf $tmp_;
cat <<EOT >> $tmp_;
[Match]
Name=$nName_

[Network]
Bridge=bridge-0
EOT
  sudo mv $tmp_ $uNetwork_;
  sudo chown -R root:root $uNetwork_;



  sudo systemctl enable systemd-networkd.service;
  sudo systemctl disable NetworkManager;


  rm -rf $tmp_;
  echo -e "\nNetwork bridge Arch was created successfully,";
  echo -e "The system will reboot in 15 seconds so changes can reflect...\n\n\n";
  sleep 15;
  sudo reboot now;
}



# -  - #
# - createNetworkManagerBridge_ - #
function createNetworkManagerBridge_(){
  local nmBridge_=/etc/netctl/bridge-0 \
        ipName_=/etc/netctl/$nName_ \
        tmp_=~/tmp.txt;

  echo -e "\n\nCreating network manager bridge...";

<<'comment'
  # -  - #
  # - Documentation - #
  https://computingforgeeks.com/how-to-create-and-use-network-bridge-on-arch-linux-and-manjaro/
  
comment



  # -  - #
  # - bridge-0 - #
  rm -rf $tmp_;
cat <<EOT >> $tmp_;
Description="Bridge connection"
Interface=bridge-0
Connection=bridge
BindsToInterfaces=($nName_)
IP=static
Address='192.168.1.$iP_/24'
Gateway='192.168.1.1'
DNS='192.168.1.1'
MACAddressOf=$nName_

## Ignore (R)STP and immediately activate the bridge
SkipForwardingDelay=yes
EOT
  sudo mv $tmp_ $nmBridge_;
  sudo chown -R root:root $nmBridge_;



  # -  - #
  # - IP name - #
  rm -rf $tmp_;
cat <<EOT >> $tmp_;
IP=no
EOT
  sudo mv $tmp_ $ipName_;
  sudo chown -R root:root $ipName_;



  sudo netctl enable bridge-0;
  sudo netctl start bridge-0;



  rm -rf $tmp_;
  echo -e "\nNetwork manager bridge was created successfully,";
  echo -e "The system will reboot in 15 seconds so changes can reflect...\n\n\n";
  sleep 15;
  sudo reboot now;
}



# -  - #
# - createSystemdNetworkdStaticIP_ - #
function createSystemdNetworkdStaticIP_(){
  local bNetwork_=/etc/systemd/network/$nName_.network \
        sResolv_=/etc/resolv.conf \
        tmp_=~/tmp.txt;

  echo -e "\n\nCreating static IP address...";

<<'comment'
  # -  - #
  # - Documentation - #
  https://askubuntu.com/questions/1380719/ubuntu-server-20-04-installing-a-dhcp-without-internet-access
comment



  # -  - #
  # - iP_.network - #
  rm -rf $tmp_;
cat <<EOT >> $tmp_;
[Match]
Name=$nName_

[Network]
Address=192.168.1.$iP_/24
Gateway=192.168.1.1
DHCP=no
IPv6AcceptRA=false
EOT

<<'comment'
DNS=8.8.8.8 8.8.4.4
Domains=ArchBtrfs.local
LinkLocalAddressing=no
NTP=time.google.com
comment

  sudo mv $tmp_ $bNetwork_;
  sudo chown -R root:root $bNetwork_;



<<'comment'
  # -  - #
  # - iP_.network - #
  rm -rf $tmp_;
  sudo rm -rf $sResolv_;
cat <<EOT >> $tmp_;
nameserver 8.8.8.8 8.8.4.4
EOT
  sudo mv $tmp_ $sResolv_;
  sudo chown -R root:root $sResolv_;
comment



  sudo systemctl enable systemd-networkd;
  sudo systemctl disable NetworkManager;

  rm -rf $tmp_;
  echo -e "\nStatic IP address was created successfully,";
  echo -e "The system will reboot in 15 seconds so changes can reflect...\n\n\n";
  sleep 15;
  sudo reboot now;
}



# -  - #
# - createNetworkManagerStaticIP_ - #
function createNetworkManagerStaticIP_(){
  local sConn_='' \
        tmp_=~/tmp.txt;

  echo -e "\n\nCreating static IP address...\n";

<<'comment'
  # -  - #
  # - Documentation - #
  https://askubuntu.com/questions/1380719/ubuntu-server-20-04-installing-a-dhcp-without-internet-access
comment



  # -  - #
  # - iP_.network - #
  sConn_=$(nmcli -f name,device connection show | grep $nName_ | awk '{$NF=""; print $0}' | sed 's/[[:space:]]*$//');
  nmcli connection show | grep $nName_;

  sudo nmcli connection modify \
            "$sConn_" \
            ipv4.method manual \
            ipv4.address 192.168.1.$iP_/24 \
            ipv4.gateway 192.168.1.1 \
            ipv4.dns "8.8.8.8,8.8.4.4"

  sudo nmcli connection up "$sConn_";



  echo -e "\nStatic IP address was created successfully,";
  echo -e "The system will reboot in 15 seconds so changes can reflect...\n\n\n";
  sleep 15;
  sudo reboot now;
}



<<'comment'

#natbr0.netdev
[NetDev]
Description=Bridge interface for containers/vms
Name=natbr0
Kind=bridge

#natbr0.network
[Match]
Name=natbr0

[Network]
Description=IP configuration for natbr0
Address=172.16.10.1/16
IPForward=yes
IPMasquerade=yes



sudo systemctl enable systemd-networkd;
sudo systemctl disable NetworkManager;
systemctl status systemd-networkd;



# :B
# -  - #
# - Ubuntu - #


# :B.1
# -  - #
# - HPLaptop (Host computer) - #
# - Original network file - #
sudo vim /etc/netplan/1-network-manager-all.yaml;

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager


# :B.2
# -  - #
# - Modified network file - #
# - Create bridge using original enp2s0f1 network host name - #
# - Assign same IP address as it was in host (addresses: [10.0.0.4/24]) - #

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp2s0f1:
      dhcp4: false
  bridges:
    bridge-enp2s0f1:
      interfaces: [enp2s0f1]
      dhcp4: false
      addresses: [10.0.0.4/24]
      gateway4: 10.0.0.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]



# :B.3
# -  - #
# - List new network bridge - #

ip a;

2: enp2s0f1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master bridge-enp2s0f1 state UP group default qlen 1000
    link/ether 80:ce:62:2a:c1:08 brd ff:ff:ff:ff:ff:ff

5: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:64:b7:ed brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.1/24 brd 192.168.122.255 scope global virbr0
       valid_lft forever preferred_lft forever

6: bridge-enp2s0f1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 2a:8c:de:f4:41:58 brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.4/24 brd 10.0.0.255 scope global noprefixroute bridge-enp2s0f1
       valid_lft forever preferred_lft forever
    inet6 fe80::288c:deff:fef4:4158/64 scope link 
       valid_lft forever preferred_lft forever



# -  - #
# - Assing new bridge to each VirtualManager machine - #

grep -q "^flags.*hypervisor" /proc/cpuinfo && echo "Machine running under VM" 

comment



source <(curl -s https://www.education.isdevelopment.us/Functions/Functions.sh)

lightVersion_=$( lVersion_ );
nName_=$( networkName_ );
sVM_=$( virtualManager_ );
echo $sVM_;



checkComputer_;
Greetings_;
distroVersion_;

case $yn in
  [1]* )
    createNetworkManagerBridge_
    break;;
  [2Yy]* )
    createNetworkManagerStaticIP_;
    break;;
esac



# - eof - #