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