2020-01-07 16:59:31 -08:00
//go:generate struct-markdown
//go:generate mapstructure-to-hcl2 -type HardwareConfig
2018-05-06 00:41:14 +03:00
package common
import (
"context"
"fmt"
2020-02-14 11:42:29 -05:00
2020-01-07 16:59:31 -08:00
"github.com/hashicorp/packer/builder/vsphere/driver"
2018-11-01 00:42:24 +03:00
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
2018-05-06 00:41:14 +03:00
)
type HardwareConfig struct {
2020-01-07 16:59:31 -08:00
// Number of CPU sockets.
CPUs int32 `mapstructure:"CPUs"`
// Number of CPU cores per socket.
CpuCores int32 `mapstructure:"cpu_cores"`
// Amount of reserved CPU resources in MHz.
CPUReservation int64 `mapstructure:"CPU_reservation"`
// Upper limit of available CPU resources in MHz.
CPULimit int64 `mapstructure:"CPU_limit"`
// Enable CPU hot plug setting for virtual machine. Defaults to `false`.
CpuHotAddEnabled bool `mapstructure:"CPU_hot_plug"`
// Amount of RAM in MB.
RAM int64 `mapstructure:"RAM"`
// Amount of reserved RAM in MB.
RAMReservation int64 `mapstructure:"RAM_reservation"`
// Reserve all available RAM. Defaults to `false`. Cannot be used together
// with `RAM_reservation`.
RAMReserveAll bool `mapstructure:"RAM_reserve_all"`
// Enable RAM hot plug setting for virtual machine. Defaults to `false`.
MemoryHotAddEnabled bool `mapstructure:"RAM_hot_plug"`
2020-05-06 09:39:41 -07:00
// Amount of video memory in KB.
2018-12-17 15:49:04 +03:00
VideoRAM int64 `mapstructure:"video_ram"`
2020-03-24 15:28:04 +01:00
// vGPU profile for accelerated graphics. See [NVIDIA GRID vGPU documentation](https://docs.nvidia.com/grid/latest/grid-vgpu-user-guide/index.html#configure-vmware-vsphere-vm-with-vgpu)
// for examples of profile names. Defaults to none.
VGPUProfile string `mapstructure:"vgpu_profile"`
2020-01-07 16:59:31 -08:00
// Enable nested hardware virtualization for VM. Defaults to `false`.
NestedHV bool `mapstructure:"NestedHV"`
2020-04-07 22:45:03 +03:00
// Set the Firmware for virtual machine. Supported values: `bios`, `efi`, `efi-secure` or empty string to keep as in template. Defaults to empty string.
Firmware string `mapstructure:"firmware"`
2018-05-06 00:41:14 +03:00
}
func ( c * HardwareConfig ) Prepare () [] error {
var errs [] error
if c . RAMReservation > 0 && c . RAMReserveAll != false {
errs = append ( errs , fmt . Errorf ( "'RAM_reservation' and 'RAM_reserve_all' cannot be used together" ))
}
2020-04-07 22:45:03 +03:00
if c . Firmware != "" && c . Firmware != "bios" && c . Firmware != "efi" && c . Firmware != "efi-secure" {
errs = append ( errs , fmt . Errorf ( "'firmware' must be '', 'bios', 'efi' or 'efi-secure'" ))
}
2018-05-06 00:41:14 +03:00
return errs
}
type StepConfigureHardware struct {
Config * HardwareConfig
}
func ( s * StepConfigureHardware ) Run ( _ context . Context , state multistep . StateBag ) multistep . StepAction {
ui := state . Get ( "ui" ).( packer . Ui )
vm := state . Get ( "vm" ).( * driver . VirtualMachine )
if * s . Config != ( HardwareConfig {}) {
2018-05-07 00:26:04 +03:00
ui . Say ( "Customizing hardware..." )
2018-05-06 00:41:14 +03:00
err := vm . Configure ( & driver . HardwareConfig {
CPUs : s . Config . CPUs ,
2018-12-18 20:51:56 +03:00
CpuCores : s . Config . CpuCores ,
2018-05-06 00:41:14 +03:00
CPUReservation : s . Config . CPUReservation ,
CPULimit : s . Config . CPULimit ,
RAM : s . Config . RAM ,
RAMReservation : s . Config . RAMReservation ,
RAMReserveAll : s . Config . RAMReserveAll ,
NestedHV : s . Config . NestedHV ,
CpuHotAddEnabled : s . Config . CpuHotAddEnabled ,
MemoryHotAddEnabled : s . Config . MemoryHotAddEnabled ,
2018-12-17 15:49:04 +03:00
VideoRAM : s . Config . VideoRAM ,
2020-03-24 15:28:04 +01:00
VGPUProfile : s . Config . VGPUProfile ,
2020-04-07 22:45:03 +03:00
Firmware : s . Config . Firmware ,
2018-05-06 00:41:14 +03:00
})
if err != nil {
state . Put ( "error" , err )
return multistep . ActionHalt
}
}
return multistep . ActionContinue
}
func ( s * StepConfigureHardware ) Cleanup ( multistep . StateBag ) {}