virtual-dsm/run/network.sh

164 lines
5.3 KiB
Bash
Raw Normal View History

2023-04-20 03:17:25 +02:00
#!/usr/bin/env bash
2023-04-02 17:55:25 +02:00
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-20 00:16:13 +02:00
configureDHCP() {
2023-04-19 21:50:35 +02:00
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-20 04:19:33 +02:00
DHCP_IP=$( dhclient -v ${VM_NET_TAP} 2>&1 | grep ^bound | cut -d' ' -f3 )
if [[ "${DHCP_IP}" == [0-9.]* ]]; then
echo "Info: Retrieved IP ${DHCP_IP} via DHCP"
else
echo "ERROR: Cannot retrieve IP from DHCP using MAC ${VM_NET_MAC}" && exit 16
fi
2023-04-19 21:50:35 +02:00
ip a flush ${VM_NET_TAP}
2023-04-19 22:34:38 +02:00
2023-04-20 04:19:33 +02:00
TAP_PATH="/dev/tap$(</sys/class/net/${VM_NET_TAP}/ifindex)"
2023-04-19 23:07:38 +02:00
2023-04-20 04:07:38 +02:00
# create dev file (there is no udev in container: need to be done manually)
2023-04-20 04:41:15 +02:00
IFS=: read MAJOR MINOR < <(cat /sys/devices/virtual/net/${VM_NET_TAP}/tap*/dev)
2023-04-20 04:19:33 +02:00
if [[ "x${MAJOR}" != "x" ]]; then
echo "Info: Please make sure that the following docker setting is used: --device-cgroup-rule='c ${MAJOR}:* rwm'"
else
echo "Info: Macvtap creation issue: Cannot find: /sys/class/net/${VM_NET_TAP}/" && exit 18
fi
2023-04-19 22:34:38 +02:00
2023-04-20 04:19:33 +02:00
[[ ! -e ${TAP_PATH} ]] && [[ -e /dev0/${TAP_PATH##*/} ]] && ln -s /dev0/${TAP_PATH##*/} ${TAP_PATH}
2023-04-19 22:34:38 +02:00
2023-04-20 04:19:33 +02:00
if [[ ! -e ${TAP_PATH} ]]; then
mknod ${TAP_PATH} c $MAJOR $MINOR && : || ("ERROR: Cannot mknod: ${TAP_PATH}" && exit 20)
2023-04-19 21:50:35 +02:00
fi
2023-04-20 04:48:00 +02:00
if ! exec 30>>$TAP_PATH; then
echo "ERROR: Please add the following docker variable: --device-cgroup-rule='c ${MAJOR}:* rwm'" && exit 21
fi
if ! exec 40>>/dev/vhost-net; then
echo "ERROR: Cannot find vhost! && exit 22
fi
2023-04-20 04:07:38 +02:00
NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30"
2023-04-19 21:50:35 +02:00
}
2023-04-20 00:16:13 +02:00
configureNAT () {
2023-04-19 21:50:35 +02:00
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
2023-04-20 01:29:37 +02:00
# 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
2023-04-02 21:37:31 +02:00
#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:16:13 +02:00
NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0"
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-20 04:07:38 +02:00
#echo && ifconfig
#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:44:54 +02:00
#if [[ "$GATEWAY" == "172."* ]]; then
2023-04-20 00:16:13 +02:00
# Configuration for static IP
2023-04-20 00:42:51 +02:00
#configureNAT
2023-04-20 00:44:54 +02:00
#else
2023-04-20 00:16:13 +02:00
# Configuration for DHCP IP
2023-04-20 04:07:38 +02:00
configureDHCP
2023-04-20 00:44:54 +02:00
#fi
2023-04-02 17:55:25 +02:00
2023-04-20 00:16:13 +02:00
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"