2014-07-17 10:01:19 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2014-07-17 10:01:19 -04:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2014-11-06 15:47:00 -05:00
|
|
|
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
2014-07-17 10:01:19 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// This step copies the virtual disk that will be used as the
|
|
|
|
// hard drive for the virtual machine.
|
2020-09-16 14:52:19 -04:00
|
|
|
type stepCopyDisk struct {
|
|
|
|
DiskImage bool
|
|
|
|
Format string
|
|
|
|
OutputDir string
|
|
|
|
UseBackingFile bool
|
|
|
|
VMName string
|
2020-09-16 16:40:23 -04:00
|
|
|
|
|
|
|
QemuImgArgs QemuImgArgs
|
2020-09-16 14:52:19 -04:00
|
|
|
}
|
2014-07-17 10:01:19 -04:00
|
|
|
|
2019-03-29 11:50:02 -04:00
|
|
|
func (s *stepCopyDisk) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2014-07-17 10:01:19 -04:00
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
isoPath := state.Get("iso_path").(string)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2020-09-16 14:52:19 -04:00
|
|
|
path := filepath.Join(s.OutputDir, s.VMName)
|
|
|
|
|
2020-12-03 11:30:17 -05:00
|
|
|
// Only make a copy of the ISO as the 'main' or 'default' disk if is a disk
|
|
|
|
// image and not using a backing file. The create disk step (step_create_disk.go)
|
|
|
|
// would already have made the disk otherwise
|
2020-09-16 14:52:19 -04:00
|
|
|
if !s.DiskImage || s.UseBackingFile {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-12-03 11:30:17 -05:00
|
|
|
// In some cases, the file formats provided are equivalent by comparing the
|
|
|
|
// file extensions. Skip the conversion step
|
|
|
|
// This also serves as a workaround for a QEMU bug: https://bugs.launchpad.net/qemu/+bug/1776920
|
2020-09-16 14:52:19 -04:00
|
|
|
ext := filepath.Ext(isoPath)
|
2020-12-03 11:30:17 -05:00
|
|
|
if len(ext) >= 1 && ext[1:] == s.Format && len(s.QemuImgArgs.Convert) == 0 {
|
2020-09-16 14:52:19 -04:00
|
|
|
ui.Message("File extension already matches desired output format. " +
|
|
|
|
"Skipping qemu-img convert step")
|
|
|
|
err := driver.Copy(isoPath, path)
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
2014-07-17 10:01:19 -04:00
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
command := s.buildConvertCommand(isoPath, path)
|
2014-07-17 10:01:19 -04:00
|
|
|
|
|
|
|
ui.Say("Copying hard drive...")
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
2020-09-16 16:40:23 -04:00
|
|
|
func (s *stepCopyDisk) buildConvertCommand(sourcePath, targetPath string) []string {
|
|
|
|
command := []string{"convert"}
|
|
|
|
|
|
|
|
// Add user-provided convert args
|
|
|
|
command = append(command, s.QemuImgArgs.Convert...)
|
|
|
|
|
|
|
|
// Add format, and paths.
|
|
|
|
command = append(command, "-O", s.Format, sourcePath, targetPath)
|
|
|
|
|
|
|
|
return command
|
|
|
|
}
|
|
|
|
|
2014-07-17 10:01:19 -04:00
|
|
|
func (s *stepCopyDisk) Cleanup(state multistep.StateBag) {}
|