virtual-dsm/run/network.sh

251 lines
8.3 KiB
Bash
Raw Normal View History

2023-04-20 03:17:25 +02:00
#!/usr/bin/env bash
2023-05-03 19:00:10 +02:00
set -Eeuo pipefail
2023-04-02 17:55:25 +02:00
2023-04-23 01:48:08 +02:00
# Docker environment variables
2023-04-18 17:47:41 +02:00
2023-04-21 14:07:50 +02:00
: ${VM_NET_TAP:='dsm'}
2023-04-21 20:09:24 +02:00
: ${VM_NET_DEV:='eth0'}
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-20 06:04:42 +02:00
: ${DHCP:='N'}
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
2023-05-03 15:19:40 +02:00
VM_NET_VLAN="${VM_NET_TAP}_vlan"
2023-04-21 19:39:25 +02:00
GATEWAY=$(ip r | grep default | awk '{print $3}')
2023-04-21 20:09:24 +02:00
NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}')
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
2023-04-21 19:39:25 +02:00
2023-05-01 22:25:38 +02:00
[ "$DEBUG" = "Y" ] && set -x
2023-04-30 00:42:35 +02:00
{ ip link add link "${VM_NET_DEV}" "${VM_NET_VLAN}" type macvlan mode bridge 2> /dev/null ; rc=$?; } || :
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2023-04-29 19:44:33 +02:00
echo -n "ERROR: Capability NET_ADMIN has not been set ($rc/1). Please add the "
2023-04-27 19:29:18 +02:00
echo "following docker setting to your container: --cap-add NET_ADMIN" && exit 15
fi
2023-04-21 20:09:24 +02:00
ip address add "${IP}" dev "${VM_NET_VLAN}"
ip link set dev "${VM_NET_VLAN}" up
2023-04-21 19:39:25 +02:00
2023-04-29 19:12:02 +02:00
ip route flush dev "${VM_NET_DEV}"
2023-04-21 20:09:24 +02:00
ip route flush dev "${VM_NET_VLAN}"
2023-04-21 19:39:25 +02:00
2023-04-29 19:12:02 +02:00
ip route add "${NETWORK}" dev "${VM_NET_VLAN}" metric 0
ip route add default via "${GATEWAY}"
2023-04-21 19:39:25 +02:00
2023-04-30 00:42:35 +02:00
{ ip link add link "${VM_NET_DEV}" name "${VM_NET_TAP}" address "${VM_NET_MAC}" type macvtap mode bridge 2> /dev/null ; rc=$?; } || :
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2023-04-29 19:44:33 +02:00
echo -n "ERROR: Capability NET_ADMIN has not been set ($rc/2). Please add the "
2023-04-27 19:32:59 +02:00
echo "following docker setting to your container: --cap-add NET_ADMIN" && exit 16
fi
2023-04-27 19:39:31 +02:00
2023-04-27 19:20:02 +02:00
ip link set "${VM_NET_TAP}" up
2023-04-19 22:34:38 +02:00
2023-04-29 17:00:49 +02:00
ip address flush "${VM_NET_DEV}"
ip address flush "${VM_NET_TAP}"
2023-04-19 22:11:39 +02:00
2023-05-01 22:54:24 +02:00
{ set +x; } 2>/dev/null
2023-04-21 14:06:32 +02:00
TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
TAP_PATH="/dev/tap${TAP_NR}"
2023-04-19 23:07:38 +02:00
2023-04-21 11:25:24 +02:00
# Create dev file (there is no udev in container: need to be done manually)
2023-04-21 08:51:05 +02:00
IFS=: read -r MAJOR MINOR < <(cat /sys/devices/virtual/net/"${VM_NET_TAP}"/tap*/dev)
2023-04-20 04:41:15 +02:00
2023-04-20 05:09:38 +02:00
if (( MAJOR < 1)); then
echo "ERROR: Cannot find: sys/devices/virtual/net/${VM_NET_TAP}" && exit 18
2023-04-20 04:19:33 +02:00
fi
2023-04-19 22:34:38 +02:00
2023-04-21 07:17:35 +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-21 07:11:36 +02:00
if [[ ! -e "${TAP_PATH}" ]]; then
2023-04-29 19:40:07 +02:00
{ mknod "${TAP_PATH}" c "$MAJOR" "$MINOR" ; rc=$?; } || :
2023-04-29 19:44:33 +02:00
(( rc != 0 )) && echo "ERROR: Cannot mknod: ${TAP_PATH} ($rc)" && exit 20
2023-04-19 21:50:35 +02:00
fi
2023-04-29 19:40:07 +02:00
{ exec 30>>"$TAP_PATH"; rc=$?; } || :
if (( rc != 0 )); then
2023-04-29 19:44:33 +02:00
echo -n "ERROR: Cannot create TAP interface ($rc). Please add the following docker settings to your "
2023-04-27 19:29:18 +02:00
echo "container: --device-cgroup-rule='c ${MAJOR}:* rwm' --device=/dev/vhost-net" && exit 21
2023-04-20 04:48:00 +02:00
fi
# Create /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then
mknod /dev/vhost-net c 10 238
chmod 660 /dev/vhost-net
fi
2023-04-29 19:40:07 +02:00
{ exec 40>>/dev/vhost-net; rc=$?; } || :
if (( rc != 0 )); then
2023-04-29 19:44:33 +02:00
echo -n "ERROR: VHOST can not be found ($rc). Please add the following "
2023-04-27 19:20:02 +02:00
echo "docker setting to your container: --device=/dev/vhost-net" && exit 22
2023-04-20 04:48:00 +02:00
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'
2023-05-01 22:25:38 +02:00
[ "$DEBUG" = "Y" ] && set -x
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-27 19:20:02 +02:00
2023-04-30 00:42:35 +02:00
{ ip link add dev dockerbridge type bridge 2> /dev/null ; rc=$?; } || :
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2023-04-29 19:44:33 +02:00
echo -n "ERROR: Capability NET_ADMIN has not been set ($rc/3). Please add the "
2023-04-27 19:29:18 +02:00
echo "following docker setting to your container: --cap-add NET_ADMIN" && exit 23
2023-04-27 19:20:02 +02:00
fi
2023-04-29 17:00:49 +02:00
ip address add ${VM_NET_IP%.*}.1/24 broadcast ${VM_NET_IP%.*}.255 dev dockerbridge
2023-04-02 21:37:31 +02:00
ip link set dockerbridge up
2023-04-21 20:09:24 +02:00
2023-04-02 21:37:31 +02:00
#QEMU Works with taps, set tap to the bridge created
2023-04-21 14:00:56 +02:00
ip tuntap add dev "${VM_NET_TAP}" mode tap
ip link set "${VM_NET_TAP}" up promisc on
ip link set dev "${VM_NET_TAP}" master dockerbridge
2023-04-02 21:37:31 +02:00
#Add internet connection to the VM
2023-04-21 20:09:24 +02:00
iptables -t nat -A POSTROUTING -o "${VM_NET_DEV}" -j MASQUERADE
iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p tcp -j DNAT --to $VM_NET_IP
iptables -t nat -A PREROUTING -i "${VM_NET_DEV}" -p udp -j DNAT --to $VM_NET_IP
2023-04-02 21:37:31 +02:00
2023-04-22 01:40:03 +02:00
if (( KERNEL > 4 )); then
# 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 || true
fi
2023-04-20 01:29:37 +02:00
2023-05-01 22:54:24 +02:00
{ set +x; } 2>/dev/null
2023-05-01 23:08:33 +02:00
[ "$DEBUG" = "Y" ] && echo
2023-05-01 22:54:24 +02:00
2023-04-29 19:12:02 +02:00
#Check port forwarding flag
if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
2023-04-30 00:42:35 +02:00
{ sysctl -w net.ipv4.ip_forward=1 2> /dev/null ; rc=$?; } || :
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2023-04-29 19:44:33 +02:00
echo -n "ERROR: IP forwarding is disabled ($rc). Please add the following "
2023-04-29 19:12:02 +02:00
echo "docker setting to your container: --sysctl net.ipv4.ip_forward=1" && exit 24
fi
fi
2023-04-02 21:37:31 +02:00
2023-04-14 01:23:09 +02:00
# dnsmasq configuration:
2023-04-22 17:51:09 +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
if [ "$DEBUG" = "Y" ]; then
2023-05-01 23:11:08 +02:00
echo "/etc/resolv.conf:" && echo && cat /etc/resolv.conf && echo
fi
2023-05-03 20:10:03 +02:00
mapfile -t nameservers < <( { grep '^nameserver' /etc/resolv.conf || true; } | sed 's/\t/ /g' | sed 's/nameserver //' | sed 's/ //g')
searchdomains=$( { grep '^search' /etc/resolv.conf || true; } | sed 's/\t/ /g' | sed 's/search //' | sed 's/#.*//' | sed 's/\s*$//g' | sed 's/ /,/g')
2023-04-21 07:19:29 +02:00
domainname=$(echo "$searchdomains" | awk -F"," '{print $1}')
2023-04-19 21:50:35 +02:00
for nameserver in "${nameservers[@]}"; do
nameserver=$(echo "$nameserver" | sed 's/#.*//' )
2023-04-21 07:17:35 +02:00
if ! [[ "$nameserver" =~ .*:.* ]]; then
[[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="$nameserver" || DNS_SERVERS="$DNS_SERVERS,$nameserver"
2023-04-19 21:50:35 +02:00
fi
done
2023-04-21 07:17:35 +02:00
[[ -z "$DNS_SERVERS" ]] && DNS_SERVERS="1.1.1.1"
2023-04-19 21:50:35 +02:00
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:dns-server,$DNS_SERVERS --dhcp-option=option:router,${VM_NET_IP%.*}.1"
2023-04-21 07:48:51 +02:00
if [ -n "$searchdomains" ] && [ "$searchdomains" != "." ]; then
2023-04-19 21:50:35 +02:00
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
2023-04-22 17:59:50 +02:00
DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//')
2023-05-01 22:58:10 +02:00
2023-05-01 23:12:42 +02:00
[ "$DEBUG" = "Y" ] && set -x
2023-04-19 21:50:35 +02:00
2023-04-21 20:31:30 +02:00
$DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
2023-05-01 22:25:38 +02:00
2023-05-01 22:54:24 +02:00
{ set +x; } 2>/dev/null
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
2023-04-27 22:31:29 +02:00
[ ! -c /dev/net/tun ] && echo "ERROR: TUN network interface not available..." && exit 85
2023-04-03 22:08:47 +02:00
2023-04-20 06:07:03 +02:00
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
2023-04-26 22:25:18 +02:00
VM_NET_MAC="${VM_NET_MAC//-/:}"
2023-04-20 06:07:03 +02:00
GATEWAY=$(ip r | grep default | awk '{print $3}')
2023-04-19 19:50:47 +02:00
if [ "$DEBUG" = "Y" ]; then
2023-04-19 22:19:08 +02:00
2023-04-21 20:09:24 +02:00
IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
2023-04-27 18:59:26 +02:00
echo "INFO: Container IP is ${IP} with gateway ${GATEWAY}" && echo
2023-04-22 20:29:05 +02:00
ifconfig
2023-04-22 17:37:38 +02:00
ip route && echo
2023-04-21 13:54:39 +02:00
2023-04-19 18:51:24 +02:00
fi
2023-04-20 06:25:46 +02:00
if [ "$DHCP" != "Y" ]; then
2023-04-20 06:43:16 +02:00
# Configuration for static IP
configureNAT
2023-04-20 06:25:46 +02:00
2023-04-20 06:02:38 +02:00
else
2023-04-20 06:25:46 +02:00
if [[ "$GATEWAY" == "172."* ]]; then
echo -n "ERROR: You cannot enable DHCP while the container is "
echo "in a bridge network, only on a macvlan network!" && exit 86
2023-04-20 06:02:38 +02:00
fi
2023-04-20 06:25:46 +02:00
# Configuration for DHCP IP
configureDHCP
2023-04-20 15:12:48 +02:00
# Display the received IP on port 5000
2023-05-03 16:45:46 +02:00
HTML="DSM is using another IP address.<br><br>(Check the logfile to see which one was assigned.)"
2023-04-21 20:25:08 +02:00
2023-05-03 06:22:27 +02:00
pkill -f server.sh || true
/run/server.sh 80 "${HTML}" > /dev/null &
/run/server.sh 5000 "${HTML}" > /dev/null &
2023-04-02 17:55:25 +02:00
2023-05-03 06:22:27 +02:00
fi
2023-05-03 06:12:17 +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"
2023-05-03 04:06:02 +02:00
if [ "$DHCP" = "Y" ]; then
2023-05-03 16:47:57 +02:00
# Add extra LAN interface for Docker Healthcheck script
2023-05-03 08:17:12 +02:00
NET_OPTS="${NET_OPTS} -netdev user,id=hostnet1,restrict=y,hostfwd=tcp::5555-:5000"
2023-05-03 04:06:02 +02:00
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet1,id=net1"
fi
[ "$DEBUG" = "Y" ] && echo && echo "Finished network setup.." && echo
2023-05-03 18:06:23 +02:00
return 0