Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    raspbeguy
    @raspbeguy
    provider "libvirt" {
    alias = "garnet"
    uri = "qemu+tls://garnet/system"
    }
    Then I define a ressource :
    resource "libvirt_domain" "blabla" {
    provider = libvirt.garnet
    name = "blabla"
    }
    Then when I do terraform plan the command freezes
    this does not happen when I use a local libvirt ibstab
    instance*
    I can connect to my host using virsh -c qemu+tls://garnet/system
    raspbeguy
    @raspbeguy
    When I stop terraform with Ctrl+C twice, I have this in garnet logs :
    Cannot recv data: Input/output error
    ```
    Also I tried to simply use SSH, but I had an error saying there is a fingerprint mismatch in knownhosts even though there isn't and I can use SSH without problem
    raspbeguy
    @raspbeguy
    Test from IRC
    Much better
    Yassine Elassad
    @atlasloewenherz_twitter
    good evening everyone
    anyone of you guys specifiying the remote ssh port as this: uri = "qemu+ssh://yelassad@192.168.178.122:2222/system"
    Walter Vargas
    @waltervargas
    hi, I am facing:
    Error: Failed to query available provider packages
    
    Could not retrieve the list of available versions for provider
    hashicorp/libvirt: provider registry registry.terraform.io does not have a
    provider named registry.terraform.io/hashicorp/libvirt
    → terraform version 
    Terraform v0.14.9
    
    Your version of Terraform is out of date! The latest version
    is 1.1.5. You can update by downloading from https://www.terraform.io/downloads.html
    for one directory, but not for the other
    Walter Vargas
    @waltervargas
    ok, the difference is that I am using a module on dirA and plain code on dirB
    now I am not able to pull the recent version 0.6.14, only able to pull 0.6.3
    bitblt
    @bitblt:matrix.org
    [m]
    Hello there

    I found an issue with the provider: After creating rebooting the host (hypervisor) and running terraform apply again I get:

    Error: error creating libvirt domain: Requested operation is not valid: network 'network' is not active

    Any ideas?

    Loester Franco
    @LoesterFranco
    hi
    Error: error creating libvirt domain: internal error: process exited while connecting to monitor: 2022-04-16T01:23:25.176982Z qemu-system-x86_64: -blockdev {"driver":"file","filename":"/var/lib/libvirt/images/focal-server-cloudimg-amd64-disk-kvm.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/var/lib/libvirt/images/focal-server-cloudimg-amd64-disk-kvm.qcow2': Permission denied
    terraform apply

    sudo rm /etc/libvirt/qemu.conf
    sudo echo -e "#user = \"qemu\" # A user named \"qemu\"

    user = \"+0\" # Super user (uid=0)

    user = \"100\" # A user named \"100\" or a user with uid=100

    user = \"root\"
    group = \"libvirt\"" >> /etc/libvirt/qemu.conf
    sudo systemctl restart libvirtd
    sudo systemctl status libvirtd

    sudo usermod -a -G libvirt $(whoami)
    sudo usermod -a -G libvirt loester
    sudo usermod -a -G libvirt root

    sudo chmod -R 0777 /var/run/libvirt/libvirt-sock

    ccat /etc/libvirt/qemu.conf

    user = "qemu" # A user named "qemu"

    user = "+0" # Super user (uid=0)

    user = "100" # A user named "100" or a user with uid=100

    user = "root"
    group = "libvirt"

    Andreas Trawoeger
    @atrawog
    @LoesterFranco Have tried a ls -lah /var/lib/libvirt/images/focal-server-cloudimg-amd64-disk-kvm.qcow2 with the user your running Terraform?
    Andreas Trawoeger
    @atrawog
    dmacvicar/libvirt uses the libvirtd daemon to create a new volume, but the image source itself is read directly from the filesystem and you need the proper read permissions for it.
    driftavalii
    @driftavalii
    Is it possible to use the provider in conjunction with libvirt hooks? Can I get help looking at why my configuration below fails?
    testmachine@infrahost:/etc/libvirt/hooks$ cat qemu 
    #!/bin/bash
    
    # Create network port in openvswitch
    if [ "${2}" = "prepare" ]; then
      echo "Creating port in Openvswitch bridge" | tee -a /home/testmachine/triggered.txt
      vm=$1
      portNumber=$(cut -d "_" -f2 <<< "$vm")
      divider="_"
      vmName=${vm%$divider*}
      ovs-vsctl add-port br0 $vmName
      ovs-vsctl set port $vmName tag=$portNumber
    fi
    
    # Delete network port in openvswitch bridge 
    if ["${2}" = "release" ]; then
      echo "Cleaning up port in Openvswitch bridge" | tee -a /home/testmachine/triggered.txt
      vm=$1
      portNumber=$(cut -d "_" -f2 <<< "$vm")
      divider="_"
      vmName=${vm%divider*}
      ovs-vsctl del-port br0 $vmName
    fi
    testmachine@infrahost:~/project/terraform/generic_vm$ cat ovs_bridge_br0.xsl 
    <?xml version="1.0" encoding="UTF-8" ?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" omit-xml-declaration="yes" indent="yes"/>
      <xsl:template match="node()|@*">
        <xsl:copy>
          <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="/domain/devices/interface[@type='bridge']/target/@dev">
        <xsl:attribute name="dev">
          <xsl:value-of select="'gene01'"/>
        </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="/domain/devices/interface[@type='bridge']/model/@type">
        <xsl:attribute name="type">
          <xsl:value-of select="'virtio'"/>
        </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="/domain/devices/interface[@type='bridge']/source/@bridge">
        <xsl:attribute name="bridge">
          <xsl:value-of select="'br0'"/>
        </xsl:attribute>
      </xsl:template>
    
      <xsl:template match="/domain/devices/interface[@type='bridge']/source/@bridge">
        <xsl:attribute name="bridge">
        <xsl:copy-of select="."/>
          <virtualport type="openvswitch">
          </virtualport>
        </xsl:attribute>
      </xsl:template>
    </xsl:stylesheet>
    testmachine@infrahost:~/project/terraform/generic_vm$ cat main.tf 
    terraform {
      required_providers {
        libvirt = {
          source = "dmacvicar/libvirt"
        }
      }
    }
    
    provider "libvirt" {
      uri = "qemu:///system"
    }
    
    resource "libvirt_domain" "terraform_test" {
      name = "gene01_16"
      memory = "512"
      vcpu = 1
      cloudinit = libvirt_cloudinit_disk.commoninit.id
      cpu {
        mode = "host-passthrough"
      }
    
      network_interface {
        bridge = "br0"
      }
    
      xml {
        xslt = file("ovs_bridge_br0.xsl")
      }
    }
    
    resource "libvirt_volume" "ubuntu_1804" {
      name = "generic_vm_bionic"
      source = "./bionic.qcow2"
    }
    
    resource "libvirt_volume" "main" {
      name = "main.qcow2"
      base_volume_id = libvirt_volume.ubuntu_1804.id
    }
    
    resource "libvirt_cloudinit_disk" "commoninit" {
      name = "commoninit.iso"
      user_data = data.template_file.user_data.rendered
      meta_data = data.template_file.meta_data.rendered
      network_config = data.template_file.network_config.rendered
    }
    
    data "template_file" "user_data" {
      template = file("${path.module}/user-data")
    }
    
    data "template_file" "meta_data" {
      template = file("${path.module}/meta-data")
    }
    
    data "template_file" "network_config" {
      template = file("${path.module}/network-config")
    }
      Enter a value: yes
    
    libvirt_volume.ubuntu_1804: Creating...
    libvirt_cloudinit_disk.commoninit: Creating...
    libvirt_volume.ubuntu_1804: Creation complete after 3s [id=/mnt/sdadisk/default/generic_vm_bionic]
    libvirt_volume.main: Creating...
    libvirt_cloudinit_disk.commoninit: Creation complete after 3s [id=/mnt/sdadisk/default/commoninit.iso;a7d20423-ecfd-4544-8aba-f118c33a9cdd]
    libvirt_domain.terraform_test: Creating...
    libvirt_volume.main: Creation complete after 0s [id=/mnt/sdadisk/default/main.qcow2]
    ╷
    │ Error: error creating libvirt domain: Cannot get interface MTU on '': No such device
    │ 
    │   with libvirt_domain.terraform_test,
    │   on main.tf line 13, in resource "libvirt_domain" "terraform_test":
    │   13: resource "libvirt_domain" "terraform_test" {
    │ 
    ╵
    I have created a libvirt network using virsh net-define libvirt_network.xml with the configuration
    <network>
      <name>ovs_br0</name>
      <forward mode='bridge'/>
      <bridge name='br0'/>
      <virtualport type='openvswitch'/>
    </network>
    Andreas Trawoeger
    @atrawog
    I'm not using libvirt hooks. But what works for me is to create a bridge called brnat with Ansible/Networkmanager and then define a libvirt_network in terraform using it:
    resource "libvirt_network" "brnat" {
      name      = "brnat"
      bridge    = "brnat"
      mode      = "nat"
      autostart = "true"
      addresses = ["100.100.0.0/24"]
      dhcp {
          enabled = true
      }
      dns {
        enabled = true
      }
    }
    And then use network_name instead of bridge in my network_interface config
      network_interface {
        network_name = "brnat"
        addresses      = ["100.100.0.2"]
        wait_for_lease = true
      }
    driftavalii
    @driftavalii
    Is the bride a Linux bridge or ovs? I am trying to use ovs
    Andreas Trawoeger
    @atrawog
    It's a Linux bridge, but it should work with ovs too. The bridge just has to be configured before your running terraform.
    driftavalii
    @driftavalii
    yep, works, thanks @atrawog
    1 reply
    Rob Mengert
    @TheFutonEng
    I am trying to simple execute the ubuntu example out of the repo with minor modifications.
    terraform {
     required_version = ">= 0.13"
      required_providers {
        libvirt = {
          source  = "dmacvicar/libvirt"
          version = "0.6.14"
        }
      }
    }
    
    # instance the provider
    provider "libvirt" {
      uri = "qemu:///system"
    }
    
    resource "libvirt_pool" "ubuntu" {
      name = "ubuntu"
      type = "dir"
      path = "/home/user1/terraform-provider-libvirt-pool-ubuntu"
    }
    
    # We fetch the latest ubuntu release image from their mirrors
    resource "libvirt_volume" "ubuntu-qcow2" {
      name   = "ubuntu-qcow2"
      pool   = libvirt_pool.ubuntu.name
      source = "/home/user1/ubuntu-20.04-server-cloudimg-amd64.img"
      format = "qcow2"
    }
    
    data "template_file" "user_data" {
      template = file("${path.module}/cloud_init.cfg")
    }
    
    data "template_file" "network_config" {
      template = file("${path.module}/network_config.cfg")
    }
    
    # for more info about paramater check this out
    # https://github.com/dmacvicar/terraform-provider-libvirt/blob/master/website/docs/r/cloudinit.html.markdown
    # Use CloudInit to add our ssh-key to the instance
    # you can add also meta_data field
    resource "libvirt_cloudinit_disk" "commoninit" {
      name           = "commoninit.iso"
      user_data      = data.template_file.user_data.rendered
      network_config = data.template_file.network_config.rendered
      pool           = libvirt_pool.ubuntu.name
    }
    
    # Create the machine
    resource "libvirt_domain" "domain-ubuntu" {
      name   = "ubuntu-terraform"
      memory = "512"
      vcpu   = 1
    
      cloudinit = libvirt_cloudinit_disk.commoninit.id
    
      network_interface {
        network_name = "default"
      }
    
      # IMPORTANT: this is a known bug on cloud images, since they expect a console
      # we need to pass it
      # https://bugs.launchpad.net/cloud-images/+bug/1573095
      console {
        type        = "pty"
        target_port = "0"
        target_type = "serial"
      }
    
      console {
        type        = "pty"
        target_type = "virtio"
        target_port = "1"
      }
    
      disk {
        volume_id = libvirt_volume.ubuntu-qcow2.id
      }
    
      graphics {
        type        = "spice"
        listen_type = "address"
        autoport    = true
      }
    }
    
    # IPs: use wait_for_lease true or after creation use terraform refresh and terraform show for the ips of domain
    When attempting to apply this configuration, a permission error is thrown for the qcow2 file:
    Error: error creating libvirt domain: internal error: qemu unexpectedly closed the monitor: 2022-07-04T13:33:57.798517Z qemu-system-x86_64: -blockdev {"driver":"file","filename":"/home/user1/terraform-provider-libvirt-pool-ubuntu/ubuntu-qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/home/user1/terraform-provider-libvirt-pool-ubuntu/ubuntu-qcow2': Permission denied
    │ 
    │   with libvirt_domain.domain-ubuntu,
    │   on ubuntu-example.tf line 52, in resource "libvirt_domain" "domain-ubuntu":
    │   52: resource "libvirt_domain" "domain-ubuntu" {
    │
    Rob Mengert
    @TheFutonEng
    The source qcow2 file has permissions set which should allow the user running the terraform command to read the file:
    [user1@oakridge:~]
    $ pwd ; la -la ubuntu-20.04-server-cloudimg-amd64.img 
    /home/user1
    -rw-rw-r-- 1 user1 user1 593166664 Apr 16 19:30 ubuntu-20.04-server-cloudimg-amd64.img
    [user1@oakridge:~]
    $
    The target does indeed get transformed to a file owned by root:root
    [user1@oakridge:~]
    $ ls -la terraform-provider-libvirt-pool-ubuntu/
    total 579648
    drwxrwxr-x  2 user1    user1      4096 Jul  4 07:33 .
    drwxr-xr-x 55 user1     user1      4096 Jul  4 07:29 ..
    -rw-r--r--  1 libvirt-qemu kvm         374784 Jul  4 07:33 commoninit.iso
    -rw-r--r--  1 root         root     593166664 Jul  4 07:33 ubuntu-qcow2
    [user1@oakridge:~]
    $
    Rob Mengert
    @TheFutonEng
    Running the terraform command as root results in the exact same error:
    [root@oakridge:~/projects/terraform-provider-libvirt/examples/v0.13/ubuntu]
    $ terraform apply
    data.template_file.network_config: Reading...
    data.template_file.user_data: Reading...
    data.template_file.network_config: Read complete after 0s [id=b36a1372ce4ea68b514354202c26c0365df9a17f25cd5acdeeaea525cd913
    
    <<OMITTED>>
    
    ╷
    │ Error: error creating libvirt domain: internal error: qemu unexpectedly closed the monitor: 2022-07-04T13:56:55.125878Z qemu-system-x86_64: -blockdev {"driver":"file","filename":"/home/rmengert/terraform-provider-libvirt-pool-ubuntu/ubuntu-qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/home/rmengert/terraform-provider-libvirt-pool-ubuntu/ubuntu-qcow2': Permission denied
    │ 
    │   with libvirt_domain.domain-ubuntu,
    │   on ubuntu-example.tf line 52, in resource "libvirt_domain" "domain-ubuntu":52: resource "libvirt_domain" "domain-ubuntu" {
    │ 
    ╵
    [root@oakridge:~/projects/terraform-provider-libvirt/examples/v0.13/ubuntu]
    $
    Thanks in advance for any assistance!
    Rob Mengert
    @TheFutonEng
    Did some more digging, disabled apparmor and set the security_driver to "none" in /etc/libvirt/qemu.conf and now tf apply works.
    Marcelo Galas
    @peanutsguy
    Hi! I'm trying to build a Coder Workspace Template using this library but I keep getting the following error (which I understand is generated by the library)
    Error: failed to dial libvirt: could not configure SSH authentication methods
    I've used the (almost) same script directly from Terraform, with success, which would indicate that it works

    The line that I think is causing the error is this one

     provider "libvirt" {
      uri = "qemu+ssh://[user]@[ip]:[port]/system"
    }

    Obviously, I've removed the user,ip and port for the purpose of posting here

    Any help would be greatly appreciated!
    P.S. Here's the full script
    terraform {
      required_providers {
        coder = {
          source = "coder/coder"
        }
        libvirt = {
          source = "dmacvicar/libvirt"
          version = "0.6.14"
        }
        macaddress = {
          source = "ivoronin/macaddress"
          version = "0.3.2"
        }
      }
    }
    
    provider "macaddress" {
    }
    
    provider "libvirt" {
      uri = "qemu+ssh://[HOST USER - REDACTED]@[IP - REDACTED]:[PORT - REDACTED]/system"
    }
    
    data "coder_workspace" "me" {
    }
    
    variable "sizeninGB" {
      description = "Size of VM disk in GB"
      default = 10
      validation {
        condition     = var.sizeninGB >= 5
        error_message = "Value must be greater than or equal to 5."
      }
    }
    
    variable "vcpu" {
      description = "Amount of vCPUs required"
      default = 4
      validation {
        condition     = var.vcpu >= 1
        error_message = "Value must be greater than or equal to 1."
      }
    }
    
    variable "ram" {
      description = "Amount of RAM required in MB"
      default = 1024
      validation {
        condition     = var.ram >= 512
        error_message = "Value must be greater than or equal to 512."
      }
    }
    
    locals {
      prefix = "coder-${data.coder_workspace.me.owner}-${data.coder_workspace.me.name}"
      userdata = templatefile("cloud-config.yaml.tftpl", {
        username    = "coder" # Ensure this user/group does not exist in your VM image
        init_script = base64encode(coder_agent.main.init_script)
        hostname    = lower(data.coder_workspace.me.name)
        ssh_key     = "[SSH KEY - REDACTED]"
        pswd        = "[PASSWORD - REDACTED]"
      })
    }
    
    resource "coder_agent" "main" {
      os   = "linux"
      arch = "amd64"
      auth = "token"
      env = {
        GIT_AUTHOR_NAME = "${data.coder_workspace.me.owner}"
        GIT_COMMITTER_NAME = "${data.coder_workspace.me.owner}"
        GIT_AUTHOR_EMAIL = "${data.coder_workspace.me.owner_email}"
        GIT_COMMITTER_EMAIL = "${data.coder_workspace.me.owner_email}"
      }
    }
    
    resource "macaddress" "main" {
        prefix = [82,84,0]
    }
    
    resource "libvirt_volume" "ubuntu_cloud" {
      name = "ubuntu_cloud"
      source = "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
    }
    
    resource "libvirt_cloudinit_disk" "ubuntu" {
      name           = "${local.prefix}-ubuntu_22.04.iso"
      user_data      = base64encode(local.userdata)
    }
    
    resource "libvirt_volume" "osdisk" {
      name = "${local.prefix}-osdisk.img"
      base_volume_id = libvirt_volume.ubuntu_cloud.id
      size = var.sizeninGB*1024*1024*1024
    }
    
    resource "libvirt_domain" "main" {
      count = data.coder_workspace.me.transition == "start" ? 1 : 0
      name = "${local.prefix}-vm"
      cpu {
        mode = "host-passthrough"
      }
      vcpu = var.vcpu
      memory = var.ram
      cloudinit = libvirt_cloudinit_disk.ubuntu.id
    
      network_interface {
        bridge = "br0"
        mac = ${macaddress.main.address}
      }
    
      console {
        type        = "pty"
        target_port = "0"
        target_type = "serial"
      }
    
      disk {
        volume_id = libvirt_volume.osdisk.id
      }
    }
    Ananth Balasubramanian
    @linuxananth1976
    Hello anybody have idea on the below?
    secondary n/w not up in terraform libvirt kvm. It'll be useful whoever face the same and fix it means. Please
    Ananth Balasubramanian
    @linuxananth1976
    Arezki DJAOUDI
    @dj-arezki
    I need help I would like to use libvirt hypervisor install in a centos-stream virtual machine in the proxmox hypervisor using terraform how can I declare the provider in terraform and the url thank you