hashicorp / packer-plugin-vsphere

Packer plugin for VMware vSphere Builder
https://www.packer.io/docs/builders/vsphere
Mozilla Public License 2.0
94 stars 91 forks source link

vsphere-clone: unable to use disk_size when source image has multiple disks #303

Closed weakcamel closed 4 months ago

weakcamel commented 1 year ago

When filing a bug please include the following headings, if possible.

Overview of the Issue

I'm using vsphere-clone image which has 1 primary and 2 extra disks. When trying to use disk_size parameter, I'm getting an error:

2023-09-04T12:38:33Z: Build 'vsphere-clone.customise' errored after 1 second 229 milliseconds: failed to resize primary disk: VM has multiple disks

There is no mention in the docs that's not supported though.

Looking at the old implementation ticket https://github.com/jetbrains-infra/packer-builder-vsphere/issues/68#issuecomment-374897243 it appears that the intention was to support resizing of existing disks with the storage stanza but all that does is add even more disks.

Reproduction Steps

packer build with an source VM/image containing more than 1 disk

Packer Version

# packer version
Packer v1.9.1

Your version of Packer is out of date! The latest version
is 1.9.4. You can update by downloading from www.packer.io/downloads

Plugin Version and Builders

# packer plugins installed
/root/.config/packer/plugins/github.com/hashicorp/ansible/packer-plugin-ansible_v1.1.0_x5.0_linux_amd64
/root/.config/packer/plugins/github.com/hashicorp/vsphere/packer-plugin-vsphere_v1.2.1_x5.0_linux_amd64

VMware vSphere Version

Please provide the VMware vSphere version.

Guest Operating System

Simplified Packer Buildfile

locals {
  vm_hostname = "foo-ubuntu"
  folder      = "appliance-builds"
  build_ts    = "${timestamp()}"
  description = <<-EOT
  Ubuntu ${var.ubuntu_version} based appliance.
  EOT
}

source "vsphere-clone" "customise" {
  communicator = "ssh"
  ssh_username = "foouser"
  ssh_password = "foopass"

  vcenter_server = var.vsphere_server
  username       = var.vsphere_username
  password       = var.vsphere_password
  CPUs           = var.vsphere_cpu_cores
  RAM            = var.vsphere_ram_size
  datacenter     = var.vsphere_datacenter
  cluster        = var.vsphere_cluster
  resource_pool  = var.vsphere_resource_pool
  datastore      = var.vsphere_datastore
  folder         = local.folder
  network        = var.vsphere_network
  template       = var.source_template_name
  vm_name        = var.vm_name
  notes          = local.description
  disk_size      = 25 * 1024
  configuration_parameters = {
    "guestinfo.userdata" = base64encode(file("user-data"))
    "guestinfo.metadata" = base64encode(templatefile(
      "meta-data.tpl",
      { vm_name = var.vm_name, hostname = local.vm_hostname },
    ))
    "guestinfo.userdata.encoding"        = "base64"
    "guestinfo.metadata.encoding"        = "base64"
  }
  disk_controller_type = var.vsphere_disk_controller_type
}

build {
  sources = [
    "source.vsphere-clone.customise"
  ]
}

Operating System and Environment Details

Build within a Linux 22.04 Docker container

Log Fragments and crash.log Files

2023-09-04T12:38:33Z: Build 'vsphere-clone.customise' errored after 1 second 229 milliseconds: failed to resize primary disk: VM has multiple disks
weakcamel commented 1 year ago

See also

tenthirtyam commented 5 months ago

Added the documentation label.

Per the docs this only is for the primary disk but the docs do not account for this explicitly.

The following function provides the error seen.

func findDisk(devices object.VirtualDeviceList) (*types.VirtualDisk, error) {
    var disks []*types.VirtualDisk
    for _, device := range devices {
        switch d := device.(type) {
        case *types.VirtualDisk:
            disks = append(disks, d)
        }
    }

    switch len(disks) {
    case 0:
        return nil, errors.New("VM has no disks")
    case 1:
        return disks[0], nil
    }
    return nil, errors.New("VM has multiple disks")
}