2013-06-11 19:34:44 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates the virtual disk that will be used as the
|
|
|
|
// hard drive for the virtual machine.
|
2013-06-12 00:08:45 -04:00
|
|
|
type stepCreateDisk struct{}
|
2013-06-11 19:34:44 -04:00
|
|
|
|
|
|
|
func (s *stepCreateDisk) Run(state map[string]interface{}) multistep.StepAction {
|
|
|
|
config := state["config"].(*config)
|
|
|
|
driver := state["driver"].(Driver)
|
|
|
|
ui := state["ui"].(packer.Ui)
|
2013-06-11 19:44:43 -04:00
|
|
|
vmName := state["vmName"].(string)
|
2013-06-11 19:34:44 -04:00
|
|
|
|
|
|
|
format := "VDI"
|
|
|
|
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format)))
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"createhd",
|
|
|
|
"--filename", path,
|
2013-06-11 19:48:01 -04:00
|
|
|
"--size", "40000",
|
2013-06-11 19:34:44 -04:00
|
|
|
"--format", format,
|
|
|
|
"--variant", "Standard",
|
|
|
|
}
|
|
|
|
|
|
|
|
ui.Say("Creating hard drive...")
|
|
|
|
err := driver.VBoxManage(command...)
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error creating hard drive: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:44:43 -04:00
|
|
|
// Add the IDE controller so we can later attach the disk
|
|
|
|
controllerName := "IDE Controller"
|
|
|
|
err = driver.VBoxManage("storagectl", vmName, "--name", controllerName, "--add", "ide")
|
|
|
|
if err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error creating disk controller: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attach the disk to the controller
|
|
|
|
command = []string{
|
2013-06-11 19:48:01 -04:00
|
|
|
"storageattach", vmName,
|
2013-06-11 19:44:43 -04:00
|
|
|
"--storagectl", controllerName,
|
|
|
|
"--port", "0",
|
|
|
|
"--device", "0",
|
|
|
|
"--type", "hdd",
|
|
|
|
"--medium", path,
|
|
|
|
}
|
|
|
|
if err := driver.VBoxManage(command...); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error attaching hard drive: %s", err))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-06-11 19:34:44 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2013-06-11 23:29:39 -04:00
|
|
|
func (s *stepCreateDisk) Cleanup(state map[string]interface{}) {}
|