2013-12-21 17:27:00 -05:00
|
|
|
package iso
|
2013-06-11 23:07:11 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-06-11 23:07:11 -04:00
|
|
|
"fmt"
|
2018-06-11 19:07:43 -04:00
|
|
|
"path/filepath"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vboxcommon "github.com/hashicorp/packer/builder/virtualbox/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"
|
2013-06-11 23:07:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step attaches the ISO to the virtual machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
//
|
|
|
|
// Produces:
|
2013-06-11 23:29:39 -04:00
|
|
|
type stepAttachISO struct {
|
2013-06-11 23:07:11 -04:00
|
|
|
diskPath string
|
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepAttachISO) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-05-27 17:01:08 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
isoPath := state.Get("iso_path").(string)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-11 23:07:11 -04:00
|
|
|
|
2014-05-23 21:14:24 -04:00
|
|
|
controllerName := "IDE Controller"
|
|
|
|
port := "0"
|
|
|
|
device := "1"
|
|
|
|
if config.ISOInterface == "sata" {
|
|
|
|
controllerName = "SATA Controller"
|
|
|
|
port = "1"
|
|
|
|
device = "0"
|
|
|
|
}
|
|
|
|
|
2018-06-11 18:53:54 -04:00
|
|
|
// If it's a symlink, resolve it to it's target.
|
|
|
|
resolvedIsoPath, err := filepath.EvalSymlinks(isoPath)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error resolving symlink for ISO: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
isoPath = resolvedIsoPath
|
2018-06-11 19:02:52 -04:00
|
|
|
|
2013-06-11 23:07:11 -04:00
|
|
|
// Attach the disk to the controller
|
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
2014-05-23 21:14:24 -04:00
|
|
|
"--storagectl", controllerName,
|
|
|
|
"--port", port,
|
|
|
|
"--device", device,
|
2013-06-11 23:07:11 -04:00
|
|
|
"--type", "dvddrive",
|
|
|
|
"--medium", isoPath,
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
2013-06-20 00:07:53 -04:00
|
|
|
err := fmt.Errorf("Error attaching ISO: %s", err)
|
2013-08-31 15:44:58 -04:00
|
|
|
state.Put("error", err)
|
2013-06-20 00:07:53 -04:00
|
|
|
ui.Error(err.Error())
|
2013-06-11 23:07:11 -04:00
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the path so that we can unregister it from VirtualBox later
|
|
|
|
s.diskPath = isoPath
|
|
|
|
|
2013-12-22 20:08:07 -05:00
|
|
|
// Set some state so we know to remove
|
|
|
|
state.Put("attachedIso", true)
|
2014-05-23 21:14:24 -04:00
|
|
|
if controllerName == "SATA Controller" {
|
|
|
|
state.Put("attachedIsoOnSata", true)
|
|
|
|
}
|
2013-12-22 20:08:07 -05:00
|
|
|
|
2013-06-11 23:07:11 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-08-31 15:44:58 -04:00
|
|
|
func (s *stepAttachISO) Cleanup(state multistep.StateBag) {
|
2013-06-11 23:07:11 -04:00
|
|
|
if s.diskPath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-05-27 17:01:08 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2013-08-31 15:44:58 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
2013-06-11 23:07:11 -04:00
|
|
|
|
2014-05-23 21:14:24 -04:00
|
|
|
controllerName := "IDE Controller"
|
|
|
|
port := "0"
|
|
|
|
device := "1"
|
|
|
|
if config.ISOInterface == "sata" {
|
|
|
|
controllerName = "SATA Controller"
|
|
|
|
port = "1"
|
|
|
|
device = "0"
|
|
|
|
}
|
|
|
|
|
2013-06-11 23:29:39 -04:00
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
2014-05-23 21:14:24 -04:00
|
|
|
"--storagectl", controllerName,
|
|
|
|
"--port", port,
|
|
|
|
"--device", device,
|
2013-06-11 23:29:39 -04:00
|
|
|
"--medium", "none",
|
|
|
|
}
|
|
|
|
|
2013-12-19 11:49:23 -05:00
|
|
|
// Remove the ISO. Note that this will probably fail since
|
|
|
|
// stepRemoveDevices does this as well. No big deal.
|
|
|
|
driver.VBoxManage(command...)
|
2013-06-11 23:07:11 -04:00
|
|
|
}
|