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
|
|
|
|
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"
|
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
|
|
|
|
}
|
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)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
2020-09-16 14:52:19 -04:00
|
|
|
path := filepath.Join(s.OutputDir, s.VMName)
|
|
|
|
|
|
|
|
if !s.DiskImage || s.UseBackingFile {
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
// isoPath extention is:
|
|
|
|
ext := filepath.Ext(isoPath)
|
|
|
|
if ext[1:] == s.Format {
|
|
|
|
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
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"convert",
|
2020-09-16 14:52:19 -04:00
|
|
|
"-O", s.Format,
|
2014-07-17 10:01:19 -04:00
|
|
|
isoPath,
|
|
|
|
path,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stepCopyDisk) Cleanup(state multistep.StateBag) {}
|