github.com/alouche/packer@v0.3.7/builder/virtualbox/step_create_disk.go (about) 1 package virtualbox 2 3 import ( 4 "fmt" 5 "github.com/mitchellh/multistep" 6 "github.com/mitchellh/packer/packer" 7 "path/filepath" 8 "strconv" 9 "strings" 10 ) 11 12 // This step creates the virtual disk that will be used as the 13 // hard drive for the virtual machine. 14 type stepCreateDisk struct{} 15 16 func (s *stepCreateDisk) Run(state multistep.StateBag) multistep.StepAction { 17 config := state.Get("config").(*config) 18 driver := state.Get("driver").(Driver) 19 ui := state.Get("ui").(packer.Ui) 20 vmName := state.Get("vmName").(string) 21 22 format := "VDI" 23 path := filepath.Join(config.OutputDir, fmt.Sprintf("%s.%s", config.VMName, strings.ToLower(format))) 24 25 command := []string{ 26 "createhd", 27 "--filename", path, 28 "--size", strconv.FormatUint(uint64(config.DiskSize), 10), 29 "--format", format, 30 "--variant", "Standard", 31 } 32 33 ui.Say("Creating hard drive...") 34 err := driver.VBoxManage(command...) 35 if err != nil { 36 err := fmt.Errorf("Error creating hard drive: %s", err) 37 state.Put("error", err) 38 ui.Error(err.Error()) 39 return multistep.ActionHalt 40 } 41 42 // Add the IDE controller so we can later attach the disk. 43 // When the hard disk controller is not IDE, this device is still used 44 // by VirtualBox to deliver the guest extensions. 45 controllerName := "IDE Controller" 46 err = driver.VBoxManage("storagectl", vmName, "--name", controllerName, "--add", "ide") 47 if err != nil { 48 err := fmt.Errorf("Error creating disk controller: %s", err) 49 state.Put("error", err) 50 ui.Error(err.Error()) 51 return multistep.ActionHalt 52 } 53 54 // Add a SATA controller if we were asked to use SATA. We still attach 55 // the IDE controller above because some other things (disks) require 56 // that. 57 if config.HardDriveInterface == "sata" { 58 controllerName = "SATA Controller" 59 command = []string{ 60 "storagectl", vmName, 61 "--name", controllerName, 62 "--add", "sata", 63 "--sataportcount", "1", 64 } 65 if err := driver.VBoxManage(command...); err != nil { 66 err := fmt.Errorf("Error creating disk controller: %s", err) 67 state.Put("error", err) 68 ui.Error(err.Error()) 69 return multistep.ActionHalt 70 } 71 } 72 73 // Attach the disk to the controller 74 command = []string{ 75 "storageattach", vmName, 76 "--storagectl", controllerName, 77 "--port", "0", 78 "--device", "0", 79 "--type", "hdd", 80 "--medium", path, 81 } 82 if err := driver.VBoxManage(command...); err != nil { 83 err := fmt.Errorf("Error attaching hard drive: %s", err) 84 state.Put("error", err) 85 ui.Error(err.Error()) 86 return multistep.ActionHalt 87 } 88 89 return multistep.ActionContinue 90 } 91 92 func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}