From 4f6ab64fed719987f83330afd4cb294d777a56c2 Mon Sep 17 00:00:00 2001 From: hellt Date: Thu, 26 Jun 2025 11:19:10 +0200 Subject: [PATCH 1/3] added ifwait script --- Dockerfile | 1 + entrypoint.sh | 4 ++++ if-wait.sh | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+) create mode 100644 if-wait.sh diff --git a/Dockerfile b/Dockerfile index 6bf876d..47262f8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -82,6 +82,7 @@ COPY gotty-service /usr/local/bin/gotty-service RUN chmod +x /usr/local/bin/gotty-service COPY entrypoint.sh /docker/entrypoint.sh +COPY if-wait.sh /docker/if-wait.sh # Start nginx in foreground (pass CMD to docker entrypoint.sh): CMD ["/usr/sbin/nginx", "-g", "daemon off;"] diff --git a/entrypoint.sh b/entrypoint.sh index 9e2b618..7d007c9 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,3 +1,7 @@ +#!/bin/sh + +sh /docker/if-wait.sh + ifup -a ######### # create directories for ssh host keys diff --git a/if-wait.sh b/if-wait.sh new file mode 100644 index 0000000..0d54f68 --- /dev/null +++ b/if-wait.sh @@ -0,0 +1,52 @@ +#!/bin/sh + +# Validate CLAB_INTFS environment variable +REQUIRED_INTFS_NUM=${CLAB_INTFS:-0} +if ! echo "$REQUIRED_INTFS_NUM" | grep -qE '^[0-9]+$' || [ "$REQUIRED_INTFS_NUM" -eq 0 ]; then + echo "Warning: CLAB_INTFS not set or invalid, skipping interface wait" + REQUIRED_INTFS_NUM=0 +fi + +SLEEP=0 +TIMEOUT=300 # 5 minute timeout +WAIT_TIME=0 + +int_calc() { + if [ ! -d "/sys/class/net/" ]; then + echo "Error: /sys/class/net/ not accessible" + AVAIL_INTFS_NUM=0 + return 1 + fi + + # More comprehensive interface pattern including common container interfaces + AVAIL_INTFS_NUM=$(ls -1 /sys/class/net/ 2>/dev/null | grep -cE '^(eth[1-9]|et[0-9]|ens|eno|enp|e[1-9]|net[0-9])' || echo 0) + return 0 +} + +# Only wait for interfaces if CLAB_INTFS is set +if [ "$REQUIRED_INTFS_NUM" -gt 0 ]; then + echo "Waiting for $REQUIRED_INTFS_NUM interfaces to be connected (timeout: ${TIMEOUT}s)" + + while [ "$WAIT_TIME" -lt "$TIMEOUT" ]; do + if ! int_calc; then + echo "Failed to check interfaces, continuing..." + break + fi + + if [ "$AVAIL_INTFS_NUM" -ge "$REQUIRED_INTFS_NUM" ]; then + echo "Found $AVAIL_INTFS_NUM interfaces (required: $REQUIRED_INTFS_NUM)" + break + fi + + echo "Connected $AVAIL_INTFS_NUM interfaces out of $REQUIRED_INTFS_NUM (waited ${WAIT_TIME}s)" + sleep 1 + WAIT_TIME=$((WAIT_TIME + 1)) + done + + if [ "$WAIT_TIME" -ge "$TIMEOUT" ]; then + echo "Warning: Timeout reached, proceeding with $AVAIL_INTFS_NUM interfaces" + fi +fi + +echo "Sleeping $SLEEP seconds before boot" +sleep $SLEEP \ No newline at end of file From e06eb8638d3a3739f59b562398a8166c70d39129 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Thu, 26 Jun 2025 13:16:36 +0200 Subject: [PATCH 2/3] sleep if more than 0 --- if-wait.sh | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/if-wait.sh b/if-wait.sh index 0d54f68..6bbfb39 100644 --- a/if-wait.sh +++ b/if-wait.sh @@ -48,5 +48,7 @@ if [ "$REQUIRED_INTFS_NUM" -gt 0 ]; then fi fi -echo "Sleeping $SLEEP seconds before boot" -sleep $SLEEP \ No newline at end of file +if [ "$SLEEP" -ne 0 ]; then + echo "Sleeping $SLEEP seconds before boot" + sleep $SLEEP +fi \ No newline at end of file From 63ed75c0d05ede0ce13fa6805da9f8f0d6fd42d4 Mon Sep 17 00:00:00 2001 From: Roman Dodin Date: Thu, 26 Jun 2025 14:12:58 +0200 Subject: [PATCH 3/3] remove unnecessary echo --- if-wait.sh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/if-wait.sh b/if-wait.sh index 6bbfb39..77bede5 100644 --- a/if-wait.sh +++ b/if-wait.sh @@ -19,7 +19,7 @@ int_calc() { fi # More comprehensive interface pattern including common container interfaces - AVAIL_INTFS_NUM=$(ls -1 /sys/class/net/ 2>/dev/null | grep -cE '^(eth[1-9]|et[0-9]|ens|eno|enp|e[1-9]|net[0-9])' || echo 0) + AVAIL_INTFS_NUM=$(ls -1 /sys/class/net/ 2>/dev/null | grep -cE '^(eth[1-9]|et[0-9]|ens|eno|enp|e[1-9]|net[0-9])') return 0 } @@ -51,4 +51,4 @@ fi if [ "$SLEEP" -ne 0 ]; then echo "Sleeping $SLEEP seconds before boot" sleep $SLEEP -fi \ No newline at end of file +fi