2013-12-24 00:58:41 -05:00
|
|
|
package iso
|
2013-06-04 19:52:59 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-04 19:52:59 -04:00
|
|
|
"fmt"
|
2013-08-27 20:23:22 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-06-04 19:52:59 -04:00
|
|
|
"path/filepath"
|
2017-02-27 16:34:53 -05:00
|
|
|
"runtime"
|
2015-11-11 07:39:06 -05:00
|
|
|
"strings"
|
2015-05-27 17:16:28 -04:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
2013-06-04 19:52:59 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type vmxTemplateData struct {
|
2017-12-25 15:08:08 -05:00
|
|
|
Name string
|
|
|
|
GuestOS string
|
|
|
|
ISOPath string
|
|
|
|
Version string
|
|
|
|
|
|
|
|
SCSI_Present string
|
|
|
|
SCSI_diskAdapterType string
|
|
|
|
SATA_Present string
|
|
|
|
NVME_Present string
|
|
|
|
|
|
|
|
DiskName string
|
|
|
|
DiskType string
|
|
|
|
CDROMType string
|
|
|
|
CDROMType_MasterSlave string
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2018-01-16 14:17:37 -05:00
|
|
|
Network_Type string
|
|
|
|
Network_Device string
|
|
|
|
Network_Adapter string
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
Sound_Present string
|
2017-02-27 16:34:53 -05:00
|
|
|
Usb_Present string
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
Serial_Present string
|
|
|
|
Serial_Type string
|
2015-11-11 07:39:06 -05:00
|
|
|
Serial_Endpoint string
|
2017-02-27 16:34:53 -05:00
|
|
|
Serial_Host string
|
|
|
|
Serial_Yield string
|
2015-11-11 07:39:06 -05:00
|
|
|
Serial_Filename string
|
2017-02-27 16:34:53 -05:00
|
|
|
Serial_Auto string
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
Parallel_Present string
|
2015-11-11 07:39:06 -05:00
|
|
|
Parallel_Bidirectional string
|
2017-02-27 16:34:53 -05:00
|
|
|
Parallel_Filename string
|
|
|
|
Parallel_Auto string
|
2013-06-04 19:52:59 -04:00
|
|
|
}
|
|
|
|
|
2014-03-04 13:23:07 -05:00
|
|
|
type additionalDiskTemplateData struct {
|
2014-03-04 15:00:24 -05:00
|
|
|
DiskNumber int
|
|
|
|
DiskName string
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
|
|
|
|
2013-06-05 17:47:19 -04:00
|
|
|
// This step creates the VMX file for the VM.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
2013-06-10 00:50:15 -04:00
|
|
|
// iso_path string
|
2013-06-05 17:47:19 -04:00
|
|
|
// ui packer.Ui
|
|
|
|
//
|
|
|
|
// Produces:
|
2013-06-05 17:49:04 -04:00
|
|
|
// vmx_path string - The path to the VMX file.
|
2013-11-08 00:02:12 -05:00
|
|
|
type stepCreateVMX struct {
|
|
|
|
tempDir string
|
|
|
|
}
|
2013-06-04 19:52:59 -04:00
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
/* serial conversions */
|
|
|
|
type serialConfigPipe struct {
|
|
|
|
filename string
|
|
|
|
endpoint string
|
2017-02-27 16:34:53 -05:00
|
|
|
host string
|
|
|
|
yield string
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type serialConfigFile struct {
|
|
|
|
filename string
|
2017-02-27 16:34:53 -05:00
|
|
|
yield string
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type serialConfigDevice struct {
|
|
|
|
devicename string
|
2017-02-27 16:34:53 -05:00
|
|
|
yield string
|
2016-10-05 22:51:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type serialConfigAuto struct {
|
|
|
|
devicename string
|
2017-02-27 16:34:53 -05:00
|
|
|
yield string
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type serialUnion struct {
|
|
|
|
serialType interface{}
|
2017-02-27 16:34:53 -05:00
|
|
|
pipe *serialConfigPipe
|
|
|
|
file *serialConfigFile
|
|
|
|
device *serialConfigDevice
|
|
|
|
auto *serialConfigAuto
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
func unformat_serial(config string) (*serialUnion, error) {
|
2016-10-05 22:51:42 -04:00
|
|
|
var defaultSerialPort string
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
defaultSerialPort = "COM1"
|
|
|
|
} else {
|
|
|
|
defaultSerialPort = "/dev/ttyS0"
|
|
|
|
}
|
|
|
|
|
|
|
|
input := strings.SplitN(config, ":", 2)
|
|
|
|
if len(input) < 1 {
|
2017-02-27 16:34:53 -05:00
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port: %s", config)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
|
|
|
var formatType, formatOptions string
|
|
|
|
formatType = input[0]
|
|
|
|
if len(input) == 2 {
|
|
|
|
formatOptions = input[1]
|
|
|
|
} else {
|
|
|
|
formatOptions = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToUpper(formatType) {
|
2017-02-27 16:34:53 -05:00
|
|
|
case "PIPE":
|
|
|
|
comp := strings.Split(formatOptions, ",")
|
|
|
|
if len(comp) < 3 || len(comp) > 4 {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : pipe : %s", config)
|
|
|
|
}
|
|
|
|
if res := strings.ToLower(comp[1]); res != "client" && res != "server" {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : pipe : endpoint : %s : %s", res, config)
|
|
|
|
}
|
|
|
|
if res := strings.ToLower(comp[2]); res != "app" && res != "vm" {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : pipe : host : %s : %s", res, config)
|
|
|
|
}
|
|
|
|
res := &serialConfigPipe{
|
|
|
|
filename: comp[0],
|
|
|
|
endpoint: comp[1],
|
|
|
|
host: map[string]string{"app": "TRUE", "vm": "FALSE"}[strings.ToLower(comp[2])],
|
|
|
|
yield: "FALSE",
|
|
|
|
}
|
|
|
|
if len(comp) == 4 {
|
|
|
|
res.yield = strings.ToUpper(comp[3])
|
|
|
|
}
|
|
|
|
if res.yield != "TRUE" && res.yield != "FALSE" {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : pipe : yield : %s : %s", res.yield, config)
|
|
|
|
}
|
|
|
|
return &serialUnion{serialType: res, pipe: res}, nil
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
case "FILE":
|
|
|
|
comp := strings.Split(formatOptions, ",")
|
|
|
|
if len(comp) > 2 {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : file : %s", config)
|
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
res := &serialConfigFile{yield: "FALSE"}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
res.filename = filepath.FromSlash(comp[0])
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
res.yield = map[bool]string{true: strings.ToUpper(comp[0]), false: "FALSE"}[len(comp) > 1]
|
|
|
|
if res.yield != "TRUE" && res.yield != "FALSE" {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : file : yield : %s : %s", res.yield, config)
|
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
return &serialUnion{serialType: res, file: res}, nil
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
case "DEVICE":
|
|
|
|
comp := strings.Split(formatOptions, ",")
|
|
|
|
if len(comp) > 2 {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : device : %s", config)
|
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
res := new(serialConfigDevice)
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
if len(comp) == 2 {
|
|
|
|
res.devicename = map[bool]string{true: filepath.FromSlash(comp[0]), false: defaultSerialPort}[len(comp[0]) > 0]
|
|
|
|
res.yield = strings.ToUpper(comp[1])
|
|
|
|
} else if len(comp) == 1 {
|
|
|
|
res.devicename = map[bool]string{true: filepath.FromSlash(comp[0]), false: defaultSerialPort}[len(comp[0]) > 0]
|
|
|
|
res.yield = "FALSE"
|
|
|
|
} else if len(comp) == 0 {
|
|
|
|
res.devicename = defaultSerialPort
|
|
|
|
res.yield = "FALSE"
|
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
if res.yield != "TRUE" && res.yield != "FALSE" {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : device : yield : %s : %s", res.yield, config)
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
return &serialUnion{serialType: res, device: res}, nil
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
case "AUTO":
|
|
|
|
res := new(serialConfigAuto)
|
|
|
|
res.devicename = defaultSerialPort
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
if len(formatOptions) > 0 {
|
|
|
|
res.yield = strings.ToUpper(formatOptions)
|
|
|
|
} else {
|
|
|
|
res.yield = "FALSE"
|
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
if res.yield != "TRUE" && res.yield != "FALSE" {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for serial port : auto : yield : %s : %s", res.yield, config)
|
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
return &serialUnion{serialType: res, auto: res}, nil
|
|
|
|
|
2017-04-12 18:41:36 -04:00
|
|
|
case "NONE":
|
|
|
|
return &serialUnion{serialType: nil}, nil
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown serial type : %s : %s", strings.ToUpper(formatType), config)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parallel port */
|
|
|
|
type parallelUnion struct {
|
|
|
|
parallelType interface{}
|
2017-02-27 16:34:53 -05:00
|
|
|
file *parallelPortFile
|
|
|
|
device *parallelPortDevice
|
|
|
|
auto *parallelPortAuto
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
type parallelPortFile struct {
|
|
|
|
filename string
|
|
|
|
}
|
|
|
|
type parallelPortDevice struct {
|
|
|
|
bidirectional string
|
2017-02-27 16:34:53 -05:00
|
|
|
devicename string
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
type parallelPortAuto struct {
|
|
|
|
bidirectional string
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
func unformat_parallel(config string) (*parallelUnion, error) {
|
2016-10-05 22:51:42 -04:00
|
|
|
input := strings.SplitN(config, ":", 2)
|
|
|
|
if len(input) < 1 {
|
2017-02-27 16:34:53 -05:00
|
|
|
return nil, fmt.Errorf("Unexpected format for parallel port: %s", config)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2016-10-05 22:51:42 -04:00
|
|
|
|
|
|
|
var formatType, formatOptions string
|
|
|
|
formatType = input[0]
|
|
|
|
if len(input) == 2 {
|
|
|
|
formatOptions = input[1]
|
|
|
|
} else {
|
|
|
|
formatOptions = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
switch strings.ToUpper(formatType) {
|
2017-02-27 16:34:53 -05:00
|
|
|
case "FILE":
|
|
|
|
res := ¶llelPortFile{filename: filepath.FromSlash(formatOptions)}
|
|
|
|
return ¶llelUnion{parallelType: res, file: res}, nil
|
|
|
|
case "DEVICE":
|
|
|
|
comp := strings.Split(formatOptions, ",")
|
|
|
|
if len(comp) < 1 || len(comp) > 2 {
|
|
|
|
return nil, fmt.Errorf("Unexpected format for parallel port: %s", config)
|
|
|
|
}
|
|
|
|
res := new(parallelPortDevice)
|
|
|
|
res.bidirectional = "FALSE"
|
|
|
|
res.devicename = filepath.FromSlash(comp[0])
|
|
|
|
if len(comp) > 1 {
|
|
|
|
switch strings.ToUpper(comp[1]) {
|
|
|
|
case "BI":
|
|
|
|
res.bidirectional = "TRUE"
|
|
|
|
case "UNI":
|
|
|
|
res.bidirectional = "FALSE"
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown parallel port direction : %s : %s", strings.ToUpper(comp[0]), config)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2017-02-27 16:34:53 -05:00
|
|
|
}
|
|
|
|
return ¶llelUnion{parallelType: res, device: res}, nil
|
|
|
|
|
|
|
|
case "AUTO":
|
|
|
|
res := new(parallelPortAuto)
|
|
|
|
switch strings.ToUpper(formatOptions) {
|
|
|
|
case "":
|
|
|
|
fallthrough
|
|
|
|
case "UNI":
|
2015-11-11 07:39:06 -05:00
|
|
|
res.bidirectional = "FALSE"
|
2017-02-27 16:34:53 -05:00
|
|
|
case "BI":
|
|
|
|
res.bidirectional = "TRUE"
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("Unknown parallel port direction : %s : %s", strings.ToUpper(formatOptions), config)
|
|
|
|
}
|
|
|
|
return ¶llelUnion{parallelType: res, auto: res}, nil
|
2017-04-12 18:41:36 -04:00
|
|
|
|
|
|
|
case "NONE":
|
|
|
|
return ¶llelUnion{parallelType: nil}, nil
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2017-04-12 18:41:36 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
return nil, fmt.Errorf("Unexpected format for parallel port: %s", config)
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* regular steps */
|
2018-01-22 18:31:41 -05:00
|
|
|
func (s *stepCreateVMX) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-05-27 17:16:28 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-08-31 15:50:25 -04:00
|
|
|
isoPath := state.Get("iso_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2013-06-04 19:52:59 -04:00
|
|
|
|
2017-12-25 15:08:08 -05:00
|
|
|
// Convert the iso_path into a path relative to the .vmx file if possible
|
|
|
|
if relativeIsoPath, err := filepath.Rel(config.VMXTemplatePath, filepath.FromSlash(isoPath)); err == nil {
|
|
|
|
isoPath = relativeIsoPath
|
|
|
|
}
|
|
|
|
|
2013-06-07 19:20:58 -04:00
|
|
|
ui.Say("Building and writing VMX file")
|
2013-06-04 19:52:59 -04:00
|
|
|
|
2013-08-27 20:23:22 -04:00
|
|
|
vmxTemplate := DefaultVMXTemplate
|
|
|
|
if config.VMXTemplatePath != "" {
|
|
|
|
f, err := os.Open(config.VMXTemplatePath)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error reading VMX template: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-08-27 20:23:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2013-06-07 19:20:58 -04:00
|
|
|
|
2013-08-27 20:23:22 -04:00
|
|
|
rawBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error reading VMX template: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-08-27 20:23:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxTemplate = string(rawBytes)
|
|
|
|
}
|
2014-03-04 15:00:24 -05:00
|
|
|
|
2015-06-04 14:44:07 -04:00
|
|
|
ctx := config.ctx
|
|
|
|
|
2014-03-04 13:23:07 -05:00
|
|
|
if len(config.AdditionalDiskSize) > 0 {
|
2016-11-01 17:08:04 -04:00
|
|
|
for i := range config.AdditionalDiskSize {
|
2015-06-04 14:44:07 -04:00
|
|
|
ctx.Data = &additionalDiskTemplateData{
|
2014-03-04 15:00:24 -05:00
|
|
|
DiskNumber: i + 1,
|
|
|
|
DiskName: config.DiskName,
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
2014-03-04 15:00:24 -05:00
|
|
|
|
2015-06-15 15:40:34 -04:00
|
|
|
diskTemplate := DefaultAdditionalDiskTemplate
|
|
|
|
if config.VMXDiskTemplatePath != "" {
|
|
|
|
f, err := os.Open(config.VMXDiskTemplatePath)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error reading VMX disk template: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
rawBytes, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error reading VMX disk template: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
diskTemplate = string(rawBytes)
|
|
|
|
}
|
|
|
|
|
|
|
|
diskContents, err := interpolate.Render(diskTemplate, &ctx)
|
2014-03-04 13:23:07 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error preparing VMX template for additional disk: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2014-03-04 15:00:24 -05:00
|
|
|
|
2015-06-15 15:40:34 -04:00
|
|
|
vmxTemplate += diskContents
|
2014-03-04 13:23:07 -05:00
|
|
|
}
|
|
|
|
}
|
2013-08-27 20:23:22 -04:00
|
|
|
|
2015-11-11 07:39:06 -05:00
|
|
|
templateData := vmxTemplateData{
|
2015-06-04 14:44:07 -04:00
|
|
|
Name: config.VMName,
|
|
|
|
GuestOS: config.GuestOSType,
|
|
|
|
DiskName: config.DiskName,
|
|
|
|
Version: config.Version,
|
|
|
|
ISOPath: isoPath,
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-12-25 15:08:08 -05:00
|
|
|
SCSI_Present: "FALSE",
|
|
|
|
SCSI_diskAdapterType: "lsilogic",
|
|
|
|
SATA_Present: "FALSE",
|
|
|
|
NVME_Present: "FALSE",
|
|
|
|
|
|
|
|
DiskType: "scsi",
|
|
|
|
CDROMType: "ide",
|
|
|
|
CDROMType_MasterSlave: "0",
|
|
|
|
|
2018-01-16 14:17:37 -05:00
|
|
|
Network_Adapter: "e1000",
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
Sound_Present: map[bool]string{true: "TRUE", false: "FALSE"}[bool(config.Sound)],
|
|
|
|
Usb_Present: map[bool]string{true: "TRUE", false: "FALSE"}[bool(config.USB)],
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
Serial_Present: "FALSE",
|
|
|
|
Parallel_Present: "FALSE",
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
|
2017-12-25 15:08:08 -05:00
|
|
|
/// Use the disk adapter type that the user specified to tweak the .vmx
|
|
|
|
// Also sync the cdrom adapter type according to what's common for that disk type.
|
|
|
|
diskAdapterType := strings.ToLower(config.DiskAdapterType)
|
|
|
|
switch diskAdapterType {
|
|
|
|
case "ide":
|
|
|
|
templateData.DiskType = "ide"
|
|
|
|
templateData.CDROMType = "ide"
|
|
|
|
templateData.CDROMType_MasterSlave = "1"
|
|
|
|
case "sata":
|
|
|
|
templateData.SATA_Present = "TRUE"
|
|
|
|
templateData.DiskType = "sata"
|
|
|
|
templateData.CDROMType = "sata"
|
|
|
|
templateData.CDROMType_MasterSlave = "1"
|
|
|
|
case "nvme":
|
|
|
|
templateData.NVME_Present = "TRUE"
|
|
|
|
templateData.DiskType = "nvme"
|
|
|
|
templateData.SATA_Present = "TRUE"
|
|
|
|
templateData.CDROMType = "sata"
|
|
|
|
templateData.CDROMType_MasterSlave = "0"
|
|
|
|
case "scsi":
|
|
|
|
diskAdapterType = "lsilogic"
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
templateData.SCSI_Present = "TRUE"
|
|
|
|
templateData.SCSI_diskAdapterType = diskAdapterType
|
|
|
|
templateData.DiskType = "scsi"
|
|
|
|
templateData.CDROMType = "ide"
|
|
|
|
templateData.CDROMType_MasterSlave = "0"
|
|
|
|
}
|
|
|
|
|
2018-01-16 14:17:37 -05:00
|
|
|
/// Handle the cdrom adapter type. If the disk adapter type and the
|
|
|
|
// cdrom adapter type are the same, then ensure that the cdrom is the
|
|
|
|
// slave device on whatever bus the disk adapter is on.
|
|
|
|
cdromAdapterType := strings.ToLower(config.CdromAdapterType)
|
|
|
|
if cdromAdapterType == diskAdapterType {
|
|
|
|
templateData.CDROMType_MasterSlave = "1"
|
|
|
|
} else {
|
|
|
|
templateData.CDROMType_MasterSlave = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cdromAdapterType {
|
|
|
|
case "ide":
|
|
|
|
templateData.CDROMType = "ide"
|
|
|
|
case "sata":
|
|
|
|
templateData.SATA_Present = "TRUE"
|
|
|
|
templateData.CDROMType = "sata"
|
|
|
|
case "scsi":
|
|
|
|
templateData.SCSI_Present = "TRUE"
|
|
|
|
templateData.CDROMType = "scsi"
|
|
|
|
default:
|
|
|
|
err := fmt.Errorf("Error procesing VMX template: %s", cdromAdapterType)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Assign the network adapter type into the template if one was specified.
|
|
|
|
network_adapter := strings.ToLower(config.NetworkAdapterType)
|
|
|
|
if network_adapter != "" {
|
|
|
|
templateData.Network_Adapter = network_adapter
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:51:42 -04:00
|
|
|
/// Check the network type that the user specified
|
|
|
|
network := config.Network
|
2017-04-13 18:39:55 -04:00
|
|
|
driver := state.Get("driver").(vmwcommon.Driver).GetVmwareDriver()
|
2016-10-05 22:51:42 -04:00
|
|
|
|
|
|
|
// read netmap config
|
2017-10-23 21:26:16 -04:00
|
|
|
netmap, err := driver.NetworkMapper()
|
|
|
|
if err != nil {
|
2016-10-05 22:51:42 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// try and convert the specified network to a device
|
2017-02-27 16:34:53 -05:00
|
|
|
device, err := netmap.NameIntoDevice(network)
|
2016-10-05 22:51:42 -04:00
|
|
|
|
|
|
|
// success. so we know that it's an actual network type inside netmap.conf
|
|
|
|
if err == nil {
|
|
|
|
templateData.Network_Type = network
|
|
|
|
templateData.Network_Device = device
|
2017-02-27 16:34:53 -05:00
|
|
|
// we were unable to find the type, so assume it's a custom network device.
|
2016-10-05 22:51:42 -04:00
|
|
|
} else {
|
|
|
|
templateData.Network_Type = "custom"
|
|
|
|
templateData.Network_Device = network
|
|
|
|
}
|
2015-11-11 07:39:06 -05:00
|
|
|
// store the network so that we can later figure out what ip address to bind to
|
2016-10-05 22:51:42 -04:00
|
|
|
state.Put("vmnetwork", network)
|
2015-11-11 07:39:06 -05:00
|
|
|
|
2016-10-05 22:51:42 -04:00
|
|
|
/// check if serial port has been configured
|
2017-04-13 21:01:10 -04:00
|
|
|
if config.Serial == "" {
|
|
|
|
templateData.Serial_Present = "FALSE"
|
|
|
|
} else {
|
2017-02-27 16:34:53 -05:00
|
|
|
serial, err := unformat_serial(config.Serial)
|
2015-11-11 07:39:06 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error procesing VMX template: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
templateData.Serial_Present = "TRUE"
|
|
|
|
templateData.Serial_Filename = ""
|
|
|
|
templateData.Serial_Yield = ""
|
|
|
|
templateData.Serial_Endpoint = ""
|
|
|
|
templateData.Serial_Host = ""
|
2016-10-05 22:51:42 -04:00
|
|
|
templateData.Serial_Auto = "FALSE"
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
switch serial.serialType.(type) {
|
2017-02-27 16:34:53 -05:00
|
|
|
case *serialConfigPipe:
|
|
|
|
templateData.Serial_Type = "pipe"
|
|
|
|
templateData.Serial_Endpoint = serial.pipe.endpoint
|
|
|
|
templateData.Serial_Host = serial.pipe.host
|
|
|
|
templateData.Serial_Yield = serial.pipe.yield
|
|
|
|
templateData.Serial_Filename = filepath.FromSlash(serial.pipe.filename)
|
|
|
|
case *serialConfigFile:
|
|
|
|
templateData.Serial_Type = "file"
|
|
|
|
templateData.Serial_Filename = filepath.FromSlash(serial.file.filename)
|
|
|
|
case *serialConfigDevice:
|
|
|
|
templateData.Serial_Type = "device"
|
|
|
|
templateData.Serial_Filename = filepath.FromSlash(serial.device.devicename)
|
|
|
|
case *serialConfigAuto:
|
|
|
|
templateData.Serial_Type = "device"
|
|
|
|
templateData.Serial_Filename = filepath.FromSlash(serial.auto.devicename)
|
|
|
|
templateData.Serial_Yield = serial.auto.yield
|
|
|
|
templateData.Serial_Auto = "TRUE"
|
2017-04-12 18:41:36 -04:00
|
|
|
case nil:
|
2017-04-13 21:01:10 -04:00
|
|
|
templateData.Serial_Present = "FALSE"
|
2017-04-12 18:41:36 -04:00
|
|
|
break
|
2016-10-05 22:51:42 -04:00
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
default:
|
|
|
|
err := fmt.Errorf("Error procesing VMX template: %v", serial)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
2015-06-04 14:44:07 -04:00
|
|
|
}
|
|
|
|
|
2016-10-05 22:51:42 -04:00
|
|
|
/// check if parallel port has been configured
|
2017-04-13 21:01:10 -04:00
|
|
|
if config.Parallel == "" {
|
|
|
|
templateData.Parallel_Present = "FALSE"
|
|
|
|
} else {
|
2017-02-27 16:34:53 -05:00
|
|
|
parallel, err := unformat_parallel(config.Parallel)
|
2015-11-11 07:39:06 -05:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error procesing VMX template: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2016-10-05 22:51:42 -04:00
|
|
|
templateData.Parallel_Auto = "FALSE"
|
2015-11-11 07:39:06 -05:00
|
|
|
switch parallel.parallelType.(type) {
|
2017-02-27 16:34:53 -05:00
|
|
|
case *parallelPortFile:
|
|
|
|
templateData.Parallel_Present = "TRUE"
|
|
|
|
templateData.Parallel_Filename = filepath.FromSlash(parallel.file.filename)
|
|
|
|
case *parallelPortDevice:
|
|
|
|
templateData.Parallel_Present = "TRUE"
|
|
|
|
templateData.Parallel_Bidirectional = parallel.device.bidirectional
|
|
|
|
templateData.Parallel_Filename = filepath.FromSlash(parallel.device.devicename)
|
|
|
|
case *parallelPortAuto:
|
|
|
|
templateData.Parallel_Present = "TRUE"
|
|
|
|
templateData.Parallel_Auto = "TRUE"
|
|
|
|
templateData.Parallel_Bidirectional = parallel.auto.bidirectional
|
2017-04-12 18:41:36 -04:00
|
|
|
case nil:
|
2017-04-13 21:01:10 -04:00
|
|
|
templateData.Parallel_Present = "FALSE"
|
2017-04-12 18:41:36 -04:00
|
|
|
break
|
|
|
|
|
2017-02-27 16:34:53 -05:00
|
|
|
default:
|
|
|
|
err := fmt.Errorf("Error procesing VMX template: %v", parallel)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2015-11-11 07:39:06 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.Data = &templateData
|
|
|
|
|
2016-10-05 22:51:42 -04:00
|
|
|
/// render the .vmx template
|
2015-05-27 17:16:28 -04:00
|
|
|
vmxContents, err := interpolate.Render(vmxTemplate, &ctx)
|
2013-08-27 20:23:22 -04:00
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error procesing VMX template: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-08-27 20:23:22 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-11-08 00:02:12 -05:00
|
|
|
vmxDir := config.OutputDir
|
|
|
|
if config.RemoteType != "" {
|
|
|
|
// For remote builds, we just put the VMX in a temporary
|
|
|
|
// directory since it just gets uploaded anyways.
|
|
|
|
vmxDir, err = ioutil.TempDir("", "packer-vmx")
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error preparing VMX template: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the tempDir so we clean it up
|
|
|
|
s.tempDir = vmxDir
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxPath := filepath.Join(vmxDir, config.VMName+".vmx")
|
2013-12-24 01:09:47 -05:00
|
|
|
if err := vmwcommon.WriteVMX(vmxPath, vmwcommon.ParseVMX(vmxContents)); err != nil {
|
2013-06-20 00:20:48 -04:00
|
|
|
err := fmt.Errorf("Error creating VMX file: %s", err)
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:20:48 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-07 19:20:58 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2013-06-04 19:52:59 -04:00
|
|
|
|
2013-08-31 15:50:25 -04:00
|
|
|
state.Put("vmx_path", vmxPath)
|
2013-06-05 16:17:56 -04:00
|
|
|
|
2013-06-04 19:52:59 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-11-08 00:02:12 -05:00
|
|
|
func (s *stepCreateVMX) Cleanup(multistep.StateBag) {
|
|
|
|
if s.tempDir != "" {
|
|
|
|
os.RemoveAll(s.tempDir)
|
|
|
|
}
|
2013-06-04 19:52:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is the default VMX template used if no other template is given.
|
|
|
|
// This is hardcoded here. If you wish to use a custom template please
|
|
|
|
// do so by specifying in the builder configuration.
|
|
|
|
const DefaultVMXTemplate = `
|
|
|
|
.encoding = "UTF-8"
|
|
|
|
bios.bootOrder = "hdd,CDROM"
|
|
|
|
checkpoint.vmState = ""
|
|
|
|
cleanShutdown = "TRUE"
|
|
|
|
config.version = "8"
|
|
|
|
displayName = "{{ .Name }}"
|
|
|
|
ehci.pciSlotNumber = "34"
|
|
|
|
ehci.present = "TRUE"
|
|
|
|
ethernet0.addressType = "generated"
|
|
|
|
ethernet0.bsdName = "en0"
|
2016-10-05 22:51:42 -04:00
|
|
|
ethernet0.connectionType = "{{ .Network_Type }}"
|
|
|
|
ethernet0.vnet = "{{ .Network_Device }}"
|
2013-06-04 19:52:59 -04:00
|
|
|
ethernet0.displayName = "Ethernet"
|
|
|
|
ethernet0.linkStatePropagation.enable = "FALSE"
|
|
|
|
ethernet0.pciSlotNumber = "33"
|
|
|
|
ethernet0.present = "TRUE"
|
2018-01-16 14:17:37 -05:00
|
|
|
ethernet0.virtualDev = "{{ .Network_Adapter }}"
|
2013-06-04 19:52:59 -04:00
|
|
|
ethernet0.wakeOnPcktRcv = "FALSE"
|
|
|
|
extendedConfigFile = "{{ .Name }}.vmxf"
|
|
|
|
floppy0.present = "FALSE"
|
|
|
|
guestOS = "{{ .GuestOS }}"
|
|
|
|
gui.fullScreenAtPowerOn = "FALSE"
|
|
|
|
gui.viewModeAtPowerOn = "windowed"
|
|
|
|
hgfs.linkRootShare = "TRUE"
|
|
|
|
hgfs.mapRootShare = "TRUE"
|
2017-12-25 15:08:08 -05:00
|
|
|
|
|
|
|
scsi0.present = "{{ .SCSI_Present }}"
|
|
|
|
scsi0.virtualDev = "{{ .SCSI_diskAdapterType }}"
|
|
|
|
scsi0.pciSlotNumber = "16"
|
|
|
|
scsi0:0.redo = ""
|
|
|
|
sata0.present = "{{ .SATA_Present }}"
|
|
|
|
nvme0.present = "{{ .NVME_Present }}"
|
|
|
|
|
|
|
|
{{ .DiskType }}0:0.present = "TRUE"
|
|
|
|
{{ .DiskType }}0:0.fileName = "{{ .DiskName }}.vmdk"
|
|
|
|
|
|
|
|
{{ .CDROMType }}0:{{ .CDROMType_MasterSlave }}.present = "TRUE"
|
|
|
|
{{ .CDROMType }}0:{{ .CDROMType_MasterSlave }}.fileName = "{{ .ISOPath }}"
|
|
|
|
{{ .CDROMType }}0:{{ .CDROMType_MasterSlave }}.deviceType = "cdrom-image"
|
|
|
|
|
2013-06-04 19:52:59 -04:00
|
|
|
isolation.tools.hgfs.disable = "FALSE"
|
|
|
|
memsize = "512"
|
|
|
|
nvram = "{{ .Name }}.nvram"
|
|
|
|
pciBridge0.pciSlotNumber = "17"
|
|
|
|
pciBridge0.present = "TRUE"
|
|
|
|
pciBridge4.functions = "8"
|
|
|
|
pciBridge4.pciSlotNumber = "21"
|
|
|
|
pciBridge4.present = "TRUE"
|
|
|
|
pciBridge4.virtualDev = "pcieRootPort"
|
|
|
|
pciBridge5.functions = "8"
|
|
|
|
pciBridge5.pciSlotNumber = "22"
|
|
|
|
pciBridge5.present = "TRUE"
|
|
|
|
pciBridge5.virtualDev = "pcieRootPort"
|
|
|
|
pciBridge6.functions = "8"
|
|
|
|
pciBridge6.pciSlotNumber = "23"
|
|
|
|
pciBridge6.present = "TRUE"
|
|
|
|
pciBridge6.virtualDev = "pcieRootPort"
|
|
|
|
pciBridge7.functions = "8"
|
|
|
|
pciBridge7.pciSlotNumber = "24"
|
|
|
|
pciBridge7.present = "TRUE"
|
|
|
|
pciBridge7.virtualDev = "pcieRootPort"
|
|
|
|
powerType.powerOff = "soft"
|
|
|
|
powerType.powerOn = "soft"
|
|
|
|
powerType.reset = "soft"
|
|
|
|
powerType.suspend = "soft"
|
|
|
|
proxyApps.publishToHost = "FALSE"
|
|
|
|
replay.filename = ""
|
|
|
|
replay.supported = "FALSE"
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
// Sound
|
|
|
|
sound.startConnected = "{{ .Sound_Present }}"
|
|
|
|
sound.present = "{{ .Sound_Present }}"
|
|
|
|
sound.fileName = "-1"
|
|
|
|
sound.autodetect = "TRUE"
|
|
|
|
|
2013-06-04 19:52:59 -04:00
|
|
|
tools.syncTime = "TRUE"
|
|
|
|
tools.upgrade.policy = "upgradeAtPowerCycle"
|
2015-11-11 07:39:06 -05:00
|
|
|
|
|
|
|
// USB
|
2013-06-04 19:52:59 -04:00
|
|
|
usb.pciSlotNumber = "32"
|
2015-11-11 07:39:06 -05:00
|
|
|
usb.present = "{{ .Usb_Present }}"
|
|
|
|
usb_xhci.present = "TRUE"
|
|
|
|
|
|
|
|
// Serial
|
|
|
|
serial0.present = "{{ .Serial_Present }}"
|
|
|
|
serial0.startConnected = "{{ .Serial_Present }}"
|
|
|
|
serial0.fileName = "{{ .Serial_Filename }}"
|
2016-10-05 22:51:42 -04:00
|
|
|
serial0.autodetect = "{{ .Serial_Auto }}"
|
2015-11-11 07:39:06 -05:00
|
|
|
serial0.fileType = "{{ .Serial_Type }}"
|
|
|
|
serial0.yieldOnMsrRead = "{{ .Serial_Yield }}"
|
|
|
|
serial0.pipe.endPoint = "{{ .Serial_Endpoint }}"
|
|
|
|
serial0.tryNoRxLoss = "{{ .Serial_Host }}"
|
|
|
|
|
|
|
|
// Parallel
|
|
|
|
parallel0.present = "{{ .Parallel_Present }}"
|
|
|
|
parallel0.startConnected = "{{ .Parallel_Present }}"
|
|
|
|
parallel0.fileName = "{{ .Parallel_Filename }}"
|
2016-10-05 22:51:42 -04:00
|
|
|
parallel0.autodetect = "{{ .Parallel_Auto }}"
|
2015-11-11 07:39:06 -05:00
|
|
|
parallel0.bidirectional = "{{ .Parallel_Bidirectional }}"
|
|
|
|
|
2013-06-04 19:52:59 -04:00
|
|
|
virtualHW.productCompatibility = "hosted"
|
2014-09-25 00:20:35 -04:00
|
|
|
virtualHW.version = "{{ .Version }}"
|
2013-06-04 19:52:59 -04:00
|
|
|
vmci0.id = "1861462627"
|
|
|
|
vmci0.pciSlotNumber = "35"
|
|
|
|
vmci0.present = "TRUE"
|
|
|
|
vmotion.checkpointFBSize = "65536000"
|
|
|
|
`
|
2014-03-04 13:23:07 -05:00
|
|
|
|
|
|
|
const DefaultAdditionalDiskTemplate = `
|
|
|
|
scsi0:{{ .DiskNumber }}.fileName = "{{ .DiskName}}-{{ .DiskNumber }}.vmdk"
|
|
|
|
scsi0:{{ .DiskNumber }}.present = "TRUE"
|
|
|
|
scsi0:{{ .DiskNumber }}.redo = ""
|
|
|
|
`
|