github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/virtualbox/iso/step_create_disk.go (about)

     1  package iso
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/mitchellh/multistep"
     6  	vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
     7  	"github.com/mitchellh/packer/packer"
     8  	"path/filepath"
     9  	"strconv"
    10  	"strings"
    11  )
    12  
    13  // This step creates the virtual disk that will be used as the
    14  // hard drive for the virtual machine.
    15  type stepCreateDisk struct{}
    16  
    17  func (s *stepCreateDisk) Run(state multistep.StateBag) multistep.StepAction {
    18  	config := state.Get("config").(*config)
    19  	driver := state.Get("driver").(vboxcommon.Driver)
    20  	ui := state.Get("ui").(packer.Ui)
    21  	vmName := state.Get("vmName").(string)
    22  
    23  	format := "VDI"
    24  	path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format)))
    25  
    26  	command := []string{
    27  		"createhd",
    28  		"--filename", path,
    29  		"--size", strconv.FormatUint(uint64(config.DiskSize), 10),
    30  		"--format", format,
    31  		"--variant", "Standard",
    32  	}
    33  
    34  	ui.Say("Creating hard drive...")
    35  	err := driver.VBoxManage(command...)
    36  	if err != nil {
    37  		err := fmt.Errorf("Error creating hard drive: %s", err)
    38  		state.Put("error", err)
    39  		ui.Error(err.Error())
    40  		return multistep.ActionHalt
    41  	}
    42  
    43  	// Add the IDE controller so we can later attach the disk.
    44  	// When the hard disk controller is not IDE, this device is still used
    45  	// by VirtualBox to deliver the guest extensions.
    46  	err = driver.VBoxManage("storagectl", vmName, "--name", "IDE Controller", "--add", "ide")
    47  	if err != nil {
    48  		err := fmt.Errorf("Error creating disk controller: %s", err)
    49  		state.Put("error", err)
    50  		ui.Error(err.Error())
    51  		return multistep.ActionHalt
    52  	}
    53  
    54  	// Add a SATA controller if we were asked to use SATA. We still attach
    55  	// the IDE controller above because some other things (disks) require
    56  	// that.
    57  	if config.HardDriveInterface == "sata" || config.ISOInterface == "sata" {
    58  		if err := driver.CreateSATAController(vmName, "SATA Controller"); err != nil {
    59  			err := fmt.Errorf("Error creating disk controller: %s", err)
    60  			state.Put("error", err)
    61  			ui.Error(err.Error())
    62  			return multistep.ActionHalt
    63  		}
    64  	}
    65  
    66  	// Attach the disk to the controller
    67  	controllerName := "IDE Controller"
    68  	if config.HardDriveInterface == "sata" {
    69  		controllerName = "SATA Controller"
    70  	}
    71  
    72  	command = []string{
    73  		"storageattach", vmName,
    74  		"--storagectl", controllerName,
    75  		"--port", "0",
    76  		"--device", "0",
    77  		"--type", "hdd",
    78  		"--medium", path,
    79  	}
    80  	if err := driver.VBoxManage(command...); err != nil {
    81  		err := fmt.Errorf("Error attaching hard drive: %s", err)
    82  		state.Put("error", err)
    83  		ui.Error(err.Error())
    84  		return multistep.ActionHalt
    85  	}
    86  
    87  	return multistep.ActionContinue
    88  }
    89  
    90  func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}