dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.61k stars 460 forks source link

Can't connect domains to VLAN #1023

Open stijn-acc opened 1 year ago

stijn-acc commented 1 year ago

System Information

Linux distribution

Oracle Linux 8.3

Terraform version

Terraform v1.4.4

Provider and libvirt versions

provider registry.terraform.io/dmacvicar/libvirt v0.7.1


Checklist

Description of Issue/Question

Setup

terraform {
  required_version = ">=1.3.2"

  required_providers {
    libvirt = {
      source = "dmacvicar/libvirt"
      version = "0.7.1"
    }
  }
}

variable "vm_configs" {

}

provider "libvirt" {
  uri = "qemu:///system"
}

resource "libvirt_volume" "volume" {
  for_each = var.vm_configs
  name = "${each.key}_Libvirt_volume"
  source = each.value.source
  pool = each.value.pool
}

resource "libvirt_volume" "domain_disk" {
  for_each = var.vm_configs
  name = "${each.key}.qcow2"
  format = "qcow2"
  pool = each.value.pool
  base_volume_id = libvirt_volume.volume[each.key].id
  size = each.value.disk_size
}

resource "libvirt_domain" "domain" {
    for_each = var.vm_configs
    name   = each.key
    memory = each.value.memory
    vcpu   = each.value.vcpu
    autostart = true

    cpu {
        mode = "host-passthrough"
    }

    network_interface {
        mac = each.value.mac_address
        network_name = each.value.network_name
    }

    disk {
      volume_id = libvirt_volume.domain_disk[each.key].id
    }

    console {
        type = "pty"
        target_type = "serial"
        target_port = "0"
    }

    graphics {
        type        = "vnc"
        listen_type = "address"
        websocket = each.value.vnc_address
    }
}

I use this in combination with a "systems.tfvars" file to create a bunch of vms on my system.

vm_configs = {
  "vm1" = { 
    mac_address = "11:22:33:44:55:66"
    network_name = "br0"
    source = "/path/to/image"
    pool = "default"
    disk_size = 40*1024*1024*1024
    memory = 8*1024
    vnc_address = 5901
    vcpu = 2
  },
  "vm2" = { 
    mac_address = "11:22:33:44:55:77"
    network_name = "br0"
    ... etc

Description of Issue/Question

Currently I'm using the above main.tf and systems.tfvars file to create a bunch of vms on my system. This works fine, but now we are trying to put the vms in different vlans.

I have Open vSwitch installed on my system, created a bridge, etc. I followed this tutorial https://www.redhat.com/sysadmin/libvirt-open-vswitch The problem I have now is that I don't really understand how I can get my vms to connect to the correct vlan.

My understanding is that the domain XML should end up looking something like this. (Assuming this after reading https://libvirt.org/formatdomain.html#setting-vlan-tag-on-supported-network-types-only)

<interface type='bridge'>
  <mac address='11:22:33:44:55:66'/>
  <vlan>
    <tag id='42'/>
  </vlan>
  <source bridge='br0'/>
  <virtualport type='openvswitch'>
    <parameters interfaceid='09b11c53-8b5c-4eeb-8f00-d84eaa0aaa4f'/>
  </virtualport>
</interface>

But I have no idea how to update my current configuration to get to this point. I assume the best (only) way to get there is using the xml block, and passing a .xsl file to update the domain file. As assigning a VLAN can't be done in the network_interface block. But then I look at the example file https://github.com/dmacvicar/terraform-provider-libvirt/blob/main/examples/v0.13/xslt/nicmodel.xsl and it seems that this would only be usable to update the domains to connect to the same VLAN ? But I want the VLAN to be configurable. Anyone have an example for this? or am I looking in the wrong place?

scabala commented 3 months ago

Hi, I do not think it is possible to natively set VLAN tag on domain's interface with this provider right now. This is, however, nice feature to have.