From 22a872d04824ebeeb5cc21dde75fe6d07f1e46ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlo=20Marcelo=20Arenas=20Bel=C3=B3n?= Date: Tue, 12 Apr 2022 12:36:59 -0700 Subject: [PATCH] Add `use_pflash` argument to use the QEMU pflash parameter for loading firmware (#67) Since the merge of #43 any script with a firmware setting, will use an `-drive if=pflash` option instead of `-bios` when calling QEMU, which has been shown to be problematic. Implement a new option `pflash` instead, that could be set to true for when that would be preferred (ex: when using a custom firmware with variables), but use the old interface by default. Fixes: #66 --- builder/qemu/config.go | 14 ++++++++++---- builder/qemu/config.hcl2spec.go | 2 ++ builder/qemu/step_run.go | 7 ++++++- docs-partials/builder/qemu/Config-not-required.mdx | 14 ++++++++++---- 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/builder/qemu/config.go b/builder/qemu/config.go index 144ad86e..a2166a8d 100644 --- a/builder/qemu/config.go +++ b/builder/qemu/config.go @@ -109,12 +109,18 @@ type Config struct { // The number of cpus to use when building the VM. // The default is `1` CPU. CpuCount int `mapstructure:"cpus" required:"false"` - // The firmware file to be used by QEMU, which is to be set by the -bios - // option of QEMU. Particularly, this option can be set to use EFI instead - // of BIOS, by using "OVMF.fd" from OpenFirmware. - // If unset, no -bios option is passed to QEMU, using the default of QEMU. + // The firmware file to be used by QEMU + // this option could be set to use EFI instead of BIOS, + // by using "OVMF.fd" from OpenFirmware, for example. + // If unset, QEMU will load its default firmware. // Also see the QEMU documentation. Firmware string `mapstructure:"firmware" required:"false"` + // If a firmware file option was provided, this option can be + // used to change how qemu will get it. + // If false (the default), then the firmware is provided through + // the -bios option, but if true, a pflash drive will be used + // instead. + PFlash bool `mapstructure:"use_pflash" required:"false"` // The interface to use for the disk. Allowed values include any of `ide`, // `sata`, `scsi`, `virtio` or `virtio-scsi`^\*. Note also that any boot // commands or kickstart type scripts must have proper adjustments for diff --git a/builder/qemu/config.hcl2spec.go b/builder/qemu/config.hcl2spec.go index cd21f122..5bce06d0 100644 --- a/builder/qemu/config.hcl2spec.go +++ b/builder/qemu/config.hcl2spec.go @@ -102,6 +102,7 @@ type FlatConfig struct { AdditionalDiskSize []string `mapstructure:"disk_additional_size" required:"false" cty:"disk_additional_size" hcl:"disk_additional_size"` CpuCount *int `mapstructure:"cpus" required:"false" cty:"cpus" hcl:"cpus"` Firmware *string `mapstructure:"firmware" required:"false" cty:"firmware" hcl:"firmware"` + PFlash *bool `mapstructure:"use_pflash" required:"false" cty:"use_pflash" hcl:"use_pflash"` DiskInterface *string `mapstructure:"disk_interface" required:"false" cty:"disk_interface" hcl:"disk_interface"` DiskSize *string `mapstructure:"disk_size" required:"false" cty:"disk_size" hcl:"disk_size"` SkipResizeDisk *bool `mapstructure:"skip_resize_disk" required:"false" cty:"skip_resize_disk" hcl:"skip_resize_disk"` @@ -239,6 +240,7 @@ func (*FlatConfig) HCL2Spec() map[string]hcldec.Spec { "disk_additional_size": &hcldec.AttrSpec{Name: "disk_additional_size", Type: cty.List(cty.String), Required: false}, "cpus": &hcldec.AttrSpec{Name: "cpus", Type: cty.Number, Required: false}, "firmware": &hcldec.AttrSpec{Name: "firmware", Type: cty.String, Required: false}, + "use_pflash": &hcldec.AttrSpec{Name: "use_pflash", Type: cty.Bool, Required: false}, "disk_interface": &hcldec.AttrSpec{Name: "disk_interface", Type: cty.String, Required: false}, "disk_size": &hcldec.AttrSpec{Name: "disk_size", Type: cty.String, Required: false}, "skip_resize_disk": &hcldec.AttrSpec{Name: "skip_resize_disk", Type: cty.Bool, Required: false}, diff --git a/builder/qemu/step_run.go b/builder/qemu/step_run.go index 0ca37401..f15cff84 100644 --- a/builder/qemu/step_run.go +++ b/builder/qemu/step_run.go @@ -104,6 +104,11 @@ func (s *stepRun) getDefaultArgs(config *Config, state multistep.StateBag) map[s config.MachineType, config.Accelerator) } + // Firmware + if config.Firmware != "" && !config.PFlash { + defaultArgs["-bios"] = config.Firmware + } + // Configure "-netdev" arguments defaultArgs["-netdev"] = fmt.Sprintf("bridge,id=user.0,br=%s", config.NetBridge) if config.NetBridge == "" { @@ -275,7 +280,7 @@ func (s *stepRun) getDeviceAndDriveArgs(config *Config, state multistep.StateBag } } // Firmware - if config.Firmware != "" { + if config.Firmware != "" && config.PFlash { driveArgs = append(driveArgs, fmt.Sprintf("file=%s,if=pflash,format=raw,readonly=on", config.Firmware)) } diff --git a/docs-partials/builder/qemu/Config-not-required.mdx b/docs-partials/builder/qemu/Config-not-required.mdx index b7c6e86a..642a9ee7 100644 --- a/docs-partials/builder/qemu/Config-not-required.mdx +++ b/docs-partials/builder/qemu/Config-not-required.mdx @@ -35,12 +35,18 @@ - `cpus` (int) - The number of cpus to use when building the VM. The default is `1` CPU. -- `firmware` (string) - The firmware file to be used by QEMU, which is to be set by the -bios - option of QEMU. Particularly, this option can be set to use EFI instead - of BIOS, by using "OVMF.fd" from OpenFirmware. - If unset, no -bios option is passed to QEMU, using the default of QEMU. +- `firmware` (string) - The firmware file to be used by QEMU + this option could be set to use EFI instead of BIOS, + by using "OVMF.fd" from OpenFirmware, for example. + If unset, QEMU will load its default firmware. Also see the QEMU documentation. +- `use_pflash` (bool) - If a firmware file option was provided, this option can be + used to change how qemu will get it. + If false (the default), then the firmware is provided through + the -bios option, but if true, a pflash drive will be used + instead. + - `disk_interface` (string) - The interface to use for the disk. Allowed values include any of `ide`, `sata`, `scsi`, `virtio` or `virtio-scsi`^\*. Note also that any boot commands or kickstart type scripts must have proper adjustments for