From 04d0635b80eec4496d908221f7943a421c61b077 Mon Sep 17 00:00:00 2001 From: Kroese Date: Sun, 9 Apr 2023 16:02:06 +0200 Subject: [PATCH] Simplify network code --- network.sh | 115 ++++++++++++++--------------------------------------- 1 file changed, 29 insertions(+), 86 deletions(-) diff --git a/network.sh b/network.sh index d120cdc..920f446 100644 --- a/network.sh +++ b/network.sh @@ -9,14 +9,9 @@ set -eu : ${DNSMASQ_CONF_DIR:='/etc/dnsmasq.d'} : ${DNS_SERVERS:=''} -# # (VM_NET_IP: Dont need to change coz all is port forwarded) -# # (VM_NET_DHCP: It use MACVTAP which is not compatible with all configuration) - : ${VM_NET_TAP:=''} : ${VM_NET_IP:='20.20.20.21'} : ${VM_NET_MAC:='00:11:32:2C:A7:85'} -: ${VM_NET_DHCP:='N'} -: ${VM_ENABLE_VIRTIO:='Y'} # ###################################### # Functions @@ -47,12 +42,11 @@ setupLocalDhcp () { CIDR="24" MAC="$1" IP="$2" - #HOSTNAME=$(hostname -s) HOSTNAME="VirtualDSM" # dnsmasq configuration: - log "INFO" "DHCP configured to serve IP $IP/$CIDR via dockerbridge" + 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 + # Create lease file for faster resolve echo "0 $MAC $IP $HOSTNAME 01:${MAC}" > /var/lib/misc/dnsmasq.leases chmod 644 /var/lib/misc/dnsmasq.leases } @@ -62,7 +56,7 @@ setupLocalDhcp () { 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 (Connection VM-Docker) brctl addbr dockerbridge ip addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge ip link set dockerbridge up @@ -87,9 +81,6 @@ configureNatNetworks () { # Configure Network # ###################################### -MAJOR="" -_DhcpIP="" - # Create the necessary file structure for /dev/net/tun if [ ! -c /dev/net/tun ]; then [ ! -d /dev/net ] && mkdir -m 755 /dev/net @@ -106,89 +97,41 @@ update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null log "INFO" "Configuring network ..." #DEFAULT_ROUTE=$(ip route | grep default | awk '{print $3}') -if [[ "x${VM_NET_TAP}" == "x" ]]; then - if [[ "${VM_NET_DHCP}" == [Yy1]* ]]; then - VM_NET_TAP="_VmMacvtap" - log "INFO" "... to retrieve IP via DHCP through Macvtap (${VM_NET_TAP}) and MAC: ${VM_NET_MAC}" +VM_NET_TAP="_VmNatTap" +log "INFO" "... NAT Network (${VM_NET_TAP}) to ${VM_NET_IP}" - ip l add link eth0 name ${VM_NET_TAP} address ${VM_NET_MAC} type macvtap mode bridge || true - ip l set ${VM_NET_TAP} up +configureNatNetworks +KVM_NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0" - ip a flush eth0 - ip a flush ${VM_NET_TAP} +# Build DNS options from container /etc/resolv.conf +nameservers=($(grep '^nameserver' /etc/resolv.conf | sed 's/nameserver //')) +searchdomains=$(grep '^search' /etc/resolv.conf | sed 's/search //' | sed 's/ /,/g') +domainname=$(echo $searchdomains | awk -F"," '{print $1}') - _DhcpIP=$( dhclient -v ${VM_NET_TAP} 2>&1 | grep ^bound | cut -d' ' -f3 ) - [[ "${_DhcpIP}" == [0-9.]* ]] \ - && log "INFO" "... Retrieve IP: ${_DhcpIP} from DHCP with MAC: ${VM_NET_MAC}" \ - || ( log "ERROR" "... Cannot retrieve IP from DHCP with MAC: ${VM_NET_MAC}" && exit 16 ) +for nameserver in "${nameservers[@]}"; do + if [[ $nameserver =~ .*:.* ]]; then + log "INFO" "Skipping IPv6 nameserver: $nameserver" + else + [[ -z $DNS_SERVERS ]] && DNS_SERVERS=$nameserver || DNS_SERVERS="$DNS_SERVERS,$nameserver" + fi +done - ip a flush ${VM_NET_TAP} +DNSMASQ_OPTS="$DNSMASQ_OPTS \ + --dhcp-option=option:dns-server,$DNS_SERVERS \ + --dhcp-option=option:router,${VM_NET_IP%.*}.1 \ + --dhcp-option=option:domain-search,$searchdomains \ + --dhcp-option=option:domain-name,$domainname" - _tmpTapPath="/dev/tap$(