Create a VM with a nic on a virtual distributed switch

Visto 1.960 veces
Saltar al primer mensaje no leído

Romeo Theriault

no leída,
26 feb 2013, 19:11:0826/2/13
a pysp...@googlegroups.com
Hi, I'm trying to create a new VM on vsphere. Following the script found in this previous post: 


I am able to get the new VM created. I'd now like to specify that I want it's network interface on one of our distributed virtual switches portgroups.

This snippet adds a new nic to the VM and attaches it to one of the specified standard network:

# add a NIC. the network Name must be set as the device name to create the NIC.
# Different network card types are: "VirtualE1000", "VirtualE1000e","VirtualPCNet32", "VirtualVmxnet", "VirtualNmxnet2", "VirtualVmxnet3"
nic_spec = config.new_deviceChange()
if network_name:
    nic_spec.set_element_operation("add")
    nic_ctlr = VI.ns0.VirtualVmxnet3_Def("nic_ctlr").pyclass()
    # Standard switch
    nic_backing = VI.ns0.VirtualEthernetCardNetworkBackingInfo_Def("nic_backing").pyclass()
    nic_backing.set_element_deviceName(network_name)   
    nic_ctlr.set_element_addressType("generated")
    nic_ctlr.set_element_backing(nic_backing)
    nic_ctlr.set_element_key(4)
    nic_spec.set_element_device(nic_ctlr)
    devices.append(nic_spec)


But I'm unable to determine how to add the nic to a DVS. After lots of looking I found these which I believe I need to use to specify the dvswitch. Problem is I don't know how...


    VI.ns0.DistributedVirtualSwitchPortConnection
    nic_backing = VI.ns0.VirtualEthernetCardDistributedVirtualPortBackingInfo_Def

When I try to create a create a Distributed switch managed object with below it shows up empty even though we have dvswitches:

# Get the DVS info
dvss = s._get_managed_objects(MORTypes.VmwareDistributedVirtualSwitch)

# Iterate through all distributed virtual switches looking for the specified dvs
for mor, dvs_name in dvss.items():
    print "DVS: %s (%s)"% (dvs_name, mor)
    if dvs_name == dvswitch_name:
        dvswitch_mor = mor


Any help on how to create a VM with it's nic on a DVS would be very much appreciated.

Thanks,
Romeo

Romeo Theriault

no leída,
1 mar 2013, 20:22:081/3/13
a pysp...@googlegroups.com
If anyone has any idea on this I'd really appreciate any help I could get. Thanks!

Romeo Theriault

no leída,
4 mar 2013, 19:47:124/3/13
a pysp...@googlegroups.com
Ok, after much head scratching and looking at the pysphere sources to better understand the odd vmware api I've finally figured out how to do this. I'm posting how I did this below. There may be a more elegant way but this works for me.

Basically, to create a VM with a nic on a distributed switch you need two pieces of info. The distributed virtual switches uuid and the portgroups key. The script below allows you to just give the portgroups name and it will find the portgroup key and the switches uuid. You then specify those when adding the nic to the VM.

#!/usr/bin/env python

import sys
from pysphere import VIServer, VIProperty, MORTypes
from pysphere.resources import VimService_services as VI
from pysphere.vi_task import VITask

# CONNECTION PARAMTERS
server = "vcr40.mgt.host.com"
user = 'username'
password = 'password'
datacentername = "Dev_Datacenter"
hostname = "esx14.mgt.hawaii.edu"
network_name = "dv_172_16_1_0_24"

# CONNECT TO THE SERVER
s = VIServer()
s.connect(server, user, password)


# GET INITIAL PROPERTIES AND OBJECTS
dcmor = [k for k,v in s.get_datacenters().items() if v==datacentername][0]
dcprops = VIProperty(s, dcmor)

# networkFolder managed object reference
nfmor = dcprops.networkFolder._obj
dvpg_mors = s._retrieve_properties_traversal(property_names=['name','key'],
                                    from_node=nfmor, obj_type='DistributedVirtualPortgroup')

# Get the portgroup managed object.
dvpg_mor = None
for dvpg in dvpg_mors:
    if dvpg_mor:
        break
    for p in dvpg.PropSet:
        if p.Name == "name" and p.Val == network_name:
            dvpg_mor = dvpg
        if dvpg_mor:
            break

if dvpg_mor == None:
    print "Didnt find the dvpg %s, exiting now" % (network_name)
    sys.exit()

