Skip to content

Commit 2a256db

Browse files
committed
fix: always apply hardware configuration
- Only set non-zero or enabled fields, preventing unnecessary changes. - Always applies the configuration to ensure template settings are preserved. Signed-off-by: Ryan Johnson <rya@tenthirtyam.org>
1 parent 1f30c27 commit 2a256db

File tree

3 files changed

+112
-62
lines changed

3 files changed

+112
-62
lines changed

builder/vsphere/common/step_hardware.go

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package common
99
import (
1010
"context"
1111
"fmt"
12-
"reflect"
1312

1413
"github.com/hashicorp/packer-plugin-sdk/multistep"
1514
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
@@ -147,41 +146,70 @@ func (s *StepConfigureHardware) Run(_ context.Context, state multistep.StateBag)
147146
ui := state.Get("ui").(packersdk.Ui)
148147
vm := state.Get("vm").(driver.VirtualMachine)
149148

150-
if !reflect.DeepEqual(*s.Config, HardwareConfig{}) {
151-
ui.Say("Customizing hardware...")
152-
153-
var allowedDevices []driver.PCIPassthroughAllowedDevice
154-
for _, device := range s.Config.AllowedDevices {
155-
allowedDevices = append(allowedDevices, driver.PCIPassthroughAllowedDevice(device))
156-
}
157-
158-
err := vm.Configure(&driver.HardwareConfig{
159-
CPUs: s.Config.CPUs,
160-
CpuCores: s.Config.CpuCores,
161-
CPUReservation: s.Config.CPUReservation,
162-
CPULimit: s.Config.CPULimit,
163-
RAM: s.Config.RAM,
164-
RAMReservation: s.Config.RAMReservation,
165-
RAMReserveAll: s.Config.RAMReserveAll,
166-
NestedHV: s.Config.NestedHV,
167-
CpuHotAddEnabled: s.Config.CpuHotAddEnabled,
168-
MemoryHotAddEnabled: s.Config.MemoryHotAddEnabled,
169-
VideoRAM: s.Config.VideoRAM,
170-
Displays: s.Config.Displays,
171-
AllowedDevices: allowedDevices,
172-
VGPUProfile: s.Config.VGPUProfile,
173-
Firmware: s.Config.Firmware,
174-
ForceBIOSSetup: s.Config.ForceBIOSSetup,
175-
VTPMEnabled: s.Config.VTPMEnabled,
176-
VirtualPrecisionClock: s.Config.VirtualPrecisionClock,
177-
})
178-
if err != nil {
179-
state.Put("error", err)
180-
return multistep.ActionHalt
181-
}
149+
hasCustomConfig := s.hasCustomHardwareConfig()
150+
151+
if hasCustomConfig {
152+
ui.Say("Applying custom hardware configuration...")
153+
} else {
154+
ui.Say("Applying hardware configuration...")
155+
}
156+
157+
var allowedDevices []driver.PCIPassthroughAllowedDevice
158+
for _, device := range s.Config.AllowedDevices {
159+
allowedDevices = append(allowedDevices, driver.PCIPassthroughAllowedDevice(device))
160+
}
161+
162+
err := vm.Configure(&driver.HardwareConfig{
163+
CPUs: s.Config.CPUs,
164+
CpuCores: s.Config.CpuCores,
165+
CPUReservation: s.Config.CPUReservation,
166+
CPULimit: s.Config.CPULimit,
167+
RAM: s.Config.RAM,
168+
RAMReservation: s.Config.RAMReservation,
169+
RAMReserveAll: s.Config.RAMReserveAll,
170+
NestedHV: s.Config.NestedHV,
171+
CpuHotAddEnabled: s.Config.CpuHotAddEnabled,
172+
MemoryHotAddEnabled: s.Config.MemoryHotAddEnabled,
173+
VideoRAM: s.Config.VideoRAM,
174+
Displays: s.Config.Displays,
175+
AllowedDevices: allowedDevices,
176+
VGPUProfile: s.Config.VGPUProfile,
177+
Firmware: s.Config.Firmware,
178+
ForceBIOSSetup: s.Config.ForceBIOSSetup,
179+
VTPMEnabled: s.Config.VTPMEnabled,
180+
VirtualPrecisionClock: s.Config.VirtualPrecisionClock,
181+
})
182+
if err != nil {
183+
state.Put("error", err)
184+
return multistep.ActionHalt
182185
}
183186

184187
return multistep.ActionContinue
185188
}
186189

190+
// hasCustomHardwareConfig checks if user provided custom hardware configuration.
191+
func (s *StepConfigureHardware) hasCustomHardwareConfig() bool {
192+
c := s.Config
193+
194+
hasCpuConfig := c.CPUs != 0 || c.CpuCores != 0 || c.CPUReservation != 0 || c.CPULimit != 0
195+
hasMemoryConfig := c.RAM != 0 || c.RAMReservation != 0 || c.RAMReserveAll
196+
hasHotAddConfig := c.CpuHotAddEnabled || c.MemoryHotAddEnabled
197+
hasDisplayConfig := c.VideoRAM != 0 || c.Displays != 0
198+
hasNestedConfig := c.NestedHV
199+
hasGpuConfig := c.VGPUProfile != ""
200+
hasFirmwareConfig := c.Firmware != "" || c.ForceBIOSSetup || c.VTPMEnabled
201+
hasClockConfig := c.VirtualPrecisionClock != ""
202+
hasDeviceConfig := len(c.AllowedDevices) > 0
203+
204+
return hasCpuConfig ||
205+
hasMemoryConfig ||
206+
hasHotAddConfig ||
207+
hasDisplayConfig ||
208+
hasNestedConfig ||
209+
hasGpuConfig ||
210+
hasFirmwareConfig ||
211+
hasClockConfig ||
212+
hasDeviceConfig
213+
}
214+
187215
func (s *StepConfigureHardware) Cleanup(multistep.StateBag) {}

builder/vsphere/common/step_hardware_test.go

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ func TestStepConfigureHardware_Run(t *testing.T) {
150150
hardwareConfig: driverHardwareConfigFromConfig(basicStepConfigureHardware().Config),
151151
},
152152
{
153-
name: "Don't configure hardware when config is empty",
153+
name: "Configure hardware even when config is empty (preserves template settings)",
154154
step: &StepConfigureHardware{Config: &HardwareConfig{}},
155155
action: multistep.ActionContinue,
156156
configureError: nil,
157-
configureCalled: false,
157+
configureCalled: true,
158+
hardwareConfig: driverHardwareConfigFromConfig(&HardwareConfig{}),
158159
},
159160
{
160161
name: "Halt when configure return error",
@@ -234,20 +235,23 @@ func driverHardwareConfigFromConfig(config *HardwareConfig) *driver.HardwareConf
234235
}
235236

236237
return &driver.HardwareConfig{
237-
CPUs: config.CPUs,
238-
CpuCores: config.CpuCores,
239-
CPUReservation: config.CPUReservation,
240-
CPULimit: config.CPULimit,
241-
RAM: config.RAM,
242-
RAMReservation: config.RAMReservation,
243-
RAMReserveAll: config.RAMReserveAll,
244-
NestedHV: config.NestedHV,
245-
CpuHotAddEnabled: config.CpuHotAddEnabled,
246-
MemoryHotAddEnabled: config.MemoryHotAddEnabled,
247-
VideoRAM: config.VideoRAM,
248-
AllowedDevices: allowedDevices,
249-
VGPUProfile: config.VGPUProfile,
250-
Firmware: config.Firmware,
251-
ForceBIOSSetup: config.ForceBIOSSetup,
238+
CPUs: config.CPUs,
239+
CpuCores: config.CpuCores,
240+
CPUReservation: config.CPUReservation,
241+
CPULimit: config.CPULimit,
242+
RAM: config.RAM,
243+
RAMReservation: config.RAMReservation,
244+
RAMReserveAll: config.RAMReserveAll,
245+
NestedHV: config.NestedHV,
246+
CpuHotAddEnabled: config.CpuHotAddEnabled,
247+
MemoryHotAddEnabled: config.MemoryHotAddEnabled,
248+
VideoRAM: config.VideoRAM,
249+
Displays: config.Displays,
250+
AllowedDevices: allowedDevices,
251+
VGPUProfile: config.VGPUProfile,
252+
Firmware: config.Firmware,
253+
ForceBIOSSetup: config.ForceBIOSSetup,
254+
VTPMEnabled: config.VTPMEnabled,
255+
VirtualPrecisionClock: config.VirtualPrecisionClock,
252256
}
253257
}

