2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
2019-03-22 09:53:28 -04:00
|
|
|
"context"
|
2013-09-02 23:23:52 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2014-09-03 23:23:39 -04:00
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
"github.com/hashicorp/hcl/v2/hcldec"
|
2020-12-01 18:30:31 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/communicator"
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep/commonsteps"
|
2020-11-19 14:54:31 -05:00
|
|
|
packersdk "github.com/hashicorp/packer/packer-plugin-sdk/packer"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
2013-09-03 11:08:04 -04:00
|
|
|
const BuilderId = "transcend.qemu"
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
type Builder struct {
|
2015-05-27 16:39:43 -04:00
|
|
|
config Config
|
2013-09-02 23:23:52 -04:00
|
|
|
runner multistep.Runner
|
|
|
|
}
|
|
|
|
|
2019-12-17 05:25:56 -05:00
|
|
|
func (b *Builder) ConfigSpec() hcldec.ObjectSpec { return b.config.FlatMapstructure().HCL2Spec() }
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
func (b *Builder) Prepare(raws ...interface{}) ([]string, []string, error) {
|
2020-09-15 15:28:34 -04:00
|
|
|
warnings, errs := b.config.Prepare(raws...)
|
|
|
|
if errs != nil {
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, errs
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2019-12-17 00:23:05 -05:00
|
|
|
return nil, warnings, nil
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2020-11-19 18:10:00 -05:00
|
|
|
func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) (packersdk.Artifact, error) {
|
2013-09-02 23:23:52 -04:00
|
|
|
// Create the driver that we'll use to communicate with Qemu
|
2014-01-24 13:01:42 -05:00
|
|
|
driver, err := b.newDriver(b.config.QemuBinary)
|
2013-09-02 23:23:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("Failed creating Qemu driver: %s", err)
|
|
|
|
}
|
|
|
|
|
2016-01-25 04:01:37 -05:00
|
|
|
steps := []multistep.Step{}
|
|
|
|
if !b.config.ISOSkipCache {
|
2020-11-11 18:04:28 -05:00
|
|
|
steps = append(steps, &commonsteps.StepDownload{
|
2020-05-28 05:02:09 -04:00
|
|
|
Checksum: b.config.ISOChecksum,
|
|
|
|
Description: "ISO",
|
|
|
|
Extension: b.config.TargetExtension,
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
TargetPath: b.config.TargetPath,
|
|
|
|
Url: b.config.ISOUrls,
|
2020-09-15 19:36:51 -04:00
|
|
|
})
|
2016-01-25 04:01:37 -05:00
|
|
|
} else {
|
|
|
|
steps = append(steps, &stepSetISO{
|
|
|
|
ResultKey: "iso_path",
|
|
|
|
Url: b.config.ISOUrls,
|
2020-09-15 19:36:51 -04:00
|
|
|
})
|
2016-01-25 04:01:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
steps = append(steps, new(stepPrepareOutputDir),
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCreateFloppy{
|
2016-10-11 17:43:50 -04:00
|
|
|
Files: b.config.FloppyConfig.FloppyFiles,
|
2016-09-28 00:31:42 -04:00
|
|
|
Directories: b.config.FloppyConfig.FloppyDirectories,
|
2019-09-12 08:25:22 -04:00
|
|
|
Label: b.config.FloppyConfig.FloppyLabel,
|
2013-12-06 19:20:25 -05:00
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepCreateCD{
|
2020-09-08 19:33:18 -04:00
|
|
|
Files: b.config.CDConfig.CDFiles,
|
|
|
|
Label: b.config.CDConfig.CDLabel,
|
|
|
|
},
|
2020-09-16 16:40:23 -04:00
|
|
|
&stepCreateDisk{
|
|
|
|
AdditionalDiskSize: b.config.AdditionalDiskSize,
|
|
|
|
DiskImage: b.config.DiskImage,
|
|
|
|
DiskSize: b.config.DiskSize,
|
|
|
|
Format: b.config.Format,
|
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
UseBackingFile: b.config.UseBackingFile,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
QemuImgArgs: b.config.QemuImgArgs,
|
|
|
|
},
|
2020-09-16 14:52:19 -04:00
|
|
|
&stepCopyDisk{
|
|
|
|
DiskImage: b.config.DiskImage,
|
|
|
|
Format: b.config.Format,
|
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
UseBackingFile: b.config.UseBackingFile,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
},
|
2020-09-16 16:40:23 -04:00
|
|
|
&stepResizeDisk{
|
|
|
|
DiskCompression: b.config.DiskCompression,
|
|
|
|
DiskImage: b.config.DiskImage,
|
|
|
|
Format: b.config.Format,
|
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
SkipResizeDisk: b.config.SkipResizeDisk,
|
|
|
|
VMName: b.config.VMName,
|
|
|
|
DiskSize: b.config.DiskSize,
|
|
|
|
QemuImgArgs: b.config.QemuImgArgs,
|
|
|
|
},
|
2020-01-27 10:49:31 -05:00
|
|
|
new(stepHTTPIPDiscover),
|
2020-11-11 18:04:28 -05:00
|
|
|
&commonsteps.StepHTTPServer{
|
2015-11-01 17:29:24 -05:00
|
|
|
HTTPDir: b.config.HTTPDir,
|
|
|
|
HTTPPortMin: b.config.HTTPPortMin,
|
|
|
|
HTTPPortMax: b.config.HTTPPortMax,
|
2020-05-27 16:34:24 -04:00
|
|
|
HTTPAddress: b.config.HTTPAddress,
|
2015-11-01 17:29:24 -05:00
|
|
|
},
|
2020-09-15 19:36:51 -04:00
|
|
|
&stepPortForward{
|
|
|
|
CommunicatorType: b.config.CommConfig.Comm.Type,
|
|
|
|
NetBridge: b.config.NetBridge,
|
|
|
|
},
|
2013-09-02 23:23:52 -04:00
|
|
|
new(stepConfigureVNC),
|
2020-09-15 19:14:52 -04:00
|
|
|
&stepRun{
|
|
|
|
DiskImage: b.config.DiskImage,
|
|
|
|
},
|
2020-01-07 05:18:01 -05:00
|
|
|
&stepConfigureQMP{
|
2020-05-03 09:47:03 -04:00
|
|
|
QMPSocketPath: b.config.QMPSocketPath,
|
2020-01-07 05:18:01 -05:00
|
|
|
},
|
2014-01-30 02:46:54 -05:00
|
|
|
&stepTypeBootCommand{},
|
2020-09-15 19:36:51 -04:00
|
|
|
&stepWaitGuestAddress{
|
|
|
|
CommunicatorType: b.config.CommConfig.Comm.Type,
|
|
|
|
NetBridge: b.config.NetBridge,
|
|
|
|
timeout: b.config.CommConfig.Comm.SSHTimeout,
|
|
|
|
},
|
|
|
|
&communicator.StepConnect{
|
|
|
|
Config: &b.config.CommConfig.Comm,
|
|
|
|
Host: commHost(b.config.CommConfig.Comm.Host()),
|
|
|
|
SSHConfig: b.config.CommConfig.Comm.SSHConfigFunc(),
|
|
|
|
SSHPort: commPort,
|
|
|
|
WinRMPort: commPort,
|
|
|
|
},
|
2020-11-11 18:04:28 -05:00
|
|
|
new(commonsteps.StepProvision),
|
|
|
|
&commonsteps.StepCleanupTempKeys{
|
2020-06-02 05:56:36 -04:00
|
|
|
Comm: &b.config.CommConfig.Comm,
|
2018-09-14 14:03:23 -04:00
|
|
|
},
|
2020-10-28 06:14:03 -04:00
|
|
|
&stepShutdown{
|
|
|
|
ShutdownTimeout: b.config.ShutdownTimeout,
|
|
|
|
ShutdownCommand: b.config.ShutdownCommand,
|
|
|
|
Comm: &b.config.CommConfig.Comm,
|
|
|
|
},
|
2020-09-16 16:40:23 -04:00
|
|
|
&stepConvertDisk{
|
|
|
|
DiskCompression: b.config.DiskCompression,
|
|
|
|
Format: b.config.Format,
|
|
|
|
OutputDir: b.config.OutputDir,
|
|
|
|
SkipCompaction: b.config.SkipCompaction,
|
|
|
|
VMName: b.config.VMName,
|
2020-09-28 05:43:20 -04:00
|
|
|
QemuImgArgs: b.config.QemuImgArgs,
|
2020-09-16 16:40:23 -04:00
|
|
|
},
|
2016-01-25 04:01:37 -05:00
|
|
|
)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
// Setup the state bag
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("config", &b.config)
|
2016-05-17 17:14:50 -04:00
|
|
|
state.Put("debug", b.config.PackerDebug)
|
2013-09-02 23:23:52 -04:00
|
|
|
state.Put("driver", driver)
|
|
|
|
state.Put("hook", hook)
|
|
|
|
state.Put("ui", ui)
|
|
|
|
|
|
|
|
// Run
|
2020-11-11 18:04:28 -05:00
|
|
|
b.runner = commonsteps.NewRunnerWithPauseFn(steps, b.config.PackerConfig, ui, state)
|
2019-03-22 09:53:28 -04:00
|
|
|
b.runner.Run(ctx, state)
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
// If there was an error, return that
|
|
|
|
if rawErr, ok := state.GetOk("error"); ok {
|
|
|
|
return nil, rawErr.(error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we were interrupted or cancelled, then just exit.
|
|
|
|
if _, ok := state.GetOk(multistep.StateCancelled); ok {
|
|
|
|
return nil, errors.New("Build was cancelled.")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk(multistep.StateHalted); ok {
|
|
|
|
return nil, errors.New("Build was halted.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compile the artifact list
|
|
|
|
files := make([]string, 0, 5)
|
|
|
|
visit := func(path string, info os.FileInfo, err error) error {
|
2017-01-26 19:32:21 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
if !info.IsDir() {
|
|
|
|
files = append(files, path)
|
|
|
|
}
|
|
|
|
|
2017-01-26 19:32:21 -05:00
|
|
|
return nil
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := filepath.Walk(b.config.OutputDir, visit); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
artifact := &Artifact{
|
2014-10-28 11:43:19 -04:00
|
|
|
dir: b.config.OutputDir,
|
|
|
|
f: files,
|
2014-01-05 20:02:30 -05:00
|
|
|
state: make(map[string]interface{}),
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2020-01-30 05:27:58 -05:00
|
|
|
artifact.state["generated_data"] = state.Get("generated_data")
|
2019-06-24 19:35:06 -04:00
|
|
|
artifact.state["diskName"] = b.config.VMName
|
2020-09-18 12:39:32 -04:00
|
|
|
|
2020-10-07 18:22:08 -04:00
|
|
|
// placed in state in step_create_disk.go
|
2019-06-24 19:35:06 -04:00
|
|
|
diskpaths, ok := state.Get("qemu_disk_paths").([]string)
|
|
|
|
if ok {
|
|
|
|
artifact.state["diskPaths"] = diskpaths
|
|
|
|
}
|
2014-01-05 20:02:30 -05:00
|
|
|
artifact.state["diskType"] = b.config.Format
|
2019-11-04 17:28:48 -05:00
|
|
|
artifact.state["diskSize"] = b.config.DiskSize
|
2014-01-05 20:02:30 -05:00
|
|
|
artifact.state["domainType"] = b.config.Accelerator
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
return artifact, nil
|
|
|
|
}
|
|
|
|
|
2014-01-24 13:01:42 -05:00
|
|
|
func (b *Builder) newDriver(qemuBinary string) (Driver, error) {
|
|
|
|
qemuPath, err := exec.LookPath(qemuBinary)
|
2013-09-02 23:23:52 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
qemuImgPath, err := exec.LookPath("qemu-img")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("Qemu path: %s, Qemu Image page: %s", qemuPath, qemuImgPath)
|
2013-11-06 00:43:20 -05:00
|
|
|
driver := &QemuDriver{
|
|
|
|
QemuPath: qemuPath,
|
|
|
|
QemuImgPath: qemuImgPath,
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
|
|
|
|
if err := driver.Verify(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return driver, nil
|
|
|
|
}
|