[PATCH 2/3] netconsole: add support for binding to specific IP

Ahmad Fatoum a.fatoum at barebox.org
Sun May 3 01:30:05 PDT 2026


If scripts/netconsole is already running when QEMU starts up, the host
forwarding rule may break QEMU startup:

  qemu-system-aarch64: -nic user,id=net0,hostfwd=udp:127.0.0.2:6666-:6666,\
  tftp=/src/build/barebox/imx8/testfs:

    Could not set up host forwarding rule 'udp:127.0.0.2:6666-:6666'

This can be worked around by having QEMU bind to a different loopback
address than what's netcat is going to use.

Add a -s option to control this.

Signed-off-by: Ahmad Fatoum <a.fatoum at barebox.org>
---
 scripts/netconsole | 33 ++++++++++++++++++++++++---------
 1 file changed, 24 insertions(+), 9 deletions(-)

diff --git a/scripts/netconsole b/scripts/netconsole
index 7ab81a3a9191..1945de7f9381 100755
--- a/scripts/netconsole
+++ b/scripts/netconsole
@@ -2,9 +2,14 @@
 
 usage() {
 	(
-	echo "Usage: $0 <board IP> [board port]"
+	echo "Usage: $0 [-sh] <board IP> [board port]"
 	echo ""
 	echo "If port is not specified, '6666' will be used"
+	echo ""
+	echo "Options:"
+	echo "  -s SADDR   Bind to SADDR on host side"
+	echo "  -h         Show this help text and exit"
+
 	[ -z "$*" ] && exit 0
 	echo ""
 	echo "ERROR: $*"
@@ -13,16 +18,26 @@ usage() {
 	exit $?
 }
 
-while [ -n "$1" ] ; do
-	case $1 in
-		-h|--help) usage;;
-		--)        break;;
-		-*)        usage "Invalid option $1";;
-		*)         break;;
+if [ "$1" = "--help" ]; then
+	usage
+fi
+
+while getopts ":hs:" opt; do
+	case "$opt" in
+		h) usage ;;
+		s) saddr="$OPTARG" ;;
+		\?) usage "Invalid option -$OPTARG" ;;
+		:)  usage "Option -$OPTARG requires an argument" ;;
 	esac
-	shift
 done
 
+shift $((OPTIND - 1))
+
+# handle end-of-options marker
+if [ "$1" = "--" ]; then
+	shift
+fi
+
 ip=$1
 port=${2:-6666}
 
@@ -50,7 +65,7 @@ echo "NOTE: the interrupt signal (normally ^C) has been remapped to ^T"
 stty -icanon -echo intr ^T
 set -m
 (
-	while ${nc} -u -l -p ${port} < /dev/null ; do
+	while ${nc} ${saddr:+-s "$saddr"} -u -l -p ${port} < /dev/null ; do
 		:
 	done
 ) &
-- 
2.47.3




More information about the barebox mailing list