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"
|
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/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.
|
2020-09-16 16:40:23 -04:00
|
|
|
type stepCreateDisk struct {
|
|
|
|
AdditionalDiskSize []string
|
|
|
|
DiskImage bool
|
|
|
|
DiskSize string
|
|
|
|
Format string
|
|
|
|
OutputDir string
|
|
|
|
UseBackingFile bool
|
|
|
|
VMName string
|
|
|
|
QemuImgArgs QemuImgArgs
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCreateDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2013-09-02 23:23:52 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-09-16 16:40:23 -04:00
|
|
|
name := s.VMName
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2020-12-03 11:30:17 -05:00
|
|
|
if len(s.AdditionalDiskSize) > 0 || s.UseBackingFile {
|
|
|
|
ui.Say("Creating required virtual machine disks")
|
2018-05-02 00:56:13 -04:00
|
|
|
}
|
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
// The 'main' or 'default' disk
|
2020-11-30 10:40:03 -05:00
|
|
|
diskFullPaths := []string{filepath.Join(s.OutputDir, name)}
|
|
|
|
diskSizes := []string{s.DiskSize}
|
|
|
|
|
2019-06-24 19:35:06 -04:00
|
|
|
// Additional disks
|
2020-09-16 16:40:23 -04:00
|
|
|
if len(s.AdditionalDiskSize) > 0 {
|
|
|
|
for i, diskSize := range s.AdditionalDiskSize {
|
|
|
|
path := filepath.Join(s.OutputDir, fmt.Sprintf("%s-%d", name, i+1))
|
2019-06-24 19:35:06 -04:00
|
|
|
diskFullPaths = append(diskFullPaths, path)
|
2020-12-03 11:30:17 -05:00
|
|
|
diskSizes = append(diskSizes, diskSize)
|
2019-06-24 19:35:06 -04:00
|
|
|
}
|
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 {
|
2020-12-03 11:30:17 -05:00
|
|
|
if s.DiskImage && !s.UseBackingFile && i == 0 {
|
|
|
|
// Let the copy disk step (step_copy_disk.go) create the 'main' or
|
|
|
|
// 'default' disk.
|
|
|
|
continue
|
|
|
|
}
|
2019-06-24 19:35:06 -04:00
|
|
|
log.Printf("[INFO] Creating disk with Path: %s and Size: %s", diskFullPath, diskSizes[i])
|
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
command := s.buildCreateCommand(diskFullPath, diskSizes[i], i, state)
|
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
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
func (s *stepCreateDisk) buildCreateCommand(path string, size string, i int, state multistep.StateBag) []string {
|
|
|
|
command := []string{"create", "-f", s.Format}
|
|
|
|
|
2020-12-03 11:30:17 -05:00
|
|
|
if s.DiskImage && s.UseBackingFile && i == 0 {
|
|
|
|
// Use a backing file for the 'main' or 'default' disk
|
2020-09-16 16:40:23 -04:00
|
|
|
isoPath := state.Get("iso_path").(string)
|
|
|
|
command = append(command, "-b", isoPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add user-provided convert args
|
|
|
|
command = append(command, s.QemuImgArgs.Create...)
|
|
|
|
|
|
|
|
// add target path and size.
|
|
|
|
command = append(command, path, size)
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
func (s *stepCreateDisk) Cleanup(state multistep.StateBag) {}
|