ESXi Host Disconnects When Attempting to Resize Disk of VM through pyvmomi
search cancel

ESXi Host Disconnects When Attempting to Resize Disk of VM through pyvmomi

book

Article ID: 389003

calendar_today

Updated On:

Products

VMware vCenter Server VMware vSphere ESX 7.x VMware vSphere ESX 8.x VMware vSphere ESXi VMware vSphere ESXi 7.0 VMware vSphere ESXi 8.0 VMware vCenter Server 7.0 VMware vCenter Server 8.0

Issue/Introduction

While attempting to extend the disk of a Virtual Machine (vm) using PyVmomi, the task shows as failed in vCenter but completes the disk resize.

The Virtual Machine's host disconnects.

The Reconfigure Virtual Machine task shows as failed with "An error occurred while communicating with the remote host." on vCenter.

Reconfiguration of Virtual Machine functions as expected through manual execution in the GUI.

Environment

  • VMware vCenter Server 7.x
  • VMware vCenter Server 8.x
  • VMware vSphere ESXi 7.x
  • VMware vSphere ESXi 8.x

 

Cause

Occurs if the reconfiguration task calls for a new Virtual Disk device instead of accessing the Virtual Disk that already exists on the vm.

Resolution

To resolve this issue, make sure to call the appropriate Virtual Disk device when setting the spec for the reconfigure task. Below is an example of what that looks like.

def modify_vm_disk(new_vm, disk_size_gb):
    content = service_instance.content
    new_vm_name = None
    disk_spec = None
    vm_view = content.viewManager.CreateContainerView(content.rootFolder, [vim.VirtualMachine], True)
    vm_list = vm_view.view
    disk_to_resize = None
       
    for vm in vm_list:
        if vm.name == new_vm:
            for device in vm.config.hardware.device:
                if isinstance(device, vim.vm.device.VirtualDisk):
                    if device.deviceInfo.label == "Hard disk 1":
                        disk_to_resize = device
                        break
            new_vm_name = vm
            spec = vim.vm.ConfigSpec()
            spec_deviceChange_0 = vim.vm.device.VirtualDeviceSpec()
            spec_deviceChange_0.device = disk_to_resize
            spec_deviceChange_0.device.capacityInKB = disk_size_gb * 1024 * 1024
            spec_deviceChange_0.operation = vim.vm.device.VirtualDeviceSpec.Operation.edit
            spec.deviceChange = [spec_deviceChange_0]                      
            task = new_vm_name.Reconfigure(spec=spec)

Additional Information


For more information on using vSphere SDKs and pyVmomi, refer to the Broadcom Developer Portal, pyVmomi download page and pyVmomi community samples.