virtual-dsm/src/network.sh

360 lines
10 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
: "${MAC:=""}"
2024-01-14 16:01:15 +01:00
: "${DHCP:="N"}"
: "${NETWORK:="Y"}"
2024-04-30 00:30:44 +02:00
: "${HOST_PORTS:=""}"
2023-05-07 14:34:16 +02:00
2024-01-14 16:01:15 +01:00
: "${VM_NET_DEV:=""}"
: "${VM_NET_TAP:="dsm"}"
2024-01-14 01:11:58 +01:00
: "${VM_NET_MAC:="$MAC"}"
: "${VM_NET_IP:="20.20.20.21"}"
2024-01-14 16:01:15 +01:00
: "${VM_NET_HOST:="VirtualDSM"}"
2023-04-02 17:55:25 +02:00
2024-01-14 16:01:15 +01:00
: "${DNSMASQ_OPTS:=""}"
: "${DNSMASQ:="/usr/sbin/dnsmasq"}"
: "${DNSMASQ_CONF_DIR:="/etc/dnsmasq.d"}"
2023-04-02 17:55:25 +02:00
2023-12-25 05:03:00 +01:00
ADD_ERR="Please add the following setting to your container:"
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
# Create the necessary file structure for /dev/vhost-net
if [ ! -c /dev/vhost-net ]; then
if mknod /dev/vhost-net c 10 238; then
chmod 660 /dev/vhost-net
fi
fi
2023-04-29 19:40:07 +02:00
# Create a macvtap network for the VM guest
{ ip link add link "$VM_NET_DEV" name "$VM_NET_TAP" address "$VM_NET_MAC" type macvtap mode bridge ; rc=$?; } || :
2023-05-12 12:57:14 +02:00
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2024-04-30 00:30:44 +02:00
error "Cannot create macvtap interface. Please make sure that the network type is 'macvlan' and not 'ipvlan',"
error "that your kernel is recent (>4) and supports it, and that the container has the NET_ADMIN capability set." && exit 16
2023-04-27 19:32:59 +02:00
fi
2023-04-27 19:39:31 +02:00
while ! ip link set "$VM_NET_TAP" up; do
info "Waiting for MAC address $VM_NET_MAC to become available..."
2023-07-04 12:52:49 +02:00
sleep 2
done
2023-04-19 22:34:38 +02:00
local TAP_NR TAP_PATH MAJOR MINOR
TAP_NR=$(</sys/class/net/"$VM_NET_TAP"/ifindex)
2023-04-21 14:06:32 +02:00
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)
IFS=: read -r MAJOR MINOR < <(cat /sys/devices/virtual/net/"$VM_NET_TAP"/tap*/dev)
(( MAJOR < 1)) && error "Cannot find: sys/devices/virtual/net/$VM_NET_TAP" && exit 18
2023-04-19 22:34:38 +02:00
[[ ! -e "$TAP_PATH" ]] && [[ -e "/dev0/${TAP_PATH##*/}" ]] && ln -s "/dev0/${TAP_PATH##*/}" "$TAP_PATH"
2023-04-19 22:34:38 +02:00
if [[ ! -e "$TAP_PATH" ]]; then
{ mknod "$TAP_PATH" c "$MAJOR" "$MINOR" ; rc=$?; } || :
(( rc != 0 )) && error "Cannot mknod: $TAP_PATH ($rc)" && exit 20
2023-04-19 21:50:35 +02:00
fi
2023-05-12 15:42:20 +02:00
{ exec 30>>"$TAP_PATH"; rc=$?; } 2>/dev/null || :
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2023-12-25 05:03:00 +01:00
error "Cannot create TAP interface ($rc). $ADD_ERR --device-cgroup-rule='c *:* rwm'" && exit 21
2023-04-20 04:48:00 +02:00
fi
2023-05-12 15:42:20 +02:00
{ exec 40>>/dev/vhost-net; rc=$?; } 2>/dev/null || :
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2023-12-25 05:03:00 +01:00
error "VHOST can not be found ($rc). $ADD_ERR --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-05-12 12:57:14 +02:00
return 0
2023-04-19 21:50:35 +02:00
}
2023-12-28 18:26:56 +01:00
configureDNS() {
2023-05-13 07:51:53 +02:00
# dnsmasq configuration:
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"
# Create lease file for faster resolve
echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:$VM_NET_MAC" > /var/lib/misc/dnsmasq.leases
2023-05-13 07:51:53 +02:00
chmod 644 /var/lib/misc/dnsmasq.leases
# Set DNS server and gateway
DNSMASQ_OPTS="$DNSMASQ_OPTS --dhcp-option=option:dns-server,${VM_NET_IP%.*}.1 --dhcp-option=option:router,${VM_NET_IP%.*}.1"
2023-05-13 07:51:53 +02:00
# Add DNS entry for container
DNSMASQ_OPTS="$DNSMASQ_OPTS --address=/host.lan/${VM_NET_IP%.*}.1"
DNSMASQ_OPTS=$(echo "$DNSMASQ_OPTS" | sed 's/\t/ /g' | tr -s ' ' | sed 's/^ *//')
[[ "$DEBUG" == [Yy1]* ]] && set -x
2024-01-26 02:19:31 +01:00
if ! $DNSMASQ ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}; then
error "Failed to start dnsmasq, reason: $?" && exit 29
fi
2023-05-13 07:51:53 +02:00
{ set +x; } 2>/dev/null
[[ "$DEBUG" == [Yy1]* ]] && echo
2023-05-13 07:51:53 +02:00
return 0
}
2024-04-30 00:30:44 +02:00
getPorts() {
local list=$1
[ -z "$list" ] && echo "" && return 0
if [[ "$list" != *","* ]]; then
echo " ! --dport $list"
else
echo " -m multiport ! --dports $list"
fi
return 0
}
2023-12-28 18:26:56 +01:00
configureNAT() {
2023-04-19 21:50:35 +02:00
2023-12-28 21:20:38 +01:00
# Create the necessary file structure for /dev/net/tun
if [ ! -c /dev/net/tun ]; then
[ ! -d /dev/net ] && mkdir -m 755 /dev/net
if mknod /dev/net/tun c 10 200; then
chmod 666 /dev/net/tun
fi
fi
if [ ! -c /dev/net/tun ]; then
2024-03-31 04:57:56 +02:00
error "TUN device missing. $ADD_ERR --device /dev/net/tun --cap-add NET_ADMIN" && exit 25
2023-12-28 21:20:38 +01:00
fi
2023-12-28 21:30:28 +01:00
# Check port forwarding flag
if [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
2024-04-30 00:30:44 +02:00
{ sysctl -w net.ipv4.ip_forward=1 > /dev/null; rc=$?; } || :
if (( rc != 0 )) || [[ $(< /proc/sys/net/ipv4/ip_forward) -eq 0 ]]; then
2023-12-28 21:30:28 +01:00
error "IP forwarding is disabled. $ADD_ERR --sysctl net.ipv4.ip_forward=1" && exit 24
fi
fi
2024-04-30 00:30:44 +02:00
local tables="The 'ip_tables' kernel module is not loaded. Try this command: sudo modprobe ip_tables iptable_nat"
local tuntap="The 'tun' kernel module is not available. Try this command: 'sudo modprobe tun' or run the container with 'privileged: true'."
2023-05-11 19:29:56 +02:00
# Create a bridge with a static IP for the VM guest
2023-05-11 19:36:02 +02:00
2023-05-07 13:28:25 +02:00
{ ip link add dev dockerbridge type bridge ; rc=$?; } || :
2023-04-29 19:40:07 +02:00
if (( rc != 0 )); then
2023-12-25 05:03:00 +01:00
error "Failed to create bridge. $ADD_ERR --cap-add NET_ADMIN" && exit 23
2023-04-27 19:20:02 +02:00
fi
ip address add "${VM_NET_IP%.*}.1/24" broadcast "${VM_NET_IP%.*}.255" dev dockerbridge
2023-07-04 12:52:49 +02:00
2023-07-04 18:36:38 +02:00
while ! ip link set dockerbridge up; do
info "Waiting for IP address to become available..."
2023-07-04 12:52:49 +02:00
sleep 2
done
2023-04-21 20:09:24 +02:00
2023-05-10 05:48:46 +02:00
# QEMU Works with taps, set tap to the bridge created
2024-04-30 00:30:44 +02:00
if ! ip tuntap add dev "$VM_NET_TAP" mode tap; then
error "$tuntap" && exit 31
fi
2023-07-04 18:20:03 +02:00
while ! ip link set "$VM_NET_TAP" up promisc on; do
info "Waiting for TAP to become available..."
2023-07-04 18:20:03 +02:00
sleep 2
done
ip link set dev "$VM_NET_TAP" master dockerbridge
2023-04-02 21:37:31 +02:00
2023-05-10 05:48:46 +02:00
# Add internet connection to the VM
2023-12-28 21:20:38 +01:00
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null
2024-04-30 00:30:44 +02:00
exclude=$(getPorts "$HOST_PORTS")
if ! iptables -t nat -A POSTROUTING -o "$VM_NET_DEV" -j MASQUERADE; then
error "$tables" && exit 30
fi
# shellcheck disable=SC2086
iptables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP" -p tcp${exclude} -j DNAT --to "$VM_NET_IP"
iptables -t nat -A PREROUTING -i "$VM_NET_DEV" -d "$IP" -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"
2024-04-30 00:30:44 +02:00
iptables -A POSTROUTING -t mangle -p udp --dport bootpc -j CHECKSUM --checksum-fill > /dev/null 2>&1 || true
2023-04-22 01:40:03 +02:00
fi
2023-04-20 01:29:37 +02:00
NET_OPTS="-netdev tap,ifname=$VM_NET_TAP,script=no,downscript=no,id=hostnet0"
2023-05-13 06:06:32 +02:00
if [ -c /dev/vhost-net ]; then
{ exec 40>>/dev/vhost-net; rc=$?; } 2>/dev/null || :
(( rc == 0 )) && NET_OPTS="$NET_OPTS,vhost=on,vhostfd=40"
fi
2023-05-13 06:06:32 +02:00
2023-05-13 06:13:56 +02:00
configureDNS
return 0
}
2023-12-28 18:26:56 +01:00
closeNetwork() {
2023-07-04 18:20:03 +02:00
if [[ "$DHCP" == [Yy1]* ]]; then
2023-07-04 18:20:03 +02:00
2024-01-20 19:59:44 +01:00
# Shutdown nginx
nginx -s stop 2> /dev/null
fWait "nginx"
fi
[[ "$NETWORK" != [Yy1]* ]] && return 0
exec 30<&- || true
exec 40<&- || true
if [[ "$DHCP" == [Yy1]* ]]; then
ip link set "$VM_NET_TAP" down || true
ip link delete "$VM_NET_TAP" || true
2023-07-04 18:36:38 +02:00
else
2024-01-20 19:59:44 +01:00
local pid="/var/run/dnsmasq.pid"
2024-04-16 15:35:35 +02:00
[ -s "$pid" ] && pKill "$(<"$pid")"
ip link set "$VM_NET_TAP" down promisc off || true
ip link delete "$VM_NET_TAP" || true
2023-07-04 18:36:38 +02:00
ip link set dockerbridge down || true
ip link delete dockerbridge || true
2023-07-04 18:20:03 +02:00
fi
2023-12-28 20:54:33 +01:00
return 0
}
2024-05-11 22:11:11 +02:00
checkOS() {
local name
local os=""
name=$(uname -a)
[[ "${name,,}" == *"darwin"* ]] && os="MacOS"
[[ "${name,,}" == *"microsoft"* ]] && os="Windows"
if [ -n "$os" ]; then
error "You are using Docker Desktop for $os which does not support macvlan, please revert to bridge networking!"
return 1
fi
return 0
}
2023-12-28 20:54:33 +01:00
getInfo() {
if [ -z "$VM_NET_DEV" ]; then
# Give Kubernetes priority over the default interface
[ -d "/sys/class/net/net0" ] && VM_NET_DEV="net0"
[ -d "/sys/class/net/net1" ] && VM_NET_DEV="net1"
[ -d "/sys/class/net/net2" ] && VM_NET_DEV="net2"
[ -d "/sys/class/net/net3" ] && VM_NET_DEV="net3"
2023-12-28 20:54:33 +01:00
# Automaticly detect the default network interface
[ -z "$VM_NET_DEV" ] && VM_NET_DEV=$(awk '$2 == 00000000 { print $1 }' /proc/net/route)
2023-12-28 20:54:33 +01:00
[ -z "$VM_NET_DEV" ] && VM_NET_DEV="eth0"
fi
if [ ! -d "/sys/class/net/$VM_NET_DEV" ]; then
error "Network interface '$VM_NET_DEV' does not exist inside the container!"
error "$ADD_ERR -e \"VM_NET_DEV=NAME\" to specify another interface name." && exit 27
fi
if [ -z "$VM_NET_MAC" ]; then
2024-02-28 08:40:00 +01:00
local file="$STORAGE/dsm.mac"
2024-04-16 15:35:35 +02:00
[ -s "$file" ] && VM_NET_MAC=$(<"$file")
2024-04-13 23:33:17 +02:00
if [ -z "$VM_NET_MAC" ]; then
2024-02-28 08:40:00 +01:00
# Generate MAC address based on Docker container ID in hostname
VM_NET_MAC=$(echo "$HOST" | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/02:11:32:\3:\4:\5/')
echo "${VM_NET_MAC^^}" > "$file"
fi
fi
VM_NET_MAC="${VM_NET_MAC^^}"
VM_NET_MAC="${VM_NET_MAC//-/:}"
2023-12-28 20:54:33 +01:00
if [[ ${#VM_NET_MAC} == 12 ]]; then
m="$VM_NET_MAC"
VM_NET_MAC="${m:0:2}:${m:2:2}:${m:4:2}:${m:6:2}:${m:8:2}:${m:10:2}"
fi
if [[ ${#VM_NET_MAC} != 17 ]]; then
error "Invalid MAC address: '$VM_NET_MAC', should be 12 or 17 digits long!" && exit 28
2023-12-28 20:54:33 +01:00
fi
GATEWAY=$(ip route list dev "$VM_NET_DEV" | awk ' /^default/ {print $3}')
2023-12-28 20:54:33 +01:00
IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
2024-01-13 20:25:57 +01:00
echo "$IP" > /run/shm/qemu.ip
2023-12-28 20:54:33 +01:00
return 0
2023-07-04 18:20:03 +02:00
}
2023-04-02 21:37:31 +02:00
# ######################################
# Configure Network
# ######################################
if [[ "$NETWORK" != [Yy1]* ]]; then
NET_OPTS=""
return 0
2023-05-06 14:32:10 +02:00
fi
2023-12-28 20:54:33 +01:00
getInfo
2024-01-20 19:59:44 +01:00
html "Initializing network..."
2023-04-19 18:51:24 +02:00
if [[ "$DEBUG" == [Yy1]* ]]; then
info "Host: $HOST IP: $IP Gateway: $GATEWAY Interface: $VM_NET_DEV MAC: $VM_NET_MAC"
2024-01-31 03:54:40 +01:00
[ -f /etc/resolv.conf ] && grep '^nameserver*' /etc/resolv.conf
echo
fi
2024-04-30 00:30:44 +02:00
if [[ "$IP" == "172.17."* ]]; then
warn "your container IP starts with 172.17.* which will cause conflicts when you install the Container Manager package inside DSM!"
fi
if [[ "$DHCP" == [Yy1]* ]]; then
2023-04-20 06:25:46 +02:00
2024-05-21 13:48:05 +02:00
! checkOS && [[ "$DEBUG" != [Yy1]* ]] && exit 19
2024-05-11 22:11:11 +02:00
2024-04-30 00:30:44 +02:00
if [[ "$GATEWAY" == "172."* ]]; then
warn "your gateway IP starts with 172.* which is often a sign that you are not on a macvlan network (required for DHCP)!"
2023-04-20 06:02:38 +02:00
fi
2023-04-20 06:25:46 +02:00
# Configuration for DHCP IP
configureDHCP
2024-01-21 18:35:55 +01:00
MSG="Booting DSM instance..."
html "$MSG"
2023-04-02 17:55:25 +02:00
2023-05-06 13:38:21 +02:00
else
2024-05-21 13:48:05 +02:00
if [[ "$GATEWAY" != "172."* ]] && [[ "$GATEWAY" != "10.8"* ]] && [[ "$GATEWAY" != "10.9"* ]]; then
! checkOS && [[ "$DEBUG" != [Yy1]* ]] && exit 19
2024-05-11 22:11:11 +02:00
fi
2024-01-20 19:59:44 +01:00
# Shutdown nginx
nginx -s stop 2> /dev/null
fWait "nginx"
2023-05-06 13:38:21 +02:00
# Configuration for static IP
configureNAT
2023-05-03 06:22:27 +02:00
fi
2023-05-03 06:12:17 +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
2023-05-03 18:06:23 +02:00
return 0