github.com/sneal/packer@v0.5.2/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  	controllerName := "IDE Controller"
    47  	err = driver.VBoxManage("storagectl", vmName, "--name", controllerName, "--add", "ide")
    48  	if err != nil {
    49  		err := fmt.Errorf("Error creating disk controller: %s", err)
    50  		state.Put("error", err)
    51  		ui.Error(err.Error())
    52  		return multistep.ActionHalt
    53  	}
    54  
    55  	// Add a SATA controller if we were asked to use SATA. We still attach
    56  	// the IDE controller above because some other things (disks) require
    57  	// that.
    58  	if config.HardDriveInterface == "sata" {
    59  		controllerName = "SATA Controller"
    60  		if err := driver.CreateSATAController(vmName, controllerName); err != nil {
    61  			err := fmt.Errorf("Error creating disk controller: %s", err)
    62  			state.Put("error", err)
    63  			ui.Error(err.Error())
    64  			return multistep.ActionHalt
    65  		}
    66  	}
    67  
    68  	// Attach the disk to the controller
    69  	command = []string{
    70  		"storageattach", vmName,
    71  		"--storagectl", controllerName,
    72  		"--port", "0",
    73  		"--device", "0",
    74  		"--type", "hdd",
    75  		"--medium", path,
    76  	}
    77  	if err := driver.VBoxManage(command...); err != nil {
    78  		err := fmt.Errorf("Error attaching hard drive: %s", err)
    79  		state.Put("error", err)
    80  		ui.Error(err.Error())
    81  		return multistep.ActionHalt
    82  	}
    83  
    84  	return multistep.ActionContinue
    85  }
    86  
    87  func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}