Skip to content
Luca Dell'Oca Principal Cloud Architect @Veeam
Virtual To The Core Virtual To The Core

Virtualization blog, the italian way.

  • Media
  • About me
Virtual To The Core
Virtual To The Core

Virtualization blog, the italian way.

My Automated Lab project: #5 Deploy a Linux vSphere VM with Terraform and custom disks

Luca Dell'Oca, October 17, 2023October 17, 2023
After deploying a Windows VM in the previous article, this time I will deploy a Linux VM to be used as a Veeam Hardened Repository. Since I need some more space for storing the backup, I will use this opportunity to create the VM with a additional disks.

The configuration files

This time, I have a little bit different configuration:
As always, you can grab the files in my Github repo at https://github.com/dellock6/veeam-iac-lab/tree/main/terraform/lhr
First, the easy files: variables.tf is where I define all the variables and lhr.auto.tfvars is where I set values for them. Same as always with Terraform.
base.tf is one of the two interesting files I want to talk about today. Together with the vSphere-specific configurations that I also used in the previous example for a Windows VM, it has two sections I’d like to highlight:
locals {
  # flexible number of data disks for VM
  # mount as disk or LVM is done by remote-exec script
  disks = [
    { "id":1, "dev":"sdb", "lvm":0, "sizeGB":100, "dir":"/backup" },
  ]
  # construct arguments passed to disk partition/filesystem/fstab script
  # e.g. "sdb,0,10,/data1 sdc,1,20,/data2"
  disk_format_args = join(" ", [for disk in local.disks: "${disk.dev},${disk.lvm},${disk.sizeGB},${disk.dir}"] )
}
In this section, under the “disks” array, I define a second disk, numbered 1 (0 is the first disk already existing in the template that I will clone). The disk will have scsi id 1, its device name will be sdb, it’s size will be 100 GB, it’s going to be managed via LVM aand it will be mounted under /backup. Then, we have this following section later in the file:
# creates variable number of disks for VM
dynamic "disk" {
  for_each = [ for disk in local.disks: disk ]
  
  content {
   label            = "disk${disk.value.id}"
   unit_number      = disk.value.id
   datastore_id     = data.vsphere_datastore.datastore.id
   size             = disk.value.sizeGB
   eagerly_scrub    = false
   thin_provisioned = true
  }
}
Here I instruct Terraform to create all the disks I have defined in the previous array. I could have in fact a longer list of disks, depending on my needs. We just need to add more lines as the one I wrote, each inside brackets.
But I don’t settle with just creating an empty disk, I want to have it ready to be used. So I have these additional lines at the end of the file:
# make script from template
provisioner "file" {
  destination = "/tmp/xfs_reflink_disk_filesystem.sh"
  content = templatefile(
    "${path.module}/template/xfs_reflink_disk_filesystem.sh.tpl",
    { 
      "disks": local.disks
      "default_args" : local.disk_format_args
    }
  )
}
This first part takes the file template/xfs_reflink_disk_filesystem.sh.tpl and copies it into the VM in the /tmp/ folder.
Then, I have this last part:
  # script that creates partition and filesystem for data disks
  provisioner "remote-exec" {
    inline = [
      "chmod +x /tmp/xfs_reflink_disk_filesystem.sh",
      "echo ${var.host_password} | sudo -S /tmp/xfs_reflink_disk_filesystem.sh ${local.disk_format_args} > /tmp/xfs_reflink_disk_filesystem.log"
    ]
  }
}
where I set the script as executable and I instruct Linux to run it.
But what does the script does? Well, as you can guess from its name, it formats Disk1 with XFS and enables reflink, useful for then using Veeam Fast Clone.

Deploy the VM

As usual, I run in sequence terraform init, validate and apply to build my new Linux virtual machine:
Once it’s deployed, If I look at its configuration there are the two disks:
And the second one is formatted with XFS:
Note the parameter reflink=1, to confirm that this feature is enabled. My Veeam Hardened Repository is reday to be used, and I only spent a few minutes the first time to configure the Terraform files; from now on, I can redeploy it every time in a few seconds, and the vm will always be identical to what I wanted it to be.

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to email a link to a friend (Opens in new window) Email
  • Click to share on Tumblr (Opens in new window) Tumblr
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on Pocket (Opens in new window) Pocket
Tech automationcodehardened repositoryiacinfrastructurelablinuxreflinkservertemplateterraformveeamvspherexfs

Post navigation

Previous post
Next post

Search

Sponsors

Latest Posts

  • Migrate WSL (Windows Subsystem for Linux) to a new computer
  • Pass keystrokes to a pfSense virtual machine to install it automatically
  • Automatically deploy pfSense with Terraform and Ansible
  • My Automated Lab project: #6 Create a S3 Bucket with Terraform
  • My Automated Lab project: #5 Deploy a Linux vSphere VM with Terraform and custom disks
©2025 Virtual To The Core | WordPress Theme by SuperbThemes
We use cookies to ensure that we give you the best experience on our website, and to collect anonymous data regarding navigations stats using 3rd party plugins; they all adhere to the EU Privacy Laws. If you continue to use this site we will assume that you are ok with it.OkNoPrivacy Policy