Skip to content

st.sh: pick free ports instead of counting QEMU instances - #325

Open
daniel-rossier wants to merge 1 commit into
mainfrom
fix/st-free-ports
Open

st.sh: pick free ports instead of counting QEMU instances#325
daniel-rossier wants to merge 1 commit into
mainfrom
fix/st-free-ports

Conversation

@daniel-rossier

Copy link
Copy Markdown
Contributor

Running two emulators at once — two trees side by side, or a leftover QEMU from
an earlier run — kills the second one at startup:

qemu-system-arm: -netdev user,id=n1,hostfwd=tcp::2222-:22:
Could not set up host forwarding rule 'tcp::2222-:22'

st.sh already offset the gdb port per instance, counting running
qemu-system processes, but the ssh forward stayed hardcoded at 2222. And that
counter cannot work under dbuild.sh: each command runs in its own container,
hence its own PID namespace, so a second container sees none of the first one's
processes and hands out the ports it already holds — docker run --network host puts both on the same host stack.

This replaces the counter with a probe for the ports: free_port() walks up
from a base until a TCP connect to 127.0.0.1 fails, which notices every
listener on the host whichever namespace opened it. Both the gdb stub and the
ssh forward go through it, and the chosen port is printed, so attaching a
debugger or an scp knows where to aim. The instance counter stays where it is
still correct: the guest MAC addresses.

Validated with two emulators side by side, one on the host and one inside a
container: 1234/2222 and 1235/2223, both booting to the SO3 prompt.

Starting a second emulator killed it on the spot:

    qemu-system-arm: -netdev user,id=n1,hostfwd=tcp::2222-:22:
    Could not set up host forwarding rule 'tcp::2222-:22'

The gdb port was already offset per instance, derived from a count of running
qemu-system processes, but the ssh forward stayed hardcoded at 2222. That
counter cannot work here anyway: dbuild.sh runs every command in its own
container, hence its own PID namespace, so a second container sees none of the
first one's processes and hands out the very ports it already holds — while
`docker run --network host` puts both on the same host stack.

Probe instead. free_port() walks up from a base until a TCP connect to
127.0.0.1 fails, which notices every listener on the host whichever namespace
opened it. The gdb stub and the ssh forward both go through it, and the chosen
port is printed, so attaching a debugger or an scp knows where to aim.

Validated with two emulators side by side, one on the host and one in a
container: 1234/2222 and 1235/2223, both booting to the SO3 prompt.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant