我正在尝试使用Pyvmomi创建一个新VM。我成功创建了带有RAM和CPU的VM,但我找不到有关如何使用附加磁盘创建此VM的文档。 我希望创建一个配备20GB精简配置硬盘的虚拟机,但我找不到有关如何执行此操作的文档。
这就是我正在使用的:
import atexit
import hashlib
import json
import random
import time
import requests
from pyVim import connect
from pyVmomi import vim
from tools import tasks
vc_host = 'vc.example.com'
vc_user = 'john@example.com'
vc_ds = 'datastore1'
vc_password = 'secret'
def create_vm(name, service_instance, vm_folder, resource_pool,datastore):
vm_name = 'VM-' + name
datastore_path = '[' + datastore + '] ' + vm_name
# bare minimum VM shell, no disks. Feel free to edit
vmx_file = vim.vm.FileInfo(logDirectory=None,
snapshotDirectory=None,
suspendDirectory=None,
vmPathName=datastore_path)
config = vim.vm.ConfigSpec(
name=vm_name,
memoryMB=128,
numCPUs=1,
files=vmx_file,
guestId='dosGuest',
version='vmx-07'
)
print "Creating VM {}...".format(vm_name)
task = vm_folder.CreateVM_Task(config=config, pool=resource_pool)
tasks.wait_for_tasks(service_instance, [task])
def main():
name = 'testvm'
service_instance = connect.SmartConnect(host=vc_host,
user=vc_user,
pwd=vc_password,
)
if not service_instance:
print("Could not connect to the specified host using specified "
"username and password")
return -1
atexit.register(connect.Disconnect, service_instance)
content = service_instance.RetrieveContent()
datacenter = content.rootFolder.childEntity[0]
vmfolder = datacenter.vmFolder
hosts = datacenter.hostFolder.childEntity
resource_pool = hosts[0].resourcePool
create_vm(name, service_instance, vmfolder, resource_pool, vc_ds)
return 0
# Start program
if __name__ == "__main__":
main()
答案 0 :(得分:1)
首先创建一个VM:
datastore_path = '[datastore1] vm1'
vmx_file = vim.vm.FileInfo(logDirectory=None,
snapshotDirectory=None,
suspendDirectory=None,
vmPathName=datastore_path)
config = vim.vm.ConfigSpec(
name=vm_name,
memoryMB=2048,
numCPUs=cpu,
files=vmx_file,
guestId=None,
version='vmx-07'
)
hostobj = <get the esx host object>
vm_folder = hostobj.vm[0].parent
resource_pool = hostobj.vm[0].resourcePool
task = vm_folder.CreateVM_Task(config=config, pool=resource_pool)
然后通过添加所需设备重新配置VM:
spec = vim.vm.ConfigSpec()
scsi_ctr = vim.vm.device.VirtualDeviceSpec()
scsi_ctr.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
scsi_ctr.device = vim.vm.device.VirtualLsiLogicController()
scsi_ctr.device.deviceInfo = vim.Description()
scsi_ctr.device.slotInfo = vim.vm.device.VirtualDevice.PciBusSlotInfo()
scsi_ctr.device.slotInfo.pciSlotNumber = 16
scsi_ctr.device.controllerKey = 100
scsi_ctr.device.unitNumber = 3
scsi_ctr.device.busNumber = 0
scsi_ctr.device.hotAddRemove = True
scsi_ctr.device.sharedBus = 'noSharing'
scsi_ctr.device.scsiCtlrUnitNumber = 7
unit_number = 0
controller = scsi_ctr.device
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()
disk_spec.device.backing = \
vim.vm.device.VirtualDisk.FlatVer2BackingInfo()
disk_spec.device.backing.diskMode = 'persistent'
disk_spec.device.backing.fileName = '[%s] %s/%s.vmdk' % \
( ds_name, client, vmdk_name )
disk_spec.device.unitNumber = unit_number
disk_spec.device.capacityInKB = <size in kb>
disk_spec.device.controllerKey = controller.key
dev_changes = []
dev_changes.append( scsi_ctr )
dev_changes.append( disk_spec )
spec.deviceChange = dev_changes
vmobj.ReconfigVM_Task( spec=spec )
答案 1 :(得分:0)
我无法看到您在devices
所以你可以尝试这样的事情
vdisk_pec = vim.vm.device.VirtualDeviceSpec()
vdisk_spec.operation = vim.vm.device.VirtualDeviceSpec.Operation.add
vdisk_spec.operation.backing = vim.vm.device.VirtualDisk.RawDiskVer2BackingInfo()
vdisk_spec.operation.backing.changeId = change_id
vdisk_spec.operation.backing.descriptorFileName = descriptor_file_name
然后将vdisk_spec
附加到您的devices