2020-08-26 04:13:11 -04:00
|
|
|
package common
|
2013-06-04 18:00:58 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-04 18:00:58 -04:00
|
|
|
"fmt"
|
2018-04-24 18:23:48 -04:00
|
|
|
"log"
|
2018-01-22 20:21:10 -05:00
|
|
|
"path/filepath"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-06-04 18:00:58 -04:00
|
|
|
)
|
|
|
|
|
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
|
2020-11-19 14:54:31 -05:00
|
|
|
// ui packersdk.Ui
|
2013-06-05 17:49:04 -04:00
|
|
|
//
|
|
|
|
// Produces:
|
2018-04-24 18:23:48 -04:00
|
|
|
// disk_full_paths ([]string) - The full paths to all created disks
|
2020-08-26 04:13:11 -04:00
|
|
|
type StepCreateDisks struct {
|
|
|
|
OutputDir *string
|
|
|
|
CreateMainDisk bool
|
|
|
|
DiskName string
|
|
|
|
MainDiskSize uint
|
|
|
|
AdditionalDiskSize []uint
|
|
|
|
DiskAdapterType string
|
|
|
|
DiskTypeId string
|
|
|
|
}
|
2013-06-04 18:00:58 -04:00
|
|
|
|
2020-08-26 04:13:11 -04:00
|
|
|
func (s *StepCreateDisks) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-06-04 18:00:58 -04:00
|
|
|
|
2018-04-24 18:23:48 -04:00
|
|
|
ui.Say("Creating required virtual machine disks")
|
2014-03-04 15:00:24 -05:00
|
|
|
|
2018-04-24 18:23:48 -04:00
|
|
|
// Users can configure disks at several locations in the template so
|
|
|
|
// first collate all the disk requirements
|
2018-04-27 15:09:19 -04:00
|
|
|
var diskFullPaths, diskSizes []string
|
2020-08-26 04:13:11 -04:00
|
|
|
// 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)))
|
|
|
|
}
|
2018-04-24 18:23:48 -04:00
|
|
|
// Additional disks
|
2020-08-26 04:13:11 -04:00
|
|
|
if len(s.AdditionalDiskSize) > 0 {
|
2020-09-14 14:20:34 -04:00
|
|
|
incrementer := 1
|
2020-08-26 04:13:11 -04:00
|
|
|
for i, diskSize := range s.AdditionalDiskSize {
|
2020-09-14 14:20:34 -04:00
|
|
|
// 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))
|
2018-04-27 15:09:19 -04:00
|
|
|
diskFullPaths = append(diskFullPaths, path)
|
2018-04-24 18:23:48 -04:00
|
|
|
size := fmt.Sprintf("%dM", uint64(diskSize))
|
|
|
|
diskSizes = append(diskSizes, size)
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
2018-04-24 18:23:48 -04:00
|
|
|
}
|
2014-03-04 15:00:24 -05:00
|
|
|
|
2018-04-24 18:23:48 -04:00
|
|
|
// Create all required disks
|
2018-04-27 15:09:19 -04:00
|
|
|
for i, diskFullPath := range diskFullPaths {
|
|
|
|
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i])
|
2018-04-24 18:23:48 -04:00
|
|
|
// Additional disks currently use the same adapter type and disk
|
|
|
|
// type as specified for the main disk
|
2020-08-26 04:13:11 -04:00
|
|
|
if err := driver.CreateDisk(diskFullPath, diskSizes[i], s.DiskAdapterType, s.DiskTypeId); err != nil {
|
2018-04-24 18:23:48 -04:00
|
|
|
err := fmt.Errorf("Error creating disk: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
2013-07-02 12:09:55 -04:00
|
|
|
|
2018-04-24 18:23:48 -04:00
|
|
|
// Stash the disk paths so we can retrieve later e.g. when compacting
|
2018-04-27 15:09:19 -04:00
|
|
|
state.Put("disk_full_paths", diskFullPaths)
|
2013-06-04 18:00:58 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-08-26 04:13:11 -04:00
|
|
|
func (s *StepCreateDisks) Cleanup(multistep.StateBag) {}
|