如何使用 pyvmomi 删除虚拟磁盘

How do you delete a virtual disk with pyvmomi

我正在尝试使用 pyvmomi 库编写 Python 程序到 "erase" 与 VM 关联的虚拟硬盘驱动器。手动完成的方法是删除虚拟磁盘并创建具有相同规格的新虚拟磁盘。我希望我需要对 pyvmomi 做同样的事情,所以我已经开始了这条路。我的问题是我可以使用 ReconfigVM_Task 删除虚拟驱动器,但会留下 VMDK 文件本身。

我最初尝试使用 DeleteVStorageObject_Task(因为 DeleteVirtualDisk_Task 已被弃用)来删除虚拟磁盘文件,但这需要我无法在任何地方找到的对象(VMDK 文件)的 ID .理论上可以从 VirtualDisk 属性 vDiskId 获得,但它是空的。在进一步的研究中,它似乎只填充了前 class 个磁盘。

所以我尝试直接使用 DeleteDatastoreFile_Task 删除 VMDK 文件,但是当我这样做时,我最终在数据存储中得到了一个 XXXX-flat.vmdk 文件,所以它似乎并没有真正删除文件。

知道我在哪里出错或如何更好地做到这一点吗? pyvmomi 的 VMWare SDK 文档是......缺乏。

谢谢!

您必须执行 ReconfigVM_Task 操作。这样做的关键是文件操作应该被销毁。这是在 UI 中执行操作的原始输出:

spec = vim.vm.ConfigSpec()
spec_deviceChange_0 = vim.vm.device.VirtualDeviceSpec()
spec_deviceChange_0.fileOperation = 'destroy'
spec_deviceChange_0.device = vim.vm.device.VirtualDisk()
spec_deviceChange_0.device.shares = vim.SharesInfo()
spec_deviceChange_0.device.shares.shares = 1000
spec_deviceChange_0.device.shares.level = 'normal'
spec_deviceChange_0.device.capacityInBytes = 8589934592
spec_deviceChange_0.device.storageIOAllocation = vim.StorageResourceManager.IOAllocationInfo()
spec_deviceChange_0.device.storageIOAllocation.shares = vim.SharesInfo()
spec_deviceChange_0.device.storageIOAllocation.shares.shares = 1000
spec_deviceChange_0.device.storageIOAllocation.shares.level = 'normal'
spec_deviceChange_0.device.storageIOAllocation.limit = -1
spec_deviceChange_0.device.storageIOAllocation.reservation = 0
spec_deviceChange_0.device.backing = vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
spec_deviceChange_0.device.backing.backingObjectId = ''
spec_deviceChange_0.device.backing.fileName = '[kruddy_2TB_01] web01/web01_2.vmdk'
spec_deviceChange_0.device.backing.split = False
spec_deviceChange_0.device.backing.writeThrough = False
spec_deviceChange_0.device.backing.datastore = search_index.FindByUuid(None, "datastore-14", True, True)
spec_deviceChange_0.device.backing.eagerlyScrub = True
spec_deviceChange_0.device.backing.contentId = 'e26f44020e7897006bec81b1fffffffe'
spec_deviceChange_0.device.backing.thinProvisioned = False
spec_deviceChange_0.device.backing.diskMode = 'persistent'
spec_deviceChange_0.device.backing.digestEnabled = False
spec_deviceChange_0.device.backing.sharing = 'sharingNone'
spec_deviceChange_0.device.backing.uuid = '6000C292-7895-54ee-a55c-49d0036ef1bb'
spec_deviceChange_0.device.controllerKey = 200
spec_deviceChange_0.device.unitNumber = 0
spec_deviceChange_0.device.nativeUnmanagedLinkedClone = False
spec_deviceChange_0.device.capacityInKB = 8388608
spec_deviceChange_0.device.deviceInfo = vim.Description()
spec_deviceChange_0.device.deviceInfo.summary = '8,388,608 KB'
spec_deviceChange_0.device.deviceInfo.label = 'Hard disk 2'
spec_deviceChange_0.device.diskObjectId = '148-3000'
spec_deviceChange_0.device.key = 3000
spec_deviceChange_0.operation = 'remove'
spec.deviceChange = [spec_deviceChange_0]
spec.cpuFeatureMask = []
managedObject.ReconfigVM_Task(spec)  

Kyle Ruddy 为我指明了正确的方向。这是一个代码片段,展示了我如何让它为未来搜索有关如何执行此操作的信息的人工作:

#Assuming dev is already set to the vim.vm.device.VirtualDisk you want to delete...
virtual_hdd_spec = vim.vm.device.VirtualDeviceSpec()
virtual_hdd_spec.fileOperation = vim.vm.device.VirtualDeviceSpec.FileOperation.destroy
virtual_hdd_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.remove
virtual_hdd_spec.device = dev
spec = vim.vm.ConfigSpec()
spec.deviceChange = [virtual_hdd_spec]
WaitForTask(vm.ReconfigVM_Task(spec=spec))

API 文档位于 https://pubs.vmware.com/vi3/sdk/ReferenceGuide/vim.vm.device.VirtualDeviceSpec.html