packer-cn/builder/virtualbox/iso/step_attach_iso.go

106 lines
2.4 KiB
Go
Raw Normal View History

2013-12-21 17:27:00 -05:00
package iso
2013-06-11 23:07:11 -04:00
import (
"context"
2013-06-11 23:07:11 -04:00
"fmt"
"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"
"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:
type stepAttachISO struct {
2013-06-11 23:07:11 -04:00
diskPath string
}
func (s *stepAttachISO) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
config := state.Get("config").(*Config)
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
controllerName := "IDE Controller"
port := "0"
device := "1"
if config.ISOInterface == "sata" {
controllerName = "SATA Controller"
port = "1"
device = "0"
}
// 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
2013-06-11 23:07:11 -04:00
// Attach the disk to the controller
command := []string{
"storageattach", vmName,
"--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 {
err := fmt.Errorf("Error attaching ISO: %s", err)
2013-08-31 15:44:58 -04:00
state.Put("error", err)
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
// Set some state so we know to remove
state.Put("attachedIso", true)
if controllerName == "SATA Controller" {
state.Put("attachedIsoOnSata", true)
}
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
}
config := state.Get("config").(*Config)
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
controllerName := "IDE Controller"
port := "0"
device := "1"
if config.ISOInterface == "sata" {
controllerName = "SATA Controller"
port = "1"
device = "0"
}
command := []string{
"storageattach", vmName,
"--storagectl", controllerName,
"--port", port,
"--device", device,
"--medium", "none",
}
// 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
}