WARNING - OLD ARCHIVES

This is an archived copy of the Xen.org mailing list, which we have preserved to ensure that existing links to archives are not broken. The live archive, which contains the latest emails, can be found at http://lists.xen.org/
   
 
 
Xen 
 
Home Products Support Community News
 
   
 

xen-changelog

[Xen-changelog] Change to choose a free vnc display based on netstat.

To: xen-changelog@xxxxxxxxxxxxxxxxxxxxx
Subject: [Xen-changelog] Change to choose a free vnc display based on netstat.
From: BitKeeper Bot <riel@xxxxxxxxxxx>
Date: Mon, 21 Mar 2005 16:33:03 +0000
Delivery-date: Fri, 25 Mar 2005 12:07:28 +0000
Envelope-to: www-data@xxxxxxxxxxxxxxxxxxx
List-archive: <http://sourceforge.net/mailarchive/forum.php?forum=xen-changelog>
List-help: <mailto:xen-changelog-request@lists.sourceforge.net?subject=help>
List-id: <xen-changelog.lists.sourceforge.net>
List-post: <mailto:xen-changelog@lists.sourceforge.net>
List-subscribe: <https://lists.sourceforge.net/lists/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.sourceforge.net?subject=subscribe>
List-unsubscribe: <https://lists.sourceforge.net/lists/listinfo/xen-changelog>, <mailto:xen-changelog-request@lists.sourceforge.net?subject=unsubscribe>
Reply-to: Xen Development List <xen-devel@xxxxxxxxxxxxxxxxxxxxx>
Sender: xen-changelog-admin@xxxxxxxxxxxxxxxxxxxxx
ChangeSet 1.1159.256.63, 2005/03/21 16:33:03+00:00, mjw@xxxxxxxxxxxxxxxxxxx

        Change to choose a free vnc display based on netstat.
        
        Signed-off-by: Mike Wray <mike.wray@xxxxxx>



 create.py |   44 ++++++++++++++++++++++++++++++++++++++------
 1 files changed, 38 insertions(+), 6 deletions(-)


diff -Nru a/tools/python/xen/xm/create.py b/tools/python/xen/xm/create.py
--- a/tools/python/xen/xm/create.py     2005-03-25 07:04:41 -05:00
+++ b/tools/python/xen/xm/create.py     2005-03-25 07:04:41 -05:00
@@ -82,11 +82,12 @@
           fn=set_true, default=0,
           use="Connect to the console after the domain is created.")
 
-gopts.opt('vnc', val='DISPLAY',
-          fn=set_int, default=None,
+gopts.var('vnc', val='no|yes',
+          fn=set_bool, default=None,
           use="""Spawn a vncviewer listening for a vnc server in the domain.
           The address of the vncviewer is passed to the domain on the kernel 
command
           line using 'VNC_SERVER=<host>:<port>'. The port used by vnc is 5500 
+ DISPLAY.
+          A display value with a free port is chosen if possible.
           """)
 
 gopts.var('name', val='NAME',
@@ -430,18 +431,49 @@
     addr = socket.gethostbyname(host)
     return addr
 
+VNC_BASE_PORT = 5500
+
+def choose_vnc_display():
+    """Try to choose a free vnc display.
+    """
+    def netstat_local_ports():
+        """Run netstat to get a list of the local ports in use.
+        """
+        l = os.popen("netstat -nat").readlines()
+        r = []
+        # Skip 2 lines of header.
+        for x in l[2:]:
+            # Local port is field 3.
+            y = x.split()[3]
+            # Field is addr:port, split off the port.
+            y = y.split(':')[1]
+            r.append(int(y))
+        return r
+
+    ports = netstat_local_ports()
+    for d in range(1, 100):
+        port = VNC_BASE_PORT + d
+        if port in ports: continue
+        return d
+    return None
+
 def spawn_vnc(display):
     os.system("vncviewer -listen %d &" % display)
-    return 5500 + display
+    return VNC_BASE_PORT + display
     
 def preprocess_vnc(opts, vals):
     """If vnc was specified, spawn a vncviewer in listen mode
     and pass its address to the domain on the kernel command line.
     """
-    if vals.vnc is None: return
-    vnc_host = get_host_addr()
-    vnc_port = spawn_vnc(vals.vnc)
+    if not vals.vnc: return
+    vnc_display = choose_vnc_display()
+    if not vnc_display:
+        opts.warn("No free vnc display")
+        return
+    print 'VNC=', vnc_display
+    vnc_port = spawn_vnc(vnc_display)
     if vnc_port > 0:
+        vnc_host = get_host_addr()
         vnc = 'VNC_VIEWER=%s:%d' % (vnc_host, vnc_port)
         vals.extra = vnc + ' ' + vals.extra
     


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxxxx
https://lists.sourceforge.net/lists/listinfo/xen-changelog

<Prev in Thread] Current Thread [Next in Thread>