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

130 lines
3.6 KiB
Go
Raw Normal View History

2013-12-21 17:27:00 -05:00
package iso
import (
"context"
"fmt"
2017-04-04 16:39:01 -04:00
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 16:39:01 -04:00
"github.com/hashicorp/packer/packer"
"path/filepath"
"strconv"
"strings"
)
// This step creates the virtual disk that will be used as the
// hard drive for the virtual machine.
2013-06-12 00:08:45 -04:00
type stepCreateDisk struct{}
func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
driver := state.Get("driver").(vboxcommon.Driver)
2013-08-31 15:44:58 -04:00
ui := state.Get("ui").(packer.Ui)
vmName := state.Get("vmName").(string)
format := "VDI"
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format)))
command := []string{
"createhd",
"--filename", path,
"--size", strconv.FormatUint(uint64(config.DiskSize), 10),
"--format", format,
"--variant", "Standard",
}
ui.Say("Creating hard drive...")
err := driver.VBoxManage(command...)
if err != nil {
err := fmt.Errorf("Error creating hard drive: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Add the IDE controller so we can later attach the disk.
// When the hard disk controller is not IDE, this device is still used
// by VirtualBox to deliver the guest extensions.
err = driver.VBoxManage("storagectl", vmName, "--name", "IDE Controller", "--add", "ide")
if err != nil {
err := fmt.Errorf("Error creating disk controller: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
// Add a SATA controller if we were asked to use SATA. We still attach
// the IDE controller above because some other things (disks) require
// that.
if config.HardDriveInterface == "sata" || config.ISOInterface == "sata" {
if err := driver.CreateSATAController(vmName, "SATA Controller", config.SATAPortCount); err != nil {
err := fmt.Errorf("Error creating disk controller: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
2015-01-16 11:34:12 -05:00
if config.HardDriveInterface == "scsi" {
if err := driver.CreateSCSIController(vmName, "SCSI Controller"); err != nil {
err := fmt.Errorf("Error creating disk controller: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
} else if config.HardDriveInterface == "pcie" {
if err := driver.CreateNVMeController(vmName, "NVMe Controller", config.NVMePortCount); err != nil {
err := fmt.Errorf("Error creating NVMe controller: %s", err)
2015-01-16 11:34:12 -05:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// Attach the disk to the controller
controllerName := "IDE Controller"
if config.HardDriveInterface == "sata" {
controllerName = "SATA Controller"
}
2015-01-16 11:34:12 -05:00
if config.HardDriveInterface == "scsi" {
controllerName = "SCSI Controller"
}
if config.HardDriveInterface == "pcie" {
controllerName = "NVMe Controller"
}
nonrotational := "off"
if config.HardDriveNonrotational {
nonrotational = "on"
}
discard := "off"
if config.HardDriveDiscard {
discard = "on"
}
command = []string{
"storageattach", vmName,
"--storagectl", controllerName,
"--port", "0",
"--device", "0",
"--type", "hdd",
"--medium", path,
"--nonrotational", nonrotational,
"--discard", discard,
}
if err := driver.VBoxManage(command...); err != nil {
err := fmt.Errorf("Error attaching hard drive: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
return multistep.ActionContinue
}
2013-08-31 15:44:58 -04:00
func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}