virtual-dsm/run/network.sh

151 lines
5.1 KiB
Bash
Raw Normal View History

2023-04-02 17:55:25 +02:00
#!/usr/bin/env bash
set -eu
2023-04-18 17:49:41 +02:00
# Docker environment variabeles
2023-04-18 17:47:41 +02:00
2023-04-15 12:50:48 +02:00
: ${VM_NET_HOST:='VirtualDSM'}
2023-04-14 01:23:09 +02:00
: ${VM_NET_MAC:='02:11:32:AA:BB:CC'}
2023-04-02 17:55:25 +02:00
2023-04-11 05:58:31 +02:00
: ${DNS_SERVERS:=''}
2023-04-02 21:37:31 +02:00
: ${DNSMASQ_OPTS:=''}
2023-04-19 21:50:35 +02:00
: ${DNSMASQ:='/usr/sbin/dnsmasq'}
2023-04-02 21:37:31 +02:00
: ${DNSMASQ_CONF_DIR:='/etc/dnsmasq.d'}
2023-04-02 17:55:25 +02:00
2023-04-02 21:37:31 +02:00
# ######################################
# Functions
# ######################################
2023-04-19 21:50:35 +02:00
configureMacVlan () {
VM_NET_TAP="_VmMacvtap"
2023-04-19 22:11:39 +02:00
echo "Info: Retrieving IP via DHCP using MAC ${VM_NET_MAC}..."
2023-04-19 22:34:38 +02:00
2023-04-19 21:50:35 +02:00
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
2023-04-19 22:34:38 +02:00
2023-04-19 21:50:35 +02:00
ip a flush eth0
ip a flush ${VM_NET_TAP}
2023-04-19 22:11:39 +02:00
2023-04-19 22:34:38 +02:00
_DhcpIP=$( dhclient -v ${VM_NET_TAP} 2>&1 | grep ^bound | cut -d' ' -f3 )
2023-04-19 21:50:35 +02:00
[[ "${_DhcpIP}" == [0-9.]* ]] \
2023-04-19 23:11:23 +02:00
&& echo "Info: Retrieved IP ${_DhcpIP} from DHCP using MAC ${VM_NET_MAC}" \
|| ( echo "ERROR: Cannot retrieve IP from DHCP using MAC ${VM_NET_MAC}" && exit 16 )
2023-04-19 21:50:35 +02:00
ip a flush ${VM_NET_TAP}
2023-04-19 22:34:38 +02:00
2023-04-19 21:50:35 +02:00
_tmpTapPath="/dev/tap$(</sys/class/net/${VM_NET_TAP}/ifindex)"
2023-04-19 23:07:38 +02:00
2023-04-20 00:09:26 +02:00
# get MAJOR MINOR DEVNAME
2023-04-19 21:50:35 +02:00
MAJOR=""
eval "$(</sys/class/net/${VM_NET_TAP}/macvtap/${_tmpTapPath##*/}/uevent) _tmp=0"
2023-04-19 22:34:38 +02:00
2023-04-19 21:50:35 +02:00
[[ "x${MAJOR}" != "x" ]] \
2023-04-19 23:35:59 +02:00
&& echo "Info: Please make sure that the following docker setting is used: --device-cgroup-rule='c ${MAJOR}:* rwm'" \
2023-04-19 23:07:38 +02:00
|| ( echo "Info: Macvtap creation issue: Cannot find: /sys/class/net/${VM_NET_TAP}/" && exit 18 )
2023-04-19 22:34:38 +02:00
2023-04-19 21:50:35 +02:00
[[ ! -e ${_tmpTapPath} ]] && [[ -e /dev0/${_tmpTapPath##*/} ]] && ln -s /dev0/${_tmpTapPath##*/} ${_tmpTapPath}
2023-04-19 22:34:38 +02:00
2023-04-19 21:50:35 +02:00
if [[ ! -e ${_tmpTapPath} ]]; then
2023-04-19 23:46:24 +02:00
mknod ${_tmpTapPath} c $MAJOR $MINOR && : || ("ERROR: Cannot mknod: ${_tmpTapPath}" && exit 20)
2023-04-19 21:50:35 +02:00
fi
2023-04-20 00:09:26 +02:00
NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30 ${NET_OPTS} 30<>${_tmpTapPath} 40<>/dev/vhost-net"
2023-04-19 21:50:35 +02:00
}
configureNatNetwork () {
VM_NET_IP='20.20.20.21'
VM_NET_TAP="_VmNatTap"
2023-04-02 21:37:31 +02:00
2023-04-10 20:14:16 +02:00
#Create bridge with static IP for the VM guest
2023-04-02 21:37:31 +02:00
brctl addbr dockerbridge
ip addr add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
ip link set dockerbridge up
#QEMU Works with taps, set tap to the bridge created
ip tuntap add dev ${VM_NET_TAP} mode tap
ip link set ${VM_NET_TAP} up promisc on
brctl addif dockerbridge ${VM_NET_TAP}
#Add internet connection to the VM
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -i eth0 -p tcp -j DNAT --to $VM_NET_IP
iptables -t nat -A PREROUTING -i eth0 -p udp -j DNAT --to $VM_NET_IP
#Enable port forwarding flag
[[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]] && sysctl -w net.ipv4.ip_forward=1
2023-04-14 01:23:09 +02:00
# dnsmasq configuration:
2023-04-15 12:50:48 +02:00
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-range=$VM_NET_IP,$VM_NET_IP --dhcp-host=$VM_NET_MAC,,$VM_NET_IP,$VM_NET_HOST,infinite --dhcp-option=option:netmask,255.255.255.0"
2023-04-14 01:23:09 +02:00
# Create lease file for faster resolve
2023-04-15 12:50:48 +02:00
echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:${VM_NET_MAC}" > /var/lib/misc/dnsmasq.leases
2023-04-14 01:23:09 +02:00
chmod 644 /var/lib/misc/dnsmasq.leases
2023-04-19 21:50:35 +02:00
2023-04-20 00:09:26 +02:00
NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0 ${NET_OPTS}"
2023-04-19 21:50:35 +02:00
# 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
2023-04-02 17:55:25 +02:00
}
2023-04-02 21:37:31 +02:00
# ######################################
# Configure Network
# ######################################
2023-04-03 22:21:14 +02:00
# Create the necessary file structure for /dev/net/tun
if [ ! -c /dev/net/tun ]; then
[ ! -d /dev/net ] && mkdir -m 755 /dev/net
2023-04-03 22:08:47 +02:00
mknod /dev/net/tun c 10 200
2023-04-03 22:15:24 +02:00
chmod 666 /dev/net/tun
2023-04-03 22:08:47 +02:00
fi
[ ! -c /dev/net/tun ] && echo "Error: TUN network interface not available..." && exit 85
2023-04-19 19:50:47 +02:00
if [ "$DEBUG" = "Y" ]; then
2023-04-19 22:19:08 +02:00
IP=$(ip address show dev eth0 | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
2023-04-19 21:50:35 +02:00
echo && ifconfig
2023-04-19 22:10:26 +02:00
echo && ip route && echo
2023-04-19 22:58:17 +02:00
echo "Container IP: ${IP}" && echo
2023-04-19 22:19:08 +02:00
2023-04-19 18:51:24 +02:00
fi
2023-04-02 21:37:31 +02:00
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
2023-04-19 22:58:17 +02:00
GATEWAY=$(ip r | grep default | awk '{print $3}')
2023-04-20 00:09:26 +02:00
NET_OPTS="-device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
2023-04-19 22:58:17 +02:00
if [[ "$GATEWAY" == "172."* ]]; then
# Configuration for bridge network
configureNatNetwork
else
# Configuration for macvlan network
configureMacVlan
fi
2023-04-02 17:55:25 +02:00
2023-04-02 21:37:31 +02:00
# Hack for guest VMs complaining about "bad udp checksums in 5 packets"
2023-04-10 20:14:16 +02:00
iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill