2014-05-04 11:56:57 -04:00
|
|
|
package common
|
2013-09-18 21:15:46 -04:00
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-09-18 21:15:46 -04:00
|
|
|
"fmt"
|
2018-01-22 18:32:33 -05:00
|
|
|
"log"
|
|
|
|
|
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-09-18 21:15:46 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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:
|
2014-05-04 11:56:57 -04:00
|
|
|
type StepAttachGuestAdditions struct {
|
2019-02-08 12:15:15 -05:00
|
|
|
attachedPath string
|
|
|
|
GuestAdditionsMode string
|
|
|
|
GuestAdditionsInterface string
|
2013-09-18 21:15:46 -04:00
|
|
|
}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *StepAttachGuestAdditions) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-05-04 11:56:57 -04:00
|
|
|
driver := state.Get("driver").(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
|
2014-05-04 11:56:57 -04:00
|
|
|
if s.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
|
2019-02-08 12:15:15 -05:00
|
|
|
|
|
|
|
controllerName := "IDE Controller"
|
|
|
|
port := "1"
|
|
|
|
device := "0"
|
|
|
|
if s.GuestAdditionsInterface == "sata" {
|
|
|
|
controllerName = "SATA Controller"
|
|
|
|
port = "2"
|
|
|
|
device = "0"
|
|
|
|
}
|
|
|
|
|
2013-09-18 21:15:46 -04:00
|
|
|
log.Println("Attaching guest additions ISO onto IDE controller...")
|
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
2019-02-08 12:15:15 -05:00
|
|
|
"--storagectl", controllerName,
|
|
|
|
"--port", port,
|
|
|
|
"--device", device,
|
2013-09-18 21:15:46 -04:00
|
|
|
"--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
|
2017-02-02 02:45:48 -05:00
|
|
|
state.Put("guest_additions_attached", true)
|
2013-09-18 21:15:46 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2014-05-04 11:56:57 -04:00
|
|
|
func (s *StepAttachGuestAdditions) Cleanup(state multistep.StateBag) {
|
2013-09-18 21:15:46 -04:00
|
|
|
if s.attachedPath == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2014-05-04 11:56:57 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2013-09-18 21:15:46 -04:00
|
|
|
vmName := state.Get("vmName").(string)
|
|
|
|
|
2019-02-08 12:15:15 -05:00
|
|
|
controllerName := "IDE Controller"
|
|
|
|
port := "1"
|
|
|
|
device := "0"
|
|
|
|
if s.GuestAdditionsInterface == "sata" {
|
|
|
|
controllerName = "SATA Controller"
|
|
|
|
port = "2"
|
|
|
|
device = "0"
|
|
|
|
}
|
|
|
|
|
2013-09-18 21:15:46 -04:00
|
|
|
command := []string{
|
|
|
|
"storageattach", vmName,
|
2019-02-08 12:15:15 -05:00
|
|
|
"--storagectl", controllerName,
|
|
|
|
"--port", port,
|
|
|
|
"--device", device,
|
2013-09-18 21:15:46 -04:00
|
|
|
"--medium", "none",
|
|
|
|
}
|
|
|
|
|
2017-02-02 02:45:48 -05:00
|
|
|
// Remove the ISO. Note that this will probably fail since
|
|
|
|
// stepRemoveDevices does this as well. No big deal.
|
|
|
|
driver.VBoxManage(command...)
|
2013-09-18 21:15:46 -04:00
|
|
|
}
|