2013-12-21 17:27:00 -05:00
|
|
|
package iso
|
2013-09-18 21:15:46 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
2013-12-21 18:00:48 -05:00
|
|
|
vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common"
|
2013-09-18 21:15:46 -04:00
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step attaches the VirtualBox guest additions as a inserted CD onto
|
|
|
|
// the virtual machine.
|
|
|
|
//
|
|
|
|
// Uses:
|
|
|
|
// config *config
|
|
|
|
// driver Driver
|
|
|
|
// guest_additions_path string
|
|
|
|
// ui packer.Ui
|
|
|
|
// vmName string
|
|
|
|
//
|
|
|
|
// Produces:
|
|
|
|
type stepAttachGuestAdditions struct {
|
|
|
|
attachedPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAttachGuestAdditions) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
config := state.Get("config").(*config)
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2013-09-18 21:15:46 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
// If we're not attaching the guest additions then just return
|
2013-11-02 04:22:56 -04:00
|
|
|
if config.GuestAdditionsMode != GuestAdditionsModeAttach {
|
2013-09-18 21:15:46 -04:00
|
|
|
log.Println("Not attaching guest additions since we're uploading.")
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-12-18 14:27:25 -05:00
|
|
|
// Get the guest additions path since we're doing it
|
|
|
|
guestAdditionsPath := state.Get("guest_additions_path").(string)
|
|
|
|
|
2013-09-18 21:15:46 -04:00
|
|
|
// Attach the guest additions to the computer
|
|
|
|
log.Println("Attaching guest additions ISO onto IDE controller...")
|
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
|
|
|
"--storagectl", "IDE Controller",
|
|
|
|
"--port", "1",
|
|
|
|
"--device", "0",
|
|
|
|
"--type", "dvddrive",
|
|
|
|
"--medium", guestAdditionsPath,
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Error attaching guest additions: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Track the path so that we can unregister it from VirtualBox later
|
|
|
|
s.attachedPath = guestAdditionsPath
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepAttachGuestAdditions) Cleanup(state multistep.StateBag) {
|
|
|
|
if s.attachedPath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:00:48 -05:00
|
|
|
driver := state.Get("driver").(vboxcommon.Driver)
|
2013-09-18 21:15:46 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
|
|
|
"--storagectl", "IDE Controller",
|
|
|
|
"--port", "1",
|
|
|
|
"--device", "0",
|
|
|
|
"--medium", "none",
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error unregistering guest additions: %s", err))
|
|
|
|
}
|
|
|
|
}
|