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