Troubleshooting: Terraform vSphere VM Clone Created but Not Booting

Scenario

A VM was successfully created by Terraform from an existing RHEL VM named "redhat", but the newly cloned VM "lamp01-vm" was not booting.
Terraform was able to find the source VM and complete the clone operation, so the
problem was investigated at the VM firmware and disk level.

1. Check the Terraform Plan

Run:
# .\terraform.exe plan

Check the VM firmware shown in the plan.
Initial problem found:
firmware = "bios"

However, the source "redhat" VM was using EFI.
A BIOS/EFI mismatch can cause a cloned Linux VM to fail to boot.

2. Check the Source VM Firmware

Use Terraform console:
#.\terraform.exe console

Run:
data.vsphere_virtual_machine.redhat_template.firmware

Result:
"efi"

Therefore, the source VM is using EFI.

3. Check the Source VM Disk

Run:
data.vsphere_virtual_machine.redhat_template.disks

Result:
size = 16
label = "Hard disk 1"
thin_provisioned = false
unit_number = 0
Therefore, the source VM has a 16 GB thick-provisioned disk.

4. Correct Terraform Configuration

Match the cloned VM with the source VM:

firmware = "efi"
disk {
  label            = "disk0"
  size             = 16
  thin_provisioned = false
}

Keep the clone configuration:

clone {
  template_uuid = data.vsphere_virtual_machine.redhat_template.id
}


5. Clean Up the Failed VM

If the failed VM already exists in Terraform state, remove it cleanly:
# .\terraform.exe destroy
Enter:
yes

Verify that lamp01-vm is removed from vSphere.

6. Run Terraform Again

Run:
#.\terraform.exe plan
Verify that the plan shows:
firmware = "efi"
and:
disk size = 16 GB
thin_provisioned = false

Then run:
#.\terraform.exe apply
Enter:
yes

7. Verify the New VM

Open vSphere Client:
lamp01-vm
  -> Configure
  -> VM Hardware
  -> VM Options
  -> Boot Options
Confirm:
Firmware = EFI
Also verify that the 16 GB OS disk is present.
Open the VM Console and check whether RHEL starts through the EFI/GRUB boot process.

Troubleshooting Summary
-----------------------
Problem:
VM was created successfully but did not boot.

Cause identified:
Source RHEL VM used EFI, while Terraform was creating the cloned VM with BIOS.
Checks performed:
1. Verified source VM firmware using Terraform console.
2. Verified source VM disk size and provisioning type.
3. Matched the cloned VM firmware to EFI.
4. Matched the disk configuration to the source VM.
5. Destroyed the failed clone and recreated it.

Useful Terraform Console Commands

# .\terraform.exe console
data.vsphere_virtual_machine.redhat_template.firmware
data.vsphere_virtual_machine.redhat_template.disks

Key Lesson
When cloning Linux VMs with Terraform and the vSphere provider, always verify
the source VM's firmware (BIOS/EFI) and disk configuration before troubleshooting the guest operating system. A VM can be created successfully in vSphere but still fail to boot if its firmware configuration does not match the source VM.

cat main.tf
provider "vsphere" {
  user                 = "administrator@vsphere.local"
  password             = "Pass@1234"
  vsphere_server       = "vcenter01.darole.org"
  allow_unverified_ssl = true
}
data "vsphere_datacenter" "dc" {
  name = "darole-dc"
}
data "vsphere_datastore" "datastore" {
  name          = "esxi02-datastore1"
  datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_compute_cluster" "cluster" {
  name          = "my-cluster"
  datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network1" {
  name          = "VM Network"
  datacenter_id = data.vsphere_datacenter.dc.id
}
data "vsphere_network" "network2" {
  name          = "VM Network 2"
  datacenter_id = data.vsphere_datacenter.dc.id
}
# =========================================================
# REDHAT SOURCE VM
# =========================================================
data "vsphere_virtual_machine" "redhat_template" {
  name          = "redhat"
  datacenter_id = data.vsphere_datacenter.dc.id
}
# =========================================================
# LAMP VM
# =========================================================
resource "vsphere_virtual_machine" "lamp01" {
  name             = "lamp01-vm"
  resource_pool_id = data.vsphere_compute_cluster.cluster.resource_pool_id
  datastore_id     = data.vsphere_datastore.datastore.id
  num_cpus = 1
  memory   = 1024
  # Source VM uses EFI
  firmware = "efi"
  # Same OS type as source
  guest_id = data.vsphere_virtual_machine.redhat_template.guest_id
  # =======================================================
  # NETWORK 1
  # =======================================================
  network_interface {
    network_id   = data.vsphere_network.network1.id
    adapter_type = "vmxnet3"
  }
  # =======================================================
  # NETWORK 2
  # =======================================================
  network_interface {
    network_id   = data.vsphere_network.network2.id
    adapter_type = "vmxnet3"
  }
  # =======================================================
  # SOURCE DISK
  # =======================================================
  disk {
    label            = "disk0"
    size             = 16
    thin_provisioned = false
  }
  # =======================================================
  # CLONE
  # =======================================================
  clone {
    template_uuid = data.vsphere_virtual_machine.redhat_template.id
  }
}

No comments:

Post a Comment