builder/vsphere/driver/vm.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -597,26 +597,34 @@ func (vm *VirtualMachineDriver) Configure(config *HardwareConfig) error {
597597
confSpec.MemoryMB = config.RAM
598598

599599
var cpuSpec types.ResourceAllocationInfo
600-
cpuSpec.Reservation = &config.CPUReservation
600+
if config.CPUReservation != 0 {
601+
cpuSpec.Reservation = &config.CPUReservation
602+
}
601603
if config.CPULimit != 0 {
602604
cpuSpec.Limit = &config.CPULimit
603605
}
604606
confSpec.CpuAllocation = &cpuSpec
605607

606608
var ramSpec types.ResourceAllocationInfo
607-
ramSpec.Reservation = &config.RAMReservation
609+
if config.RAMReservation != 0 {
610+
ramSpec.Reservation = &config.RAMReservation
611+
}
608612
confSpec.MemoryAllocation = &ramSpec
609613

610-
confSpec.MemoryReservationLockedToMax = &config.RAMReserveAll
614+
if config.RAMReserveAll {
615+
confSpec.MemoryReservationLockedToMax = &config.RAMReserveAll
616+
}
617+
611618
if config.NestedHV {
612619
confSpec.NestedHVEnabled = &config.NestedHV
613620
}
614621

615-
confSpec.CpuHotAddEnabled = &config.CpuHotAddEnabled
616-
confSpec.MemoryHotAddEnabled = &config.MemoryHotAddEnabled
622+
if config.CpuHotAddEnabled {
623+
confSpec.CpuHotAddEnabled = &config.CpuHotAddEnabled
624+
}
617625

618-
if config.Displays == 0 {
619-
config.Displays = 1
626+
if config.MemoryHotAddEnabled {
627+
confSpec.MemoryHotAddEnabled = &config.MemoryHotAddEnabled
620628
}
621629

622630
if config.VideoRAM != 0 || config.Displays != 0 {
@@ -629,8 +637,15 @@ func (vm *VirtualMachineDriver) Configure(config *HardwareConfig) error {
629637
return err
630638
}
631639
card := l[0].(*types.VirtualMachineVideoCard)
632-
card.VideoRamSizeInKB = config.VideoRAM
633-
card.NumDisplays = config.Displays
640+
641+
if config.VideoRAM != 0 {
642+
card.VideoRamSizeInKB = config.VideoRAM
643+
}
644+
645+
if config.Displays != 0 {
646+
card.NumDisplays = config.Displays
647+
}
648+
634649
spec := &types.VirtualDeviceConfigSpec{
635650
Device: card,
636651
Operation: types.VirtualDeviceConfigSpecOperationEdit,
@@ -686,9 +701,12 @@ func (vm *VirtualMachineDriver) Configure(config *HardwareConfig) error {
686701
}
687702

688703
confSpec.Firmware = firmware
689-
confSpec.BootOptions = &types.VirtualMachineBootOptions{
690-
EnterBIOSSetup: types.NewBool(config.ForceBIOSSetup),
691-
EfiSecureBootEnabled: types.NewBool(efiSecureBootEnabled),
704+
705+
if config.Firmware != "" || config.ForceBIOSSetup {
706+
confSpec.BootOptions = &types.VirtualMachineBootOptions{
707+
EnterBIOSSetup: types.NewBool(config.ForceBIOSSetup),
708+
EfiSecureBootEnabled: types.NewBool(efiSecureBootEnabled),
709+
}
692710
}
693711

694712
task, err := vm.vm.Reconfigure(vm.driver.ctx, confSpec)

0 commit comments

Comments
 (0)