2013-12-21 17:27:00 -05:00
|
|
|
package iso
|
2013-06-11 19:34:44 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-11 19:34:44 -04:00
|
|
|
"fmt"
|
2017-04-30 19:23:05 -04:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
2017-04-30 19:23:05 -04:00
|
|
|
|
2013-06-11 19:34:44 -04:00
|
|
|
"path/filepath"
|
2013-06-23 23:43:40 -04:00
|
|
|
"strconv"
|
2013-06-11 19:34:44 -04:00
|
|
|
"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{}
|
2013-06-11 19:34:44 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-05-27 17:01:08 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2013-08-31 15:44:58 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-11 19:34:44 -04:00
|
|
|
|
|
|
|
format := "VDI"
|
|
|
|
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format)))
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"createhd",
|
|
|
|
"--filename", path,
|
2013-06-23 23:43:40 -04:00
|
|
|
"--size", strconv.FormatUint(uint64(config.DiskSize), 10),
|
2013-06-11 19:34:44 -04:00
|
|
|
"--format", format,
|
|
|
|
"--variant", "Standard",
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating hard drive...")
|
|
|
|
err := driver.VBoxManage(command...)
|
|
|
|
if err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error creating hard drive: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 19:34:44 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-09-05 14:00:08 -04:00
|
|
|
// 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.
|
2014-05-23 21:14:24 -04:00
|
|
|
err = driver.VBoxManage("storagectl", vmName, "--name", "IDE Controller", "--add", "ide")
|
2013-06-11 19:44:43 -04:00
|
|
|
if err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error creating disk controller: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 19:44:43 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-09-05 15:07:58 -04:00
|
|
|
// 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.
|
2014-05-23 21:14:24 -04:00
|
|
|
if config.HardDriveInterface == "sata" || config.ISOInterface == "sata" {
|
2017-03-22 08:09:25 -04:00
|
|
|
if err := driver.CreateSATAController(vmName, "SATA Controller", config.SATAPortCount); err != nil {
|
2013-09-05 14:00:08 -04:00
|
|
|
err := fmt.Errorf("Error creating disk controller: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-14 17:43:48 -05:00
|
|
|
// Add a VirtIO controller if we were asked to use VirtIO. We still attach
|
|
|
|
// the VirtIO controller above because some other things (disks) require
|
|
|
|
// that.
|
|
|
|
if config.HardDriveInterface == "virtio" || config.ISOInterface == "virtio" {
|
|
|
|
if err := driver.CreateVirtIOController(vmName, "VirtIO Controller"); err != nil {
|
2015-01-16 11:34:12 -05:00
|
|
|
err := fmt.Errorf("Error creating disk controller: %s", err)
|
2019-10-31 18:21:52 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2021-02-14 17:43:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.HardDriveInterface == "scsi" {
|
|
|
|
if err := driver.CreateSCSIController(vmName, "SCSI Controller"); err != nil {
|
2021-02-14 17:24:59 -05:00
|
|
|
err := fmt.Errorf("Error creating disk controller: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-10-31 18:21:52 -04:00
|
|
|
} 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
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 20:53:01 -05:00
|
|
|
|
2013-06-11 19:44:43 -04:00
|
|
|
// Attach the disk to the controller
|
2014-05-23 21:14:24 -04:00
|
|
|
controllerName := "IDE Controller"
|
|
|
|
if config.HardDriveInterface == "sata" {
|
|
|
|
controllerName = "SATA Controller"
|
2021-02-14 17:24:59 -05:00
|
|
|
} else if config.HardDriveInterface == "scsi" {
|
2015-01-16 11:34:12 -05:00
|
|
|
controllerName = "SCSI Controller"
|
2021-02-14 17:24:59 -05:00
|
|
|
} else if config.HardDriveInterface == "virtio" {
|
|
|
|
controllerName = "VirtIO Controller"
|
|
|
|
} else if config.HardDriveInterface == "pcie" {
|
2019-11-01 09:43:59 -04:00
|
|
|
controllerName = "NVMe Controller"
|
|
|
|
}
|
|
|
|
|
2016-10-16 13:41:15 -04:00
|
|
|
nonrotational := "off"
|
|
|
|
if config.HardDriveNonrotational {
|
|
|
|
nonrotational = "on"
|
|
|
|
}
|
|
|
|
|
|
|
|
discard := "off"
|
|
|
|
if config.HardDriveDiscard {
|
|
|
|
discard = "on"
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:44:43 -04:00
|
|
|
command = []string{
|
2013-06-11 19:48:01 -04:00
|
|
|
"storageattach", vmName,
|
2013-06-11 19:44:43 -04:00
|
|
|
"--storagectl", controllerName,
|
|
|
|
"--port", "0",
|
|
|
|
"--device", "0",
|
|
|
|
"--type", "hdd",
|
|
|
|
"--medium", path,
|
2016-10-16 13:41:15 -04:00
|
|
|
"--nonrotational", nonrotational,
|
|
|
|
"--discard", discard,
|
2013-06-11 19:44:43 -04:00
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error attaching hard drive: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 19:44:43 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:34:44 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}
|