Skip to content

Implement experimental QEMU on Windows code #15781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 34 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 @@ -424,8 +427,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 +457,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 +530,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 +808,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