2018-11-17 06:16:14 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hashicorp/packer/template/interpolate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type HWConfig struct {
|
|
|
|
|
|
|
|
// cpu information
|
2018-11-17 07:31:49 -05:00
|
|
|
CpuCount int `mapstructure:"cpus"`
|
|
|
|
MemorySize int `mapstructure:"memory"`
|
2018-11-17 06:16:14 -05:00
|
|
|
|
|
|
|
// device presence
|
|
|
|
Sound bool `mapstructure:"sound"`
|
|
|
|
USB bool `mapstructure:"usb"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *HWConfig) Prepare(ctx *interpolate.Context) []error {
|
|
|
|
var errs []error
|
|
|
|
|
|
|
|
// Hardware and cpu options
|
|
|
|
if c.CpuCount < 0 {
|
2018-11-17 07:31:49 -05:00
|
|
|
errs = append(errs, fmt.Errorf("An invalid number of cpus was specified (cpus < 0): %d", c.CpuCount))
|
2018-11-17 06:16:14 -05:00
|
|
|
c.CpuCount = 0
|
|
|
|
}
|
|
|
|
if c.CpuCount == 0 {
|
|
|
|
c.CpuCount = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.MemorySize < 0 {
|
2018-11-17 07:31:49 -05:00
|
|
|
errs = append(errs, fmt.Errorf("An invalid memory size was specified (memory < 0): %d", c.MemorySize))
|
2018-11-17 06:16:14 -05:00
|
|
|
c.MemorySize = 0
|
|
|
|
}
|
|
|
|
if c.MemorySize == 0 {
|
|
|
|
c.MemorySize = 512
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peripherals
|
|
|
|
if !c.Sound {
|
|
|
|
c.Sound = false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !c.USB {
|
|
|
|
c.USB = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|