How to create a VM (not clone) with a vmdk disk attached using Pyvmomi?

5.8k views Asked by At

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

There are 2 answers

1
Reuben On

I can't see what you have initialized in devices

So you can try something like this

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

then append vdisk_spec to your devices

0
Niket Patel On

First create a 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)

Then reconfigure the VM by adding required devices:

    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 )