packer-cn/builder/vmware/common/step_create_disks.go

80 lines
2.4 KiB
Go
Raw Normal View History

package common
import (
"context"
"fmt"
"log"
2018-01-22 20:21:10 -05:00
"path/filepath"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
)
2013-06-05 17:49:04 -04:00
// This step creates the virtual disks for the VM.
//
// Uses:
// config *config
2013-06-06 15:19:38 -04:00
// driver Driver
2013-06-05 17:49:04 -04:00
// ui packer.Ui
//
// Produces:
// disk_full_paths ([]string) - The full paths to all created disks
type StepCreateDisks struct {
OutputDir *string
CreateMainDisk bool
DiskName string
MainDiskSize uint
AdditionalDiskSize []uint
DiskAdapterType string
DiskTypeId string
}
func (s *StepCreateDisks) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
driver := state.Get("driver").(Driver)
2013-08-31 15:50:25 -04:00
ui := state.Get("ui").(packer.Ui)
ui.Say("Creating required virtual machine disks")
2014-03-04 15:00:24 -05:00
// Users can configure disks at several locations in the template so
// first collate all the disk requirements
var diskFullPaths, diskSizes []string
// The 'main' or 'default' disk, only used in vmware-iso
if s.CreateMainDisk {
diskFullPaths = append(diskFullPaths, filepath.Join(*s.OutputDir, s.DiskName+".vmdk"))
diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(s.MainDiskSize)))
}
// Additional disks
if len(s.AdditionalDiskSize) > 0 {
incrementer := 1
for i, diskSize := range s.AdditionalDiskSize {
// scsi slot 7 is reserved, so we skip it.
if i+incrementer == 7 {
incrementer = 2
}
path := filepath.Join(*s.OutputDir, fmt.Sprintf("%s-%d.vmdk", s.DiskName, i+incrementer))
diskFullPaths = append(diskFullPaths, path)
size := fmt.Sprintf("%dM", uint64(diskSize))
diskSizes = append(diskSizes, size)
}
}
2014-03-04 15:00:24 -05:00
// Create all required disks
for i, diskFullPath := range diskFullPaths {
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i])
// Additional disks currently use the same adapter type and disk
// type as specified for the main disk
if err := driver.CreateDisk(diskFullPath, diskSizes[i], s.DiskAdapterType, s.DiskTypeId); err != nil {
err := fmt.Errorf("Error creating disk: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// Stash the disk paths so we can retrieve later e.g. when compacting
state.Put("disk_full_paths", diskFullPaths)
return multistep.ActionContinue
}
func (s *StepCreateDisks) Cleanup(multistep.StateBag) {}