Skip to content

PCI address fields with value 0 returned as null causing perpetual drift #1246

@BohdanTkachenko

Description

@BohdanTkachenko

When using libvirt_domain with PCI passthrough devices (hostdevs with subsys_pci), any address field (domain, bus, slot, function) that has a value of 0 is incorrectly returned as null by the provider after apply. This causes Terraform/OpenTofu to report "Provider produced inconsistent result after apply" errors and makes it impossible to manage PCI passthrough devices where any address component is 0.

This is extremely common since most PCI devices have domain=0, and many have slot=0 or function=0 (e.g., 0000:03:00.0).

Steps to Reproduce

  1. Create the following minimal configuration:
terraform {
  required_providers {
    libvirt = {
      source  = "dmacvicar/libvirt"
      version = "~> 0.9.0"
    }
  }
}

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

resource "libvirt_volume" "test" {
  name     = "test-pci-bug.qcow2"
  pool     = "default"
  capacity = 1073741824

  target = {
    format = {
      type = "qcow2"
    }
  }
}

resource "libvirt_domain" "test" {
  name   = "test-pci-bug"
  memory = 512
  vcpu   = 1
  type   = "kvm"

  os = {
    type      = "hvm"
    type_arch = "x86_64"
  }

  devices = {
    disks = [
      {
        source = {
          volume = {
            pool   = "default"
            volume = libvirt_volume.test.name
          }
        }
        target = {
          dev = "vda"
          bus = "virtio"
        }
      }
    ]

    # PCI passthrough device - update bus to match a real device on your system
    # The bug occurs when domain, slot, or function equals 0
    hostdevs = [
      {
        managed = true
        subsys_pci = {
          source = {
            address = {
              domain   = 0  # This will be returned as null
              bus      = 3  # Change to match your device
              slot     = 0  # This will be returned as null
              function = 0  # This will be returned as null
            }
          }
        }
      }
    ]
  }
}
  1. Run terraform init && terraform apply

Expected Behavior

Apply should succeed and terraform plan should show "No changes".

Actual Behavior

Apply fails with:

│ Error: Provider produced inconsistent result after apply
│
│ When applying changes to libvirt_domain.test, provider "provider[\"registry.opentofu.org/dmacvicar/libvirt\"]"
│ produced an unexpected new value: .devices.hostdevs[0].subsys_pci.source.address.function: was cty.NumberIntVal(0), but now null.
│
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.
╵
╷
│ Error: Provider produced inconsistent result after apply
│
│ When applying changes to libvirt_domain.test, provider "provider[\"registry.opentofu.org/dmacvicar/libvirt\"]"
│ produced an unexpected new value: .devices.hostdevs[0].subsys_pci.source.address.slot: was cty.NumberIntVal(0), but now null.
│
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.
╵
╷
│ Error: Provider produced inconsistent result after apply
│
│ When applying changes to libvirt_domain.test, provider "provider[\"registry.opentofu.org/dmacvicar/libvirt\"]"
│ produced an unexpected new value: .devices.hostdevs[0].subsys_pci.source.address.domain: was cty.NumberIntVal(0), but now null.
│
│ This is a bug in the provider, which should be reported in the provider's own issue tracker.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions