Simplify network code

This commit is contained in:
Kroese 2023-04-09 16:15:26 +02:00 committed by GitHub
parent 04d0635b80
commit 06118fca12

View File

@ -1,50 +1,25 @@
#!/usr/bin/env bash
set -eu
: ${INFO:='N'}
: ${DEBUG:='N'}
: ${VM_NET_TAP:=''}
: ${VM_NET_IP:='20.20.20.21'}
: ${VM_NET_MAC:='00:11:32:2C:A7:85'}
: ${DNSMASQ:='/usr/sbin/dnsmasq'}
: ${DNSMASQ_OPTS:=''}
: ${DNSMASQ_CONF_DIR:='/etc/dnsmasq.d'}
: ${DNS_SERVERS:=''}
: ${VM_NET_TAP:=''}
: ${VM_NET_IP:='20.20.20.21'}
: ${VM_NET_MAC:='00:11:32:2C:A7:85'}
# ######################################
# Functions
# ######################################
log () {
case "$1" in
WARNING | ERROR )
echo "$1: ${@:2}"
;;
INFO)
if [[ "$INFO" == [Yy1]* ]]; then
echo "$1: ${@:2}"
fi
;;
DEBUG)
if [[ "$DEBUG" == [Yy1]* ]]; then
echo "$1: ${@:2}"
fi
;;
*)
echo "-- $@"
;;
esac
}
setupLocalDhcp () {
CIDR="24"
MAC="$1"
IP="$2"
HOSTNAME="VirtualDSM"
# dnsmasq configuration:
log "DEBUG" "DHCP configured to serve IP $IP/$CIDR via dockerbridge"
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$IP,$IP --dhcp-host=$MAC,,$IP,$HOSTNAME,infinite --dhcp-option=option:netmask,255.255.255.0"
# Create lease file for faster resolve
echo "0 $MAC $IP $HOSTNAME 01:${MAC}" > /var/lib/misc/dnsmasq.leases
@ -55,8 +30,7 @@ setupLocalDhcp () {
# to connect the host machine to the network
configureNatNetworks () {
#For now we define static MAC because DHCP is very slow if MAC change every VM Boot
#Create bridge with static IP for the VM Guest (Connection VM-Docker)
#Create bridge with static IP for the VM guest
brctl addbr dockerbridge
ip addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
ip link set dockerbridge up
@ -73,7 +47,6 @@ configureNatNetworks () {
#Enable port forwarding flag
[[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]] && sysctl -w net.ipv4.ip_forward=1
#For now we define static MAC because DHCP is very slow if DHCP change every VM Boot
setupLocalDhcp $VM_NET_MAC $VM_NET_IP
}
@ -90,16 +63,10 @@ fi
[ ! -c /dev/net/tun ] && echo "Error: TUN network interface not available..." && exit 85
#log "INFO" "Little dirty trick ..."
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
log "INFO" "Configuring network ..."
#DEFAULT_ROUTE=$(ip route | grep default | awk '{print $3}')
VM_NET_TAP="_VmNatTap"
log "INFO" "... NAT Network (${VM_NET_TAP}) to ${VM_NET_IP}"
configureNatNetworks
KVM_NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0"
@ -110,7 +77,7 @@ domainname=$(echo $searchdomains | awk -F"," '{print $1}')
for nameserver in "${nameservers[@]}"; do
if [[ $nameserver =~ .*:.* ]]; then
log "INFO" "Skipping IPv6 nameserver: $nameserver"
echo "Skipping IPv6 nameserver: $nameserver"
else
[[ -z $DNS_SERVERS ]] && DNS_SERVERS=$nameserver || DNS_SERVERS="$DNS_SERVERS,$nameserver"
fi
@ -124,14 +91,9 @@ DNSMASQ_OPTS="$DNSMASQ_OPTS \
[[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)"
log "INFO" "... Lauching dnsmasq"
log "DEBUG" "dnsmasq options: $DNSMASQ_OPTS"
$DNSMASQ $DNSMASQ_OPTS
KVM_NET_OPTS="${KVM_NET_OPTS} -device virtio-net-pci,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
# Hack for guest VMs complaining about "bad udp checksums in 5 packets"
iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill \
|| ( log "WARNING" "Iptables hack for checksum FAILED" && ethtool -K eth0 tx off || true )
log "INFO" "Done setting up network.."
iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill