2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2015-06-22 20:56:35 -04:00
|
|
|
"runtime"
|
2013-09-02 23:23:52 -04:00
|
|
|
"time"
|
2014-09-03 23:23:39 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/common"
|
|
|
|
"github.com/hashicorp/packer/helper/communicator"
|
|
|
|
"github.com/hashicorp/packer/helper/config"
|
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2014-09-03 23:23:39 -04:00
|
|
|
"github.com/mitchellh/multistep"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
2013-09-03 11:08:04 -04:00
|
|
|
const BuilderId = "transcend.qemu"
|
|
|
|
|
2014-09-02 12:31:53 -04:00
|
|
|
var accels = map[string]struct{}{
|
2016-11-01 17:08:04 -04:00
|
|
|
"none": {},
|
|
|
|
"kvm": {},
|
|
|
|
"tcg": {},
|
|
|
|
"xen": {},
|
2014-09-02 12:31:53 -04:00
|
|
|
}
|
|
|
|
|
2013-09-03 11:08:04 -04:00
|
|
|
var netDevice = map[string]bool{
|
2014-09-02 12:31:53 -04:00
|
|
|
"ne2k_pci": true,
|
|
|
|
"i82551": true,
|
|
|
|
"i82557b": true,
|
|
|
|
"i82559er": true,
|
|
|
|
"rtl8139": true,
|
|
|
|
"e1000": true,
|
|
|
|
"pcnet": true,
|
|
|
|
"virtio": true,
|
|
|
|
"virtio-net": true,
|
2014-07-29 10:41:07 -04:00
|
|
|
"virtio-net-pci": true,
|
2014-09-02 12:31:53 -04:00
|
|
|
"usb-net": true,
|
|
|
|
"i82559a": true,
|
|
|
|
"i82559b": true,
|
|
|
|
"i82559c": true,
|
|
|
|
"i82550": true,
|
|
|
|
"i82562": true,
|
|
|
|
"i82557a": true,
|
|
|
|
"i82557c": true,
|
|
|
|
"i82801": true,
|
|
|
|
"vmxnet3": true,
|
|
|
|
"i82558a": true,
|
|
|
|
"i82558b": true,
|
2013-09-03 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var diskInterface = map[string]bool{
|
2015-09-08 06:58:21 -04:00
|
|
|
"ide": true,
|
|
|
|
"scsi": true,
|
|
|
|
"virtio": true,
|
|
|
|
"virtio-scsi": true,
|
2013-09-03 11:08:04 -04:00
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2014-10-05 17:09:45 -04:00
|
|
|
var diskCache = map[string]bool{
|
|
|
|
"writethrough": true,
|
|
|
|
"writeback": true,
|
|
|
|
"none": true,
|
|
|
|
"unsafe": true,
|
|
|
|
"directsync": true,
|
|
|
|
}
|
|
|
|
|
2015-05-17 13:48:58 -04:00
|
|
|
var diskDiscard = map[string]bool{
|
|
|
|
"unmap": true,
|
|
|
|
"ignore": true,
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
type Builder struct {
|
2015-05-27 16:39:43 -04:00
|
|
|
config Config
|
2013-09-02 23:23:52 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:39:43 -04:00
|
|
|
type Config struct {
|
2013-09-02 23:23:52 -04:00
|
|
|
common.PackerConfig `mapstructure:",squash"`
|
2015-11-01 17:29:24 -05:00
|
|
|
common.HTTPConfig `mapstructure:",squash"`
|
2015-10-20 19:27:47 -04:00
|
|
|
common.ISOConfig `mapstructure:",squash"`
|
2015-06-13 18:47:59 -04:00
|
|
|
Comm communicator.Config `mapstructure:",squash"`
|
2016-07-26 15:42:04 -04:00
|
|
|
common.FloppyConfig `mapstructure:",squash"`
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2016-12-16 20:33:33 -05:00
|
|
|
ISOSkipCache bool `mapstructure:"iso_skip_cache"`
|
|
|
|
Accelerator string `mapstructure:"accelerator"`
|
|
|
|
BootCommand []string `mapstructure:"boot_command"`
|
|
|
|
DiskInterface string `mapstructure:"disk_interface"`
|
|
|
|
DiskSize uint `mapstructure:"disk_size"`
|
|
|
|
DiskCache string `mapstructure:"disk_cache"`
|
|
|
|
DiskDiscard string `mapstructure:"disk_discard"`
|
|
|
|
SkipCompaction bool `mapstructure:"skip_compaction"`
|
|
|
|
DiskCompression bool `mapstructure:"disk_compression"`
|
|
|
|
Format string `mapstructure:"format"`
|
|
|
|
Headless bool `mapstructure:"headless"`
|
|
|
|
DiskImage bool `mapstructure:"disk_image"`
|
|
|
|
MachineType string `mapstructure:"machine_type"`
|
|
|
|
NetDevice string `mapstructure:"net_device"`
|
|
|
|
OutputDir string `mapstructure:"output_directory"`
|
|
|
|
QemuArgs [][]string `mapstructure:"qemuargs"`
|
|
|
|
QemuBinary string `mapstructure:"qemu_binary"`
|
|
|
|
ShutdownCommand string `mapstructure:"shutdown_command"`
|
|
|
|
SSHHostPortMin uint `mapstructure:"ssh_host_port_min"`
|
|
|
|
SSHHostPortMax uint `mapstructure:"ssh_host_port_max"`
|
|
|
|
UseDefaultDisplay bool `mapstructure:"use_default_display"`
|
|
|
|
VNCBindAddress string `mapstructure:"vnc_bind_address"`
|
|
|
|
VNCPortMin uint `mapstructure:"vnc_port_min"`
|
|
|
|
VNCPortMax uint `mapstructure:"vnc_port_max"`
|
|
|
|
VMName string `mapstructure:"vm_name"`
|
2014-02-21 18:18:03 -05:00
|
|
|
|
2015-06-13 18:47:59 -04:00
|
|
|
// These are deprecated, but we keep them around for BC
|
|
|
|
// TODO(@mitchellh): remove
|
|
|
|
SSHWaitTimeout time.Duration `mapstructure:"ssh_wait_timeout"`
|
|
|
|
|
2014-02-21 18:18:03 -05:00
|
|
|
// TODO(mitchellh): deprecate
|
2014-02-21 19:48:30 -05:00
|
|
|
RunOnce bool `mapstructure:"run_once"`
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
RawBootWait string `mapstructure:"boot_wait"`
|
|
|
|
RawShutdownTimeout string `mapstructure:"shutdown_timeout"`
|
|
|
|
|
|
|
|
bootWait time.Duration ``
|
|
|
|
shutdownTimeout time.Duration ``
|
2015-05-27 16:39:43 -04:00
|
|
|
ctx interpolate.Context
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:44:38 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, error) {
|
2015-05-27 16:39:43 -04:00
|
|
|
err := config.Decode(&b.config, &config.DecodeOpts{
|
2015-06-22 12:22:42 -04:00
|
|
|
Interpolate: true,
|
|
|
|
InterpolateContext: &b.config.ctx,
|
2015-05-27 16:39:43 -04:00
|
|
|
InterpolateFilter: &interpolate.RenderFilter{
|
|
|
|
Exclude: []string{
|
|
|
|
"boot_command",
|
|
|
|
"qemuargs",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}, raws...)
|
2013-09-02 23:23:52 -04:00
|
|
|
if err != nil {
|
2013-11-05 18:44:38 -05:00
|
|
|
return nil, err
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2016-07-26 15:42:04 -04:00
|
|
|
var errs *packer.MultiError
|
|
|
|
warnings := make([]string, 0)
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if b.config.DiskSize == 0 {
|
|
|
|
b.config.DiskSize = 40000
|
|
|
|
}
|
|
|
|
|
2014-10-05 17:09:45 -04:00
|
|
|
if b.config.DiskCache == "" {
|
|
|
|
b.config.DiskCache = "writeback"
|
|
|
|
}
|
|
|
|
|
2015-05-17 13:48:58 -04:00
|
|
|
if b.config.DiskDiscard == "" {
|
|
|
|
b.config.DiskDiscard = "ignore"
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if b.config.Accelerator == "" {
|
2015-06-22 20:56:35 -04:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
b.config.Accelerator = "tcg"
|
|
|
|
} else {
|
2016-05-20 01:53:53 -04:00
|
|
|
// /dev/kvm is a kernel module that may be loaded if kvm is
|
|
|
|
// installed and the host supports VT-x extensions. To make sure
|
|
|
|
// this will actually work we need to os.Open() it. If os.Open fails
|
|
|
|
// the kernel module was not installed or loaded correctly.
|
2016-05-17 09:26:59 -04:00
|
|
|
if fp, err := os.Open("/dev/kvm"); err != nil {
|
|
|
|
b.config.Accelerator = "tcg"
|
|
|
|
} else {
|
|
|
|
fp.Close()
|
|
|
|
b.config.Accelerator = "kvm"
|
|
|
|
}
|
2015-06-22 20:56:35 -04:00
|
|
|
}
|
2016-05-17 09:26:59 -04:00
|
|
|
log.Printf("use detected accelerator: %s", b.config.Accelerator)
|
|
|
|
} else {
|
|
|
|
log.Printf("use specified accelerator: %s", b.config.Accelerator)
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2014-08-12 03:02:03 -04:00
|
|
|
if b.config.MachineType == "" {
|
2014-10-03 11:37:03 -04:00
|
|
|
b.config.MachineType = "pc"
|
2014-08-12 03:02:03 -04:00
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if b.config.OutputDir == "" {
|
|
|
|
b.config.OutputDir = fmt.Sprintf("output-%s", b.config.PackerBuildName)
|
|
|
|
}
|
|
|
|
|
2014-02-21 18:22:58 -05:00
|
|
|
if b.config.QemuBinary == "" {
|
|
|
|
b.config.QemuBinary = "qemu-system-x86_64"
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if b.config.RawBootWait == "" {
|
|
|
|
b.config.RawBootWait = "10s"
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SSHHostPortMin == 0 {
|
|
|
|
b.config.SSHHostPortMin = 2222
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SSHHostPortMax == 0 {
|
|
|
|
b.config.SSHHostPortMax = 4444
|
|
|
|
}
|
|
|
|
|
2016-05-25 05:10:12 -04:00
|
|
|
if b.config.VNCBindAddress == "" {
|
|
|
|
b.config.VNCBindAddress = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if b.config.VNCPortMin == 0 {
|
|
|
|
b.config.VNCPortMin = 5900
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.VNCPortMax == 0 {
|
|
|
|
b.config.VNCPortMax = 6000
|
|
|
|
}
|
|
|
|
|
2015-08-21 19:46:29 -04:00
|
|
|
if b.config.VMName == "" {
|
|
|
|
b.config.VMName = fmt.Sprintf("packer-%s", b.config.PackerBuildName)
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2015-08-21 19:46:29 -04:00
|
|
|
if b.config.Format == "" {
|
|
|
|
b.config.Format = "qcow2"
|
2015-08-18 20:44:17 -04:00
|
|
|
}
|
|
|
|
|
2016-07-26 15:42:04 -04:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...)
|
2013-12-06 19:20:25 -05:00
|
|
|
|
2013-09-03 11:08:04 -04:00
|
|
|
if b.config.NetDevice == "" {
|
2013-10-09 08:11:10 -04:00
|
|
|
b.config.NetDevice = "virtio-net"
|
2013-09-03 11:08:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskInterface == "" {
|
|
|
|
b.config.DiskInterface = "virtio"
|
|
|
|
}
|
|
|
|
|
2015-06-13 18:47:59 -04:00
|
|
|
// TODO: backwards compatibility, write fixer instead
|
|
|
|
if b.config.SSHWaitTimeout != 0 {
|
|
|
|
b.config.Comm.SSHTimeout = b.config.SSHWaitTimeout
|
|
|
|
}
|
|
|
|
|
2016-01-25 04:01:37 -05:00
|
|
|
if b.config.ISOSkipCache {
|
|
|
|
b.config.ISOChecksumType = "none"
|
|
|
|
}
|
|
|
|
|
2015-10-20 19:27:47 -04:00
|
|
|
isoWarnings, isoErrs := b.config.ISOConfig.Prepare(&b.config.ctx)
|
|
|
|
warnings = append(warnings, isoWarnings...)
|
|
|
|
errs = packer.MultiErrorAppend(errs, isoErrs...)
|
|
|
|
|
2015-11-01 17:29:24 -05:00
|
|
|
errs = packer.MultiErrorAppend(errs, b.config.HTTPConfig.Prepare(&b.config.ctx)...)
|
2015-06-13 18:47:59 -04:00
|
|
|
if es := b.config.Comm.Prepare(&b.config.ctx); len(es) > 0 {
|
|
|
|
errs = packer.MultiErrorAppend(errs, es...)
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if !(b.config.Format == "qcow2" || b.config.Format == "raw") {
|
|
|
|
errs = packer.MultiErrorAppend(
|
2013-12-04 14:05:07 -05:00
|
|
|
errs, errors.New("invalid format, only 'qcow2' or 'raw' are allowed"))
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2015-10-29 06:54:25 -04:00
|
|
|
if b.config.Format != "qcow2" {
|
|
|
|
b.config.SkipCompaction = true
|
|
|
|
b.config.DiskCompression = false
|
|
|
|
}
|
|
|
|
|
2014-09-02 12:31:53 -04:00
|
|
|
if _, ok := accels[b.config.Accelerator]; !ok {
|
2013-09-02 23:23:52 -04:00
|
|
|
errs = packer.MultiErrorAppend(
|
2014-09-02 12:31:53 -04:00
|
|
|
errs, errors.New("invalid accelerator, only 'kvm', 'tcg', 'xen', or 'none' are allowed"))
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-09-03 11:08:04 -04:00
|
|
|
if _, ok := netDevice[b.config.NetDevice]; !ok {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("unrecognized network device type"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := diskInterface[b.config.DiskInterface]; !ok {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("unrecognized disk interface type"))
|
|
|
|
}
|
|
|
|
|
2014-10-05 17:09:45 -04:00
|
|
|
if _, ok := diskCache[b.config.DiskCache]; !ok {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("unrecognized disk cache type"))
|
|
|
|
}
|
|
|
|
|
2015-05-17 13:48:58 -04:00
|
|
|
if _, ok := diskDiscard[b.config.DiskDiscard]; !ok {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("unrecognized disk cache type"))
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if !b.config.PackerForce {
|
|
|
|
if _, err := os.Stat(b.config.OutputDir); err == nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs,
|
|
|
|
fmt.Errorf("Output directory '%s' already exists. It must not exist.", b.config.OutputDir))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b.config.bootWait, err = time.ParseDuration(b.config.RawBootWait)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing boot_wait: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RawShutdownTimeout == "" {
|
|
|
|
b.config.RawShutdownTimeout = "5m"
|
|
|
|
}
|
|
|
|
|
|
|
|
b.config.shutdownTimeout, err = time.ParseDuration(b.config.RawShutdownTimeout)
|
|
|
|
if err != nil {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("Failed parsing shutdown_timeout: %s", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SSHHostPortMin > b.config.SSHHostPortMax {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, errors.New("ssh_host_port_min must be less than ssh_host_port_max"))
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.VNCPortMin > b.config.VNCPortMax {
|
|
|
|
errs = packer.MultiErrorAppend(
|
|
|
|
errs, fmt.Errorf("vnc_port_min must be less than vnc_port_max"))
|
|
|
|
}
|
|
|
|
|
2013-12-06 19:20:25 -05:00
|
|
|
if b.config.QemuArgs == nil {
|
|
|
|
b.config.QemuArgs = make([][]string, 0)
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if errs != nil && len(errs.Errors) > 0 {
|
2014-10-31 09:07:04 -04:00
|
|
|
return warnings, errs
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2014-10-31 09:07:04 -04:00
|
|
|
return warnings, nil
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packer.Artifact, error) {
|
|
|
|
// Create the driver that we'll use to communicate with Qemu
|
2014-01-24 13:01:42 -05:00
|
|
|
driver, err := b.newDriver(b.config.QemuBinary)
|
2013-09-02 23:23:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating Qemu driver: %s", err)
|
|
|
|
}
|
|
|
|
|
2014-11-06 15:13:09 -05:00
|
|
|
steprun := &stepRun{}
|
|
|
|
if !b.config.DiskImage {
|
|
|
|
steprun.BootDrive = "once=d"
|
|
|
|
steprun.Message = "Starting VM, booting from CD-ROM"
|
|
|
|
} else {
|
|
|
|
steprun.BootDrive = "c"
|
|
|
|
steprun.Message = "Starting VM, booting disk image"
|
|
|
|
}
|
|
|
|
|
2016-01-25 04:01:37 -05:00
|
|
|
steps := []multistep.Step{}
|
|
|
|
if !b.config.ISOSkipCache {
|
|
|
|
steps = append(steps, &common.StepDownload{
|
2013-09-02 23:23:52 -04:00
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
ChecksumType: b.config.ISOChecksumType,
|
|
|
|
Description: "ISO",
|
2016-12-17 05:49:54 -05:00
|
|
|
Extension: b.config.TargetExtension,
|
2013-09-02 23:23:52 -04:00
|
|
|
ResultKey: "iso_path",
|
2015-07-28 07:45:02 -04:00
|
|
|
TargetPath: b.config.TargetPath,
|
2015-10-20 19:27:47 -04:00
|
|
|
Url: b.config.ISOUrls,
|
2013-09-02 23:23:52 -04:00
|
|
|
},
|
2016-01-25 04:01:37 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
steps = append(steps, &stepSetISO{
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
Url: b.config.ISOUrls,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps, new(stepPrepareOutputDir),
|
2013-12-06 19:20:25 -05:00
|
|
|
&common.StepCreateFloppy{
|
2016-10-11 17:43:50 -04:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
2016-09-28 00:31:42 -04:00
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2013-12-06 19:20:25 -05:00
|
|
|
},
|
2013-09-02 23:23:52 -04:00
|
|
|
new(stepCreateDisk),
|
2014-07-17 10:01:19 -04:00
|
|
|
new(stepCopyDisk),
|
|
|
|
new(stepResizeDisk),
|
2015-11-01 17:29:24 -05:00
|
|
|
&common.StepHTTPServer{
|
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
|
|
|
},
|
2016-07-06 11:52:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if b.config.Comm.Type != "none" {
|
|
|
|
steps = append(steps,
|
|
|
|
new(stepForwardSSH),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2013-09-02 23:23:52 -04:00
|
|
|
new(stepConfigureVNC),
|
2014-11-06 15:13:09 -05:00
|
|
|
steprun,
|
2014-01-30 02:46:54 -05:00
|
|
|
&stepBootWait{},
|
|
|
|
&stepTypeBootCommand{},
|
2016-07-06 11:52:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
if b.config.Comm.Type != "none" {
|
|
|
|
steps = append(steps,
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.Comm,
|
|
|
|
Host: commHost,
|
|
|
|
SSHConfig: sshConfig,
|
|
|
|
SSHPort: commPort,
|
2016-12-23 18:15:51 -05:00
|
|
|
WinRMPort: commPort,
|
2016-07-06 11:52:40 -04:00
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps,
|
2013-09-02 23:23:52 -04:00
|
|
|
new(common.StepProvision),
|
2016-07-06 11:52:40 -04:00
|
|
|
)
|
|
|
|
steps = append(steps,
|
2013-09-02 23:23:52 -04:00
|
|
|
new(stepShutdown),
|
2016-07-06 11:52:40 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
steps = append(steps,
|
2015-10-29 06:54:25 -04:00
|
|
|
new(stepConvertDisk),
|
2016-01-25 04:01:37 -05:00
|
|
|
)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
// Setup the state bag
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("cache", cache)
|
|
|
|
state.Put("config", &b.config)
|
2016-05-17 17:14:50 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2013-09-02 23:23:52 -04:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Run
|
2016-09-13 20:04:18 -04:00
|
|
|
b.runner = common.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2013-09-02 23:23:52 -04:00
|
|
|
b.runner.Run(state)
|
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the artifact list
|
|
|
|
files := make([]string, 0, 5)
|
|
|
|
visit := func(path string, info os.FileInfo, err error) error {
|
2017-01-26 19:32:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
if !info.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:32:21 -05:00
|
|
|
return nil
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := filepath.Walk(b.config.OutputDir, visit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
2014-10-28 11:43:19 -04:00
|
|
|
dir: b.config.OutputDir,
|
|
|
|
f: files,
|
2014-01-05 20:02:30 -05:00
|
|
|
state: make(map[string]interface{}),
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2014-01-05 20:02:30 -05:00
|
|
|
artifact.state["diskName"] = state.Get("disk_filename").(string)
|
|
|
|
artifact.state["diskType"] = b.config.Format
|
|
|
|
artifact.state["diskSize"] = uint64(b.config.DiskSize)
|
|
|
|
artifact.state["domainType"] = b.config.Accelerator
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builder) Cancel() {
|
|
|
|
if b.runner != nil {
|
|
|
|
log.Println("Cancelling the step runner...")
|
|
|
|
b.runner.Cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-24 13:01:42 -05:00
|
|
|
func (b *Builder) newDriver(qemuBinary string) (Driver, error) {
|
|
|
|
qemuPath, err := exec.LookPath(qemuBinary)
|
2013-09-02 23:23:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
qemuImgPath, err := exec.LookPath("qemu-img")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Qemu path: %s, Qemu Image page: %s", qemuPath, qemuImgPath)
|
2013-11-06 00:43:20 -05:00
|
|
|
driver := &QemuDriver{
|
|
|
|
QemuPath: qemuPath,
|
|
|
|
QemuImgPath: qemuImgPath,
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
if err := driver.Verify(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return driver, nil
|
|
|
|
}
|