1
votes

We are creating virtual machines with terraform as described in this example:

https://www.terraform.io/docs/providers/vsphere/r/virtual_machine.html#cloning-and-customization-example

We want each new machine to have a different size for its root partition without creating dozens of source VMs.

The source of our clone is an ubuntu (16.04) snapshot. The Snapshot has a root disk allocated with 20 GB.

When creating a linked clone the disk of the clone has to be the same size as the snapshot. In order to be able to resize the disks on cloned VMs we are using full clones.

This works fine except that the new disk size is not assigned to any partition of the clone. The root partition always has the size of the source vms partition. I have to assign the unallocated space manually after VM creation.

resource "vsphere_virtual_machine" "vm" {
  count            = "${var.count}"
  name             = "${var.stage}-${var.name}-${count.index}"

  ...

  network_interface {
    network_id   = "${data.vsphere_network.network.id}"
    adapter_type = "${data.vsphere_virtual_machine.source_vm.network_interface_types[0]}"
  }

  disk {
    label = "disk0"

    # This is to static for our use case as we will always result in VMs
    # having the same disk size 
    #size             = "${data.vsphere_virtual_machine.source_vm.disks.0.size}"

    size             = "${var.disk_size}" # Ideally this one should be appplied to the root partition :)
    eagerly_scrub    = false
    thin_provisioned = false
  }

  clone {
    template_uuid = "${data.vsphere_virtual_machine.source_vm.id}"

    # use full clones to adopt disk size        
    linked_clone = false

    customize {
      linux_options {
        ...
      }

      network_interface {}
    }
  }
}

I am not an expert in vSphere but I am missing something like images and flavors as you have them in Openstack.

Is there any possibility when working with Terraform and vSphere to create a clone/new VM AND resize the (root-) disk as part of the cloning process?

1
There might be a way to do this by baking a script into the image you are cloning. On the first boot you could execute this script, which takes looks at the drive information and re-creates the partition table using all the available space. After that you can reboot the machine (preferable) or reload the partition table.Augusto
Thanks for your reply. If there is no other way, I guess I have to do that. But vSphere really doesn't have any mechanism to adapt the disk size on vm creation? Sounds like a very basic feature to me that is missing here.Matthias

1 Answers

0
votes

The way I solved it now is dirty but works. I am executing a shell script that is repartitioning the disk during provisioning:

resource "vsphere_virtual_machine" "vm" {

  ...

  # partitioning

  provisioner "file" {
    source      = "${path.module}/../../files/partitioning.sh"
    destination = "/tmp/partitioning.sh"

    connection {
      ...
    }
  }

  provisioner "remote-exec" {
    inline = [
      "echo \"Initializing partitions\"",
      "echo ${var.sudo_password} | sudo -S bash /tmp/partitioning.sh",
    ]

    connection {
      ...
    }
  }
}

Does anyone know a better solution?