github.com/rothwerx/packer@v0.9.0/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  	if config.HardDriveInterface == "scsi" {
    67  		if err := driver.CreateSCSIController(vmName, "SCSI Controller"); err != nil {
    68  			err := fmt.Errorf("Error creating disk controller: %s", err)
    69  			state.Put("error", err)
    70  			ui.Error(err.Error())
    71  			return multistep.ActionHalt
    72  		}
    73  	}
    74  
    75  	// Attach the disk to the controller
    76  	controllerName := "IDE Controller"
    77  	if config.HardDriveInterface == "sata" {
    78  		controllerName = "SATA Controller"
    79  	}
    80  
    81  	if config.HardDriveInterface == "scsi" {
    82  		controllerName = "SCSI Controller"
    83  	}
    84  
    85  	command = []string{
    86  		"storageattach", vmName,
    87  		"--storagectl", controllerName,
    88  		"--port", "0",
    89  		"--device", "0",
    90  		"--type", "hdd",
    91  		"--medium", path,
    92  	}
    93  	if err := driver.VBoxManage(command...); err != nil {
    94  		err := fmt.Errorf("Error attaching hard drive: %s", err)
    95  		state.Put("error", err)
    96  		ui.Error(err.Error())
    97  		return multistep.ActionHalt
    98  	}
    99  
   100  	return multistep.ActionContinue
   101  }
   102  
   103  func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}