DHCP fixes

DHCP fixes
This commit is contained in:
Kroese 2023-05-03 16:50:27 +02:00 committed by GitHub
commit 3f581a4fb7
6 changed files with 26 additions and 43 deletions

View File

@ -27,7 +27,6 @@ RUN apt-get update && apt-get -y upgrade && \
net-tools \ net-tools \
btrfs-progs \ btrfs-progs \
ca-certificates \ ca-certificates \
isc-dhcp-client \
netcat-openbsd \ netcat-openbsd \
qemu-system-x86 \ qemu-system-x86 \
&& apt-get clean && apt-get clean
@ -66,6 +65,6 @@ LABEL org.opencontainers.image.version=${VERSION_ARG}
LABEL org.opencontainers.image.source=https://github.com/kroese/virtual-dsm/ LABEL org.opencontainers.image.source=https://github.com/kroese/virtual-dsm/
LABEL org.opencontainers.image.url=https://hub.docker.com/r/kroese/virtual-dsm/ LABEL org.opencontainers.image.url=https://hub.docker.com/r/kroese/virtual-dsm/
HEALTHCHECK --interval=30s --retries=1 CMD /run/check.sh HEALTHCHECK --interval=30s --retries=3 CMD /run/check.sh
ENTRYPOINT ["/run/run.sh"] ENTRYPOINT ["/run/run.sh"]

View File

@ -3,16 +3,16 @@ set -u
# Docker Healthcheck # Docker Healthcheck
PORT=5000 : ${DHCP:='N'}
FILE="/var/dsm.ip"
if [ ! -f "${FILE}" ]; then if [ "$DHCP" = "Y" ]; then
echo "IP not assigned" PORT=5555
exit 1 IP="127.0.0.1"
else
PORT=5000
IP="20.20.20.21"
fi fi
IP=$(cat "${FILE}")
if ! curl -m 3 -ILfSs "http://${IP}:${PORT}/" > /dev/null; then if ! curl -m 3 -ILfSs "http://${IP}:${PORT}/" > /dev/null; then
echo "Failed to reach ${IP}:${PORT}" echo "Failed to reach ${IP}:${PORT}"
exit 1 exit 1

View File

@ -5,6 +5,8 @@ set -eu
HTML="Please wait while Virtual DSM is installing...<script>\ HTML="Please wait while Virtual DSM is installing...<script>\
setTimeout(() => { document.location.reload(); }, 9999);</script>" setTimeout(() => { document.location.reload(); }, 9999);</script>"
pkill -f server.sh || true
/run/server.sh 80 "${HTML}" > /dev/null &
/run/server.sh 5000 "${HTML}" > /dev/null & /run/server.sh 5000 "${HTML}" > /dev/null &
# Download the required files from the Synology website # Download the required files from the Synology website

View File

