Files
Packer-Cn/builder/qemu/step_create_disk.go
T

75 lines
1.9 KiB
Go
Raw Normal View History

package qemu
import (
2018-01-22 15:32:33 -08:00
"context"
"fmt"
"log"
2015-05-17 17:35:39 +03:00
"path/filepath"
2018-01-19 16:18:44 -08:00
"github.com/hashicorp/packer/helper/multistep"
2017-04-04 13:39:01 -07:00
"github.com/hashicorp/packer/packer"
)
// This step creates the virtual disk that will be used as the
// hard drive for the virtual machine.
type stepCreateDisk struct{}
func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
2015-05-27 13:39:43 -07:00
config := state.Get("config").(*Config)
driver := state.Get("driver").(Driver)
ui := state.Get("ui").(packer.Ui)
2015-05-17 17:35:39 +03:00
name := config.VMName
if config.DiskImage && !config.UseBackingFile {
2014-10-28 08:43:19 -07:00
return multistep.ActionContinue
}
var diskFullPaths, diskSizes []string
ui.Say("Creating required virtual machine disks")
// The 'main' or 'default' disk
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, name))
2020-03-04 15:31:30 -05:00
diskSizes = append(diskSizes, config.DiskSize)
// Additional disks
if len(config.AdditionalDiskSize) > 0 {
for i, diskSize := range config.AdditionalDiskSize {
path := filepath.Join(config.OutputDir, fmt.Sprintf("%s-%d", name, i+1))
diskFullPaths = append(diskFullPaths, path)
2020-03-04 15:31:30 -05:00
size := diskSize
diskSizes = append(diskSizes, size)
}
}
// Create all required disks
for i, diskFullPath := range diskFullPaths {
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i])
command := []string{
"create",
"-f", config.Format,
}
if config.UseBackingFile && i == 0 {
isoPath := state.Get("iso_path").(string)
command = append(command, "-b", isoPath)
}
command = append(command,
diskFullPath,
diskSizes[i])
if err := driver.QemuImg(command...); err != nil {
err := fmt.Errorf("Error creating hard drive: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
}
// Stash the disk paths so we can retrieve later
state.Put("qemu_disk_paths", diskFullPaths)
2014-01-06 01:02:30 +00:00
return multistep.ActionContinue
}
func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}