#188 qcow2: Pick random port in range
Closed 5 years ago by walters. Opened 5 years ago by walters.
walters/standard-test-roles bind-random  into  master

qcow2: Pick random port in range
Colin Walters • 5 years ago  
@@ -8,6 +8,7 @@ 

  import shlex

  import signal

  import multiprocessing

+ import random

  import socket

  import subprocess

  import sys
@@ -105,7 +106,8 @@ 

  

  

  def start_qemu(image, cloudinit, log, portrange=(2222, 5555)):

-     for port in range(*portrange):

+     for tries in range(10):

+         port = random.randint(*portrange)

          sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

          sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

          try:

I'm working changing rpm-ostree to spawn VMs in parallel, and
currently this races because we release the socket, then start
qemu.

I have a plan for a reliable fix, but even with that let's use a random port
since it makes collissions a lot less likely.

On the reliable fix; what I was thinking is something like:

env TEST_INVENTORY_QCOW2_LOCK=/tmp/test-rpm-ostree.XXXXXX/qcow2-lock then inside the code we use flock() around the launching of qemu. Basically callers can provide a file to use as a lock.

The much better fix of course is to be able to pass the socket FD down to qemu, but would require patching it and then version detection, etc.

Hi, could you please explain what do you mean under: rpm-ostree to spawn VMs in parallel ?
rpm-ostree is AtomicHost package manager, as I know. It has nothing with spawning VMs.

Just noticed that I filed a different implementation for the same problem

@walters don't you see conflicts with vnc? See #199

Passing the socket FD would be great, an alternative would be to restart qemu until a free port is found.

Yeah...but I made this against an older branch.

Pull-Request has been closed by walters

5 years ago