TerraformPilot

DevOps

Terraform for Xen and XCP-ng Virtualization

Automate Xen and XCP-ng pools with Terraform: xenorchestra provider, VM provisioning, SR storage, and Citrix Hypervisor lab automation.

LLuca Berton1 min read

Xen / XCP-ng is the open-source hypervisor descended from XenServer. The terra-farm/xenorchestra Terraform provider talks to Xen Orchestra to create VMs, manage networks, and control SRs (storage repositories). Common in cost-sensitive shops, Citrix migrations, and homelabs.

Provider

#
terraform {
  required_providers {
    xenorchestra = {
      source  = "terra-farm/xenorchestra"
      version = "~> 0.30"
    }
  }
}
 
provider "xenorchestra" {
  url      = "wss://${var.xo_host}"
  username = var.username
  password = var.password
}

VM from Template

#
data "xenorchestra_pool" "main" { name_label = "Acme-Pool" }
data "xenorchestra_template" "ubuntu" { name_label = "ubuntu-24.04-cloud" }
data "xenorchestra_sr" "ssd" { name_label = "ssd-pool"; pool_id = data.xenorchestra_pool.main.id }
data "xenorchestra_network" "vlan100" { name_label = "vlan-100"; pool_id = data.xenorchestra_pool.main.id }
 
resource "xenorchestra_vm" "web" {
  name_label   = "web-1"
  template     = data.xenorchestra_template.ubuntu.id
  cpus         = 2
  memory_max   = 4 * 1024 * 1024 * 1024
 
  network {
    network_id = data.xenorchestra_network.vlan100.id
  }
 
  disk {
    sr_id      = data.xenorchestra_sr.ssd.id
    name_label = "root"
    size       = 32 * 1024 * 1024 * 1024
  }
 
  cloud_config = <<-EOT
    #cloud-config
    hostname: web-1
    users:
      - name: ubuntu
        ssh_authorized_keys:
          - ${chomp(file("~/.ssh/id_ed25519.pub"))}
  EOT
}

Best Practices

#
  • Use Xen Orchestra Community Edition or paid Pro — direct Xen API control without XO is painful.
  • Pin SR by name_label, never by UUID — UUIDs break across pool rebuilds.
  • Cloud-init via XO is cleaner than remote-exec.
  • Snapshot before applies that change disk size — Xen disk grow is cheap, shrink is not.
#
#Terraform#Xen#XCP-ng#Xen Orchestra#Citrix Hypervisor

Share this article