I am trying to create a new VM using Pyvmomi. I am successful creating the VM with RAM and CPU, but I cannot find docs on how to create this VM with a disk attached. I am looking to create a VM with a 20GB thin provisioned HDD, but I cannot find documentation on how to do this.
This is what I am using:
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 = '[email protected]'
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()
I can't see what you have initialized in
devices
So you can try something like this
then append
vdisk_spec
to yourdevices