github.phpd.cn/hashicorp/packer@v1.3.2/builder/vmware/iso/step_create_disk.go (about) 1 package iso 2 3 import ( 4 "context" 5 "fmt" 6 "log" 7 "path/filepath" 8 9 vmwcommon "github.com/hashicorp/packer/builder/vmware/common" 10 "github.com/hashicorp/packer/helper/multistep" 11 "github.com/hashicorp/packer/packer" 12 ) 13 14 // This step creates the virtual disks for the VM. 15 // 16 // Uses: 17 // config *config 18 // driver Driver 19 // ui packer.Ui 20 // 21 // Produces: 22 // disk_full_paths ([]string) - The full paths to all created disks 23 type stepCreateDisk struct{} 24 25 func (stepCreateDisk) Run(_ context.Context, state multistep.StateBag) multistep.StepAction { 26 config := state.Get("config").(*Config) 27 driver := state.Get("driver").(vmwcommon.Driver) 28 ui := state.Get("ui").(packer.Ui) 29 30 ui.Say("Creating required virtual machine disks") 31 32 // Users can configure disks at several locations in the template so 33 // first collate all the disk requirements 34 var diskFullPaths, diskSizes []string 35 // The 'main' or 'default' disk 36 diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, config.DiskName+".vmdk")) 37 diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(config.DiskSize))) 38 // Additional disks 39 if len(config.AdditionalDiskSize) > 0 { 40 for i, diskSize := range config.AdditionalDiskSize { 41 path := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d.vmdk", config.DiskName, i+1)) 42 diskFullPaths = append(diskFullPaths, path) 43 size := fmt.Sprintf("%dM", uint64(diskSize)) 44 diskSizes = append(diskSizes, size) 45 } 46 } 47 48 // Create all required disks 49 for i, diskFullPath := range diskFullPaths { 50 log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i]) 51 // Additional disks currently use the same adapter type and disk 52 // type as specified for the main disk 53 if err := driver.CreateDisk(diskFullPath, diskSizes[i], config.DiskAdapterType, config.DiskTypeId); err != nil { 54 err := fmt.Errorf("Error creating disk: %s", err) 55 state.Put("error", err) 56 ui.Error(err.Error()) 57 return multistep.ActionHalt 58 } 59 } 60 61 // Stash the disk paths so we can retrieve later e.g. when compacting 62 state.Put("disk_full_paths", diskFullPaths) 63 return multistep.ActionContinue 64 } 65 66 func (stepCreateDisk) Cleanup(multistep.StateBag) {}