2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2013-09-02 23:23:52 -04:00
|
|
|
"fmt"
|
2019-06-24 19:35:06 -04:00
|
|
|
"log"
|
2015-05-17 10:35:39 -04:00
|
|
|
"path/filepath"
|
|
|
|
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2017-04-04 16:39:01 -04:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step creates the virtual disk that will be used as the
|
|
|
|
// hard drive for the virtual machine.
|
|
|
|
type stepCreateDisk struct{}
|
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2015-05-27 16:39:43 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-09-02 23:23:52 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2015-05-17 10:35:39 -04:00
|
|
|
name := config.VMName
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
if config.DiskImage && !config.UseBackingFile {
|
|
|
|
return multistep.ActionContinue
|
2018-05-02 00:56:13 -04:00
|
|
|
}
|
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
var diskFullPaths, diskSizes []string
|
|
|
|
|
|
|
|
ui.Say("Creating required virtual machine disks")
|
|
|
|
// The 'main' or 'default' disk
|
|
|
|
diskFullPaths = append(diskFullPaths, filepath.Join(config.OutputDir, name))
|
2019-10-14 16:08:59 -04:00
|
|
|
diskSizes = append(diskSizes, fmt.Sprintf("%dM", uint64(config.DiskSize)))
|
2019-06-24 19:35:06 -04:00
|
|
|
// 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)
|
|
|
|
size := fmt.Sprintf("%s", diskSize)
|
|
|
|
diskSizes = append(diskSizes, size)
|
|
|
|
}
|
2018-05-02 00:56:13 -04:00
|
|
|
}
|
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
// 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,
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
if config.UseBackingFile && i == 0 {
|
|
|
|
isoPath := state.Get("iso_path").(string)
|
|
|
|
command = append(command, "-b", isoPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
command = append(command,
|
|
|
|
diskFullPath,
|
|
|
|
diskSizes[i])
|
2014-07-17 10:01:19 -04:00
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
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
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
// Stash the disk paths so we can retrieve later
|
|
|
|
state.Put("qemu_disk_paths", diskFullPaths)
|
2014-01-05 20:02:30 -05:00
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}
|