Skip to content

Commit

Permalink
qemu on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
spowelljr committed Feb 3, 2023
1 parent bd235db commit f5eacdf
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 19 deletions.
57 changes: 38 additions & 19 deletions pkg/drivers/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"io"
"math/rand"
"net"
"os"
Expand Down Expand Up @@ -169,21 +170,23 @@ func checkPid(pid int) error {
}

func (d *Driver) GetState() (state.State, error) {
if _, err := os.Stat(d.pidfilePath()); err != nil {
return state.Stopped, nil
}
p, err := os.ReadFile(d.pidfilePath())
if err != nil {
return state.Error, err
}
pid, err := strconv.Atoi(strings.TrimSpace(string(p)))
if err != nil {
return state.Error, err
}
if err := checkPid(pid); err != nil {
// No pid, remove pidfile
os.Remove(d.pidfilePath())
return state.Stopped, nil
if runtime.GOOS != "windows" {
if _, err := os.Stat(d.pidfilePath()); err != nil {
return state.Stopped, nil
}
p, err := os.ReadFile(d.pidfilePath())
if err != nil {
return state.Error, err
}
pid, err := strconv.Atoi(strings.TrimSpace(string(p)))
if err != nil {
return state.Error, err
}
if err := checkPid(pid); err != nil {
// No pid, remove pidfile
os.Remove(d.pidfilePath())
return state.Stopped, nil
}
}
ret, err := d.RunQMPCommand("query-status")
if err != nil {
Expand Down Expand Up @@ -392,6 +395,10 @@ func (d *Driver) Start() error {
// On Linux, enable the Kernel Virtual Machine accelerator.
startCmd = append(startCmd,
"-accel", "kvm")
//} else if runtime.GOOS == "windows" {
// // On Windows, enable the WHPX (Hyper-V) accelerator.
// startCmd = append(startCmd,
// "-accel", "whpx")
}

startCmd = append(startCmd,
Expand Down Expand Up @@ -424,8 +431,10 @@ func (d *Driver) Start() error {
return fmt.Errorf("unknown network: %s", d.Network)
}

startCmd = append(startCmd,
"-daemonize")
if runtime.GOOS != "windows" {
startCmd = append(startCmd,
"-daemonize")
}

if d.CloudConfigRoot != "" {
startCmd = append(startCmd,
Expand All @@ -452,7 +461,11 @@ func (d *Driver) Start() error {
startCmd = append([]string{d.SocketVMNetPath, d.Program}, startCmd...)
}

if stdout, stderr, err := cmdOutErr(startProgram, startCmd...); err != nil {
startFunc := cmdOutErr
if runtime.GOOS == "windows" {
startFunc = cmdStart
}
if stdout, stderr, err := startFunc(startProgram, startCmd...); err != nil {
fmt.Printf("OUTPUT: %s\n", stdout)
fmt.Printf("ERROR: %s\n", stderr)
return err
Expand Down Expand Up @@ -521,6 +534,12 @@ func cmdOutErr(cmdStr string, args ...string) (string, string, error) {
return stdoutStr, stderrStr, err
}

func cmdStart(cmdStr string, args ...string) (string, string, error) {
cmd := exec.Command(cmdStr, args...)
log.Debugf("executing: %s %s", cmdStr, strings.Join(args, " "))
return "", "", cmd.Start()
}

func (d *Driver) Stop() error {
if _, err := d.RunQMPCommand("system_powerdown"); err != nil {
return err
Expand Down Expand Up @@ -793,7 +812,7 @@ func WaitForTCPWithDelay(addr string, duration time.Duration) error {
continue
}
defer conn.Close()
if _, err := conn.Read(make([]byte, 1)); err != nil {
if _, err := conn.Read(make([]byte, 1)); err != nil && err != io.EOF {
time.Sleep(duration)
continue
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/minikube/registry/drvs/qemu2/qemu2.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func qemuFirmwarePath(customPath string) (string, error) {
if customPath != "" {
return customPath, nil
}
if runtime.GOOS == "windows" {
return "C:\\Program Files\\qemu\\share\\edk2-x86_64-code.fd", nil
}
arch := runtime.GOARCH
// For macOS, find the correct brew installation path for qemu firmware
if runtime.GOOS == "darwin" {
Expand Down

0 comments on commit f5eacdf

Please sign in to comment.