Optionally use SATA controller for CD-ROM devices (#174)

This commit is contained in:
Michael Kuzmin 2018-10-30 03:18:57 +03:00 committed by GitHub
parent 75cabff278
commit ebcd6174d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 21 deletions

View File

@ -115,10 +115,6 @@ func (d *Driver) CreateVM(config *CreateConfig) (*VirtualMachine, error) {
devices := object.VirtualDeviceList{}
devices, err = addIDE(devices)
if err != nil {
return nil, err
}
devices, err = addDisk(d, devices, config)
if err != nil {
return nil, err
@ -477,23 +473,34 @@ func addIDE(devices object.VirtualDeviceList) (object.VirtualDeviceList, error)
return devices, nil
}
func (vm *VirtualMachine) AddCdrom(isoPath string) error {
func (vm *VirtualMachine) AddCdrom(controllerType string, isoPath string) error {
devices, err := vm.vm.Device(vm.driver.ctx)
if err != nil {
return err
}
sata, err := vm.FindSATAController()
if err != nil {
return err
var controller *types.VirtualController
if controllerType == "sata" {
c, err := vm.FindSATAController()
if err != nil {
return err
}
controller = c.GetVirtualController()
} else {
c, err := devices.FindIDEController("")
if err != nil {
return err
}
controller = c.GetVirtualController()
}
cdrom, err := vm.CreateCdrom(sata)
cdrom, err := vm.CreateCdrom(controller)
if err != nil {
return err
}
if isoPath != "" {
cdrom = devices.InsertIso(cdrom, isoPath)
devices.InsertIso(cdrom, isoPath)
}
return vm.addDevice(cdrom)

View File

@ -24,7 +24,7 @@ func (vm *VirtualMachine) FindSATAController() (*types.VirtualAHCIController, er
return c.(*types.VirtualAHCIController), nil
}
func (vm *VirtualMachine) CreateCdrom(c *types.VirtualAHCIController) (*types.VirtualCdrom, error) {
func (vm *VirtualMachine) CreateCdrom(c *types.VirtualController) (*types.VirtualCdrom, error) {
l, err := vm.Devices()
if err != nil {
return nil, err

View File

@ -26,6 +26,7 @@
"ich7m.present": "TRUE",
"smc.present": "TRUE"
},
"cdrom_type": "sata",
"iso_paths": [
"[datastore-mac] ISO/macOS 10.13.3.iso",

View File

@ -43,6 +43,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepConfigureHardware{
Config: &b.config.HardwareConfig,
},
&StepAddCDRom{
Config: &b.config.CDRomConfig,
},
&common.StepConfigParams{
Config: &b.config.ConfigParamsConfig,
},
@ -50,9 +53,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
if b.config.Comm.Type != "none" {
steps = append(steps,
&StepAddCDRom{
Config: &b.config.CDRomConfig,
},
&packerCommon.StepCreateFloppy{
Files: b.config.FloppyFiles,
Directories: b.config.FloppyDirectories,
@ -79,7 +79,6 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
&common.StepShutdown{
Config: &b.config.ShutdownConfig,
},
&StepRemoveCDRom{},
&StepRemoveFloppy{
Datastore: b.config.Datastore,
Host: b.config.Host,
@ -88,6 +87,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}
steps = append(steps,
&StepRemoveCDRom{},
&common.StepCreateSnapshot{
CreateSnapshot: b.config.CreateSnapshot,
},

View File

@ -166,6 +166,19 @@ func checkHardware(t *testing.T) builderT.TestCheckFunc {
t.Errorf("Invalid firmware: expected 'efi', got '%v'", fw)
}
l, err := vm.Devices()
if err != nil {
t.Fatalf("Cannot read VM devices: %v", err)
}
c := l.PickController((*types.VirtualIDEController)(nil))
if c == nil {
t.Errorf("VM should have IDE controller")
}
s := l.PickController((*types.VirtualAHCIController)(nil))
if s != nil {
t.Errorf("VM should have no SATA controllers")
}
return nil
}
}
@ -204,6 +217,41 @@ func checkLimit(t *testing.T) builderT.TestCheckFunc {
}
}
func TestISOBuilderAcc_sata(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},
Template: sataConfig(),
Check: checkSata(t),
})
}
func sataConfig() string {
config := defaultConfig()
config["cdrom_type"] = "sata"
return commonT.RenderConfig(config)
}
func checkSata(t *testing.T) builderT.TestCheckFunc {
return func(artifacts []packer.Artifact) error {
d := commonT.TestConn(t)
vm := commonT.GetVM(t, d, artifacts)
l, err := vm.Devices()
if err != nil {
t.Fatalf("Cannot read VM devices: %v", err)
}
c := l.PickController((*types.VirtualAHCIController)(nil))
if c == nil {
t.Errorf("VM has no SATA controllers")
}
return nil
}
}
func TestISOBuilderAcc_cdrom(t *testing.T) {
builderT.Test(t, builderT.TestCase{
Builder: &Builder{},

View File

@ -47,6 +47,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) {
errs = packer.MultiErrorAppend(errs, c.LocationConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.HardwareConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.CDRomConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.BootConfig.Prepare()...)
errs = packer.MultiErrorAppend(errs, c.Comm.Prepare(&c.ctx)...)
errs = packer.MultiErrorAppend(errs, c.ShutdownConfig.Prepare()...)

View File

@ -9,6 +9,7 @@ import (
)
type CDRomConfig struct {
CdromType string `mapstructure:"cdrom_type"`
ISOPaths []string `mapstructure:"iso_paths"`
}
@ -16,19 +17,32 @@ type StepAddCDRom struct {
Config *CDRomConfig
}
func (c *CDRomConfig) Prepare() []error {
var errs []error
if (c.CdromType != "" && c.CdromType != "ide" && c.CdromType != "sata") {
errs = append(errs, fmt.Errorf("'cdrom_type' must be 'ide' or 'sata'"))
}
return errs
}
func (s *StepAddCDRom) Run(_ context.Context, state multistep.StateBag) multistep.StepAction {
ui := state.Get("ui").(packer.Ui)
vm := state.Get("vm").(*driver.VirtualMachine)
ui.Say("Adding CD-ROM drives...")
if err := vm.AddSATAController(); err != nil {
state.Put("error", fmt.Errorf("error adding SATA controller: %v", err))
return multistep.ActionHalt
if s.Config.CdromType == "sata" {
ui.Say("Adding SATA controller...")
if err := vm.AddSATAController(); err != nil {
state.Put("error", fmt.Errorf("error adding SATA controller: %v", err))
return multistep.ActionHalt
}
}
ui.Say("Mount ISO images...")
for _, path := range s.Config.ISOPaths {
if err := vm.AddCdrom(path); err != nil {
state.Put("error", fmt.Errorf("error adding a cdrom: %v", err))
if err := vm.AddCdrom(s.Config.CdromType, path); err != nil {
state.Put("error", fmt.Errorf("error mounting an image: %v", err))
return multistep.ActionHalt
}
}