packer-cn/builder/vmware/iso/step_create_disk.go

67 lines
2.1 KiB
Go
Raw Normal View History

2013-12-24 00:58:41 -05:00
package iso
import (
"context"
"fmt"
"log"
2018-01-22 20:21:10 -05:00
"path/filepath"
2017-04-04 16:39:01 -04:00
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
)
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 stepCreateDisk struct{}
func (stepCreateDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
2015-05-27 17:16:28 -04:00
config := state.Get("config").(*Config)
2013-12-24 13:31:57 -05:00
driver := state.Get("driver").(vmwcommon.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
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, config.DiskName+".vmdk"))
diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(config.DiskSize)))
// Additional disks
if len(config.AdditionalDiskSize) > 0 {
for i, diskSize := range config.AdditionalDiskSize {
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.vmdk", config.DiskName, i+1))
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], config.DiskAdapterType, config.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
}
2013-08-31 15:50:25 -04:00
func (stepCreateDisk) Cleanup(multistep.StateBag) {}