direct-io.hg

changeset 4242:633ed557a95c

bitkeeper revision 1.1159.258.63 (423ef73fgHxWB39GjIH7wq4Q4o6-3Q)

Change to choose a free vnc display based on netstat.

Signed-off-by: Mike Wray <mike.wray@hp.com>
author mjw@wray-m-3.hpl.hp.com
date Mon Mar 21 16:33:03 2005 +0000 (2005-03-21)
parents 3a5d8cd66c0b
children e769a20ff878 2fa2e30bcbde
files tools/python/xen/xm/create.py
line diff
     1.1 --- a/tools/python/xen/xm/create.py	Mon Mar 21 16:07:15 2005 +0000
     1.2 +++ b/tools/python/xen/xm/create.py	Mon Mar 21 16:33:03 2005 +0000
     1.3 @@ -82,11 +82,12 @@ gopts.opt('console_autoconnect', short='
     1.4            fn=set_true, default=0,
     1.5            use="Connect to the console after the domain is created.")
     1.6  
     1.7 -gopts.opt('vnc', val='DISPLAY',
     1.8 -          fn=set_int, default=None,
     1.9 +gopts.var('vnc', val='no|yes',
    1.10 +          fn=set_bool, default=None,
    1.11            use="""Spawn a vncviewer listening for a vnc server in the domain.
    1.12            The address of the vncviewer is passed to the domain on the kernel command
    1.13            line using 'VNC_SERVER=<host>:<port>'. The port used by vnc is 5500 + DISPLAY.
    1.14 +          A display value with a free port is chosen if possible.
    1.15            """)
    1.16  
    1.17  gopts.var('name', val='NAME',
    1.18 @@ -430,18 +431,49 @@ def get_host_addr():
    1.19      addr = socket.gethostbyname(host)
    1.20      return addr
    1.21  
    1.22 +VNC_BASE_PORT = 5500
    1.23 +
    1.24 +def choose_vnc_display():
    1.25 +    """Try to choose a free vnc display.
    1.26 +    """
    1.27 +    def netstat_local_ports():
    1.28 +        """Run netstat to get a list of the local ports in use.
    1.29 +        """
    1.30 +        l = os.popen("netstat -nat").readlines()
    1.31 +        r = []
    1.32 +        # Skip 2 lines of header.
    1.33 +        for x in l[2:]:
    1.34 +            # Local port is field 3.
    1.35 +            y = x.split()[3]
    1.36 +            # Field is addr:port, split off the port.
    1.37 +            y = y.split(':')[1]
    1.38 +            r.append(int(y))
    1.39 +        return r
    1.40 +
    1.41 +    ports = netstat_local_ports()
    1.42 +    for d in range(1, 100):
    1.43 +        port = VNC_BASE_PORT + d
    1.44 +        if port in ports: continue
    1.45 +        return d
    1.46 +    return None
    1.47 +
    1.48  def spawn_vnc(display):
    1.49      os.system("vncviewer -listen %d &" % display)
    1.50 -    return 5500 + display
    1.51 +    return VNC_BASE_PORT + display
    1.52      
    1.53  def preprocess_vnc(opts, vals):
    1.54      """If vnc was specified, spawn a vncviewer in listen mode
    1.55      and pass its address to the domain on the kernel command line.
    1.56      """
    1.57 -    if vals.vnc is None: return
    1.58 -    vnc_host = get_host_addr()
    1.59 -    vnc_port = spawn_vnc(vals.vnc)
    1.60 +    if not vals.vnc: return
    1.61 +    vnc_display = choose_vnc_display()
    1.62 +    if not vnc_display:
    1.63 +        opts.warn("No free vnc display")
    1.64 +        return
    1.65 +    print 'VNC=', vnc_display
    1.66 +    vnc_port = spawn_vnc(vnc_display)
    1.67      if vnc_port > 0:
    1.68 +        vnc_host = get_host_addr()
    1.69          vnc = 'VNC_VIEWER=%s:%d' % (vnc_host, vnc_port)
    1.70          vals.extra = vnc + ' ' + vals.extra
    1.71