bpg / terraform-provider-proxmox

Terraform Provider for Proxmox
https://registry.terraform.io/providers/bpg/proxmox
Mozilla Public License 2.0
858 stars 137 forks source link

Disk discard parameter doesn't work for cloned VM #360

Open Sad-Soul-Eater opened 1 year ago

Sad-Soul-Eater commented 1 year ago

Describe the bug Discard parameter doesn't enable discard for disk. Issue occurred on VM cloned from template with disabled discard disk parameter. After running terraform apply again, without making any changes in the files, it announces that it will enable discard on disk, but does nothing and even disables the cache for the target disk (the provider does not provide a cache control parameter, but can detect it, but only on the second run...)

To Reproduce

  1. Create a resource with discard = "on" in disk block
  2. Run terraform apply
  3. See discard = "on" in announced actions
  4. After the successful completion - check discard parameter state in proxmox web-ui => it still disabled :c

Minimal Terraform configuration that reproduces the issue.

resource "proxmox_virtual_environment_vm" "vm" {
  name        = "arch-vm"
  node_name   = "homelab-proxmox"

  clone {
    vm_id = 1000
    full = true
  }

  operating_system {
    type = "l26"
  }

  bios = "ovmf"
  agent {
    enabled = true
    trim    = true
  }
  machine = "q35"

  scsi_hardware = "virtio-scsi-single"
  disk {
    datastore_id = "rust-zfs"
    file_format  = "raw"
    interface    = "scsi0"
    discard      = "on"
    iothread     = true
    size         = 30
  }

  cpu {
    cores = 4
    type  = "host"
  }

  memory {
    dedicated = 4096
  }

  initialization {
    datastore_id = "rust-zfs"
    user_account {
      username = "arch"
      password = "i-use-arch-btw"
    }

    ip_config {
      ipv4 {
        address = "192.168.1.30/24"
        gateway = "192.168.1.1"
      }
    }
  }
}

Expected behavior Discard parameter works

Screenshots

Template disk parameters:

image

VM disk parameters after 1st apply:

image

VM disk parameters after 2nd apply:

image

Additional context PVE: 7.4-3 provider: 0.21.0 terraform: v1.4.6 OS: ArchLinux

1st debug apply logs 2nd debug apply log

GeorgeGedox commented 1 year ago

I'm having the same issue at the moment with a vm cloned from a template, after terraform apply everything seems fine except the disk settings for SSD Emulation and Discard. If I rerun the terraform apply command again I always get this

 ~ disk {
          + discard      = "on"
          ~ ssd          = false -> true
            # (5 unchanged attributes hidden)
        }

The storage is a local nvme ssd so it's not something exotic like ceph, nfs, iscsi, etc.

bpg commented 1 year ago

Hi @Sad-Soul-Eater! 👋🏼 Thanks for the detailed report and logs! And thanks @GeorgeGedox for confirming 👍🏼

I'll take a look at the earliest opportunity.

bpg commented 1 year ago

Working with clones is a bit of a mess in the provider. In fact, no disk attributes can be updated other than the size of the disk or its storage. The other attribute values are ignored on apply, but kept in the TF state, so terraform then starts complaining about the mismatch on re-apply. Another problem is the defaults that we have for disk attributes, like file type (which is qcow2 if not set) et. al. Currently all of this is simply ignored, but when we start applying attributes from a cloned VM those default may be incompatible like qcow2 for a block storage, or be simply wrong in the context of clone.

That's all legacy code, it will take some time to untangle and fix all of this...

RomLecat commented 9 months ago

Hello,

It evens happen if the template and the VM have discard set to "on" but when the VM node is not the same as the template's one. Subsequent terraform apply does not help either, it keeps updating but discard stays off. Tried both values true and "on", without any change.

Thanks

RomLecat commented 9 months ago

Actually keeping it on the same node also sets discard back to ignore. As far as I can tell, there's no way to turn it on when cloning from a template via this provider :(