Skip to content

Commit

Permalink
feat: add uefi and secure boot
Browse files Browse the repository at this point in the history
Adds options to explicitly enable UEFI and Secure Boot (required UEFI).

Ref: #58

Signed-off-by: Ryan Johnson <ryan@tenthirtyam.org>
  • Loading branch information
tenthirtyam committed Jul 1, 2024
1 parent 41d13a1 commit 0d8e813
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .web-docs/components/builder/iso/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,10 @@ provisioner](/packer/docs/provisioner/file).

<!-- Code generated from the comments of the HWConfig struct in builder/vmware/common/hw_config.go; DO NOT EDIT MANUALLY -->

- `firmware` (string) - The firmware type for the virtual machine.
Allowed values are `bios`, `uefi`, and `uefi-secure` (for secure boot).
Defaults to the recommended firmware type for the guest operating system.

- `cpus` (int) - The number of cpus to use when building the VM.

- `memory` (int) - The amount of memory to use when building the VM in megabytes.
Expand Down
21 changes: 21 additions & 0 deletions builder/vmware/common/hw_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,15 @@ import (
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
)

// allowedFirmwareTypes is a list of allowed firmware types for the virtual
// machine.
var allowedFirmwareTypes = []string{"bios", "uefi", "uefi-secure"}

type HWConfig struct {
// The firmware type for the virtual machine.
// Allowed values are `bios`, `uefi`, and `uefi-secure` (for secure boot).
// Defaults to the recommended firmware type for the guest operating system.
Firmware string `mapstructure:"firmware" required:"false"`
// The number of cpus to use when building the VM.
CpuCount int `mapstructure:"cpus" required:"false"`
// The amount of memory to use when building the VM in megabytes.
Expand Down Expand Up @@ -109,6 +117,19 @@ type HWConfig struct {
func (c *HWConfig) Prepare(ctx *interpolate.Context) []error {
var errs []error

if c.Firmware != "" {
isValidFirmware := false
for _, firmware := range allowedFirmwareTypes {
if c.Firmware == firmware {
isValidFirmware = true
break
}
}
if !isValidFirmware {
errs = append(errs, fmt.Errorf("invalid 'firmware' type specified: %s; must be one of %s", c.Firmware, strings.Join(allowedFirmwareTypes, ", ")))
}
}

if c.CpuCount < 0 {
errs = append(errs, fmt.Errorf("invalid number of cpus specified (cpus < 0): %d", c.CpuCount))
}
Expand Down
2 changes: 2 additions & 0 deletions builder/vmware/iso/config.hcl2spec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions builder/vmware/iso/step_create_vmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ type vmxTemplateData struct {
ISOPath string
Version string

Firmware string
SecureBoot string

CpuCount string
MemorySize string

Expand Down Expand Up @@ -263,6 +266,18 @@ func (s *stepCreateVMX) Run(ctx context.Context, state multistep.StateBag) multi
templateData.Serial_Host = ""
templateData.Serial_Auto = "FALSE"

switch config.HWConfig.Firmware {
case "bios":
templateData.Firmware = "bios"
case "uefi":
templateData.Firmware = "efi"
case "uefi-secure":
templateData.Firmware = "efi"
templateData.SecureBoot = "TRUE"
default:
// Nothing to do.
}

// Set the number of cpus if it was specified
if config.HWConfig.CpuCount > 0 {
templateData.CpuCount = strconv.Itoa(config.HWConfig.CpuCount)
Expand Down Expand Up @@ -407,6 +422,10 @@ const DefaultVMXTemplate = `
displayName = "{{ .Name }}"
// Firmware
{{ if .Firmware }}firmware = "{{ .Firmware }}"{{ end }}
{{ if .SecureBoot }}uefi.secureBoot.enabled = "TRUE"{{ end }}
// Hardware
numvcpus = "{{ .CpuCount }}"
memsize = "{{ .MemorySize }}"
Expand Down
4 changes: 4 additions & 0 deletions docs-partials/builder/vmware/common/HWConfig-not-required.mdx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<!-- Code generated from the comments of the HWConfig struct in builder/vmware/common/hw_config.go; DO NOT EDIT MANUALLY -->

- `firmware` (string) - The firmware type for the virtual machine.
Allowed values are `bios`, `uefi`, and `uefi-secure` (for secure boot).
Defaults to the recommended firmware type for the guest operating system.

- `cpus` (int) - The number of cpus to use when building the VM.

- `memory` (int) - The amount of memory to use when building the VM in megabytes.
Expand Down

0 comments on commit 0d8e813

Please sign in to comment.