This question has multiple moving parts including creating a template, modifying a template and cloning the VM.
Let's tackle the easiest issue first. This part of your code is incorrect:
new_disk_kb = int(20) * 1024 * 1024
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.RawDiskMappingVer1BackingInfo()
disk_spec.device.backing.diskMode = 'persistent'
disk_spec.device.unitNumber = 3
disk_spec.device.capacityInKB = new_disk_kb
After reviewing multiple pyvmomi
samples scripts I can see that you're not setting the unitNumber
correctly. Based on these scripts you need to determine the number of disks and assign the next unitNumber
available, which I would assume in your case is 2
and not 3
.
The code below was extracted from the add raw disk to vm pyvmomi
script.
spec = vim.vm.ConfigSpec()
# get all disks on a VM, set unit_number to the next available
unit_number = 0
controller = None
for device in vm.config.hardware.device:
if hasattr(device.backing, 'fileName'):
unit_number = int(device.unitNumber) + 1
# unit_number 7 reserved for scsi controller
if unit_number == 7:
unit_number += 1
if unit_number >= 16:
print("we don't support this many disks")
return -1
if isinstance(device, vim.vm.device.VirtualSCSIController):
controller = device
if controller is None:
print("Disk SCSI controller not found!")
return -1
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()
rdm_info = vim.vm.device.VirtualDisk.RawDiskMappingVer1BackingInfo()
disk_spec.device.backing = rdm_info
disk_spec.device.backing.compatibilityMode = disk_compatibility_mode
disk_spec.device.backing.diskMode = disk_mode
# The device_name will look something like
# /vmfs/devices/disks/naa.41412340757396001d7710df0fdd22a9
disk_spec.device.backing.deviceName = device_name
disk_spec.device.unitNumber = unit_number
disk_spec.device.controllerKey = controller.key
spec.deviceChange = [disk_spec]
Concerning the other problems that you want to tackle. There are several pyvmomi script that could be useful. One in particular is Clone VM, which provides details on cloning a VM from a template, which you're trying to create.
I'm not sure how useful that script would be, because it seems that you're looking for a complete solution to solve your use case.
I would recommend looking at this old Github
project, which has lots of useful code for creating a template, modifying a template and cloning a VM.
Concerning your comments about using an existing template that already has one disk (boot disk) and adding a secondary disk during the cloning process.
Based on my interpretation on the VM Documentation I currently don't think this is possible and where is why:
A VM template is a master copy image of a virtual machine that includes VM disks, virtual devices, and settings. This template can be use in the VM cloning process. This template is static and should be considered a configuration baseline.
This template cannot be modify during the cloning process. If this static template needs to be modified for any reason than it needs to be recreated with the new additions. This means that the original template needs to be converted into a VM, this VM needs to be edited, and then convert back into a new template.
If this isn't possible you can clone the baseline template on a new VM and use one of the pyvmomi
add disks scripts to enable a new disk on your VM.
I don't know your exact use case, but I think recreating the template is the logical choice, but your use case might work better with creating the new disk after cloning.
That GitHub project that I previously mentioned is using a baseline template for cloning a VM and it also has an addDisks
function. The latter is used to add a new disk to the VM that was created during the cloning process.
Here is an older pyvmomi
issue where someone wanted to modify a disk related to a template being cloned. It was recommended that the OP perform this task in the post-clone process.
That GitHub project shows how to use a baseline template and modify the VMs configuration as needed. From the code it seems that it is using CloneSpec()
to modify the VM during the cloning process and not the post-clone process.