Initial work to implement additional disk support in the vmware-iso builder

* Matches the syntax from mitchellh/packer#703
* Creates disk(s), adds them to the vmx template, and runs compact at the end
This commit is contained in:
Eric Richardson 2014-03-04 13:23:07 -05:00 committed by Shawn Neal
parent 793698f8a7
commit fab9ca9cdb
4 changed files with 65 additions and 0 deletions

View File

@ -36,6 +36,18 @@ func (s StepCompactDisk) Run(state multistep.StateBag) multistep.StepAction {
state.Put("error", fmt.Errorf("Error compacting disk: %s", err))
return multistep.ActionHalt
}
moreDisks := state.Get("additional_disk_paths").([]string)
if len(moreDisks) > 0 {
for i, path := range moreDisks {
ui.Say(fmt.Sprintf("Compacting additional disk image %d",i+1))
if err := driver.CompactDisk(path); err != nil {
state.Put("error", fmt.Errorf("Error compacting additional disk %d: %s", i+1, err))
return multistep.ActionHalt
}
}
}
return multistep.ActionContinue
}

View File

@ -35,6 +35,7 @@ type Config struct {
vmwcommon.ToolsConfig `mapstructure:",squash"`
vmwcommon.VMXConfig `mapstructure:",squash"`
AdditionalDiskSize []uint `mapstructure:"additionaldisk_size"`
DiskName string `mapstructure:"vmdk_name"`
DiskSize uint `mapstructure:"disk_size"`
DiskTypeId string `mapstructure:"disk_type_id"`

View File

@ -34,6 +34,28 @@ func (stepCreateDisk) Run(state multistep.StateBag) multistep.StepAction {
}
state.Put("full_disk_path", full_disk_path)
if len(config.AdditionalDiskSize) > 0 {
// stash the disk paths we create
additional_paths := make([]string,len(config.AdditionalDiskSize))
ui.Say("Creating additional hard drives...")
for i, additionalsize := range config.AdditionalDiskSize {
additionalpath := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.vmdk",config.DiskName,i+1))
size := fmt.Sprintf("%dM", uint64(additionalsize))
if err := driver.CreateDisk(additionalpath, size, config.DiskTypeId); err != nil {
err := fmt.Errorf("Error creating additional disk: %s", err)
state.Put("error",err)
ui.Error(err.Error())
return multistep.ActionHalt
}
additional_paths[i] = additionalpath
}
state.Put("additional_disk_paths", additional_paths)
}
return multistep.ActionContinue
}

View File

@ -20,6 +20,11 @@ type vmxTemplateData struct {
Version string
}
type additionalDiskTemplateData struct {
DiskNumber int
DiskName string
}
// This step creates the VMX file for the VM.
//
// Uses:
@ -70,6 +75,25 @@ func (s *stepCreateVMX) Run(state multistep.StateBag) multistep.StepAction {
vmxTemplate = string(rawBytes)
}
if len(config.AdditionalDiskSize) > 0 {
for i, _ := range config.AdditionalDiskSize {
data := &additionalDiskTemplateData{
DiskNumber: i+1,
DiskName: config.DiskName,
}
diskTemplate, err := config.tpl.Process(DefaultAdditionalDiskTemplate,data)
if err != nil {
err := fmt.Errorf("Error preparing VMX template for additional disk: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
vmxTemplate += diskTemplate
}
}
vmxContents, err := interpolate.Render(vmxTemplate, &ctx)
if err != nil {
@ -191,3 +215,9 @@ vmci0.pciSlotNumber = "35"
vmci0.present = "TRUE"
vmotion.checkpointFBSize = "65536000"
`
const DefaultAdditionalDiskTemplate = `
scsi0:{{ .DiskNumber }}.fileName = "{{ .DiskName}}-{{ .DiskNumber }}.vmdk"
scsi0:{{ .DiskNumber }}.present = "TRUE"
scsi0:{{ .DiskNumber }}.redo = ""
`