From faed18358f534ed7a743fcd168d649d06da092ab Mon Sep 17 00:00:00 2001 From: Arvind Somy Date: Fri, 17 Jun 2011 14:02:24 -0400 Subject: Fix for Bug lp:796813 - Adding support for distributed virtual portgroups on vmware ESX(i) --- Authors | 1 + nova/virt/vmwareapi/network_utils.py | 29 ++++++++++++++++++++++++----- nova/virt/vmwareapi/vm_util.py | 23 ++++++++++++++++++----- nova/virt/vmwareapi/vmops.py | 7 ++++--- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/Authors b/Authors index 94fcf7f5b..0d79af2c7 100644 --- a/Authors +++ b/Authors @@ -6,6 +6,7 @@ Anne Gentle Anthony Young Antony Messerli Armando Migliaccio +Arvind Somya Bilal Akhtar Brian Lamar Brian Schott diff --git a/nova/virt/vmwareapi/network_utils.py b/nova/virt/vmwareapi/network_utils.py index e77842535..345cd2bd0 100644 --- a/nova/virt/vmwareapi/network_utils.py +++ b/nova/virt/vmwareapi/network_utils.py @@ -45,11 +45,30 @@ def get_network_with_the_name(session, network_name="vmnet0"): networks = session._call_method(vim_util, "get_properties_for_a_collection_of_objects", "Network", vm_networks, ["summary.name"]) - for network in networks: - if network.propSet[0].val == network_name: - return network.obj - return None - + network_obj = {} + for network in vm_networks: + # Get network properties + if network._type == 'DistributedVirtualPortgroup': + props = session._call_method(vim_util, + "get_dynamic_property",network, + "DistributedVirtualPortgroup","config") + # NOTE: This only works on ESXi if the port binding is + # set to ephemeral + if props.name == network_name: + network_obj['type'] = 'DistributedVirtualPortgroup' + network_obj['dvpg'] = props.key + network_obj['dvsw'] = props.distributedVirtualSwitch.value + else: + props = session._call_method(vim_util, + "get_dynamic_property",network, + "Network","summary.name") + if props == network_name: + network_obj['type'] = 'Network' + network_obj['name'] = network_name + if (len(network_obj) > 0): + return network_obj + else: + return None def get_vswitch_for_vlan_interface(session, vlan_interface): """ diff --git a/nova/virt/vmwareapi/vm_util.py b/nova/virt/vmwareapi/vm_util.py index a2fa7600c..d23472469 100644 --- a/nova/virt/vmwareapi/vm_util.py +++ b/nova/virt/vmwareapi/vm_util.py @@ -40,7 +40,7 @@ def split_datastore_path(datastore_path): def get_vm_create_spec(client_factory, instance, data_store_name, network_name="vmnet0", - os_type="otherGuest"): + os_type="otherGuest", network_ref=None): """Builds the VM Create spec.""" config_spec = client_factory.create('ns0:VirtualMachineConfigSpec') config_spec.name = instance.name @@ -89,7 +89,7 @@ def create_controller_spec(client_factory, key): return virtual_device_config -def create_network_spec(client_factory, network_name, mac_address): +def create_network_spec(client_factory, network_name, mac_address,network_ref=None): """ Builds a config spec for the addition of a new network adapter to the VM. @@ -101,9 +101,22 @@ def create_network_spec(client_factory, network_name, mac_address): # Get the recommended card type for the VM based on the guest OS of the VM net_device = client_factory.create('ns0:VirtualPCNet32') - backing = \ - client_factory.create('ns0:VirtualEthernetCardNetworkBackingInfo') - backing.deviceName = network_name + # NOTE: Only works on ESXi if the portgroup binding is set to + # ephemeral. Invalid configuration if set to static and the NIC does + # not come up on boot if set to dynamic. + backing = None + if (network_ref['type'] == "DistributedVirtualPortgroup"): + backing = \ + client_factory.create('ns0:VirtualEthernetCardDistributedVirtualPortBackingInfo') + portgroup = \ + client_factory.create('ns0:DistributedVirtualSwitchPortConnection') + portgroup.switchUuid = network_ref['dvsw'] + portgroup.portgroupKey = network_ref['dvpg'] + backing.port = portgroup + else: + backing = \ + client_factory.create('ns0:VirtualEthernetCardNetworkBackingInfo') + backing.deviceName = network_name connectable_spec = \ client_factory.create('ns0:VirtualDeviceConnectInfo') diff --git a/nova/virt/vmwareapi/vmops.py b/nova/virt/vmwareapi/vmops.py index 5f76b0df5..d23edbdf8 100644 --- a/nova/virt/vmwareapi/vmops.py +++ b/nova/virt/vmwareapi/vmops.py @@ -116,8 +116,9 @@ class VMWareVMOps(object): net_name) if network_ref is None: raise exception.NetworkNotFoundForBridge(bridge=net_name) - - _check_if_network_bridge_exists() + return network_ref + + network_obj = _check_if_network_bridge_exists() def _get_datastore_ref(): """Get the datastore list and choose the first local storage.""" @@ -176,7 +177,7 @@ class VMWareVMOps(object): # Get the create vm config spec config_spec = vm_util.get_vm_create_spec(client_factory, instance, - data_store_name, net_name, os_type) + data_store_name, net_name, os_type, network_obj) def _execute_create_vm(): """Create VM on ESX host.""" -- cgit From c3af5e65508fb325a4a8e350c9ed6d84d87e7cd8 Mon Sep 17 00:00:00 2001 From: Arvind Somy Date: Fri, 17 Jun 2011 15:12:01 -0400 Subject: Fix for lp:796834 - Fixes and enhancements to the ESX(i) guest_tool.py script. --- nova/virt/vmwareapi/vm_util.py | 4 ++-- nova/virt/vmwareapi/vmops.py | 5 ++++- tools/esx/guest_tool.py | 49 ++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 5 deletions(-) diff --git a/nova/virt/vmwareapi/vm_util.py b/nova/virt/vmwareapi/vm_util.py index d23472469..c823ac710 100644 --- a/nova/virt/vmwareapi/vm_util.py +++ b/nova/virt/vmwareapi/vm_util.py @@ -287,9 +287,9 @@ def get_dummy_vm_create_spec(client_factory, name, data_store_name): return config_spec -def get_machine_id_change_spec(client_factory, mac, ip_addr, netmask, gateway): +def get_machine_id_change_spec(client_factory, mac, ip_addr, netmask, gateway, broadcast, dns): """Builds the machine id change config spec.""" - machine_id_str = "%s;%s;%s;%s" % (mac, ip_addr, netmask, gateway) + machine_id_str = "%s;%s;%s;%s;%s;%s" % (mac, ip_addr, netmask, gateway, broadcast, dns) virtual_machine_config_spec = \ client_factory.create('ns0:VirtualMachineConfigSpec') diff --git a/nova/virt/vmwareapi/vmops.py b/nova/virt/vmwareapi/vmops.py index d23edbdf8..9f2a36bf4 100644 --- a/nova/virt/vmwareapi/vmops.py +++ b/nova/virt/vmwareapi/vmops.py @@ -715,11 +715,14 @@ class VMWareVMOps(object): mac_addr = instance.mac_address net_mask = network["netmask"] gateway = network["gateway"] + broadcast = network["broadcast"] + dns = network["dns"] ip_addr = db.instance_get_fixed_address(context.get_admin_context(), instance['id']) machine_id_chanfge_spec = \ vm_util.get_machine_id_change_spec(client_factory, mac_addr, - ip_addr, net_mask, gateway) + ip_addr, net_mask, gateway, + broadcast, dns) LOG.debug(_("Reconfiguring VM instance %(name)s to set the machine id " "with ip - %(ip_addr)s") % ({'name': instance.name, diff --git a/tools/esx/guest_tool.py b/tools/esx/guest_tool.py index 13b0f8d33..e126721d5 100644 --- a/tools/esx/guest_tool.py +++ b/tools/esx/guest_tool.py @@ -21,6 +21,7 @@ On Windows we require pyWin32 installed on Python. """ import array +import gettext import logging import os import platform @@ -30,6 +31,8 @@ import subprocess import sys import time +_ = gettext.gettext + PLATFORM_WIN = 'win32' PLATFORM_LINUX = 'linux2' ARCH_32_BIT = '32bit' @@ -314,6 +317,43 @@ def _set_rhel_networking(network_details=[]): dns_file.close() _execute(['/sbin/service', 'network', 'restart']) +def _set_ubuntu_networking(network_details=[]): + """ Set IPv4 network settings for Ubuntu """ + all_dns_servers = [] + for network_detail in network_details: + mac_address, ip_address, subnet_mask, gateway, broadcast,\ + dns_servers = network_detail + all_dns_servers.extend(dns_servers) + adapter_name, current_ip_address = \ + _get_linux_adapter_name_and_ip_address(mac_address) + + if adapter_name and not ip_address == current_ip_address: + interface_file_name = \ + '/etc/network/interfaces' + # Remove file + os.remove(interface_file_name) + # Touch file + _execute(['touch', interface_file_name]) + interface_file = open(interface_file_name, 'w') + interface_file.write('\nauto %s' % adapter_name) + interface_file.write('\niface %s inet static' % adapter_name) + interface_file.write('\nbroadcast %s' % broadcast) + interface_file.write('\ngateway %s' % gateway) + interface_file.write('\nnetmask %s' % subnet_mask) + interface_file.write('\naddress %s' % ip_address) + interface_file.close() + if all_dns_servers: + dns_file_name = "/etc/resolv.conf" + os.remove(dns_file_name) + _execute(['touch', dns_file_name]) + dns_file = open(dns_file_name, 'w') + dns_file.write("; generated by OpenStack guest tools") + unique_entries = _filter_duplicates(all_dns_servers) + for dns_server in unique_entries: + dns_file.write("\nnameserver %s" % dns_server) + dns_file.close() + print "\nRestarting networking....\n" + _execute(['/etc/init.d/networking', 'restart']) def _linux_set_networking(): """Set IP address for the Linux VM.""" @@ -330,8 +370,13 @@ def _linux_set_networking(): cmd = [vmware_tools_bin, '--cmd', 'machine.id.get'] network_details = _parse_network_details(_execute(cmd, check_exit_code=False)) - # TODO(sateesh): For other distros like ubuntu, suse, debian, BSD, etc. - _set_rhel_networking(network_details) + # TODO(sateesh): For other distros like suse, debian, BSD, etc. + if(platform.dist()[0] == 'Ubuntu') : + _set_ubuntu_networking(network_details) + elif (platform.dist()[0] == 'redhat') : + _set_rhel_networking(network_details) + else: + logging.warn(_("Distro '%s' not supported") % platform.dist()[0]) else: logging.warn(_("VMware Tools is not installed")) -- cgit From 03db1b862f38fa864316530c0a0b22ef74f25c81 Mon Sep 17 00:00:00 2001 From: "Arvind Somya asomya@cisco.com" <> Date: Fri, 24 Jun 2011 12:15:13 -0400 Subject: Fixing PEP8 compliance issues. --- nova/virt/vmwareapi/network_utils.py | 11 ++++++----- nova/virt/vmwareapi/vm_util.py | 5 +++-- nova/virt/vmwareapi/vmops.py | 8 +++++--- tools/esx/guest_tool.py | 6 ++++-- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/nova/virt/vmwareapi/network_utils.py b/nova/virt/vmwareapi/network_utils.py index 345cd2bd0..08e3bf0b1 100644 --- a/nova/virt/vmwareapi/network_utils.py +++ b/nova/virt/vmwareapi/network_utils.py @@ -50,9 +50,9 @@ def get_network_with_the_name(session, network_name="vmnet0"): # Get network properties if network._type == 'DistributedVirtualPortgroup': props = session._call_method(vim_util, - "get_dynamic_property",network, - "DistributedVirtualPortgroup","config") - # NOTE: This only works on ESXi if the port binding is + "get_dynamic_property", network, + "DistributedVirtualPortgroup", "config") + # NOTE(asomya): This only works on ESXi if the port binding is # set to ephemeral if props.name == network_name: network_obj['type'] = 'DistributedVirtualPortgroup' @@ -60,8 +60,8 @@ def get_network_with_the_name(session, network_name="vmnet0"): network_obj['dvsw'] = props.distributedVirtualSwitch.value else: props = session._call_method(vim_util, - "get_dynamic_property",network, - "Network","summary.name") + "get_dynamic_property", network, + "Network", "summary.name") if props == network_name: network_obj['type'] = 'Network' network_obj['name'] = network_name @@ -70,6 +70,7 @@ def get_network_with_the_name(session, network_name="vmnet0"): else: return None + def get_vswitch_for_vlan_interface(session, vlan_interface): """ Gets the vswitch associated with the physical network adapter diff --git a/nova/virt/vmwareapi/vm_util.py b/nova/virt/vmwareapi/vm_util.py index c823ac710..411305081 100644 --- a/nova/virt/vmwareapi/vm_util.py +++ b/nova/virt/vmwareapi/vm_util.py @@ -89,7 +89,8 @@ def create_controller_spec(client_factory, key): return virtual_device_config -def create_network_spec(client_factory, network_name, mac_address,network_ref=None): +def create_network_spec(client_factory, network_name, mac_address, + network_ref=None): """ Builds a config spec for the addition of a new network adapter to the VM. @@ -101,7 +102,7 @@ def create_network_spec(client_factory, network_name, mac_address,network_ref=No # Get the recommended card type for the VM based on the guest OS of the VM net_device = client_factory.create('ns0:VirtualPCNet32') - # NOTE: Only works on ESXi if the portgroup binding is set to + # NOTE: Only works on ESXi if the portgroup binding is set to # ephemeral. Invalid configuration if set to static and the NIC does # not come up on boot if set to dynamic. backing = None diff --git a/nova/virt/vmwareapi/vmops.py b/nova/virt/vmwareapi/vmops.py index 9f2a36bf4..6f8b823c5 100644 --- a/nova/virt/vmwareapi/vmops.py +++ b/nova/virt/vmwareapi/vmops.py @@ -117,7 +117,7 @@ class VMWareVMOps(object): if network_ref is None: raise exception.NetworkNotFoundForBridge(bridge=net_name) return network_ref - + network_obj = _check_if_network_bridge_exists() def _get_datastore_ref(): @@ -176,8 +176,10 @@ class VMWareVMOps(object): vm_folder_mor, res_pool_mor = _get_vmfolder_and_res_pool_mors() # Get the create vm config spec - config_spec = vm_util.get_vm_create_spec(client_factory, instance, - data_store_name, net_name, os_type, network_obj) + config_spec = vm_util.get_vm_create_spec( + client_factory, instance, + data_store_name, net_name, os_type, + network_obj) def _execute_create_vm(): """Create VM on ESX host.""" diff --git a/tools/esx/guest_tool.py b/tools/esx/guest_tool.py index e126721d5..3463be5dd 100644 --- a/tools/esx/guest_tool.py +++ b/tools/esx/guest_tool.py @@ -317,6 +317,7 @@ def _set_rhel_networking(network_details=[]): dns_file.close() _execute(['/sbin/service', 'network', 'restart']) + def _set_ubuntu_networking(network_details=[]): """ Set IPv4 network settings for Ubuntu """ all_dns_servers = [] @@ -355,6 +356,7 @@ def _set_ubuntu_networking(network_details=[]): print "\nRestarting networking....\n" _execute(['/etc/init.d/networking', 'restart']) + def _linux_set_networking(): """Set IP address for the Linux VM.""" vmware_tools_bin = None @@ -371,9 +373,9 @@ def _linux_set_networking(): network_details = _parse_network_details(_execute(cmd, check_exit_code=False)) # TODO(sateesh): For other distros like suse, debian, BSD, etc. - if(platform.dist()[0] == 'Ubuntu') : + if(platform.dist()[0] == 'Ubuntu'): _set_ubuntu_networking(network_details) - elif (platform.dist()[0] == 'redhat') : + elif (platform.dist()[0] == 'redhat'): _set_rhel_networking(network_details) else: logging.warn(_("Distro '%s' not supported") % platform.dist()[0]) -- cgit From 8ec448d2bcf4afce7d0486d9cf6b07e0e30bd6be Mon Sep 17 00:00:00 2001 From: "Arvind Somya asomya@cisco.com" <> Date: Fri, 24 Jun 2011 12:22:14 -0400 Subject: Fixed the default arguments to None instead of an empty list. --- tools/esx/guest_tool.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/esx/guest_tool.py b/tools/esx/guest_tool.py index 3463be5dd..60ecfcd20 100644 --- a/tools/esx/guest_tool.py +++ b/tools/esx/guest_tool.py @@ -278,7 +278,8 @@ def _filter_duplicates(all_entries): return final_list -def _set_rhel_networking(network_details=[]): +def _set_rhel_networking(network_details=None): + network_details = network_details or [] all_dns_servers = [] for network_detail in network_details: mac_address, ip_address, subnet_mask, gateway, broadcast,\ @@ -318,7 +319,8 @@ def _set_rhel_networking(network_details=[]): _execute(['/sbin/service', 'network', 'restart']) -def _set_ubuntu_networking(network_details=[]): +def _set_ubuntu_networking(network_details=None): + network_details = network_details or [] """ Set IPv4 network settings for Ubuntu """ all_dns_servers = [] for network_detail in network_details: -- cgit From 43713a2e45862219c538ede60363053d36bb0f1b Mon Sep 17 00:00:00 2001 From: Arvind Somy Date: Mon, 27 Jun 2011 14:41:07 -0400 Subject: - Modified NOTE in vm_util.py - Changed gettext line to nova default in guest_tool.py --- nova/virt/vmwareapi/vm_util.py | 2 +- tools/esx/guest_tool.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/nova/virt/vmwareapi/vm_util.py b/nova/virt/vmwareapi/vm_util.py index 411305081..b93c1f81b 100644 --- a/nova/virt/vmwareapi/vm_util.py +++ b/nova/virt/vmwareapi/vm_util.py @@ -102,7 +102,7 @@ def create_network_spec(client_factory, network_name, mac_address, # Get the recommended card type for the VM based on the guest OS of the VM net_device = client_factory.create('ns0:VirtualPCNet32') - # NOTE: Only works on ESXi if the portgroup binding is set to + # NOTE(asomya): Only works on ESXi if the portgroup binding is set to # ephemeral. Invalid configuration if set to static and the NIC does # not come up on boot if set to dynamic. backing = None diff --git a/tools/esx/guest_tool.py b/tools/esx/guest_tool.py index 60ecfcd20..97b5302ba 100644 --- a/tools/esx/guest_tool.py +++ b/tools/esx/guest_tool.py @@ -31,7 +31,7 @@ import subprocess import sys import time -_ = gettext.gettext +gettext.install('nova', unicode=1) PLATFORM_WIN = 'win32' PLATFORM_LINUX = 'linux2' -- cgit