gpt4 book ai didi

python - 如何使用 Pyvmomi 创建带有附加 vmdk 磁盘的虚拟机(非克隆)?

转载 作者:太空宇宙 更新时间:2023-11-03 18:04:17 30 4
gpt4 key购买 nike

我正在尝试使用 Pyvmomi 创建一个新的虚拟机。我成功创建了带有 RAM 和 CPU 的虚拟机,但我找不到有关如何创建带有附加磁盘的虚拟机的文档。我希望创建一个具有 20GB 精简配置 HDD 的虚拟机,但我找不到有关如何执行此操作的文档。

这就是我正在使用的:

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()

最佳答案

首先创建一个虚拟机:

    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)

然后通过添加所需设备来重新配置虚拟机:

    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 )

关于python - 如何使用 Pyvmomi 创建带有附加 vmdk 磁盘的虚拟机(非克隆)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27157246/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com