Files
willlaptop/scripts/kvm-bridge-setup.sh
2025-10-24 11:48:52 -07:00

75 lines
1.9 KiB
Bash
Executable File

#!/bin/bash
# configure-bridge.sh
# This script configures a network bridge interface for KVM on Arch Linux
# using systemd-networkd. It creates configuration files for:
# - the bridge interface (default: br0)
# - the physical interface to be enslaved to the bridge
#
# Usage:
# sudo ./configure-bridge.sh <physical_interface> [bridge_name]
#
# Example:
# sudo ./configure-bridge.sh enp2s0 br0
#
# Reference: Arch Linux Wiki, "Bridge networking" https://wiki.archlinux.org/title/Bridge_networking
set -euo pipefail
if [ "$EUID" -ne 0 ]; then
echo "Please run as root."
exit 1
fi
if [ "$#" -lt 1 ]; then
echo "Usage: $0 <physical_interface> [bridge_name]"
exit 1
fi
PHYS_IF="$1"
BRIDGE_IF="${2:-br0}"
NETWORK_DIR="/etc/systemd/network"
if [ ! -d "$NETWORK_DIR" ]; then
echo "Directory $NETWORK_DIR does not exist. Please ensure systemd-networkd is installed."
exit 1
fi
echo "Configuring bridge interface '${BRIDGE_IF}' for physical interface '${PHYS_IF}'..."
# Create the .netdev file for the bridge
BRIDGE_NETDEV_FILE="${NETWORK_DIR}/25-${BRIDGE_IF}.netdev"
cat > "$BRIDGE_NETDEV_FILE" <<EOF
[NetDev]
Name=${BRIDGE_IF}
Kind=bridge
EOF
echo "Created ${BRIDGE_NETDEV_FILE}"
# Create the .network file for the bridge (using DHCP)
BRIDGE_NETWORK_FILE="${NETWORK_DIR}/30-${BRIDGE_IF}.network"
cat > "$BRIDGE_NETWORK_FILE" <<EOF
[Match]
Name=${BRIDGE_IF}
[Network]
DHCP=yes
EOF
echo "Created ${BRIDGE_NETWORK_FILE}"
# Create the .network file for the physical interface to attach it to the bridge
PHYS_NETWORK_FILE="${NETWORK_DIR}/20-${PHYS_IF}.network"
cat > "$PHYS_NETWORK_FILE" <<EOF
[Match]
Name=${PHYS_IF}
[Network]
Bridge=${BRIDGE_IF}
EOF
echo "Created ${PHYS_NETWORK_FILE}"
echo "Restarting systemd-networkd to apply changes..."
systemctl restart systemd-networkd
echo "Bridge configuration complete. The bridge '${BRIDGE_IF}' should now be up."
echo "You can check the status with: networkctl status ${BRIDGE_IF}"