hashicorp / terraform-provider-vsphere

Terraform Provider for VMware vSphere
https://registry.terraform.io/providers/hashicorp/vsphere/
Mozilla Public License 2.0
623 stars 453 forks source link

Add ability to configure boot sequence of `vsphere_virtual_machine` resource #1530

Open ravik694 opened 3 years ago

ravik694 commented 3 years ago

Description

It would be nice to have the ability to set the VM boot sequence through the vsphere_virtual_machine resource.

I've attempted setting this through the extra_config argument with the bios.bootOrder / bios.hddOrder parameters but the changes don't "stick" after applying the config. The same thing happens when trying to set the options through the ESXi web UI (VM -> Edit -> VM Options -> Advanced -> Edit Configuration.) The settings disappear after clicking Save. The settings do seem to be honored when I manually edit the .vmx file of the VM on the ESXi server.

Current example (extra_config settings are shown in terrraform plan and terraform apply but are not reflected in the VM config on ESXi):

resource "vsphere_virtual_machine" "vm" {
  name             = "terraform-test"
  resource_pool_id = "${data.vsphere_compute_cluster.cluster.resource_pool_id}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"

  num_cpus = 2
  memory   = 1024
  guest_id = "other3xLinux64Guest"

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

  disk {
    label = "disk0"
    size  = 20

  cdrom {
    datastore_id = data.vsphere_datastore.datastore.id
    path         = "iso/linux.iso"
  }

  extra_config = {
    "bios.bootOrder" = "cdrom,hdd"
    "bios.hddOrder"  = "scsi0:0"
  }
}

Potential Terraform Configuration

resource "vsphere_virtual_machine" "vm" {
  name             = "terraform-test"
  resource_pool_id = "${data.vsphere_compute_cluster.cluster.resource_pool_id}"
  datastore_id     = "${data.vsphere_datastore.datastore.id}"

  num_cpus = 2
  memory   = 1024
  guest_id = "other3xLinux64Guest"

  bootOrder = "floppy,cdrom,ethernet,disk"

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

  disk {
    label = "disk0"
    size  = 20

  cdrom {
    datastore_id = data.vsphere_datastore.datastore.id
    path         = "iso/linux.iso"
  }
}

References

My ESXi server (paid license) details:

Version: 6.7.0 Update 3 (Build 15160138)

State: Normal (not connected to any vCenter Server)

Community Note

thatsk commented 2 years ago

is there any update on this

tenthirtyam commented 2 years ago

Status can be viewed in the attached milestone; however, community contributions are welcome and help to accelerate features.

kevemueller commented 1 year ago

Some additional info from a working custom module (not Terraform, not Go):

The supported API way of configuring the boot order is via VirtualMachineBootOptions(vim.vm.BootOptions) in VirtualMachineConfigSpec(vim.vm.ConfigSpec). This works both at creation time as well as during reconfiguration.

BootOptions allow setting additional very useful booleans, that should also be exposed by the provider, most notably, bootDelay, efiSecureBootEnabled, networkBootProtocol (PXE over IPv4 or IPv6). The bootOrder array can only be populated with full knowledge of the deviceKeys of the underlying bootable device.

References ========

kevemueller commented 1 year ago

The behaviour of VMs created by this provider is non-deterministic and without this feature implemented cannot be made deterministic. If you create an EFI VM with only NICs and then make Terraform reconfigure it to have NICs and CDs, the boot order will be first NIC, then CD. If you create an EFI VM with NICs and CDs, the boot order will be first CD, then NIC. This breaks one of the promises of Terraform to ensure a consistent target state.

Tested with Terraform v1.4.0, vsphere v2.3.1 against ESXi-7.0U3j-21053776-standard.

nwmcsween commented 1 year ago

Some additional info from a working custom module (not Terraform, not Go):

The supported API way of configuring the boot order is via VirtualMachineBootOptions(vim.vm.BootOptions) in VirtualMachineConfigSpec(vim.vm.ConfigSpec). This works both at creation time as well as during reconfiguration.

BootOptions allow setting additional very useful booleans, that should also be exposed by the provider, most notably, bootDelay, efiSecureBootEnabled, networkBootProtocol (PXE over IPv4 or IPv6). The bootOrder array can only be populated with full knowledge of the deviceKeys of the underlying bootable device.

References

Secure boot is exposed in vsphere_virtual_machine, a good starting point would probably be looking there to make a pr. Looking at this this is acutally a trival patch and just needs the schema updated with the exposed boot options in govmomi.

Basically add to schema in virtual_machine_config_structure.go, change https://github.com/vmware/govmomi/blob/6687830863b6607134e359991c48fd758fc8f9ed/govc/device/boot.go#L42 to add the new schema options.