dmacvicar / terraform-provider-libvirt

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

How can I specify virtual network device? #1075

Open alexeyyakimovich opened 8 months ago

alexeyyakimovich commented 8 months ago

System Information

Linux distribution

Ubuntu 22.04

Terraform version

OpenTofu v1.6.1
on darwin_arm64

Provider and libvirt versions

provider registry.opentofu.org/dmacvicar/libvirt v0.7.6
libvirtd (libvirt) 8.0.0

Description of Issue/Question

I want to bind virtual network to specific host interface, but i can't find the way to do it. I don't know if this feature is missing or it's just a documentation issue.

Setup

terraform {
  required_providers {
    libvirt = {
      source = "dmacvicar/libvirt"
      version = "0.7.6"
    }
  }
}

provider "libvirt" {
  uri = "qemu+ssh://..."
}

resource "libvirt_network" "services-network" {
  name = "office-services"
  mode = "route"
  domain = "services.office.internal"
  addresses = ["192.168.2.0/28"]
  bridge = "office-services"
  autostart = true

  dhcp {
    enabled = true
  }

  dns {
    enabled = true
    local_only = true

    forwarders {
        address = "172.16.0.9"
        domain = "office.internal"
    }
  }
}

Steps to Reproduce Issue

This setup will generate the following network xml:

<network>
  <name>office-services</name>
  <uuid>553965c9-578c-4d58-abbf-e2fab9107430</uuid>
  <forward mode='route'/>
  <bridge name='office-services' stp='on' delay='0'/>
  <mac address='52:54:00:2d:74:e3'/>
  <domain name='services.office.internal' localOnly='yes'/>
  <dns enable='yes'>
    <forwarder domain='office.internal' addr='172.16.0.9'/>
  </dns>
  <ip family='ipv4' address='192.168.2.1' prefix='28'>
    <dhcp>
      <range start='192.168.2.2' end='192.168.2.14'/>
      <host mac='02:00:00:00:00:01' name='reverse' ip='192.168.2.2'/>
    </dhcp>
  </ip>
</network>

I've edited it manually to define dev attribute of forward node. After saving network xml becomes this way:

<network>
  <name>office-services</name>
  <uuid>553965c9-578c-4d58-abbf-e2fab9107430</uuid>
  <forward dev='office' mode='route'>
    <interface dev='office'/>
  </forward>
  <bridge name='office-services' stp='on' delay='0'/>
  <mac address='52:54:00:2d:74:e3'/>
  <domain name='services.office.internal' localOnly='yes'/>
  <dns enable='yes'>
    <forwarder domain='office.internal' addr='172.16.0.9'/>
  </dns>
  <ip family='ipv4' address='192.168.2.1' prefix='28'>
    <dhcp>
      <range start='192.168.2.2' end='192.168.2.14'/>
      <host mac='02:00:00:00:00:01' name='reverse' ip='192.168.2.2'/>
    </dhcp>
  </ip>
</network>

Is it possible to achieve this result with provider?

scabala commented 2 months ago

Hello, does specifying device work with pure libvirt?

And out curiosity, what are you trying to achieve?

alexeyyakimovich commented 2 months ago

Hello, yes, it works with pure libvirt )

I want to pass nic port direct to vm