vmware / pyvmomi-community-samples

A place for community contributed samples for the pyVmomi library.
Apache License 2.0
1.02k stars 923 forks source link

Help wanted with InstantClone #730

Open sYera21 opened 1 year ago

sYera21 commented 1 year ago

Describe the bug

I am using instantClone, and I am able to spin up the clone, but am unable to adapt any of the option values, in this case they are changes to the hardware of the Instant Clone. I read that the hardware is set by the GuestOS and so I have tired to use this page https://vdc-repo.vmware.com/vmwb-repository/dcr-public/723e7f8b-4f21-448b-a830-5f22fd931b01/5a8257bd-7f41-4423-9a73-03307535bd42/doc/vim.vm.GuestInfo.html to attempt it, but i'm having issues. Currently my code looks as follows:

Option Values that are passed in: {"config.hardware.numCPUs":10,"config.hardware.memoryMB":1000,"guestinfo.nicinfo.macAddress":"00:50:56:a6:e2:9j","guestinfo.ipAddress":"192.168.0.90"}

Option Values method: `def dict_to_optionvalues(self,guestinfo_vars): """ Transform Dictionary to Optional Values type to be passed to InstantClone Config

    Args:
        guestinfo_vars (dict): dictionary of values we are trying to edit

    Returns:
        optionvalues(OptionValue: formated list of type Option Value
    """

    optionvalues = []
    for k, v in guestinfo_vars.items():
        opt = vim.option.OptionValue()
        (opt.key, opt.value) = (k, v)
        optionvalues.append(opt)

    return optionvalues`

Instant Clone method: `def instant_clone_vm(self,content,parent_vm_name,vm_name,optionalvalues): """ Instant Clones a VM

    Args:
        content (Content): Data Stream from the ServiceInstance
        parent_vm_name (string): Name of the Host/Parent VM
        vm_name (string): Name of Cloned VM
        optionalvalues (dict): optional values in key value pair form
    """
    #Gets the Host VM Object
    parent_vm=self.get_obj(content,[vim.VirtualMachine],parent_vm_name)
    #Formats the optional values to type (OptionalValue) from dict
    opt=self.dict_to_optionvalues(optionalvalues)

    #Used to setup where the clone will be relocated to 
    vm_relocate_spec=vim.vm.RelocateSpec()
    vm_relocate_spec.datastore=self.get_obj(content,[vim.Datastore],"DataCenter")

    #Used to describe the given clone specifications
    instant_clone_spec=vim.vm.InstantCloneSpec()
    instant_clone_spec.name=vm_name
    instant_clone_spec.location=vm_relocate_spec
    instant_clone_spec.config=opt

    #Creates the instant clone task for the Host VM
    parent_vm.InstantClone_Task(spec=instant_clone_spec)
    #Waits for the Instant Cloning to Occur
    WaitForTask(parent_vm.InstantClone_Task(spec=instant_clone_spec))`

Reproduction steps

1. 2. 3. ... Use a ESXI 8 setup

Expected behavior

The VM is expected to have the hardware changes written above

Additional context

Any advice is helpful

prziborowski commented 1 year ago

Based on the description in https://developer.vmware.com/apis/358/vim.vm.InstantCloneSpec.html

Values will be queryable via destination VM's [extraConfig](https://developer.vmware.com/apis/358/vim.vm.ConfigInfo.html#extraConfig).

I don't think you'll be able to change anything that isn't already configurable in the extraConfig location. And maybe possibly in the location section:

A device change specification. The only allowed device changes are edits of VirtualEthernetCard and filebacked Serial/Parallel ports.

you could update the VirtualEthernetCard macAddress to something else. But you'd have to pick a static address that is in the correct range (and not using one of the dynamic ones ESXi/vCenter generates).

sYera21 commented 1 year ago

Does the extraConfig mean values that are in the vm. ConfigInfo https://developer.vmware.com/apis/358/vim.vm.ConfigInfo.html#extraConfig

or are they another set of arbitrary values that make up the extraConfig since it isn't explicitly stated.

sYera21 commented 1 year ago

This also says that you have the ability to

https://docs.vmware.com/en/VMware-vSphere/7.0/com.vmware.vsphere.vm_admin.doc/GUID-853B1E2B-76CE-4240-A654-3806912820EB.html

"To avoid network conflicts, you can customize the virtual hardware of the destination virtual machine during an Instant Clone operation. For example, you can customize the MAC addresses of the virtual NICs or the serial port configurations of the destination virtual machine. vSphere 6.7 and later does not support customization of the guest OS of the destination virtual machine."

Is this not applicable?

prziborowski commented 1 year ago

For the config settings, they would be the kind of settings that are on the source config.extraConfig.

For changing network or serial port, you would use the location property of the InstantCloneSpec and modify the deviceChange property of that to update the devices.

sYera21 commented 1 year ago

Are there any details as to what the key values of the config.extraConfig specifically are?