feat: Detect default interface

* feat: Detect default interface
This commit is contained in:
Kroese 2023-12-28 20:25:04 +01:00 committed by GitHub
parent 1208c53ebb
commit 527bded1b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 8 deletions

View File

@ -1,12 +1,12 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -Eeuo pipefail set -Eeuo pipefail
: ${VM_NET_DEV:='eth0'}
[ -f "/run/qemu.end" ] && echo "QEMU is shutting down.." && exit 1 [ -f "/run/qemu.end" ] && echo "QEMU is shutting down.." && exit 1
[ ! -f "/run/qemu.pid" ] && echo "QEMU is not running yet.." && exit 0 [ ! -f "/run/qemu.pid" ] && echo "QEMU is not running yet.." && exit 0
file="/run/dsm.url" file="/run/dsm.url"
address="/run/qemu.ip"
[ ! -f "$file" ] && echo "DSM has not enabled networking yet.." && exit 1 [ ! -f "$file" ] && echo "DSM has not enabled networking yet.." && exit 1
location=$(cat "$file") 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" echo "Failed to reach DSM at port $port"
else else
echo "Failed to reach DSM at http://$location" 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 fi
echo "You might need to whitelist IP $ip in the DSM firewall." && exit 1 echo "You might need to whitelist IP $ip in the DSM firewall." && exit 1

View File

@ -6,8 +6,8 @@ set -Eeuo pipefail
: ${DHCP:='N'} : ${DHCP:='N'}
: ${MAC:='02:11:32:AA:BB:CC'} : ${MAC:='02:11:32:AA:BB:CC'}
: ${VM_NET_DEV:=''}
: ${VM_NET_TAP:='dsm'} : ${VM_NET_TAP:='dsm'}
: ${VM_NET_DEV:='eth0'}
: ${VM_NET_MAC:="$MAC"} : ${VM_NET_MAC:="$MAC"}
: ${VM_NET_HOST:='VirtualDSM'} : ${VM_NET_HOST:='VirtualDSM'}
@ -204,14 +204,21 @@ fi
update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null update-alternatives --set iptables /usr/sbin/iptables-legacy > /dev/null
update-alternatives --set ip6tables /usr/sbin/ip6tables-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 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 error "$ADD_ERR -e \"VM_NET_DEV=NAME\" to specify another interface name." && exit 27
fi fi
VM_NET_MAC="${VM_NET_MAC//-/:}" VM_NET_MAC="${VM_NET_MAC//-/:}"
GATEWAY=$(ip r | grep default | awk '{print $3}') 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/) 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 if [[ "$DEBUG" == [Yy1]* ]]; then
info "Container IP is $IP with gateway $GATEWAY" && echo info "Container IP is $IP with gateway $GATEWAY" && echo

View File

@ -2,12 +2,12 @@
set -Eeuo pipefail set -Eeuo pipefail
: ${DHCP:='N'} : ${DHCP:='N'}
: ${VM_NET_DEV:='eth0'}
info () { printf "%b%s%b" "\E[1;34m \E[1;36m" "$1" "\E[0m\n" >&2; } 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; } error () { printf "%b%s%b" "\E[1;31m " "ERROR: $1" "\E[0m\n" >&2; }
file="/run/dsm.url" file="/run/dsm.url"
address="/run/qemu.ip"
shutdown="/run/qemu.end" shutdown="/run/qemu.end"
url="http://127.0.0.1:2210/read?command=10" url="http://127.0.0.1:2210/read?command=10"
@ -69,7 +69,7 @@ if [[ "$location" != "20.20"* ]]; then
else else
ip=$(ip address show dev "$VM_NET_DEV" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/) ip="$(cat "$address")"
port="${location##*:}" port="${location##*:}"
if [[ "$ip" == "172."* ]]; then if [[ "$ip" == "172."* ]]; then

View File

@ -38,6 +38,7 @@ STORAGE="/storage"
# Cleanup files # Cleanup files
rm -f /run/dsm.url rm -f /run/dsm.url
rm -f /run/qemu.ip
rm -f /run/qemu.log rm -f /run/qemu.log
rm -f /run/qemu.pid rm -f /run/qemu.pid
rm -f /run/qemu.end rm -f /run/qemu.end
@ -120,7 +121,7 @@ setCountry() {
return 0 return 0
} }
addPackage () { addPackage() {
local pkg=$1 local pkg=$1
local desc=$2 local desc=$2