dmacvicar / terraform-provider-libvirt

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

Resizing cloud image to +2GB fails #946

Open cwaazywabbit opened 2 years ago

cwaazywabbit commented 2 years ago

System Information

Linux distribution

Debian

Terraform version

Terraform v1.1.7

Provider and libvirt versions

provider registry.terraform.io/dmacvicar/libvirt v0.6.14
libvirtd (libvirt) 8.0.0

Checklist

Description of Issue/Question

Setup

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

provider "libvirt" {
  uri = "qemu+ssh://${var.kvm.username}@${var.kvm.hostname}/system?sshauth=privkey&keyfile=${format("%s%s", "${path.module}/keys/", var.kvm.private_key)}"
}

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

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

resource "libvirt_volume" "ubuntu-20_04" {
  name = "ubuntu-20-04.qcow2"
  pool = "default" # List storage pools using virsh pool-list
  #source = "https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.img"
  source = "./ubuntu-20.04-server-cloudimg-amd64.img"
  format = "qcow2"
}

resource "libvirt_volume" "ubuntu-20_04-resized" {
  name           = "ubuntu-20-04-resized.qcow2"
  pool           = "default" # List storage pools using virsh pool-list
  base_volume_id = libvirt_volume.ubuntu-20_04.id
  size           = 21474836480
}

resource "libvirt_network" "redteam_int" {
  name      = "redteam_intern"
  mode      = "nat"
  addresses = ["192.168.50.0/24", "2001:db8:ca2:2::/64"]
  dns {
    enabled    = true
    local_only = true
  }
  dhcp {
    enabled = true
  }
  autostart = true
}

resource "libvirt_volume" "cobaltstrike" {
  name           = "cobaltstrike-${count.index}.qcow2"
  base_volume_id = libvirt_volume.ubuntu-20_04-resized.id
  count          = 2
}

resource "libvirt_domain" "cobaltstrike-longhaul" {
  name        = "cobaltstrike-longhaul"
  memory      = "2048"
  vcpu        = 2
  qemu_agent  = true

  cloudinit = libvirt_cloudinit_disk.commoninit.id

  network_interface {
    macvtap = "bond0.20"
  }

  network_interface {
    network_id     = libvirt_network.redteam_int.id
    wait_for_lease = true
  }

  disk {
    volume_id = element(libvirt_volume.cobaltstrike.*.id, 0)
  }

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

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

Steps to Reproduce Issue

Any value for disk libvirt_volume.ubuntu-20_04-resized.size that is larger than 1.99GB=2147483647 bytes (integer max size) throws the following exemplary error:

Error: size: must be a whole number, got 2.147483648e+10

The error is gone when reducing to 2147483647 and lower. I believe it's related to this line: https://github.com/dmacvicar/terraform-provider-libvirt/blob/main/libvirt/resource_libvirt_volume.go#L34


Additional information:

Do you have SELinux or Apparmor/Firewall enabled? Some special configuration? no

rgl commented 2 years ago

~Using a 32-bit number for the volume size (go int type) was rather unfortunate. This is also preventing me for using this variable at all, which makes this whole thing quite useless.~

~Are you guys open to switching it to a int64 in PR? Any objections for that? Or change the unit to Megabytes (this would be a breaking change I guess)?~

~PS This seems to be a terraform rabbit hole. There is a Float number, but I'm starting to think that we should instead switch to a String and convert it internally to the underline uint64 type. And I also think we should let the user specify the unit, e.g. '60g' for 60 Gigabytes. What do you think @dmacvicar?~

PS Scrape all my comment... at https://github.com/dmacvicar/terraform-provider-libvirt/blob/68c9bb05fd229efd70db0fe8cd0c1faef9c6e64f/libvirt/resource_libvirt_volume.go#L166-L169 its actually using a platform-dependent int. With 64-bit Go, this is actually a 64-bit number, so this is no longer problem. Sorry for the noise.

SchoolGuy commented 1 year ago

I was able with the trick in the description of the issue to increase an image to a size bigger than an int. I see this issue as fixed.

scabala commented 2 months ago

Hi @SchoolGuy, could you elaborate a bit?