vmware / pyvmomi-community-samples

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

add RDM disk fails with pyVmomi.VmomiSupport.InvalidDeviceBacking #631

Closed erpadmin closed 4 years ago

erpadmin commented 4 years ago

code is modified sample. sample seems outdated since max disk slots is 65 not 16. maybe thats related to vmware paravirtual controller type

#! /venv/bin/python

from pyVmomi import vim
from pyVmomi import vmodl
from pyVim.connect import SmartConnect, Disconnect
from pyVim.task import WaitForTasks

def get_obj(content, vimtype, name):
    obj = None
    container = content.viewManager.CreateContainerView(
        content.rootFolder, vimtype, True)
    for c in container.view:
        if c.name == name:
            obj = c
            break
    return obj

def add_raw_disk(vm, si, device_name):
        spec = vim.vm.ConfigSpec()
        # get all disks on a VM, set unit_number to the next available
        unit_number = 0
        for dev in vm.config.hardware.device:
            if hasattr(dev.backing, 'fileName'):
                unit_number = int(dev.unitNumber) + 1
                # unit_number 7 reserved for scsi controller
                if unit_number == 7:
                    unit_number += 1
                if unit_number >= 16:
                    print "we don't support this many disks"
                    return
            if isinstance(dev, vim.vm.device.VirtualSCSIController):
                controller = dev
        disk_spec = vim.vm.device.VirtualDeviceSpec()
        disk_spec.fileOperation = "create"
        disk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
        disk_spec.device = vim.vm.device.VirtualDisk()
        rdm_info = vim.vm.device.VirtualDisk.RawDiskMappingVer1BackingInfo()
        disk_spec.device.backing = rdm_info
        disk_spec.device.backing.compatibilityMode = 'physicalMode'
        disk_spec.device.backing.diskMode = 'independent_persistent'
        disk_spec.device.backing.deviceName = device_name
        disk_spec.device.unitNumber = unit_number
        disk_spec.device.controllerKey = controller.key
        spec.deviceChange = [disk_spec]
        WaitForTasks([vm.ReconfigVM_Task(spec=spec)], si=si)
        print "Raw disk added to %s" % (vm.config.name)

si = SmartConnect(host = 'somevcenter.local', user = 'administrator@vsphere.local', pwd = 'somepassword')

vm = None
content = si.RetrieveContent()
vm = get_obj(content, [vim.VirtualMachine], 'egebsprod.bcp')

add_raw_disk(vm, si, '/vmfs/devices/disks/naa.624a9370167e9ce751f94c210004e1dc')
erpadmin commented 4 years ago

report is invalid, compat mode and disk mode were flipped, corrected original code

closing

iamkfred commented 1 year ago

I was unable to use the sample above to create an RDM. Debugging was painful, but in the hopes it helps some other poor soul down the road, I had to make the following changes to get it to work:

remove the line: disk_spec.device.backing.diskMode = 'persistent' add the line: disk_spec.device.backing.fileName = '[] -myrdm.vmdk'

In my case, I am attaching a raw SAN volume as an RDM to a guest. The storage system does snaps, so I don't need any ESX magic for snaps.