我已经安装了带有 KVM 的 Ubuntu 20.04,并尝试通过 Terraform 创建 Centos 7 客户虚拟机。
它说“无法打开‘/mnt/storage/centos7tes.qcow2’:权限被拒绝”,但我尝试使用 root 和用户。我还使用 Cockpit 作为 GUI。
提供者.tf
terraform {
required_providers {
libvirt = {
source = "dmacvicar/libvirt"
}
}
}
provider "libvirt" {
## Configuration options
#uri = "qemu:///system"
#alias = "server2"
uri = "qemu+ssh://[email protected]/system"
}
主文件
# 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" {
答案1
环境
security_driver = "none"
在 /etc/libvirt/qemu.conf 中重新启动 libvirtd 对我来说解决了类似的问题。(Debian 11,AppArmor 或 SELinux 均未激活/使用)。此处的错误如下所示:
Error: error creating libvirt domain: internal error: process exited while connecting to monitor: ...
和
Could not open '/var/lib/libvirt/images/d1test4-centos7.qcow2': Permission denied