2013-09-02 23:23:52 -04:00
|
|
|
package qemu
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2013-12-06 19:20:25 -05:00
|
|
|
"log"
|
2013-09-02 23:23:52 -04:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2014-10-03 11:39:57 -04:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2015-05-27 16:39:43 -04:00
|
|
|
"github.com/mitchellh/packer/template/interpolate"
|
2013-09-02 23:23:52 -04:00
|
|
|
)
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
// stepRun runs the virtual machine
|
2013-09-02 23:23:52 -04:00
|
|
|
type stepRun struct {
|
2013-11-06 00:40:49 -05:00
|
|
|
BootDrive string
|
|
|
|
Message string
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-12-06 19:20:25 -05:00
|
|
|
type qemuArgsTemplateData struct {
|
|
|
|
HTTPIP string
|
|
|
|
HTTPPort uint
|
|
|
|
HTTPDir string
|
|
|
|
OutputDir string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction {
|
|
|
|
driver := state.Get("driver").(Driver)
|
2013-09-02 23:23:52 -04:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
ui.Say(s.Message)
|
|
|
|
|
2013-12-06 19:20:25 -05:00
|
|
|
command, err := getCommandArgs(s.BootDrive, state)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error processing QemuArggs: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
if err := driver.Qemu(command...); err != nil {
|
|
|
|
err := fmt.Errorf("Error launching VM: %s", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
return multistep.ActionContinue
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-11-06 00:40:49 -05:00
|
|
|
func (s *stepRun) Cleanup(state multistep.StateBag) {
|
|
|
|
driver := state.Get("driver").(Driver)
|
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
|
|
|
if err := driver.Stop(); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error shutting down VM: %s", err))
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-06 19:20:25 -05:00
|
|
|
func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error) {
|
2015-05-27 16:39:43 -04:00
|
|
|
config := state.Get("config").(*Config)
|
2013-09-02 23:23:52 -04:00
|
|
|
isoPath := state.Get("iso_path").(string)
|
|
|
|
vncPort := state.Get("vnc_port").(uint)
|
|
|
|
sshHostPort := state.Get("sshHostPort").(uint)
|
2013-11-06 00:40:49 -05:00
|
|
|
ui := state.Get("ui").(packer.Ui)
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
vnc := fmt.Sprintf("0.0.0.0:%d", vncPort-5900)
|
2013-11-06 00:40:49 -05:00
|
|
|
vmName := config.VMName
|
|
|
|
imgPath := filepath.Join(config.OutputDir,
|
|
|
|
fmt.Sprintf("%s.%s", vmName, strings.ToLower(config.Format)))
|
2013-09-02 23:23:52 -04:00
|
|
|
|
2014-08-06 05:43:38 -04:00
|
|
|
defaultArgs := make(map[string]string)
|
|
|
|
|
2013-09-02 23:23:52 -04:00
|
|
|
if config.Headless == true {
|
|
|
|
ui.Message("WARNING: The VM will be started in headless mode, as configured.\n" +
|
|
|
|
"In headless mode, errors during the boot sequence or OS setup\n" +
|
|
|
|
"won't be easily visible. Use at your own discretion.")
|
2014-08-06 05:43:38 -04:00
|
|
|
} else {
|
|
|
|
defaultArgs["-display"] = "sdl"
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
|
|
|
|
2013-10-07 21:58:08 -04:00
|
|
|
defaultArgs["-name"] = vmName
|
2014-09-02 12:24:31 -04:00
|
|
|
defaultArgs["-machine"] = fmt.Sprintf("type=%s", config.MachineType)
|
2015-06-13 19:30:16 -04:00
|
|
|
defaultArgs["-netdev"] = fmt.Sprintf(
|
|
|
|
"user,id=user.0,hostfwd=tcp::%v-:%d", sshHostPort, config.SSHPort)
|
2013-10-07 21:58:08 -04:00
|
|
|
defaultArgs["-device"] = fmt.Sprintf("%s,netdev=user.0", config.NetDevice)
|
2015-05-17 13:48:58 -04:00
|
|
|
defaultArgs["-drive"] = fmt.Sprintf("file=%s,if=%s,cache=%s,discard=%s", imgPath, config.DiskInterface, config.DiskCache, config.DiskDiscard)
|
2014-11-06 15:13:09 -05:00
|
|
|
if !config.DiskImage {
|
|
|
|
defaultArgs["-cdrom"] = isoPath
|
|
|
|
}
|
2013-10-07 21:58:08 -04:00
|
|
|
defaultArgs["-boot"] = bootDrive
|
2014-10-06 11:11:39 -04:00
|
|
|
defaultArgs["-m"] = "512M"
|
2013-10-07 21:58:08 -04:00
|
|
|
defaultArgs["-vnc"] = vnc
|
|
|
|
|
2014-09-02 12:24:31 -04:00
|
|
|
// Append the accelerator to the machine type if it is specified
|
|
|
|
if config.Accelerator != "none" {
|
|
|
|
defaultArgs["-machine"] += fmt.Sprintf(",accel=%s", config.Accelerator)
|
|
|
|
} else {
|
|
|
|
ui.Message("WARNING: The VM will be started with no hardware acceleration.\n" +
|
2014-09-02 12:55:06 -04:00
|
|
|
"The installation may take considerably longer to finish.\n")
|
2014-09-02 12:24:31 -04:00
|
|
|
}
|
|
|
|
|
2013-12-06 19:20:25 -05:00
|
|
|
// Determine if we have a floppy disk to attach
|
|
|
|
if floppyPathRaw, ok := state.GetOk("floppy_path"); ok {
|
|
|
|
defaultArgs["-fda"] = floppyPathRaw.(string)
|
|
|
|
} else {
|
|
|
|
log.Println("Qemu Builder has no floppy files, not attaching a floppy.")
|
|
|
|
}
|
|
|
|
|
2013-10-07 21:58:08 -04:00
|
|
|
inArgs := make(map[string][]string)
|
|
|
|
if len(config.QemuArgs) > 0 {
|
|
|
|
ui.Say("Overriding defaults Qemu arguments with QemuArgs...")
|
|
|
|
|
2013-12-06 19:20:25 -05:00
|
|
|
httpPort := state.Get("http_port").(uint)
|
2015-05-27 16:39:43 -04:00
|
|
|
ctx := config.ctx
|
|
|
|
ctx.Data = qemuArgsTemplateData{
|
2013-12-06 19:20:25 -05:00
|
|
|
"10.0.2.2",
|
|
|
|
httpPort,
|
|
|
|
config.HTTPDir,
|
|
|
|
config.OutputDir,
|
|
|
|
config.VMName,
|
|
|
|
}
|
2015-05-27 16:39:43 -04:00
|
|
|
newQemuArgs, err := processArgs(config.QemuArgs, &ctx)
|
2013-12-06 19:20:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// because qemu supports multiple appearances of the same
|
2013-10-07 21:58:08 -04:00
|
|
|
// switch, just different values, each key in the args hash
|
|
|
|
// will have an array of string values
|
2013-12-06 19:20:25 -05:00
|
|
|
for _, qemuArgs := range newQemuArgs {
|
2013-10-07 21:58:08 -04:00
|
|
|
key := qemuArgs[0]
|
|
|
|
val := strings.Join(qemuArgs[1:], "")
|
|
|
|
if _, ok := inArgs[key]; !ok {
|
|
|
|
inArgs[key] = make([]string, 0)
|
|
|
|
}
|
|
|
|
if len(val) > 0 {
|
|
|
|
inArgs[key] = append(inArgs[key], val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// get any remaining missing default args from the default settings
|
|
|
|
for key := range defaultArgs {
|
|
|
|
if _, ok := inArgs[key]; !ok {
|
|
|
|
arg := make([]string, 1)
|
|
|
|
arg[0] = defaultArgs[key]
|
|
|
|
inArgs[key] = arg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flatten to array of strings
|
|
|
|
outArgs := make([]string, 0)
|
|
|
|
for key, values := range inArgs {
|
|
|
|
if len(values) > 0 {
|
|
|
|
for idx := range values {
|
|
|
|
outArgs = append(outArgs, key, values[idx])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
outArgs = append(outArgs, key)
|
|
|
|
}
|
2013-09-02 23:23:52 -04:00
|
|
|
}
|
2013-10-07 21:58:08 -04:00
|
|
|
|
2013-12-06 19:20:25 -05:00
|
|
|
return outArgs, nil
|
|
|
|
}
|
|
|
|
|
2015-05-27 16:39:43 -04:00
|
|
|
func processArgs(args [][]string, ctx *interpolate.Context) ([][]string, error) {
|
2013-12-06 19:20:25 -05:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if args == nil {
|
|
|
|
return make([][]string, 0), err
|
|
|
|
}
|
|
|
|
|
|
|
|
newArgs := make([][]string, len(args))
|
|
|
|
for argsIdx, rowArgs := range args {
|
|
|
|
parms := make([]string, len(rowArgs))
|
|
|
|
newArgs[argsIdx] = parms
|
|
|
|
for i, parm := range rowArgs {
|
2015-05-27 16:39:43 -04:00
|
|
|
parms[i], err = interpolate.Render(parm, ctx)
|
2013-12-06 19:20:25 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newArgs, err
|
2013-10-07 21:58:08 -04:00
|
|
|
}
|