KVM: allow binding vnc to a file
authorGuido Trotter <ultrotter@google.com>
Thu, 5 Feb 2009 13:36:43 +0000 (13:36 +0000)
committerGuido Trotter <ultrotter@google.com>
Thu, 5 Feb 2009 13:36:43 +0000 (13:36 +0000)
Before we forced the VNC_BIND_ADDRESS to be an ip. Now we also accept a
path, and bind the instance to it, or to a file in it if it's a
directory.

Reviewed-by: iustinp

lib/hypervisor/hv_kvm.py

index 0df7e42..19d9645 100644 (file)
@@ -264,18 +264,25 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     vnc_bind_address = instance.hvparams[constants.HV_VNC_BIND_ADDRESS]
     if vnc_bind_address:
       kvm_cmd.extend(['-usbdevice', 'tablet'])
-      if instance.network_port > constants.HT_HVM_VNC_BASE_PORT:
-        display = instance.network_port - constants.HT_HVM_VNC_BASE_PORT
-        if vnc_bind_address == '0.0.0.0':
-          vnc_arg = ':%d' % (display)
+      if utils.IsValidIP(vnc_bind_address):
+        if instance.network_port > constants.HT_HVM_VNC_BASE_PORT:
+          display = instance.network_port - constants.HT_HVM_VNC_BASE_PORT
+          if vnc_bind_address == '0.0.0.0':
+            vnc_arg = ':%d' % (display)
+          else:
+            vnc_arg = '%s:%d' % (constants.HV_VNC_BIND_ADDRESS, display)
         else:
-          vnc_arg = '%s:%d' % (constants.HV_VNC_BIND_ADDRESS, display)
-        kvm_cmd.extend(['-vnc', vnc_arg])
+          logging.error("Network port is not a valid VNC display (%d < %d)."
+                        " Not starting VNC" %
+                        (instance.network_port,
+                         constants.HT_HVM_VNC_BASE_PORT))
+          vnc_arg = 'none'
       else:
-        logging.error("Network port is not a valid VNC display (%d < %d)."
-                      " Not starting VNC" %
-                      (instance.network_port, constants.HT_HVM_VNC_BASE_PORT))
-        kvm_cmd.extend(['-vnc', 'none'])
+        if os.path.isdir(vnc_bind_address):
+          vnc_arg = 'unix:%s/%s.vnc' % (vnc_bind_address, instance.name)
+        else:
+          vnc_arg = 'unix:%s' % vnc_bind_address
+      kvm_cmd.extend(['-vnc', vnc_arg])
     else:
       kvm_cmd.extend(['-nographic'])
 
@@ -683,9 +690,11 @@ class KVMHypervisor(hv_base.BaseHypervisor):
     vnc_bind_address = hvparams[constants.HV_VNC_BIND_ADDRESS]
     if vnc_bind_address:
       if not utils.IsValidIP(vnc_bind_address):
-        raise errors.OpPrereqError("given VNC bind address '%s' doesn't look"
-                                   " like a valid IP address" %
-                                   vnc_bind_address)
+        if not os.path.isabs(vnc_bind_address):
+          raise errors.HypervisorError("The VNC bind address must be either"
+                                       " a valid IP address or an absolute"
+                                       " pathname. '%s' given" %
+                                       vnc_bind_address)
 
   def ValidateParameters(self, hvparams):
     """Check the given parameters for validity.