Using Onyx, I got the following script to create a vmkernel port for vMotion. I modified it to use it in a workflow. The very last line refers to "vmk2" which is the vmkernel port created by vCenter and captured by Onyx. It would be created a few lines above in the script. My question is, how do I determine which vmkernel port was created (i.e. which vmk???)? So instead of hardcoding in vmk2, I can use a variable string with the actual vmkernel port created earlier in the script.
---------------------Modified Onyx Script -------------------------------------------
// ------- Create New vSwitch and vMotion Portgroup -------
var config = new VcHostNetworkConfig();
config.vswitch = System.getModule("com.vmware.onyx").array(VcHostVirtualSwitchConfig, 1);
config.vswitch[0] = new VcHostVirtualSwitchConfig();
config.vswitch[0].changeOperation = "add";
config.vswitch[0].name = A_vSwitchName;
config.vswitch[0].spec = new VcHostVirtualSwitchSpec();
config.vswitch[0].spec.numPorts = 128;
config.vswitch[0].spec.bridge = new VcHostVirtualSwitchBondBridge();
config.vswitch[0].spec.bridge.nicDevice = System.getModule("com.vmware.onyx").array(String, A_upLinks.length);
config.vswitch[0].spec.bridge.nicDevice = A_upLinks;
config.portgroup = System.getModule("com.vmware.onyx").array(VcHostPortGroupConfig, 1);
config.portgroup[0] = new VcHostPortGroupConfig();
config.portgroup[0].changeOperation = "add";
config.portgroup[0].spec = new VcHostPortGroupSpec();
config.portgroup[0].spec.name = A_Portgroup;
config.portgroup[0].spec.vlanId = 0;
config.portgroup[0].spec.vswitchName = A_vSwitchName;
config.portgroup[0].spec.policy = new VcHostNetworkPolicy();
config.vnic = System.getModule("com.vmware.onyx").array(VcHostVirtualNicConfig, 1);
config.vnic[0] = new VcHostVirtualNicConfig();
config.vnic[0].changeOperation = "add";
config.vnic[0].portgroup = A_Portgroup;
config.vnic[0].spec = new VcHostVirtualNicSpec();
config.vnic[0].spec.ip = new VcHostIpConfig();
config.vnic[0].spec.ip.dhcp = false;
config.vnic[0].spec.ip.ipAddress = A_ipAddress;
config.vnic[0].spec.ip.subnetMask = A_subnetmask;
var managedObject = A_esxiHost.configManager.networkSystem;
managedObject.updateNetworkConfig(config, "modify"); // HostNetworkSystem
// ------- SelectVnicForNicType -------
var managedObject = A_esxiHost.configManager.virtualNicManager;
if (A_vMotionEnabled)
A_HostVirtualNicManager.selectVnicForNicType("vmotion", "vmk2"); // HostVirtualNicManager
--------------------------------------------------------------------------------------------------------
Hi!
If I remember right, the addVirtualNic()-method of the HostNEtworkSystem returns a string, which may contain the information you want (or at least a starting point)...
So maybe instead of using Onyx and updateNetworkConfig() it's better to do it all manually the hard way (lots of API reference reading necessary):
var pgName = 'cNewPortgroup';var networkSystem = host.configManager.networkSystem; //host is a input parameter of type VcHostSystemvar myVcHostIpConfig = new VcHostIpConfig() ; //use DHCP to keep the example simplemyVcHostIpConfig.dhcp = true;var pgSpec = new VcHostPortGroupSpec();pgSpec.name = pgName;pgSpec.vlanId = 0;pgSpec.vswitchName = 'vSwitch0';pgSpec.policy = new VcHostNetworkPolicy();networkSystem.addPortGroup(pgSpec);var myVcHostVirtualNicSpec = new VcHostVirtualNicSpec() ;myVcHostVirtualNicSpec.ip = myVcHostIpConfig;myVcHostVirtualNicSpec.portgroup = pgName;var newVMKPort = networkSystem.addVirtualNic(pgName, myVcHostVirtualNicSpec); //here we get the nameSystem.debug('newVMKPort: ' + newVMKPort);var managedObject = host.configManager.virtualNicManager;//if (A_vMotionEnabled) //commented to run in my simple examplemanagedObject.selectVnicForNicType("vmotion", newVMKPort); // HostVirtualNicManager
Again: All no lack of vCO, just curiousity of the vCenter API.
(Well, if there would be pre-built Actions & Workflows for this in the library, it would be as easy as in PowerCLI. However, by doing it in this fashion one understands what one automates, instead of the "monkey-see,monkey-do" script-kiddies :smileydevil:)
Cheers,
Joerg
PS.; I knew why I kept this vmkernel network cram out of the training
How to modify this code for enabling traffic(vMotion, fault tolerance etc) on an existing portgroup??