# Get the portgroup key
portgroupKey = None
for p in dvpg_mor.PropSet:
    if p.Name == "key":
        portgroupKey = p.Val

# Grab the dvswitch uuid and portgroup properties
dvswitch_mors = s._retrieve_properties_traversal(property_names=['uuid','portgroup'],
                                    from_node=nfmor, obj_type='DistributedVirtualSwitch')


dvswitch_mor = None
# Get the appropriate dvswitches managed object
for dvswitch in dvswitch_mors:
    if dvswitch_mor:
        break
    for p in dvswitch.PropSet:
        if p.Name == "portgroup":
            pg_mors = p.Val.ManagedObjectReference
            for pg_mor in pg_mors:
                if dvswitch_mor:
                    break
                key_mor = s._get_object_properties(pg_mor, property_names=['key'])
                for key in key_mor.PropSet:
                    if key.Val == portgroupKey:
                        dvswitch_mor = dvswitch

# Get the switches uuid
dvswitch_uuid = None
for p in dvswitch_mor.PropSet:
    if p.Name == "uuid":
        dvswitch_uuid = p.Val




Then when you add the nic to the VM you specify it like so:


    nic_spec = config.new_deviceChange()
    if network_name:
        nic_spec.set_element_operation("add")

        if nic_type == "e1000":
            nic_ctlr = VI.ns0.VirtualE1000_Def("nic_ctlr").pyclass()
        elif nic_type == "e1000e":
            nic_ctlr = VI.ns0.VirtualE1000e_Def("nic_ctlr").pyclass()
        elif nic_type == "pcnet32":
            nic_ctlr = VI.ns0.VirtualPCNet32_Def("nic_ctlr").pyclass()
        elif nic_type == "vmxnet":
            nic_ctlr = VI.ns0.VirtualVmxnet_Def("nic_ctlr").pyclass()
        elif nic_type == "vmxnet2":
            nic_ctlr = VI.ns0.VirtualVmxnet2_Def("nic_ctlr").pyclass()
        elif nic_type == "vmxnet3":

            nic_ctlr = VI.ns0.VirtualVmxnet3_Def("nic_ctlr").pyclass()

        if network_type == "standard":

            # Standard switch
            nic_backing = VI.ns0.VirtualEthernetCardNetworkBackingInfo_Def("nic_backing").pyclass()
            nic_backing.set_element_deviceName(network_name)
        elif network_type == "dvs":
            nic_backing_port = VI.ns0.DistributedVirtualSwitchPortConnection_Def("nic_backing_port").pyclass()
            nic_backing_port.set_element_switchUuid(dvswitch_uuid)
            nic_backing_port.set_element_portgroupKey(portgroupKey)

            nic_backing = VI.ns0.VirtualEthernetCardDistributedVirtualPortBackingInfo_Def("nic_backing").pyclass()
            nic_backing.set_element_port(nic_backing_port)


        nic_ctlr.set_element_addressType("generated")
        nic_ctlr.set_element_backing(nic_backing)
        nic_ctlr.set_element_key(4)
        nic_spec.set_element_device(nic_ctlr)
        devices.append(nic_spec)


Hope that helps someone in the future!

Romeo


On Tuesday, February 26, 2013 2:11:08 PM UTC-10, Romeo Theriault wrote:

Seba

no leída,
21 mar 2013, 7:32:5921/3/13
a pysp...@googlegroups.com
Hi Romeo,

   Thanks for sharing this. I'm sorry I couldn't help you. I've been pretty busy at work lately.

Mats Ballen Blomgren

no leída,
12 jun 2013, 9:14:1112/6/13
a pysp...@googlegroups.com
Hi,

Great post.

Can you include the entire script if possible?

/Mats

Romeo Theriault

no leída,
12 jun 2013, 19:31:3212/6/13
a pysp...@googlegroups.com
I've attached the script. I mainly just used the script as a starting point. My main goal was to convert the script into an ansible[1] module[2], which I did.

HTH's,
Romeo



--
Has recibido este mensaje porque estás suscrito a un tema del grupo "pysphere" de Grupos de Google.
Para anular la suscripción a este tema, visita https://groups.google.com/d/topic/pysphere/Yi_8FZWzGvA/unsubscribe?hl=es. Para anular la suscripción a este grupo y todos sus temas, envía un correo electrónico a pysphere+u...@googlegroups.com.

Para obtener más opciones, visita https://groups.google.com/groups/opt_out.
 
 



--
Romeo
vsphere_guest.py
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos