From 5bcabc705fc89319dd48043b75ff0f02efa8614e Mon Sep 17 00:00:00 2001 From: grossmj Date: Sun, 20 Sep 2015 13:19:57 -0600 Subject: [PATCH] Use custom VMnet interfaces without host adapter when uBridge is not used. Fixes #673. --- gns3server/handlers/api/vmware_handler.py | 2 +- gns3server/modules/vmware/__init__.py | 55 +++++++++++++++++++++-- gns3server/modules/vmware/vmware_vm.py | 2 +- utils/vmnet.py | 2 +- 4 files changed, 55 insertions(+), 6 deletions(-) diff --git a/gns3server/handlers/api/vmware_handler.py b/gns3server/handlers/api/vmware_handler.py index 69654457..c76ca1d0 100644 --- a/gns3server/handlers/api/vmware_handler.py +++ b/gns3server/handlers/api/vmware_handler.py @@ -359,7 +359,7 @@ class VMwareHandler: def allocate_vmnet(request, response): vmware_manager = VMware.instance() - vmware_manager.refresh_vmnet_list() + vmware_manager.refresh_vmnet_list(ubridge=False) vmnet = vmware_manager.allocate_vmnet() response.set_status(201) response.json({"vmnet": vmnet}) diff --git a/gns3server/modules/vmware/__init__.py b/gns3server/modules/vmware/__init__.py index 9a1b15e5..2c9e286f 100644 --- a/gns3server/modules/vmware/__init__.py +++ b/gns3server/modules/vmware/__init__.py @@ -197,7 +197,52 @@ class VMware(BaseManager): log.error("Error while looking for the VMware version: {}".format(e)) @staticmethod - def get_vmnet_interfaces(): + def _get_vmnet_interfaces_registry(): + + import winreg + vmnet_interfaces = [] + regkey = r"SOFTWARE\Wow6432Node\VMware, Inc.\VMnetLib\VMnetConfig" + try: + hkey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, regkey) + for index in range(winreg.QueryInfoKey(hkey)[0]): + vmnet = winreg.EnumKey(hkey, index) + hkeyvmnet = winreg.OpenKey(hkey, vmnet) + if winreg.QueryInfoKey(hkeyvmnet)[1]: + # the vmnet has not been configure if the key has no values + vmnet = vmnet.replace("vm", "VM") + if vmnet not in ("VMnet1", "VMnet8"): + vmnet_interfaces.append(vmnet) + winreg.CloseKey(hkeyvmnet) + winreg.CloseKey(hkey) + except OSError as e: + raise VMwareError("Could not read registry key {}: {}".format(regkey, e)) + return vmnet_interfaces + + @staticmethod + def _get_vmnet_interfaces(): + + if sys.platform.startswith("win"): + return VMware._get_vmnet_interfaces_registry() + elif sys.platform.startswith("darwin"): + vmware_networking_file = "/Library/Preferences/VMware Fusion/networking" + else: + # location on Linux + vmware_networking_file = "/etc/vmware/networking" + vmnet_interfaces = [] + try: + with open(vmware_networking_file, "r", encoding="utf-8") as f: + for line in f.read().splitlines(): + match = re.search("VNET_([0-9]+)_VIRTUAL_ADAPTER", line) + if match: + vmnet = "vmnet{}".format(match.group(1)) + if vmnet not in ("vmnet1", "vmnet8"): + vmnet_interfaces.append(vmnet) + except OSError as e: + raise VMwareError("Cannot open {}: {}".format(vmware_networking_file, e)) + return vmnet_interfaces + + @staticmethod + def _get_vmnet_interfaces_ubridge(): vmnet_interfaces = [] for interface in interfaces(): @@ -234,9 +279,13 @@ class VMware(BaseManager): raise VMwareError("No VMnet interface available between vmnet{} and vmnet{}. Go to preferences VMware / Network / Configure to add more interfaces.".format(self._vmnet_start_range, self._vmnet_end_range)) return self._vmnets.pop(0) - def refresh_vmnet_list(self): + def refresh_vmnet_list(self, ubridge=True): - vmnet_interfaces = self.get_vmnet_interfaces() + if ubridge: + # VMnet host adapters must be present when uBridge is used + vmnet_interfaces = self._get_vmnet_interfaces_ubridge() + else: + vmnet_interfaces = self._get_vmnet_interfaces() # remove vmnets already in use for vm in self._vms.values(): diff --git a/gns3server/modules/vmware/vmware_vm.py b/gns3server/modules/vmware/vmware_vm.py index 87a8fef6..a471fabe 100644 --- a/gns3server/modules/vmware/vmware_vm.py +++ b/gns3server/modules/vmware/vmware_vm.py @@ -236,7 +236,7 @@ class VMwareVM(BaseVM): # raise VMwareError("Network adapter {} is already associated with VMnet interface {} which is managed by GNS3, please remove".format(adapter_number, vmnet)) # then configure VMware network adapters - self.manager.refresh_vmnet_list() + self.manager.refresh_vmnet_list(ubridge=self._use_ubridge) for adapter_number in range(0, self._adapters): # add/update the interface diff --git a/utils/vmnet.py b/utils/vmnet.py index b96654cb..7437bec8 100644 --- a/utils/vmnet.py +++ b/utils/vmnet.py @@ -33,7 +33,7 @@ else: if sys.platform.startswith("win"): DEFAULT_RANGE = [1, 19] else: - DEFAULT_RANGE = [10, 100] + DEFAULT_RANGE = [10, 99] def parse_networking_file():