diff --git a/builder/qemu/driver.go b/builder/qemu/driver.go index 5c402cecb..e93c87836 100644 --- a/builder/qemu/driver.go +++ b/builder/qemu/driver.go @@ -4,7 +4,6 @@ import ( "bufio" "bytes" "fmt" - "github.com/mitchellh/multistep" "io" "log" "os/exec" @@ -14,6 +13,8 @@ import ( "syscall" "time" "unicode" + + "github.com/mitchellh/multistep" ) type DriverCancelCallback func(state multistep.StateBag) bool @@ -188,8 +189,8 @@ func (d *QemuDriver) Version() (string, error) { versionOutput := strings.TrimSpace(stdout.String()) log.Printf("Qemu --version output: %s", versionOutput) - versionRe := regexp.MustCompile("qemu-kvm-[0-9]\\.[0-9]") - matches := versionRe.Split(versionOutput, 2) + versionRe := regexp.MustCompile("[0-9]\\.[0-9]\\.[0-9]") + matches := versionRe.FindStringSubmatch(versionOutput) if len(matches) == 0 { return "", fmt.Errorf("No version found: %s", versionOutput) } diff --git a/builder/qemu/step_run.go b/builder/qemu/step_run.go index 356481085..8226b8651 100644 --- a/builder/qemu/step_run.go +++ b/builder/qemu/step_run.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "path/filepath" + "strconv" "strings" "github.com/mitchellh/multistep" @@ -62,6 +63,7 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error vncPort := state.Get("vnc_port").(uint) sshHostPort := state.Get("sshHostPort").(uint) ui := state.Get("ui").(packer.Ui) + driver := state.Get("driver").(Driver) vnc := fmt.Sprintf("0.0.0.0:%d", vncPort-5900) vmName := config.VMName @@ -82,7 +84,15 @@ func getCommandArgs(bootDrive string, state multistep.StateBag) ([]string, error defaultArgs["-netdev"] = fmt.Sprintf( "user,id=user.0,hostfwd=tcp::%v-:%d", sshHostPort, config.Comm.Port()) defaultArgs["-device"] = fmt.Sprintf("%s,netdev=user.0", config.NetDevice) - defaultArgs["-drive"] = fmt.Sprintf("file=%s,if=%s,cache=%s,discard=%s", imgPath, config.DiskInterface, config.DiskCache, config.DiskDiscard) + qemuVersion, err := driver.Version() + if err == nil { + parts := strings.Split(qemuVersion, ".") + if strconv.Atoi(parts[0]) >= 2 { + defaultArgs["-drive"] = fmt.Sprintf("file=%s,if=%s,cache=%s,discard=%s", imgPath, config.DiskInterface, config.DiskCache, config.DiskDiscard) + } + } else { + defaultArgs["-drive"] = fmt.Sprintf("file=%s,if=%s,cache=%s", imgPath, config.DiskInterface, config.DiskCache) + } if !config.DiskImage { defaultArgs["-cdrom"] = isoPath }