2013-12-23 22:58:41 -07:00
|
|
|
package iso
|
2013-06-04 15:00:58 -07:00
|
|
|
|
|
|
|
import (
|
2018-01-22 15:32:33 -08:00
|
|
|
"context"
|
2013-06-04 15:00:58 -07:00
|
|
|
"fmt"
|
2018-04-24 23:23:48 +01:00
|
|
|
"log"
|
2018-01-22 17:21:10 -08:00
|
|
|
"path/filepath"
|
|
|
|
|
2017-04-04 13:39:01 -07:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
2018-01-19 16:18:44 -08:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 13:39:01 -07:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-06-04 15:00:58 -07:00
|
|
|
)
|
|
|
|
|
2013-06-05 14:49:04 -07:00
|
|
|
// This step creates the virtual disks for the VM.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
2013-06-06 12:19:38 -07:00
|
|
|
// driver Driver
|
2013-06-05 14:49:04 -07:00
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
2018-04-24 23:23:48 +01:00
|
|
|
// disk_full_paths ([]string) - The full paths to all created disks
|
2013-06-04 15:00:58 -07:00
|
|
|
type stepCreateDisk struct{}
|
|
|
|
|
2019-03-29 16:50:02 +01:00
|
|
|
func (stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-05-27 14:16:28 -07:00
|
|
|
config := state.Get("config").(*Config)
|
2013-12-24 11:31:57 -07:00
|
|
|
driver := state.Get("driver").(vmwcommon.Driver)
|
2013-08-31 12:50:25 -07:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-04 15:00:58 -07:00
|
|
|
|
2018-04-24 23:23:48 +01:00
|
|
|
ui.Say("Creating required virtual machine disks")
|
2014-03-04 15:00:24 -05:00
|
|
|
|
2018-04-24 23:23:48 +01:00
|
|
|
// Users can configure disks at several locations in the template so
|
|
|
|
// first collate all the disk requirements
|
2018-04-27 20:09:19 +01:00
|
|
|
var diskFullPaths, diskSizes []string
|
2018-04-24 23:23:48 +01:00
|
|
|
// The 'main' or 'default' disk
|
2018-04-27 20:09:19 +01:00
|
|
|
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, config.DiskName+".vmdk"))
|
2018-04-24 23:23:48 +01:00
|
|
|
diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(config.DiskSize)))
|
|
|
|
// Additional disks
|
2014-03-04 13:23:07 -05:00
|
|
|
if len(config.AdditionalDiskSize) > 0 {
|
2018-04-24 23:23:48 +01:00
|
|
|
for i, diskSize := range config.AdditionalDiskSize {
|
|
|
|
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.vmdk", config.DiskName, i+1))
|
2018-04-27 20:09:19 +01:00
|
|
|
diskFullPaths = append(diskFullPaths, path)
|
2018-04-24 23:23:48 +01:00
|
|
|
size := fmt.Sprintf("%dM", uint64(diskSize))
|
|
|
|
diskSizes = append(diskSizes, size)
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
2018-04-24 23:23:48 +01:00
|
|
|
}
|
2014-03-04 15:00:24 -05:00
|
|
|
|
2018-04-24 23:23:48 +01:00
|
|
|
// Create all required disks
|
2018-04-27 20:09:19 +01:00
|
|
|
for i, diskFullPath := range diskFullPaths {
|
|
|
|
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i])
|
2018-04-24 23:23:48 +01:00
|
|
|
// Additional disks currently use the same adapter type and disk
|
|
|
|
// type as specified for the main disk
|
2018-04-27 20:09:19 +01:00
|
|
|
if err := driver.CreateDisk(diskFullPath, diskSizes[i], config.DiskAdapterType, config.DiskTypeId); err != nil {
|
2018-04-24 23:23:48 +01: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 09:09:55 -07:00
|
|
|
|
2018-04-24 23:23:48 +01:00
|
|
|
// Stash the disk paths so we can retrieve later e.g. when compacting
|
2018-04-27 20:09:19 +01:00
|
|
|
state.Put("disk_full_paths", diskFullPaths)
|
2013-06-04 15:00:58 -07:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:50:25 -07:00
|
|
|
func (stepCreateDisk) Cleanup(multistep.StateBag) {}
|