Skip to content

Commit c34e44e

Browse files
committed
WIP: qemu: randomly choose vsock port on the host side
Prevents conflicts between MicroVMs. Somehow doesn't work first try; VM might timeout a few times before it starts working. The problem is most likely between socat and systemd. More debug logging is needed. (Adding it here would be trivial, actually.)
1 parent 3910e65 commit c34e44e

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/runners/qemu.nix

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,18 @@ lib.warnIf (mem == 2048) ''
160160
preStart = ''
161161
${microvmConfig.preStart}
162162
'' + lib.optionalString supportsNotifySocket ''
163+
# Clean up temporary files.
164+
${pkgs.coreutils}/bin/rm -f ./socat.log ./notify_socket.cred
165+
# Default value: for running without systemd.
166+
NOTIFY_VSOCK_PORT=8888
163167
# Start socat to forward systemd notify socket over vsock
164168
if [ -n "''${NOTIFY_SOCKET-}" ]; then
165-
${pkgs.socat}/bin/socat VSOCK-LISTEN:8888,fork UNIX-SENDTO:$NOTIFY_SOCKET &
169+
${pkgs.socat}/bin/socat -d -d VSOCK-LISTEN:-1,fork UNIX-SENDTO:$NOTIFY_SOCKET 2> ./socat.log &
170+
NOTIFY_VSOCK_PORT=$({ ${pkgs.coreutils}/bin/tail -f ./socat.log || true; } | ${pkgs.gawk}/bin/awk '/listening on/ { if (match($0, /port:[0-9]*/)) { print substr($0, RSTART + 5, RLENGTH - 5); exit; } }')
171+
echo "Picked port for notify vsock: $NOTIFY_VSOCK_PORT"
166172
fi
173+
echo -n "vsock-stream:2:$NOTIFY_VSOCK_PORT" > ./notify_socket.cred
174+
167175
'';
168176

169177
command = lib.escapeShellArgs (
@@ -314,7 +322,7 @@ lib.warnIf (mem == 2048) ''
314322
# tried, SMBIOS Type 11 entries simply don't work. It looks like it might
315323
# be broken on QEMU side. Why? I don't know.
316324
"-fw_cfg"
317-
"name=opt/io.systemd.credentials/vmm.notify_socket,string=vsock-stream:2:8888"
325+
"name=opt/io.systemd.credentials/vmm.notify_socket,file=./notify_socket.cred"
318326
]
319327
++
320328
extraArgs

0 commit comments

Comments
 (0)