diff --git a/builder/virtualbox/builder.go b/builder/virtualbox/builder.go index 4b50b2ab5..c575feb6a 100644 --- a/builder/virtualbox/builder.go +++ b/builder/virtualbox/builder.go @@ -23,10 +23,10 @@ type Builder struct { type config struct { GuestOSType string `mapstructure:"guest_os_type"` - ISOMD5 string `mapstructure:"iso_md5"` - ISOUrl string `mapstructure:"iso_url"` - OutputDir string `mapstructure:"output_directory"` - VMName string `mapstructure:"vm_name"` + ISOMD5 string `mapstructure:"iso_md5"` + ISOUrl string `mapstructure:"iso_url"` + OutputDir string `mapstructure:"output_directory"` + VMName string `mapstructure:"vm_name"` } func (b *Builder) Prepare(raw interface{}) error { @@ -113,6 +113,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) packer new(stepSuppressMessages), new(stepCreateVM), new(stepCreateDisk), + new(stepAttachISO), } // Setup the state bag diff --git a/builder/virtualbox/driver.go b/builder/virtualbox/driver.go index 5c131da7f..73c903a42 100644 --- a/builder/virtualbox/driver.go +++ b/builder/virtualbox/driver.go @@ -34,7 +34,7 @@ func (d *VBox42Driver) SuppressMessages() error { extraData := map[string]string{ "GUI/RegistrationData": "triesLeft=0", "GUI/SuppressMessages": "confirmInputCapture,remindAboutAutoCapture,remindAboutMouseIntegrationOff,remindAboutMouseIntegrationOn,remindAboutWrongColorDepth", - "GUI/UpdateDate": fmt.Sprintf("1 d, %d-01-01, stable", time.Now().Year()+1), + "GUI/UpdateDate": fmt.Sprintf("1 d, %d-01-01, stable", time.Now().Year()+1), "GUI/UpdateCheckCount": "60", } diff --git a/builder/virtualbox/step_attach_iso.go b/builder/virtualbox/step_attach_iso.go new file mode 100644 index 000000000..6464fc1fd --- /dev/null +++ b/builder/virtualbox/step_attach_iso.go @@ -0,0 +1,57 @@ +package virtualbox + +import ( + "fmt" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "time" +) + +// This step attaches the ISO to the virtual machine. +// +// Uses: +// +// Produces: +type stepAttachISO struct{ + diskPath string +} + +func (s *stepAttachISO) Run(state map[string]interface{}) multistep.StepAction { + driver := state["driver"].(Driver) + isoPath := state["iso_path"].(string) + ui := state["ui"].(packer.Ui) + vmName := state["vmName"].(string) + + // Attach the disk to the controller + command := []string{ + "storageattach", vmName, + "--storagectl", "IDE Controller", + "--port", "0", + "--device", "1", + "--type", "dvddrive", + "--medium", isoPath, + } + if err := driver.VBoxManage(command...); err != nil { + ui.Error(fmt.Sprintf("Error attaching hard drive: %s", err)) + return multistep.ActionHalt + } + + // Track the path so that we can unregister it from VirtualBox later + s.diskPath = isoPath + + time.Sleep(15 * time.Second) + return multistep.ActionContinue +} + +func (s *stepAttachISO) Cleanup(state map[string]interface{}) { + if s.diskPath == "" { + return + } + + driver := state["driver"].(Driver) + ui := state["ui"].(packer.Ui) + + if err := driver.VBoxManage("closemedium", "disk", s.diskPath); err != nil { + ui.Error(fmt.Sprintf("Error unregistering ISO: %s", err)) + } +} diff --git a/builder/virtualbox/step_create_disk.go b/builder/virtualbox/step_create_disk.go index 90545fb0a..4f08e7085 100644 --- a/builder/virtualbox/step_create_disk.go +++ b/builder/virtualbox/step_create_disk.go @@ -6,12 +6,11 @@ import ( "github.com/mitchellh/packer/packer" "path/filepath" "strings" - "time" ) // This step creates the virtual disk that will be used as the // hard drive for the virtual machine. -type stepCreateDisk struct{ +type stepCreateDisk struct { diskPath string } @@ -64,7 +63,6 @@ func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction return multistep.ActionHalt } - time.Sleep(15 * time.Second) return multistep.ActionContinue } diff --git a/builder/virtualbox/step_create_vm.go b/builder/virtualbox/step_create_vm.go index a3d34868f..29f07e0dd 100644 --- a/builder/virtualbox/step_create_vm.go +++ b/builder/virtualbox/step_create_vm.go @@ -10,7 +10,7 @@ import ( // // Produces: // vmName string - The name of the VM -type stepCreateVM struct{ +type stepCreateVM struct { vmName string }