dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.59k stars 458 forks source link

adding additional disk to existing server #792

Open laimison opened 3 years ago

laimison commented 3 years ago

System Information

Linux distribution

Ubuntu 18.04.4 LTS

Terraform version

Terraform v0.12.29

Provider and libvirt versions

./terraform-provider-libvirt 0.6.2+git.1585292411.8cbe9ad0
Compiled against library: libvirt 4.0.0
Using library: libvirt 4.0.0
Running hypervisor: QEMU 2.11.1
Running against daemon: 4.0.0

main.tf - works fine, applied this and was using VM in prod

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

resource "libvirt_volume" "example-1-volume" {
  name = "example-1-volume.qcow2"
  pool = "default"
  source = "http://cloud-images.ubuntu.com/releases/bionic/release-20200908/ubuntu-18.04-server-cloudimg-amd64.img"
  format = "qcow2"
}

# Define KVM domain to create
resource "libvirt_domain" "example-1" {
  name   = "example-1"
  memory = "10240"
  vcpu   = 4
  autostart = "true"

  disk = [
    {
      volume_id = libvirt_volume.example-1-volume.id
      scsi = "false"
      block_device = ""
      file = ""
      url = ""
      wwn = ""
    }
  ]
  network_interface {
    bridge = "br0"
    mac    = "00:01:00:00:00:01"
  }

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

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

  console {
    type        = "pty"
    target_type = "virtio"
    target_port = "1"
  }

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

  video {
    type = "vga"
  }
}

When I add additional HD data disk:

resource "libvirt_volume" "example-1-volume-hd" {
  name   = "example-1-volume-hd.qcow2"
  pool   = "hd"
  format = "qcow2"
  size   = 1020054732800
}
  disk = [
    {
      volume_id = libvirt_volume.example-1-volume.id
      scsi = "false"
      block_device = ""
      file = ""
      url = ""
      wwn = ""
    },
    {
      volume_id = libvirt_volume.example-1-volume-hd.id
      scsi = "false"
      block_device = ""
      file = ""
      url = ""
      wwn = ""
    }
  ]

terraform plan wants to destroy virtual machine which is not desired behaviour:

  # libvirt_domain.example-1 must be replaced
-/+ resource "libvirt_domain" "example-1" {
      ~ arch        = "x86_64" -> (known after apply)
        autostart   = true
      - cmdline     = [] -> null
      ~ disk        = [ # forces replacement
            {
                block_device = ""
                file         = ""
                scsi         = false
                url          = ""
                volume_id    = "/app/prod/kvm/storage/example-1-volume.qcow2"
                wwn          = ""
            },
          + {
              + block_device = ""
              + file         = ""
              + scsi         = false
              + url          = ""
              + volume_id    = (known after apply)
              + wwn          = ""
            },
        ]

How can I add additional disk to the system which cannot be rebuilt?

Many thanks

MalloZup commented 3 years ago

thx @laimison for issue.

I think we need a way to update the domains disks without forcing its recreation.

laimison commented 3 years ago

For now I have added this block:

# Attach HD
resource "null_resource" "example-mount-hd" {
  provisioner "local-exec" {
    command = "virsh attach-disk server-1 --target vdx --source /apphd/prod/kvm/storage/example-1-volume-hd.qcow2 --persistent --driver qemu --subdriver qcow2 --targetbus virtio"
  }
}

So it confirms that only attachment is missing if that option can be used in provider's logic.

scabala commented 3 weeks ago

Related to #1069