Skip to content

Commit

Permalink
Fix handling of temporary cdrom devices (#31)
Browse files Browse the repository at this point in the history
* Track  cdrom in temporaryDevices for step_clean_vmx

There was a previous attempt to do this in step_configure_vmx,
but that only covers vmware-iso (missing potential cases when using
cd_files/cd_content in vmware-vmx) and it had the wrong device name
(would mark <type>0:<primary> as temporary when the device name used
for cd_path is actually <type>1:<primary>

* Fix overwriting an existing cdrom device

ParseVMX/EncodeVMX force the keys to lowercase, so inserting with a
mixed-case key fails to replace existing values, resulting in duplicate
lines and vmware potentially still using the previous filename

* Generalize SkipFloppy to SkipDevices

It's not just floppies, it doesn't make sense to create any new
temporaryDevices in between StepCleanFiles and StepCleanVMX.
  • Loading branch information
puetzk authored Aug 18, 2021
1 parent e38fb92 commit 048e31a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
34 changes: 19 additions & 15 deletions builder/vmware/common/step_configure_vmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type StepConfigureVMX struct {
CustomData map[string]string
DisplayName string
SkipFloppy bool
SkipDevices bool
VMName string
DiskAdapterType string
CDROMAdapterType string
Expand Down Expand Up @@ -65,9 +65,10 @@ func (s *StepConfigureVMX) Run(ctx context.Context, state multistep.StateBag) mu
vmxData[k] = v
}

// Set a floppy disk, but only if we should
if !s.SkipFloppy {
// Grab list of temporary builder devices so we can append the floppy to it
// StepConfigureVMX runs both before and after provisioning (for VmxDataPost),
// the latter time shouldn't create temporary devices
if !s.SkipDevices {
// Grab list of temporary builder devices so we can append to it
tmpBuildDevices := state.Get("temporaryDevices").([]string)

// Set a floppy disk if we have one
Expand All @@ -81,21 +82,24 @@ func (s *StepConfigureVMX) Run(ctx context.Context, state multistep.StateBag) mu
tmpBuildDevices = append(tmpBuildDevices, "floppy0")
}

// Add our custom CD, if it exists
if cdPath, ok := state.GetOk("cd_path"); ok {
if cdPath != "" {
diskAndCDConfigData := DefaultDiskAndCDROMTypes(s.DiskAdapterType, s.CDROMAdapterType)
cdromPrefix := diskAndCDConfigData.CDROMType + "1:" + diskAndCDConfigData.CDROMType_PrimarySecondary
vmxData[cdromPrefix+".present"] = "TRUE"
vmxData[cdromPrefix+".filename"] = cdPath.(string)
vmxData[cdromPrefix+".devicetype"] = "cdrom-image"

// Add it to our list of build devices to later remove
tmpBuildDevices = append(tmpBuildDevices, cdromPrefix)
}
}

// Build the list back in our statebag
state.Put("temporaryDevices", tmpBuildDevices)
}

// Add our custom CD, if it exists
if cdPath, ok := state.GetOk("cd_path"); ok {
if cdPath != "" {
diskAndCDConfigData := DefaultDiskAndCDROMTypes(s.DiskAdapterType, s.CDROMAdapterType)
cdromPrefix := diskAndCDConfigData.CDROMType + "1:" + diskAndCDConfigData.CDROMType_PrimarySecondary
vmxData[cdromPrefix+".present"] = "TRUE"
vmxData[cdromPrefix+".fileName"] = cdPath.(string)
vmxData[cdromPrefix+".deviceType"] = "cdrom-image"
}
}

// If the build is taking place on a remote ESX server, the displayName
// will be needed for discovery of the VM's IP address and for export
// of the VM. The displayName key should always be set in the VMX file,
Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/iso/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
},
&vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXDataPost,
SkipFloppy: true,
SkipDevices: true,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
},
Expand Down
7 changes: 0 additions & 7 deletions builder/vmware/iso/step_create_vmx.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,6 @@ func (s *stepCreateVMX) Run(ctx context.Context, state multistep.StateBag) multi

templateData.DiskAndCDConfigData = vmwcommon.DefaultDiskAndCDROMTypes(config.DiskAdapterType, config.CdromAdapterType)

/// Now that we figured out the CDROM device to add, store it
/// to the list of temporary build devices in our statebag
tmpBuildDevices := state.Get("temporaryDevices").([]string)
tmpCdromDevice := fmt.Sprintf("%s0:%s", templateData.CDROMType, templateData.CDROMType_PrimarySecondary)
tmpBuildDevices = append(tmpBuildDevices, tmpCdromDevice)
state.Put("temporaryDevices", tmpBuildDevices)

/// Assign the network adapter type into the template if one was specified.
network_adapter := strings.ToLower(config.HWConfig.NetworkAdapterType)
if network_adapter != "" {
Expand Down
2 changes: 1 addition & 1 deletion builder/vmware/vmx/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
},
&vmwcommon.StepConfigureVMX{
CustomData: b.config.VMXDataPost,
SkipFloppy: true,
SkipDevices: true,
VMName: b.config.VMName,
DisplayName: b.config.VMXDisplayName,
},
Expand Down

0 comments on commit 048e31a

Please sign in to comment.