Skip to content

Commit a66fef3

Browse files
committed
fix: always apply hardware configuration
Updated `StepConfigureHardware` to always run hardware configuration, even when the config is empty, to ensure template settings are preserved. Signed-off-by: Ryan Johnson <[email protected]>
1 parent 1f30c27 commit a66fef3

File tree

2 files changed

+55
-20
lines changed

2 files changed

+55
-20
lines changed

builder/vsphere/common/step_hardware.go

Lines changed: 34 additions & 3 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,8 +146,15 @@ 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...")
149+
hasCustomConfig := s.hasCustomHardwareConfig()
150+
151+
// Always run hardware configuration to preserve template settings.
152+
{
153+
if hasCustomConfig {
154+
ui.Say("Customizing hardware...")
155+
} else {
156+
ui.Say("Applying hardware configuration...")
157+
}
152158

153159
var allowedDevices []driver.PCIPassthroughAllowedDevice
154160
for _, device := range s.Config.AllowedDevices {
@@ -184,4 +190,29 @@ func (s *StepConfigureHardware) Run(_ context.Context, state multistep.StateBag)
184190
return multistep.ActionContinue
185191
}
186192

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

0 commit comments

Comments
 (0)