Getting terraform libvirt to use existing qcow2 file (original) (raw)
Hi,
I want to setup a Windows domain controller with terraform libvirt provider from my Ubuntu host machine. Instead of using autounattend.xml file, my plan is to create a new VM in virt-manager through the GUI using the Windows ISO. Then I setup the machine by clicking next,next etc until I am logged in on it. Then I shut down the vm adn then I have the dc.qcow2 vile in /var/lib/libvirt/images
So then I tried to instead use this qcow2 file directly by doing this code:
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
}
}
}
provider "libvirt" {
uri = "qemu:///system"
}
resource "libvirt_pool" "images" {
name = "images"
type = dir
target {
path = "/var/lib/libvirt/images/terraform-created"
}
}
resource "libvirt_network" "lab_network" {
name = "lab_network"
mode = nat
domain = lab.local
addresses = ["192.168.20.0/24"]
dhcp {
enabled = false
}
dns {
enabled = true
}
autostart = true
}
resource "libvirt_volume" "dc_disk" {
name = "dc_disk.qcow2"
source = "/var/lib/libvirt/images/terraform/server2022.qcow2"
}
resource "libvirt_domain" "dc" {
name = "dc.lab.local"
memory = 4096
vcpu = 2
cpu {
mode = "host-passthrough"
}
disk {
volume_id = libvirt_volume.dc_disk.id
scsi = true
}
boot_device {
dev = ["hd"]
}
network_interfaces {
network_id = libvirt_network.lab_network.id
hostname = "dc.lab.local"
addresses = ["192.168.20.10"]
}
console {
type = "pty"
target_type = "serial"
target_port = "0"
}
console {
type = "pty"
target_type = "virtio"
target_port = "1"
}
graphics {
type = "spice"
listen_type = "address"
autoport = true
}
}
But after terraform deploys the VM, and I open it from virt-manager GUI, then the machine is in the Repair state so something went wrong with it. Do you see what may be wrong in the code?
Btw on the machine that I set up manually, I ran: virsh dumpxml windowsdc, and I saw there are two disks: qemu qcow2 /var/lib/libvirt/images/windowsdc.qcow2 qenu raw /path/to/iso which is CDROM, and the it uses service sdb on the SATA bus
Appreciate all help!
brucelok April 28, 2025, 5:16am 2
It’s a community provider dmacvicar/libvirt I never used it before, better ask in their github issue