#!/usr/bin/env bash set -eu # Docker environment variabeles : ${VM_NET_HOST:='VirtualDSM'} : ${VM_NET_MAC:='02:11:32:AA:BB:CC'} : ${DNS_SERVERS:=''} : ${DNSMASQ_OPTS:=''} : ${DNSMASQ:='/usr/sbin/dnsmasq'} : ${DNSMASQ_CONF_DIR:='/etc/dnsmasq.d'} # ###################################### # Functions # ###################################### configureMacVlan () { VM_NET_TAP="_VmMacvtap" echo "... to retrieve IP via DHCP through Macvtap (${VM_NET_TAP}) and MAC: ${VM_NET_MAC}" 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 ip a flush eth0 ip a flush ${VM_NET_TAP} _DhcpIP=$( dhclient -v ${VM_NET_TAP} 2>&1 | grep ^bound | cut -d' ' -f3 ) [[ "${_DhcpIP}" == [0-9.]* ]] \ && echo "... Retrieve IP: ${_DhcpIP} from DHCP with MAC: ${VM_NET_MAC}" \ || ( echo "... Cannot retrieve IP from DHCP with MAC: ${VM_NET_MAC}" && exit 16 ) ip a flush ${VM_NET_TAP} _tmpTapPath="/dev/tap$( /var/lib/misc/dnsmasq.leases chmod 644 /var/lib/misc/dnsmasq.leases NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0" # 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}') for nameserver in "${nameservers[@]}"; do if ! [[ $nameserver =~ .*:.* ]]; then [[ -z $DNS_SERVERS ]] && DNS_SERVERS=$nameserver || DNS_SERVERS="$DNS_SERVERS,$nameserver" fi done [[ -z $DNS_SERVERS ]] && DNS_SERVERS="1.1.1.1" DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:dns-server,$DNS_SERVERS --dhcp-option=option:router,${VM_NET_IP%.*}.1" if [ -n "$searchdomains" -a "$searchdomains" != "." ]; then DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-search,$searchdomains --dhcp-option=option:domain-name,$domainname" else [[ -z $(hostname -d) ]] || DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:domain-name,$(hostname -d)" fi [ "$DEBUG" = "Y" ] && echo && echo "$DNSMASQ $DNSMASQ_OPTS" $DNSMASQ $DNSMASQ_OPTS NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0" } # ###################################### # Configure Network # ###################################### # Create the necessary file structure for /dev/net/tun if [ ! -c /dev/net/tun ]; then [ ! -d /dev/net ] && mkdir -m 755 /dev/net mknod /dev/net/tun c 10 200 chmod 666 /dev/net/tun fi [ ! -c /dev/net/tun ] && echo "Error: TUN network interface not available..." && exit 85 if [ "$DEBUG" = "Y" ]; then echo && ifconfig echo && ip route fi update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null #configureNatNetwork configureMacVlan # 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