2019-05-31 14:27:41 +02:00
|
|
|
//go:generate struct-markdown
|
2019-10-14 16:43:59 +02:00
|
|
|
//go:generate mapstructure-to-hcl2 -type Config
|
2019-05-31 14:27:41 +02:00
|
|
|
|
2013-12-21 14:27:00 -08:00
|
|
|
package iso
|
2013-06-11 15:12:45 -07:00
|
|
|
|
|
|
|
import (
|
2019-03-22 14:53:28 +01:00
|
|
|
"context"
|
2013-06-11 20:00:30 -07:00
|
|
|
"errors"
|
2013-06-11 15:45:52 -07:00
|
|
|
"fmt"
|
2014-09-02 09:35:59 -07:00
|
|
|
|
2019-12-17 11:25:56 +01:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-17 13:29:25 -08:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/bootcommand"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/common"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep/commonsteps"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/config"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2017-04-04 13:39:01 -07:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/common"
|
2013-06-11 15:12:45 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const BuilderId = "mitchellh.virtualbox"
|
|
|
|
|
|
|
|
type Builder struct {
|
2015-05-27 14:01:08 -07:00
|
|
|
config Config
|
2013-06-11 15:12:45 -07:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2015-05-27 14:01:08 -07:00
|
|
|
type Config struct {
|
2014-03-12 16:14:44 -07:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2020-11-11 15:04:28 -08:00
|
|
|
commonsteps.HTTPConfig `mapstructure:",squash"`
|
|
|
|
commonsteps.ISOConfig `mapstructure:",squash"`
|
|
|
|
commonsteps.FloppyConfig `mapstructure:",squash"`
|
|
|
|
commonsteps.CDConfig `mapstructure:",squash"`
|
2018-04-18 16:19:44 -07:00
|
|
|
bootcommand.BootConfig `mapstructure:",squash"`
|
2014-03-12 16:14:44 -07:00
|
|
|
vboxcommon.ExportConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.OutputConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.RunConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.ShutdownConfig `mapstructure:",squash"`
|
2020-01-09 08:37:30 -08:00
|
|
|
vboxcommon.CommConfig `mapstructure:",squash"`
|
2018-11-17 04:24:32 -06:00
|
|
|
vboxcommon.HWConfig `mapstructure:",squash"`
|
2014-03-12 16:14:44 -07:00
|
|
|
vboxcommon.VBoxManageConfig `mapstructure:",squash"`
|
|
|
|
vboxcommon.VBoxVersionConfig `mapstructure:",squash"`
|
2018-02-26 10:47:59 -06:00
|
|
|
vboxcommon.VBoxBundleConfig `mapstructure:",squash"`
|
2019-03-16 18:15:27 +02:00
|
|
|
vboxcommon.GuestAdditionsConfig `mapstructure:",squash"`
|
2021-02-20 19:31:46 +01:00
|
|
|
// The chipset to be used: PIIX3 or ICH9.
|
|
|
|
// When set to piix3, the firmare is PIIX3. This is the default.
|
|
|
|
// When set to ich9, the firmare is ICH9.
|
|
|
|
Chipset string `mapstructure:"chipset" required:"false"`
|
|
|
|
// The firmware to be used: BIOS or EFI.
|
|
|
|
// When set to bios, the firmare is BIOS. This is the default.
|
|
|
|
// When set to efi, the firmare is EFI.
|
|
|
|
Firmware string `mapstructure:"firmware" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// The size, in megabytes, of the hard disk to create for the VM. By
|
|
|
|
// default, this is 40000 (about 40 GB).
|
2019-06-06 16:29:25 +02:00
|
|
|
DiskSize uint `mapstructure:"disk_size" required:"false"`
|
2021-02-20 19:59:39 +01:00
|
|
|
// The NIC type to be used for the network interfaces.
|
|
|
|
// When set to 82540EM, the NICs are Intel PRO/1000 MT Desktop (82540EM). This is the default.
|
|
|
|
// When set to 82543GC, the NICs are Intel PRO/1000 T Server (82543GC).
|
|
|
|
// When set to 82545EM, the NICs are Intel PRO/1000 MT Server (82545EM).
|
|
|
|
// When set to Am79C970A, the NICs are AMD PCNet-PCI II network card (Am79C970A).
|
|
|
|
// When set to Am79C973, the NICs are AMD PCNet-FAST III network card (Am79C973).
|
|
|
|
// When set to Am79C960, the NICs are AMD PCnet-ISA/NE2100 (Am79C960).
|
|
|
|
// When set to virtio, the NICs are VirtIO.
|
|
|
|
NICType string `mapstructure:"nic_type" required:"false"`
|
2021-02-20 20:09:16 +01:00
|
|
|
// The audio controller type to be used.
|
2021-02-20 20:21:57 +01:00
|
|
|
// When set to ac97, the audio controller is ICH AC97. This is the default.
|
2021-02-20 20:09:16 +01:00
|
|
|
// When set to hda, the audio controller is Intel HD Audio.
|
|
|
|
// When set to sb16, the audio controller is SoundBlaster 16.
|
|
|
|
AudioController string `mapstructure:"audio_controller" required:"false"`
|
2021-02-20 20:21:57 +01:00
|
|
|
// The graphics controller type to be used.
|
|
|
|
// When set to vboxvga, the graphics controller is VirtualBox VGA. This is the default.
|
|
|
|
// When set to vboxsvga, the graphics controller is VirtualBox SVGA.
|
|
|
|
// When set to vmsvga, the graphics controller is VMware SVGA.
|
|
|
|
// When set to none, the graphics controller is disabled.
|
|
|
|
GfxController string `mapstructure:"gfx_controller" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// The guest OS type being installed. By default this is other, but you can
|
|
|
|
// get dramatic performance improvements by setting this to the proper
|
|
|
|
// value. To view all available values for this run VBoxManage list
|
|
|
|
// ostypes. Setting the correct value hints to VirtualBox how to optimize
|
|
|
|
// the virtual hardware to work best with that operating system.
|
2019-06-06 16:29:25 +02:00
|
|
|
GuestOSType string `mapstructure:"guest_os_type" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// When this value is set to true, a VDI image will be shrunk in response
|
|
|
|
// to the trim command from the guest OS. The size of the cleared area must
|
|
|
|
// be at least 1MB. Also set hard_drive_nonrotational to true to enable
|
|
|
|
// TRIM support.
|
2019-06-06 16:29:25 +02:00
|
|
|
HardDriveDiscard bool `mapstructure:"hard_drive_discard" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// The type of controller that the primary hard drive is attached to,
|
|
|
|
// defaults to ide. When set to sata, the drive is attached to an AHCI SATA
|
|
|
|
// controller. When set to scsi, the drive is attached to an LsiLogic SCSI
|
2019-10-31 15:21:52 -07:00
|
|
|
// controller. When set to pcie, the drive is attached to an NVMe
|
2021-02-20 14:59:41 +01:00
|
|
|
// controller. When set to virtio, the drive is attached to a VirtIO
|
2019-11-04 14:30:59 +01:00
|
|
|
// controller. Please note that when you use "pcie", you'll need to have
|
|
|
|
// Virtualbox 6, install an [extension
|
|
|
|
// pack](https://www.virtualbox.org/wiki/Downloads#VirtualBox6.0.14OracleVMVirtualBoxExtensionPack)
|
|
|
|
// and you will need to enable EFI mode for nvme to work, ex:
|
2020-03-12 10:05:08 -04:00
|
|
|
//
|
2020-08-20 15:43:38 +02:00
|
|
|
// In JSON:
|
2020-03-12 10:05:08 -04:00
|
|
|
// ```json
|
|
|
|
// "vboxmanage": [
|
2019-11-04 14:30:59 +01:00
|
|
|
// [ "modifyvm", "{{.Name}}", "--firmware", "EFI" ],
|
2020-03-12 10:05:08 -04:00
|
|
|
// ]
|
|
|
|
// ```
|
|
|
|
//
|
2020-08-20 15:43:38 +02:00
|
|
|
// In HCL2:
|
|
|
|
// ```hcl
|
|
|
|
// vboxmanage = [
|
|
|
|
// [ "modifyvm", "{{.Name}}", "--firmware", "EFI" ],
|
|
|
|
// ]
|
|
|
|
// ```
|
|
|
|
//
|
2019-06-06 16:29:25 +02:00
|
|
|
HardDriveInterface string `mapstructure:"hard_drive_interface" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// The number of ports available on any SATA controller created, defaults
|
|
|
|
// to 1. VirtualBox supports up to 30 ports on a maximum of 1 SATA
|
|
|
|
// controller. Increasing this value can be useful if you want to attach
|
|
|
|
// additional drives.
|
2019-06-06 16:29:25 +02:00
|
|
|
SATAPortCount int `mapstructure:"sata_port_count" required:"false"`
|
2019-10-31 15:21:52 -07:00
|
|
|
// The number of ports available on any NVMe controller created, defaults
|
|
|
|
// to 1. VirtualBox supports up to 255 ports on a maximum of 1 NVMe
|
|
|
|
// controller. Increasing this value can be useful if you want to attach
|
|
|
|
// additional drives.
|
|
|
|
NVMePortCount int `mapstructure:"nvme_port_count" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// Forces some guests (i.e. Windows 7+) to treat disks as SSDs and stops
|
|
|
|
// them from performing disk fragmentation. Also set hard_drive_discard to
|
|
|
|
// true to enable TRIM support.
|
2019-06-06 16:29:25 +02:00
|
|
|
HardDriveNonrotational bool `mapstructure:"hard_drive_nonrotational" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// The type of controller that the ISO is attached to, defaults to ide.
|
|
|
|
// When set to sata, the drive is attached to an AHCI SATA controller.
|
2021-02-20 14:59:41 +01:00
|
|
|
// When set to virtio, the drive is attached to a VirtIO controller.
|
2019-06-06 16:29:25 +02:00
|
|
|
ISOInterface string `mapstructure:"iso_interface" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// Set this to true if you would like to keep the VM registered with
|
|
|
|
// virtualbox. Defaults to false.
|
2019-06-06 16:29:25 +02:00
|
|
|
KeepRegistered bool `mapstructure:"keep_registered" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// Defaults to false. When enabled, Packer will not export the VM. Useful
|
|
|
|
// if the build output is not the resultant image, but created inside the
|
|
|
|
// VM.
|
2019-06-06 16:29:25 +02:00
|
|
|
SkipExport bool `mapstructure:"skip_export" required:"false"`
|
2019-06-17 16:42:49 +02:00
|
|
|
// This is the name of the OVF file for the new virtual machine, without
|
|
|
|
// the file extension. By default this is packer-BUILDNAME, where
|
|
|
|
// "BUILDNAME" is the name of the build.
|
2019-06-06 16:29:25 +02:00
|
|
|
VMName string `mapstructure:"vm_name" required:"false"`
|
2013-06-13 10:24:10 -07:00
|
|
|
|
2015-05-27 14:01:08 -07:00
|
|
|
ctx interpolate.Context
|
2013-06-11 15:12:45 -07:00
|
|
|
}
|
|
|
|
|
2019-12-17 11:25:56 +01:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-16 21:23:05 -08:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2015-05-27 14:01:08 -07:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2020-10-09 17:01:55 -07:00
|
|
|
PluginType: vboxcommon.BuilderId, // "mitchellh.virtualbox"
|
2015-06-22 09:17:09 -07:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
2015-05-27 14:01:08 -07:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
"guest_additions_path",
|
|
|
|
"guest_additions_url",
|
|
|
|
"vboxmanage",
|
|
|
|
"vboxmanage_post",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-08-08 16:00:47 -07:00
|
|
|
if err != nil {
|
2019-12-16 21:23:05 -08:00
|
|
|
return nil, nil, err
|
2013-08-08 16:00:47 -07:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:17:21 -05:00
|
|
|
// Accumulate any errors and warnings
|
2020-11-19 12:07:02 -08:00
|
|
|
var errs *packersdk.MultiError
|
2015-10-20 16:27:47 -07:00
|
|
|
warnings := make([]string, 0)
|
|
|
|
|
|
|
|
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
|
|
|
|
warnings = append(warnings, isoWarnings...)
|
2020-11-19 12:07:02 -08:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, isoErrs...)
|
2015-10-20 16:27:47 -07:00
|
|
|
|
2020-11-19 12:07:02 -08:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.ExportConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.ExportConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.CDConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(
|
2015-05-27 14:01:08 -07:00
|
|
|
errs, b.config.OutputConfig.Prepare(&b.config.ctx, &b.config.PackerConfig)...)
|
2020-11-20 10:53:16 -08:00
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.RunConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.ShutdownConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.CommConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.HWConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.VBoxBundleConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.VBoxManageConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.VBoxVersionConfig.Prepare(b.config.CommConfig.Comm.Type)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.BootConfig.Prepare(&b.config.ctx)...)
|
|
|
|
errs = packersdk.MultiErrorAppend(errs, b.config.GuestAdditionsConfig.Prepare(b.config.CommConfig.Comm.Type)...)
|
2013-07-14 09:28:56 +09:00
|
|
|
|
2021-02-20 19:31:46 +01:00
|
|
|
if b.config.Chipset == "" {
|
|
|
|
b.config.Chipset = "piix3"
|
|
|
|
}
|
|
|
|
switch b.config.Chipset {
|
|
|
|
case "piix3", "ich9":
|
|
|
|
// do nothing
|
|
|
|
default:
|
|
|
|
errs = packersdk.MultiErrorAppend(
|
|
|
|
errs, errors.New("chipset can only be piix3 or ich9"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.Firmware == "" {
|
|
|
|
b.config.Firmware = "bios"
|
|
|
|
}
|
|
|
|
switch b.config.Firmware {
|
|
|
|
case "bios", "efi":
|
|
|
|
// do nothing
|
|
|
|
default:
|
|
|
|
errs = packersdk.MultiErrorAppend(
|
|
|
|
errs, errors.New("firmware can only be bios or efi"))
|
|
|
|
}
|
|
|
|
|
2013-06-23 20:43:40 -07:00
|
|
|
if b.config.DiskSize == 0 {
|
|
|
|
b.config.DiskSize = 40000
|
|
|
|
}
|
|
|
|
|
2013-09-05 11:00:08 -07:00
|
|
|
if b.config.HardDriveInterface == "" {
|
|
|
|
b.config.HardDriveInterface = "ide"
|
|
|
|
}
|
|
|
|
|
2021-02-20 19:59:39 +01:00
|
|
|
if b.config.NICType == "" {
|
|
|
|
b.config.NICType = "82540EM"
|
|
|
|
}
|
|
|
|
switch b.config.NICType {
|
2021-02-20 20:31:09 +01:00
|
|
|
case "82540EM", "82543GC", "82545EM", "Am79C970A", "Am79C973", "Am79C960", "virtio":
|
2021-02-20 19:59:39 +01:00
|
|
|
// do nothing
|
|
|
|
default:
|
|
|
|
errs = packersdk.MultiErrorAppend(
|
|
|
|
errs, errors.New("NIC type can only be 82540EM, 82543GC, 82545EM, Am79C970A, Am79C973, Am79C960 or virtio"))
|
|
|
|
}
|
|
|
|
|
2021-02-20 20:21:57 +01:00
|
|
|
if b.config.GfxController == "" {
|
|
|
|
b.config.GfxController = "vboxvga"
|
|
|
|
}
|
|
|
|
switch b.config.GfxController {
|
|
|
|
case "vboxvga", "vboxsvga", "vmsvga", "none":
|
|
|
|
// do nothing
|
|
|
|
default:
|
|
|
|
errs = packersdk.MultiErrorAppend(
|
|
|
|
errs, errors.New("Graphics controller type can only be vboxvga, vboxsvga, vmsvga, none"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.AudioController == "" {
|
|
|
|
b.config.AudioController = "ac97"
|
|
|
|
}
|
2021-02-20 20:09:16 +01:00
|
|
|
switch b.config.AudioController {
|
|
|
|
case "ac97", "hda", "sb16":
|
|
|
|
// do nothing
|
|
|
|
default:
|
|
|
|
errs = packersdk.MultiErrorAppend(
|
|
|
|
errs, errors.New("Audio controller type can only be ac97, hda or sb16"))
|
|
|
|
}
|
|
|
|
|
2013-06-11 15:57:20 -07:00
|
|
|
if b.config.GuestOSType == "" {
|
|
|
|
b.config.GuestOSType = "Other"
|
|
|
|
}
|
|
|
|
|
2014-05-23 21:14:24 -04:00
|
|
|
if b.config.ISOInterface == "" {
|
|
|
|
b.config.ISOInterface = "ide"
|
|
|
|
}
|
|
|
|
|
2019-02-08 09:15:15 -08:00
|
|
|
if b.config.GuestAdditionsInterface == "" {
|
|
|
|
b.config.GuestAdditionsInterface = b.config.ISOInterface
|
|
|
|
}
|
|
|
|
|
2015-06-22 09:25:15 -07:00
|
|
|
if b.config.VMName == "" {
|
|
|
|
b.config.VMName = fmt.Sprintf(
|
|
|
|
"packer-%s-%d", b.config.PackerBuildName, interpolate.InitTime.Unix())
|
|
|
|
}
|
|
|
|
|
2019-10-31 14:43:46 -07:00
|
|
|
switch b.config.HardDriveInterface {
|
2021-02-14 23:24:59 +01:00
|
|
|
case "ide", "sata", "scsi", "pcie", "virtio":
|
2019-10-31 14:43:46 -07:00
|
|
|
// do nothing
|
|
|
|
default:
|
2020-11-19 12:07:02 -08:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2021-02-14 23:24:59 +01:00
|
|
|
errs, errors.New("hard_drive_interface can only be ide, sata, pcie, scsi or virtio"))
|
2013-09-05 12:07:58 -07:00
|
|
|
}
|
|
|
|
|
2017-03-22 23:09:25 +11:00
|
|
|
if b.config.SATAPortCount == 0 {
|
|
|
|
b.config.SATAPortCount = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SATAPortCount > 30 {
|
2020-11-19 12:07:02 -08:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2017-03-22 23:09:25 +11:00
|
|
|
errs, errors.New("sata_port_count cannot be greater than 30"))
|
|
|
|
}
|
|
|
|
|
2019-10-31 15:21:52 -07:00
|
|
|
if b.config.NVMePortCount == 0 {
|
|
|
|
b.config.NVMePortCount = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.NVMePortCount > 255 {
|
2020-11-19 12:07:02 -08:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2019-10-31 15:21:52 -07:00
|
|
|
errs, errors.New("nvme_port_count cannot be greater than 255"))
|
|
|
|
}
|
|
|
|
|
2021-02-14 23:43:48 +01:00
|
|
|
if b.config.ISOInterface != "ide" && b.config.ISOInterface != "sata" && b.config.ISOInterface != "virtio" {
|
2020-11-19 12:07:02 -08:00
|
|
|
errs = packersdk.MultiErrorAppend(
|
2021-02-14 23:43:48 +01:00
|
|
|
errs, errors.New("iso_interface can only be ide, sata or virtio"))
|
2014-05-23 21:14:24 -04:00
|
|
|
}
|
|
|
|
|
2013-11-02 23:17:21 -05:00
|
|
|
// Warnings
|
|
|
|
if b.config.ShutdownCommand == "" {
|
|
|
|
warnings = append(warnings,
|
|
|
|
"A shutdown_command was not specified. Without a shutdown command, Packer\n"+
|
|
|
|
"will forcibly halt the virtual machine, which may result in data loss.")
|
|
|
|
}
|
|
|
|
|
2013-07-19 19:08:25 -04:00
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2019-12-16 21:23:05 -08:00
|
|
|
return nil, warnings, errs
|
2013-06-11 15:45:52 -07:00
|
|
|
}
|
|
|
|
|
2019-12-16 21:23:05 -08:00
|
|
|
return nil, warnings, nil
|
2013-06-11 15:12:45 -07:00
|
|
|
}
|
|
|
|
|
2020-11-19 15:10:00 -08:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2013-08-13 08:55:33 -07:00
|
|
|
// Create the driver that we'll use to communicate with VirtualBox
|
2013-12-21 15:00:48 -08:00
|
|
|
driver, err := vboxcommon.NewDriver()
|
2013-08-13 08:55:33 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating VirtualBox driver: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-06-11 15:45:52 -07:00
|
|
|
steps := []multistep.Step{
|
2014-05-04 17:56:57 +02:00
|
|
|
&vboxcommon.StepDownloadGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsURL: b.config.GuestAdditionsURL,
|
|
|
|
GuestAdditionsSHA256: b.config.GuestAdditionsSHA256,
|
2015-05-27 14:01:08 -07:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-04 17:56:57 +02:00
|
|
|
},
|
2020-11-11 15:04:28 -08:00
|
|
|
&commonsteps.StepDownload{
|
2020-05-28 11:02:09 +02:00
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
Description: "ISO",
|
|
|
|
Extension: b.config.TargetExtension,
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
Url: b.config.ISOUrls,
|
2013-08-15 14:05:36 -04:00
|
|
|
},
|
2020-11-11 15:04:28 -08:00
|
|
|
&commonsteps.StepOutputDir{
|
2013-12-21 15:20:15 -08:00
|
|
|
Force: b.config.PackerForce,
|
|
|
|
Path: b.config.OutputDir,
|
|
|
|
},
|
2020-11-11 15:04:28 -08:00
|
|
|
&commonsteps.StepCreateFloppy{
|
2016-10-01 09:04:50 +02:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 12:25:22 +00:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2013-07-09 12:29:40 -07:00
|
|
|
},
|
2020-11-11 15:04:28 -08:00
|
|
|
&commonsteps.StepCreateCD{
|
2020-09-11 14:15:17 -07:00
|
|
|
Files: b.config.CDConfig.CDFiles,
|
|
|
|
Label: b.config.CDConfig.CDLabel,
|
|
|
|
},
|
2020-02-07 10:38:48 +01:00
|
|
|
new(vboxcommon.StepHTTPIPDiscover),
|
2020-11-11 15:04:28 -08:00
|
|
|
&commonsteps.StepHTTPServer{
|
2014-09-05 11:52:55 -07:00
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2014-09-05 11:52:55 -07:00
|
|
|
},
|
2019-02-03 12:20:52 -05:00
|
|
|
&vboxcommon.StepSshKeyPair{
|
|
|
|
Debug: b.config.PackerDebug,
|
2019-02-04 12:47:18 -05:00
|
|
|
DebugKeyPath: fmt.Sprintf("%s.pem", b.config.PackerBuildName),
|
2019-02-03 12:20:52 -05:00
|
|
|
Comm: &b.config.Comm,
|
|
|
|
},
|
2013-12-21 16:04:41 -08:00
|
|
|
new(vboxcommon.StepSuppressMessages),
|
2013-06-11 16:12:19 -07:00
|
|
|
new(stepCreateVM),
|
2013-06-11 16:34:44 -07:00
|
|
|
new(stepCreateDisk),
|
2020-09-11 14:15:17 -07:00
|
|
|
&vboxcommon.StepAttachISOs{
|
2020-09-14 10:40:07 -07:00
|
|
|
AttachBootISO: true,
|
2020-09-11 14:15:17 -07:00
|
|
|
ISOInterface: b.config.ISOInterface,
|
2019-02-08 09:15:15 -08:00
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsInterface: b.config.GuestAdditionsInterface,
|
2014-05-04 17:56:57 +02:00
|
|
|
},
|
2015-08-22 23:41:08 +01:00
|
|
|
&vboxcommon.StepConfigureVRDP{
|
2016-05-23 14:24:16 +01:00
|
|
|
VRDPBindAddress: b.config.VRDPBindAddress,
|
|
|
|
VRDPPortMin: b.config.VRDPPortMin,
|
|
|
|
VRDPPortMax: b.config.VRDPPortMax,
|
2015-08-22 23:41:08 +01:00
|
|
|
},
|
2013-12-22 08:10:11 -08:00
|
|
|
new(vboxcommon.StepAttachFloppy),
|
2020-01-09 08:37:30 -08:00
|
|
|
&vboxcommon.StepPortForwarding{
|
|
|
|
CommConfig: &b.config.CommConfig.Comm,
|
|
|
|
HostPortMin: b.config.HostPortMin,
|
|
|
|
HostPortMax: b.config.HostPortMax,
|
|
|
|
SkipNatMapping: b.config.SkipNatMapping,
|
2013-12-22 09:08:09 -08:00
|
|
|
},
|
2013-12-22 09:24:29 -08:00
|
|
|
&vboxcommon.StepVBoxManage{
|
|
|
|
Commands: b.config.VBoxManage,
|
2015-05-27 14:01:08 -07:00
|
|
|
Ctx: b.config.ctx,
|
2013-12-22 09:24:29 -08:00
|
|
|
},
|
2013-12-22 10:30:12 -08:00
|
|
|
&vboxcommon.StepRun{
|
|
|
|
Headless: b.config.Headless,
|
|
|
|
},
|
2014-05-12 19:02:30 -07:00
|
|
|
&vboxcommon.StepTypeBootCommand{
|
2018-08-22 11:25:43 -07:00
|
|
|
BootWait: b.config.BootWait,
|
|
|
|
BootCommand: b.config.FlatBootCommand(),
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
Ctx: b.config.ctx,
|
|
|
|
GroupInterval: b.config.BootConfig.BootGroupInterval,
|
2019-02-03 12:20:52 -05:00
|
|
|
Comm: &b.config.Comm,
|
2014-05-12 19:02:30 -07:00
|
|
|
},
|
2015-06-13 18:08:12 -04:00
|
|
|
&communicator.StepConnect{
|
2020-01-09 08:37:30 -08:00
|
|
|
Config: &b.config.CommConfig.Comm,
|
|
|
|
Host: vboxcommon.CommHost(b.config.CommConfig.Comm.SSHHost),
|
|
|
|
SSHConfig: b.config.CommConfig.Comm.SSHConfigFunc(),
|
|
|
|
SSHPort: vboxcommon.CommPort,
|
|
|
|
WinRMPort: vboxcommon.CommPort,
|
2013-07-15 14:17:09 +09:00
|
|
|
},
|
2013-12-22 11:50:29 -08:00
|
|
|
&vboxcommon.StepUploadVersion{
|
2017-03-16 18:04:36 -07:00
|
|
|
Path: *b.config.VBoxVersionFile,
|
2013-12-22 11:50:29 -08:00
|
|
|
},
|
2014-05-04 17:56:57 +02:00
|
|
|
&vboxcommon.StepUploadGuestAdditions{
|
|
|
|
GuestAdditionsMode: b.config.GuestAdditionsMode,
|
|
|
|
GuestAdditionsPath: b.config.GuestAdditionsPath,
|
2015-05-27 14:01:08 -07:00
|
|
|
Ctx: b.config.ctx,
|
2014-05-04 17:56:57 +02:00
|
|
|
},
|
2020-11-11 15:04:28 -08:00
|
|
|
new(commonsteps.StepProvision),
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2020-01-09 08:37:30 -08:00
|
|
|
Comm: &b.config.CommConfig.Comm,
|
2018-09-14 11:03:23 -07:00
|
|
|
},
|
2013-12-22 09:37:27 -08:00
|
|
|
&vboxcommon.StepShutdown{
|
2019-12-05 14:34:56 +01:00
|
|
|
Command: b.config.ShutdownCommand,
|
|
|
|
Timeout: b.config.ShutdownTimeout,
|
|
|
|
Delay: b.config.PostShutdownDelay,
|
|
|
|
DisableShutdown: b.config.DisableShutdown,
|
2020-01-09 08:36:19 -08:00
|
|
|
ACPIShutdown: b.config.ACPIShutdown,
|
2013-12-22 09:37:27 -08:00
|
|
|
},
|
2018-02-26 10:47:59 -06:00
|
|
|
&vboxcommon.StepRemoveDevices{
|
2020-09-11 14:15:17 -07:00
|
|
|
Bundling: b.config.VBoxBundleConfig,
|
2018-02-26 10:47:59 -06:00
|
|
|
},
|
2014-03-12 16:14:44 -07:00
|
|
|
&vboxcommon.StepVBoxManage{
|
|
|
|
Commands: b.config.VBoxManagePost,
|
2015-05-27 14:01:08 -07:00
|
|
|
Ctx: b.config.ctx,
|
2014-03-12 16:14:44 -07:00
|
|
|
},
|
2013-12-22 10:40:39 -08:00
|
|
|
&vboxcommon.StepExport{
|
2014-03-27 17:11:34 +11:00
|
|
|
Format: b.config.Format,
|
|
|
|
OutputDir: b.config.OutputDir,
|
2020-05-05 16:09:05 -07:00
|
|
|
OutputFilename: b.config.OutputFilename,
|
2019-06-19 16:34:14 +02:00
|
|
|
ExportOpts: b.config.ExportConfig.ExportOpts,
|
2018-02-26 10:47:59 -06:00
|
|
|
Bundling: b.config.VBoxBundleConfig,
|
2020-01-09 08:37:30 -08:00
|
|
|
SkipNatMapping: b.config.SkipNatMapping,
|
2016-12-29 13:19:59 +00:00
|
|
|
SkipExport: b.config.SkipExport,
|
2013-12-22 10:40:39 -08:00
|
|
|
},
|
2013-06-11 15:45:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the state bag
|
2013-08-31 12:44:58 -07:00
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
2016-05-17 04:03:45 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2013-08-31 12:44:58 -07:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
2013-06-11 15:45:52 -07:00
|
|
|
|
|
|
|
// Run
|
2020-11-11 15:04:28 -08:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 14:53:28 +01:00
|
|
|
b.runner.Run(ctx, state)
|
2013-06-11 15:45:52 -07:00
|
|
|
|
2013-06-19 21:07:53 -07:00
|
|
|
// If there was an error, return that
|
2013-08-31 12:44:58 -07:00
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
2013-06-19 21:07:53 -07:00
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
2013-06-27 22:24:53 -04:00
|
|
|
// If we were interrupted or cancelled, then just exit.
|
2013-08-31 12:44:58 -07:00
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
2013-06-27 22:24:53 -04:00
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
2013-08-31 12:44:58 -07:00
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
2013-06-27 22:24:53 -04:00
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
2020-01-30 11:27:58 +01:00
|
|
|
generatedData := map[string]interface{}{"generated_data": state.Get("generated_data")}
|
|
|
|
return vboxcommon.NewArtifact(b.config.OutputDir, generatedData)
|
2013-06-11 15:12:45 -07:00
|
|
|
}
|