Score:-1

New Ubuntu 20.04 with KVM, guest image not able to start/create with Terraform

jp flag

I have installed Ubuntu 20.04 with KVM and I tried to create Centos 7 guest VM via Terraform.

It is saying "Could not open '/mnt/storage/centos7tes.qcow2': Permission denied", but I tried with root and with user. Also I use Cockpit as GUI.

provider.tf

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

provider "libvirt" {
  ## Configuration options
  #uri = "qemu:///system"
  #alias = "server2"
  uri   = "qemu+ssh://[email protected]/system"
}

main.tf

# Defining VM Volume
resource "libvirt_volume" "centos7-qcow2" {
  name = "centos7tes.qcow2"
  pool = "default"
  #source = "https://cloud.centos.org/centos/7/images/CentOS-7-x86_64-GenericCloud.qcow2"
  source = "./CentOS-7-x86_64-GenericCloud.qcow2"
  format = "qcow2"
}

# get user data info
data "template_file" "user_data" {
  template = "${file("${path.module}/cloud_init.cfg")}"
}

# Use CloudInit to add the instance
resource "libvirt_cloudinit_disk" "commoninit" {
  name = "commoninit.iso"
  pool = "default" # List storage pools using virsh pool-list
  user_data      = "${data.template_file.user_data.rendered}"
}

# Define KVM domain to create
resource "libvirt_domain" "centos7" {
  name   = "centos7"
  memory = "2048"
  vcpu   = 2

  network_interface {
    network_name = "default"
  }

  disk {
    volume_id = "${libvirt_volume.centos7-qcow2.id}"
  }

  cloudinit = "${libvirt_cloudinit_disk.commoninit.id}"

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

  graphics {
    type = "spice"
    listen_type = "address"
    autoport = true
  }
}

# Output Server IP
output "ip" {
  value = "${libvirt_domain.centos7.network_interface.0.addresses.0}"
}
libvirt_volume.centos7-qcow2: Still creating... [40s elapsed]
libvirt_volume.centos7-qcow2: Still creating... [50s elapsed]
libvirt_volume.centos7-qcow2: Still creating... [1m0s elapsed]
libvirt_volume.centos7-qcow2: Still creating... [1m10s elapsed]
libvirt_volume.centos7-qcow2: Creation complete after 1m14s [id=/mnt/storage/centos7tes.qcow2]
libvirt_domain.centos7: Creating...
╷
│ Error: Error creating libvirt domain: internal error: process exited while connecting to monitor: 2021-12-11T23:02:04.400339Z qemu-system-x86_64: -blockdev {"driver":"file","filename":"/mnt/storage/centos7tes.qcow2","node-name":"libvirt-2-storage","auto-read-only":true,"discard":"unmap"}: Could not open '/mnt/storage/centos7tes.qcow2': Permission denied
│ 
│   with libvirt_domain.centos7,
│   on libvirt.tf line 23, in resource "libvirt_domain" "centos7":
│   23: resource "libvirt_domain" "centos7" {
Score:0
at flag

Setting

security_driver = "none"

in /etc/libvirt/qemu.conf and restart libvirtd worked for me to solve a similar problem. (Debian 11, neither AppArmor or SELinux activated / used at all). Error here looked like:

Error: error creating libvirt domain: internal error: process exited while connecting to monitor: ...

and

Could not open '/var/lib/libvirt/images/d1test4-centos7.qcow2': Permission denied

See also https://stackoverflow.com/questions/63984912/coreos-image-fails-to-load-ignition-file-via-libvirt-permission-denied/70563027#70563027

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.