@ -20,7 +20,7 @@ set -eu
configureDHCP() { configureDHCP() {
VM_NET_VLAN="vlan" VM_NET_VLAN="${VM_NET_TAP}_vlan"
GATEWAY=$(ip r | grep default | awk '{print $3}') GATEWAY=$(ip r | grep default | awk '{print $3}')
NETWORK=$(ip -o route | grep "${VM_NET_DEV}" | grep -v default | awk '{print $1}') 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/) IP=$(ip address show dev "${VM_NET_DEV}" | grep inet | awk '/inet / { print $2 }' | cut -f1 -d/)
@ -54,18 +54,6 @@ configureDHCP() {
ip address flush "${VM_NET_DEV}" ip address flush "${VM_NET_DEV}"
ip address flush "${VM_NET_TAP}" ip address flush "${VM_NET_TAP}"
echo "INFO: Acquiring an IP address via DHCP using MAC address ${VM_NET_MAC}..."
DHCP_IP=$(dhclient -v "${VM_NET_TAP}" 2>&1 | grep ^bound | cut -d' ' -f3)
if [[ "${DHCP_IP}" == [0-9.]* ]]; then
echo "INFO: Successfully acquired IP ${DHCP_IP} from the DHCP server..."
else
echo "ERROR: Cannot acquire an IP address from the DHCP server" && exit 17
fi
ip address flush "${VM_NET_TAP}"
{ set +x; } 2>/dev/null { set +x; } 2>/dev/null
TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex) TAP_NR=$(</sys/class/net/"${VM_NET_TAP}"/ifindex)
@ -105,9 +93,6 @@ configureDHCP() {
echo "docker setting to your container: --device=/dev/vhost-net" && exit 22 echo "docker setting to your container: --device=/dev/vhost-net" && exit 22
fi fi
# Store IP for Docker healthcheck
echo "${DHCP_IP}" > "/var/dsm.ip"
NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30" NET_OPTS="-netdev tap,id=hostnet0,vhost=on,vhostfd=40,fd=30"
} }
@ -162,9 +147,6 @@ configureNAT () {
echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:${VM_NET_MAC}" > /var/lib/misc/dnsmasq.leases echo "0 $VM_NET_MAC $VM_NET_IP $VM_NET_HOST 01:${VM_NET_MAC}" > /var/lib/misc/dnsmasq.leases
chmod 644 /var/lib/misc/dnsmasq.leases chmod 644 /var/lib/misc/dnsmasq.leases
# Store IP for Docker healthcheck
echo "${VM_NET_IP}" > "/var/dsm.ip"
NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0" NET_OPTS="-netdev tap,ifname=${VM_NET_TAP},script=no,downscript=no,id=hostnet0"
# Build DNS options from container /etc/resolv.conf # Build DNS options from container /etc/resolv.conf
@ -247,12 +229,20 @@ else
configureDHCP configureDHCP
# Display the received IP on port 5000 # Display the received IP on port 5000
HTML="The location of DSM is http://${DHCP_IP}:5000<script>\ HTML="DSM is using another IP address.<br><br>(Check the logfile to see which one was assigned.)"
setTimeout(function(){ window.location.replace('http://${DHCP_IP}:5000'); }, 2000);</script>"
pkill -f server.sh || true
/run/server.sh 80 "${HTML}" > /dev/null &
/run/server.sh 5000 "${HTML}" > /dev/null & /run/server.sh 5000 "${HTML}" > /dev/null &
fi fi
[ "$DEBUG" = "Y" ] && echo && echo "Finished network setup.." && echo
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0" NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet0,mac=${VM_NET_MAC},id=net0"
if [ "$DHCP" = "Y" ]; then
# Add extra LAN interface for Docker Healthcheck script
NET_OPTS="${NET_OPTS} -netdev user,id=hostnet1,restrict=y,hostfwd=tcp::5555-:5000"
NET_OPTS="${NET_OPTS} -device virtio-net-pci,romfile=,netdev=hostnet1,id=net1"
fi
[ "$DEBUG" = "Y" ] && echo && echo "Finished network setup.." && echo

View File

@ -54,7 +54,7 @@ _graceful_shutdown() {
echo && echo "Please update the VirtualDSM Agent to allow for gracefull shutdowns..." echo && echo "Please update the VirtualDSM Agent to allow for gracefull shutdowns..."
kill -15 "$(cat "${_QEMU_PID}")" kill -15 "$(cat "${_QEMU_PID}")"
pkill -f qemu-system-x86_64 pkill -f qemu-system-x86_64 || true
fi fi
fi fi

View File

@ -1,17 +1,9 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -eu set -eu
trap 'kill 0' EXIT
trap exit SIGINT SIGTERM trap exit SIGINT SIGTERM
# Close any previous instances
script_name="${BASH_SOURCE[0]}"
for pid in $(pidof -x "$script_name"); do
if [ "$pid" != $$ ]; then
kill -15 "$pid" 2> /dev/null
wait "$pid" 2> /dev/null
fi
done
# Serve the page # Serve the page
HTML="<HTML><HEAD><STYLE>body { color: white; background-color: #00BFFF; }</STYLE>\ HTML="<HTML><HEAD><STYLE>body { color: white; background-color: #00BFFF; }</STYLE>\
</HEAD><BODY><BR><BR><H1><CENTER>$2</CENTER></H1></BODY></HTML>" </HEAD><BODY><BR><BR><H1><CENTER>$2</CENTER></H1></BODY></HTML>"
@ -21,5 +13,5 @@ LENGTH="${#HTML}"
RESPONSE="HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\nConnection: close\n\n$HTML\n\n" RESPONSE="HTTP/1.1 200 OK\nContent-Length: ${LENGTH}\nConnection: close\n\n$HTML\n\n"
while true; do while true; do
echo -en "$RESPONSE" | nc -N -lp "${1:-8080}"; echo -en "$RESPONSE" | nc -lp "${1:-5000}" & wait $!
done done