From 527bded1b20b94a688d840123de3235a0087f5ba Mon Sep 17 00:00:00 2001 From: Kroese Date: Thu, 28 Dec 2023 20:25:04 +0100 Subject: [PATCH] feat: Detect default interface * feat: Detect default interface --- src/check.sh | 6 +++--- src/network.sh | 11 +++++++++-- src/print.sh | 4 ++-- src/reset.sh | 3 ++- 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/check.sh b/src/check.sh index f49f785..d61f998 100644 --- a/src/check.sh +++ b/src/check.sh @@ -1,12 +1,12 @@ #!/usr/bin/env bash set -Eeuo pipefail -: ${VM_NET_DEV:='eth0'} - [ -f "/run/qemu.end" ] && echo "QEMU is shutting down.." && exit 1 [ ! -f "/run/qemu.pid" ] && echo "QEMU is not running yet.." && exit 0 file="/run/dsm.url" +address="/run/qemu.ip" + [ ! -f "$file" ] && echo "DSM has not enabled networking yet.." && exit 1 location=$(cat "$file") @@ -19,7 +19,7 @@ if ! curl -m 20 -ILfSs "http://$location/" > /dev/null; then echo "Failed to reach DSM at port $port" else echo "Failed to reach DSM at http://$location" - ip=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) + ip="$(cat "$address")" fi echo "You might need to whitelist IP $ip in the DSM firewall." && exit 1 diff --git a/src/network.sh b/src/network.sh index e294d3b..5107975 100644 --- a/src/network.sh +++ b/src/network.sh @@ -6,8 +6,8 @@ set -Eeuo pipefail : ${DHCP:='N'} : ${MAC:='02:11:32:AA:BB:CC'} +: ${VM_NET_DEV:=''} : ${VM_NET_TAP:='dsm'} -: ${VM_NET_DEV:='eth0'} : ${VM_NET_MAC:="$MAC"} : ${VM_NET_HOST:='VirtualDSM'} @@ -204,14 +204,21 @@ fi update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null update-alternatives --set ip6tables /usr/sbin/ip6tables-legacy > /dev/null +if [ -z "$VM_NET_DEV" ]; then + # Automaticly detect the default network interface + VM_NET_DEV=$(awk '$2 == 00000000 { print $1 }' /proc/net/route) + [ -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 "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 VM_NET_MAC="${VM_NET_MAC//-/:}" GATEWAY=$(ip r | grep default | awk '{print $3}') IP=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) +echo "$IP" > /run/qemu.ip if [[ "$DEBUG" == [Yy1]* ]]; then info "Container IP is $IP with gateway $GATEWAY" && echo diff --git a/src/print.sh b/src/print.sh index 463ba34..dbfa250 100644 --- a/src/print.sh +++ b/src/print.sh @@ -2,12 +2,12 @@ set -Eeuo pipefail : ${DHCP:='N'} -: ${VM_NET_DEV:='eth0'} info () { printf "%b%s%b" "\E[1;34m❯ \E[1;36m" "$1" "\E[0m\n" >&2; } error () { printf "%b%s%b" "\E[1;31m❯ " "ERROR: $1" "\E[0m\n" >&2; } file="/run/dsm.url" +address="/run/qemu.ip" shutdown="/run/qemu.end" url="http://127.0.0.1:2210/read?command=10" @@ -69,7 +69,7 @@ if [[ "$location" != "20.20"* ]]; then else - ip=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) + ip="$(cat "$address")" port="${location##*:}" if [[ "$ip" == "172."* ]]; then diff --git a/src/reset.sh b/src/reset.sh index 6539058..53666ba 100644 --- a/src/reset.sh +++ b/src/reset.sh @@ -38,6 +38,7 @@ STORAGE="/storage" # Cleanup files rm -f /run/dsm.url +rm -f /run/qemu.ip rm -f /run/qemu.log rm -f /run/qemu.pid rm -f /run/qemu.end @@ -120,7 +121,7 @@ setCountry() { return 0 } -addPackage () { +addPackage() { local pkg=$1 local desc=$2