Orly Orly
Orly Orly

Reputation: 367

how to install Qemu guest agent on the domain - for KVM network

I am trying to deploy VM via terraform on KVM.

I want my VM to get an IP in the Host network, my host is 10.100.86.180. so I am using Bridge (which works well when I deploy VM manually)

but with terraform- it can't get an IP after "terraform apply",

what am I doing wrong?

here is my main.tf :

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

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

resource "libvirt_volume" "centos7-qcow2" {
    name   = "centos7.qcow2"
    pool   = "default"
    source = "http:///14.7.1/output/KVMdisk1.qcow2"
    format = "qcow2"

}

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

resource "libvirt_cloudinit_disk" "commoninit" {
  name         = "commoninit.iso"
  user_data    = "${data.template_file.user_data.rendered}"
}

resource "libvirt_network" "my_network" {
  name = "default"
  mode = "bridge"
  addresses = ["10.100.86.0/24"]
  bridge = "br0"
  dhcp {
     enabled = true
       }
}


resource "libvirt_domain" "gw" {
  name   = "gw"
  memory = "8192"
  vcpu   = 4
  
  qemu_agent = true
  
  network_interface {
   # network_id     = libvirt_network.my_network.id
   addresses = ["10.100.86.5"]
   bridge = "br0"   
   wait_for_lease = true
  }

  boot_device {
   dev = [ "hd", "network"]
  }

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

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

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

output "ips" {
  value = libvirt_domain.gw.*.network_interface.0.addresses
} 

it throws this error:

╵
╷
│ Error: Error: couldn't retrieve IP address of domain id: c49d77eb-62c4-4532-93c2-7d3f351b26e7. Please check following:
│ 1) is the domain running proplerly?
│ 2) has the network interface an IP address?
│ 3) Networking issues on your libvirt setup?
│  4) is DHCP enabled on this Domain's network?
│ 5) if you use bridge network, the domain should have the pkg qemu-agent installed
│ IMPORTANT: This error is not a terraform libvirt-provider error, but an error caused by your KVM/libvirt infrastructure configuration/setup
│  timeout while waiting for state to become 'all-addresses-obtained' (last state: 'waiting-addresses', timeout: 5m0s)
│
│   with libvirt_domain.gw,
│   on main.tf line 41, in resource "libvirt_domain" "gw":
│   41: resource "libvirt_domain" "gw" {

I am working with Bridge - I found that the Qemu guest agent must be installed and running inside of the domain

in order to discover the IP addresses of all the network interfaces attached to a LAN.

how can I install the Qemu guest agent on the domain?

I have already install it on my Host, is it enough?

How can I ensure it is working properly?

Upvotes: 2

Views: 1539

Answers (1)

etutuit
etutuit

Reputation: 26

how can I install the Qemu guest agent on the domain?

sudo yum install qemu-guest-agent
sudo systemctl enable qemu-guest-agent --now

I have already install it on my Host, is it enough?

It is not, qemu-guest-agent has to be installed on guest vm.

How can I ensure it is working properly?

For example you could check it with:

sudo systemctl status qemu-guest-agent

I personally was not able to make

wait_for_lease = true 

work on bridged networks. In my case it works only in libvirt networks. It may be necessary to configure bridging:

modprobe bridge
modprobe br_netfilter
cat >> /etc/sysctl.conf << EOF
net.ipv4.ip_forward = 1
net.ipv6.conf.all.forwarding = 1
net.bridge.bridge-nf-call-ip6tables = 0
net.bridge.bridge-nf-call-iptables = 0
net.bridge.bridge-nf-call-arptables = 0
EOF
sysctl -p /etc/sysctl.conf

If you want run your machines via Terraform and with bridged network just delete:

wait_for_lease = true

Also you define network connected to bridge and then you don't use it but you connect interface directly to the bridge. If you want to connect interface directly to the bridge delete the network definition. In case you want to have network defined, use it.

Upvotes: 1

Related Questions