Skip to content

Instantly share code, notes, and snippets.

@yankcrime
Created May 26, 2026 09:54
Show Gist options
  • Select an option

  • Save yankcrime/59e4d93df83c467d2b1abfd70327dd10 to your computer and use it in GitHub Desktop.

Select an option

Save yankcrime/59e4d93df83c467d2b1abfd70327dd10 to your computer and use it in GitHub Desktop.
Functional Terraform libvirt definition
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
version = "~> 0.9"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
variable "ssh_public_key" {
type = string
default = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIIaWxcNgP37ZP+Csyn5T2s3iHsdRiG960pRWb5TKx0sW nick@nsc1alt0066"
}
resource "libvirt_pool" "default" {
name = "default"
type = "dir"
target = {
path = "/var/lib/libvirt/images"
}
}
resource "libvirt_volume" "ubuntu_base" {
name = "noble-server-cloudimg-amd64.img"
pool = libvirt_pool.default.name
create = {
content = {
url = "https://cloud-images.ubuntu.com/noble/current/noble-server-cloudimg-amd64.img"
}
}
}
resource "libvirt_volume" "control0_disk" {
name = "control0.qcow2"
pool = libvirt_pool.default.name
capacity = 40 * 1024 * 1024 * 1024
target = {
format = {
type = "qcow2"
}
}
backing_store = {
path = libvirt_volume.ubuntu_base.path
format = {
type = "qcow2"
}
}
}
data "template_file" "user_data" {
template = <<EOF
#cloud-config
hostname: control0
fqdn: control0.42can.org
users:
- name: ubuntu
sudo: ALL=(ALL) NOPASSWD:ALL
groups: users, admin
shell: /bin/bash
ssh_authorized_keys:
- ${var.ssh_public_key}
package_update: true
packages:
- qemu-guest-agent
- curl
- vim
ssh_pwauth: false
disable_root: true
growpart:
mode: auto
devices: ['/']
resize_rootfs: true
runcmd:
- systemctl enable qemu-guest-agent
- systemctl start qemu-guest-agent
EOF
}
data "template_file" "network_config" {
template = <<EOF
version: 2
ethernets:
ens3:
dhcp4: true
ens4:
addresses:
- 10.0.8.10/24
EOF
}
resource "libvirt_cloudinit_disk" "control0_cloudinit" {
name = "control0-cloudinit"
user_data = data.template_file.user_data.rendered
meta_data = data.template_file.network_config.rendered
}
resource "libvirt_volume" "control0_cloudinit" {
name = "control0-cloudinit.iso"
pool = libvirt_pool.default.name
create = {
content = {
url = libvirt_cloudinit_disk.control0_cloudinit.path
}
}
}
resource "libvirt_domain" "control0" {
name = "control0"
memory = 8192
memory_unit = "MiB"
vcpu = 4
type = "kvm"
os = {
type = "hvm"
type_arch = "x86_64"
type_machine = "q35"
boot_devices = [{
dev = "hd"
}]
}
running = true
devices = {
graphics = [{
vnc = {
port = "5904"
passwd = "abc123"
listeners = [{
address = {
address = "0.0.0.0"
}
}]
}
}]
consoles = [
{
type = "pty"
target = {
type = "serial"
port = "0"
}
}
]
disks = [
{
driver = {
name = "qemu"
type = "qcow2"
}
source = {
volume = {
pool = libvirt_volume.control0_disk.pool
volume = libvirt_volume.control0_disk.name
}
}
target = {
dev = "vda"
bus = "virtio"
}
},
{
device = "cdrom"
source = {
volume = {
pool = libvirt_volume.control0_cloudinit.pool
volume = libvirt_volume.control0_cloudinit.name
}
}
target = {
bus = "sata"
dev = "sda"
}
}
]
interfaces = [
{
model = {
type = "virtio"
}
source = {
bridge = {
bridge = "br-mgmt"
}
}
},
{
model = {
type = "virtio"
}
source = {
bridge = {
bridge = "br-vlan8"
}
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment