Merge branch 'LLT-6054_add_support_for_bridged_networking' into 'master'

Add support for bridged networking

Closes LLT-6054

See merge request low-level-hacks/third-party-build/dockur_windows!1
This commit is contained in:
Gytis Stoškevičius 2025-03-26 14:04:33 +00:00
commit 505ab90bc0

View file

@ -6,7 +6,7 @@ set -Eeuo pipefail
: "${MAC:=""}"
: "${MAC_2:=""}"
: "${DHCP:="N"}"
: "${NETWORK:="Y"}"
: "${NETWORK:="bridge"}"
: "${USER_PORTS:=""}"
: "${HOST_PORTS:=""}"
: "${ADAPTER:="virtio-net-pci"}"
@ -20,8 +20,8 @@ set -Eeuo pipefail
: "${VM_NET_MAC_2:="$MAC_2"}"
: "${VM_NET_HOST:="QEMU"}"
: "${VM_NET_HOST_2:="QEMU_2"}"
: "${VM_NET_IP:="20.20.20.21"}"
: "${VM_NET_IP_2:="20.20.21.21"}"
: "${VM_NET_IP:="192.168.0.101"}"
: "${VM_NET_IP_2:="192.168.1.101"}"
: "${DNSMASQ_OPTS:=""}"
: "${DNSMASQ:="/usr/sbin/dnsmasq"}"
@ -277,7 +277,157 @@ configureNAT() {
configureDNS || return 1
return 0/
}
configureBridge() {
local tuntap="TUN device is missing. $ADD_ERR --device /dev/net/tun"
local tables="The 'ip_tables' kernel module is not loaded. Try this command: sudo modprobe ip_tables iptable_nat"
# Create the necessary file structure for /dev/net/tun
if [ ! -c /dev/net/tun ]; then
[ ! -d /dev/net ] && mkdir -m 755 /dev/net
if mknod /dev/net/tun c 10 200; then
chmod 666 /dev/net/tun
fi
fi
if [ ! -c /dev/net/tun ]; then
error "$tuntap" && return 1
fi
# Check port forwarding flag
if [[ $(</proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
{
sysctl -w net.ipv4.ip_forward=1 >/dev/null
rc=$?
} || :
if ((rc != 0)) || [[ $(</proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
error "IP forwarding is disabled. $ADD_ERR --sysctl net.ipv4.ip_forward=1" && return 1
fi
fi
# Create a bridge with a static IP for the VM guest
{
ip link add dev dockerbridge type bridge
rc=$?
} || :
if ((rc != 0)); then
error "Failed to create bridge. $ADD_ERR --cap-add NET_ADMIN" && return 1
fi
{
ip link add dev dockerbridge_2 type bridge
rc=$?
} || :
if ((rc != 0)); then
error "Failed to create bridge. $ADD_ERR --cap-add NET_ADMIN" && return 1
fi
# We need freshly created bridge to have IP address of the container
# For this reason we need to migrate IP from eth0 to dockerbridge.
for addr in $(ip --json addr show dev $VM_NET_DEV | jq -c '.[0].addr_info[] | select(.family == "inet")'); do
cidr_addr=$(echo $addr | jq -r '[ .local, .prefixlen|tostring] | join("/")');
if ! ip addr add dev dockerbridge $cidr_addr; then
error "Failed to add address for dockerbridge interface"
exit 30
fi
done
if ! ip addr flush dev $VM_NET_DEV; then
error "Failed to clear $VM_NET_DEV interface addresses"
exit 30
fi
while ! ip link set dockerbridge up; do
info "Waiting for IP address to become available..."
sleep 2
done
# We need freshly created bridge to have IP address of the container
# For this reason we need to migrate IP from eth0 to dockerbridge.
for addr in $(ip --json addr show dev $VM_NET_DEV_2 | jq -c '.[0].addr_info[] | select(.family == "inet")'); do
cidr_addr=$(echo $addr | jq -r '[ .local, .prefixlen|tostring] | join("/")');
if ! ip addr add dev dockerbridge_2 $cidr_addr; then
error "Failed to add address for dockerbridge_2 interface"
exit 30
fi
done
if ! ip addr flush dev $VM_NET_DEV_2; then
error "Failed to clear $VM_NET_DEV_2 interface addresses"
exit 30
fi
while ! ip link set dockerbridge_2 up; do
info "Waiting for IP address to become available..."
sleep 2
done
# QEMU Works with taps, set tap to the bridge created
if ! ip tuntap add dev "$VM_NET_TAP" mode tap; then
error "$tuntap" && return 1
fi
while ! ip link set "$VM_NET_TAP" up promisc on; do
info "Waiting for TAP to become available..."
sleep 2
done
if ! ip link set dev "$VM_NET_TAP" master dockerbridge; then
error "Failed to set IP link!" && return 1
fi
if ! ip link set dev "$VM_NET_DEV" master dockerbridge; then
error "Failed to attach docker interface to bridge"
fi
if ! ip tuntap add dev "$VM_NET_TAP_2" mode tap; then
error "$tuntap" && return 1
fi
while ! ip link set "$VM_NET_TAP_2" up promisc on; do
info "Waiting for TAP to become available..."
sleep 2
done
if ! ip link set dev "$VM_NET_TAP_2" master dockerbridge_2; then
error "Failed to set IP link!" && return 1
fi
# add initial default route as well
if ! ip route add default dev dockerbridge via ${VM_NET_IP%.*}.1; then
error "Failed to setup default route" && return 10
fi
NET_OPTS="-netdev tap,id=hostnet0,ifname=$VM_NET_TAP"
if [ -c /dev/vhost-net ]; then
{
exec 40>>/dev/vhost-net
rc=$?
} 2>/dev/null || :
((rc == 0)) && NET_OPTS+=",vhost=on,vhostfd=40"
fi
NET_OPTS+=",script=no,downscript=no"
NET_OPTS+=" -netdev tap,id=hostnet1,ifname=$VM_NET_TAP_2"
if [ -c /dev/vhost-net ]; then
{
exec 41>>/dev/vhost-net
rc=$?
} 2>/dev/null || :
((rc == 0)) && NET_OPTS+=",vhost=on,vhostfd=41"
fi
NET_OPTS+=",script=no,downscript=no"
return 0
}
closeNetwork() {
@ -434,13 +584,18 @@ if [[ "$IP" != "172."* ]] && [[ "$IP" != "10.8"* ]] && [[ "$IP" != "10.9"* ]]; t
checkOS
fi
if [[ "${NETWORK,,}" != "user"* ]]; then
if [[ "${NETWORK,,}" == "user"* ]]; then
# Configure for tap interface
if ! configureNAT; then
# Configure for usermode networking (slirp)
configureUser || exit 24
NETWORK="user"
warn "falling back to usermode networking! Performance will be bad and port mapping will not work."
elif [[ "${NETWORK,,}" == "bridge"* ]]; then
# CONFIGURE Bridge
html "Configuring bridged network"
if ! configureBridge; then
error "Failed to setup bridge networking"
ip link set "$VM_NET_TAP" down promisc off &>null || true
ip link delete "$VM_NET_TAP" &>null || true
@ -452,15 +607,30 @@ if [[ "${NETWORK,,}" != "user"* ]]; then
ip link set dockerbridge_2 down &>null || true
ip link delete dockerbridge_2 &>null || true
exit 25
fi
else
# Configure for tap interface
if ! configureNAT; then
error "Failed to setup NAT networking"
ip link set "$VM_NET_TAP" down promisc off &>null || true
ip link delete "$VM_NET_TAP" &>null || true
ip link set "$VM_NET_TAP_2" down promisc off &>null || true
ip link delete "$VM_NET_TAP_2" &>null || true
ip link set dockerbridge down &>null || true
ip link delete dockerbridge &>null || true
ip link set dockerbridge_2 down &>null || true
ip link delete dockerbridge_2 &>null || true
exit 25
fi
if [[ "${NETWORK,,}" == "user"* ]]; then
# Configure for usermode networking (slirp)
configureUser || exit 24
fi
NET_OPTS+=" -device $ADAPTER,romfile=,netdev=hostnet0,mac=$VM_NET_MAC,id=net0"