terraform-provider-vsphere: v1.17.0 - vsphere_virtual_machine : Invalid operation for device '0'

Hello,

I’m currently unable to deploy VMs using the terraform-provider-vsphere 1.17.0 and I’m getting :

vsphere_virtual_machine.svvm: Creating… vsphere_virtual_machine.svvm: Still creating… [10s elapsed] Error: error reconfiguring virtual machine: error reconfiguring virtual machine: Invalid operation for device ‘0’.

with the version 1.15, the VM is created with the same configuration

Terraform Version

Terraform v0.12.24

vSphere Provider Version

provider.vsphere = 1.17.0

Affected Resource(s)

  • vsphere_virtual_machine

Terraform Configuration Files

variable "dns_servers" {}
variable "domain" {}
variable "vsphere_datacenter" {}
variable "vsphere_cluster" {}
variable "vsphere_datastore" {}
variable "vsphere_template" {}
variable "vsphere_folder" {}
variable "vsphere_network" {}

data "vsphere_datacenter" "datacenter" {
        name = var.vsphere_datacenter
}

data "vsphere_resource_pool" "cluster" {
        name = "${var.vsphere_cluster}/Resources"
        datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_datastore" "datastore" {
        name = var.vsphere_datastore
        datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_virtual_machine" "template" {
        name          = var.vsphere_template
        datacenter_id = data.vsphere_datacenter.datacenter.id
}

data "vsphere_network" "network" {
        name = var.vsphere_network
        datacenter_id = data.vsphere_datacenter.datacenter.id
}

resource "vsphere_virtual_machine" "svvm" {
        name             = "testvm5"
        folder           = ""
        num_cpus         = 1
        memory           = 1024

        memory_hot_add_enabled = true
        cpu_hot_add_enabled    = true
        cpu_hot_remove_enabled = true

        datastore_id     = data.vsphere_datastore.datastore.id
        resource_pool_id = data.vsphere_resource_pool.cluster.id
        guest_id         = data.vsphere_virtual_machine.template.guest_id
        scsi_type        = data.vsphere_virtual_machine.template.scsi_type

        network_interface {
                network_id     = data.vsphere_network.network.id
        }

        disk {
                label            = "disk0"
                size             = data.vsphere_virtual_machine.template.disks.0.size
                eagerly_scrub    = data.vsphere_virtual_machine.template.disks.0.eagerly_scrub
                thin_provisioned = data.vsphere_virtual_machine.template.disks.0.thin_provisioned
        }
        clone {
                template_uuid = data.vsphere_virtual_machine.template.id
                customize {
                        timeout = 30
                        linux_options {
                          host_name = "testvm"
                          domain = "vm.lab.platform-essential.com"
                        }
                }
        }
}

Debug Output

https://gist.github.com/derrar05aiss/f29196d83eb90dc21380b11eca8c39d5

Expected Behavior

VM creation

Actual Behavior

Error while reconfiguring the VM

Steps to Reproduce

Please list the steps required to reproduce the issue, for example:

  1. terraform apply

References

Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here?

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 11
  • Comments: 16 (5 by maintainers)

Most upvoted comments

Definetively the issue is in the provider as pointed by @derrar05aiss

I can create the VM forcing the provider to 1.15

I believe this issue was fixed by #1397, included in the 2.0.0 release.

@gitnetofr : the root issue appears to result from the vsphere_virtual_machine data source and resource types measuring disk size in integer GiB, whilst real-world templates may have non-integer GiB disk sizes (e.g. Packer/vsphere-iso still expects sizes in integer MiB). Integer arithmetic in terraform-provider-vsphere results in these non-integer GiB disk sizes being rounded down to the nearest GiB. Unfortunately, when you clone using the rounded down size, you’re implicitly telling vSphere to shrink the disk, which is unsupported, and results in the Invalid operation for device '0' error.

I’ve opened #1397 with a fix.

I’m wondering if this is might be a Packer or Terraform related issue?

It could even be both. Packer does something that it shouldn’t, leaving the VM in an odd state Terraform should handle that and make it work anyhow

Will close, if the issue persists after upgrading to v2.0.0 of the provider please feel free to comment for re-evaluation

I found the root cause of this issue by investigating differences between 1.15.0 and 1.16.0 releases.

Investigation logs

First, i activated client_debug in vsphere provider to log SOAP requests and responses into ~/.govmomi/ folder. A difference appears in ReconfigVM_Task request. In fact, an additional edit operation on hard drive is present in the 1.16.0 request. This operation appears at rank 0.

<deviceChange xmlns:XMLSchema-instance="http://www.w3.org/2001/XMLSchema-instance" XMLSchema-instance:type="VirtualDeviceConfigSpec">
    <operation>edit</operation>
    <device XMLSchema-instance:type="VirtualDisk">
        <key>2000</key>
        <deviceInfo XMLSchema-instance:type="Description">
            <label>Hard disk 1</label>
            <summary>20,480,000 KB</summary>
        </deviceInfo>

The related error message is following.

<error>
    <fault xsi:type="InvalidDeviceOperation">
        <property>virtualDeviceSpec.device</property>
        <deviceIndex>0</deviceIndex>
        <badOp>edit</badOp>
    </fault>
    <localizedMessage>Invalid operation for device &apos;0&apos;.</localizedMessage>
</error>

So, the first clue seems to be the disk device.

Then, I decided to compile every commits between 1.15.0 and 1.16.0 and redo the same test with the compiled plugin by using dev_overrides in ~/.terraformrc. I discovered that the commit bc1eb533f4a504fb3fdd4614ea77faef45d36f68 reveals the issue. This commit adds the Storage Policy Based Management).

The second clue is the storage_policy_id parameter that has been added in 1.16.0 version.

Solution

In order to get the name of the Storage Policy at VM and disk levels, follow these instructions:

  1. Open your inventory and select your template
  2. Select Configure and Policies to get the names

See below the updates required to fix the issue.

data "vsphere_storage_policy" "vm_storage_policy" {
  name = <VM storage policy name>
}

data "vsphere_storage_policy" "disk_storage_policy" {
  name = <Disk storage policy name>
}

resource "vsphere_virtual_machine" "vm" {
  name = "vm"
  ...
  storage_policy_id = data.vsphere_storage_policy.vm_storage_policy.id

  disk {
    ...
    storage_policy_id = data.vsphere_storage_policy.disk_storage_policy.id
  }
}

Yes same issue here : error reconfiguring virtual machine: error reconfiguring virtual machine: Invalid operation for device ‘0’. vsphere 7 and hashicorp/vsphere v1.24.3… packer version 1.6.6

forcing the provider version to 1.15 helps thanks!