Terraform脚本中的boot_devices无法正常工作

编程语言 2026-07-10

我正在使用下面的代码片段,在libvirt上创建一个简单的虚拟机。

libvirt = {
      source  = "dmacvicar/libvirt"
      version = "~> 0.9.7"
}

.....

resource "libvirt_domain" "vm" {
  name   = local.config.name
  memory = local.config.memory
  memory_unit   = "MiB"
  vcpu   = local.config.vcpu
  type   = "kvm"

  os = {
    type         = "hvm"
    type_arch    = "x86_64"
    type_machine = "q35"
    # boot_devices = ["hd"]
  }

  cpu = {
    mode = "host-passthrough"
  }

  devices = {
    disks = [
      # Main system disk sda
      {
        source = {
          volume = {
            pool   = libvirt_volume.sda.pool
            volume = libvirt_volume.sda.name
          }
        }
        target = {
          bus = "sata"
          dev = "sda"
        }
        driver = {
          type = "qcow2"
        }
      } ,
      # CDROM Drive
      {
        device = "cdrom"
        source = {
          volume = {
            pool   = libvirt_volume.boot_iso.pool
            volume = libvirt_volume.boot_iso.name
          }
        }
        target = {
          bus = "sata"
          dev = "sdb"
        }
      }
    ]
    graphics = [
      {
        spice = {
          auto_port = true
        }
      }
    ]
  }
}

在使用 boot_devices = ["hd", "cdrom"] 时,我遇到了错误 Inappropriate value for attribute "os": attribute "boot_devices": element 0: object required, but have string

示例和文档都让我使用一个字符串。但我卡在这里。

解决方案

我运行 terraform providers schema -json,以便直接从提供者获取JSON格式的模式。boot_devices 属性的定义如下:

                    "boot_devices": {
                      "nested_type": {
                        "attributes": {
                          "dev": {
                            "type": "string",
                            "description": "Defines a single boot target device type in the boot order; valid values include \"hd\", \"cdrom\", \"fd\", and \"network\" (and any additional values supported by the hypervisor). Example: \"hd\".\n\nSee: <https://libvirt.org/formatdomain.html#bios-bootloader>",
                            "description_kind": "markdown",
                            "required": true
                          }
                        },
                        "nesting_mode": "list"
                      },
                      "description": "Specifies an ordered list of devices the firmware should try when booting the guest OS. Multiple entries are allowed to define a boot priority list.\n\nSee: <https://libvirt.org/formatdomain.html#bios-bootloader>",
                      "description_kind": "markdown",
                      "optional": true
                    },

这个模式描述的是一个对象列表,每个对象都具有一个必需属性 dev,因此我认为你需要把配置写成如下形式:

  os = {
    type         = "hvm"
    type_arch    = "x86_64"
    type_machine = "q35"
    boot_devices = [
      { dev = "hd" },
    ]
  }

从该提供者的源代码来看,它的模式似乎是从libvirt内部使用的XML结构派生的,因此我认为 boot_devices 参数相当于XML中的 <boot dev="hd" />,因此存在额外的一层对象,以防未来这个 boot 元素开始接受其他属性,但这只是基于上下文的猜测。

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章