Skip to content
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

fix qemu firmware path locations #14182

Merged
merged 7 commits into from
May 17, 2022
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
38 changes: 19 additions & 19 deletions pkg/drivers/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import (
"github.com/docker/machine/libmachine/mcnutils"
"github.com/docker/machine/libmachine/ssh"
"github.com/docker/machine/libmachine/state"
"github.com/pkg/errors"

pkgdrivers "k8s.io/minikube/pkg/drivers"
)
Expand Down Expand Up @@ -79,11 +80,9 @@ type Driver struct {
DiskPath string
CacheMode string
IOMode string
// conn *libvirt.Connect
// VM *libvirt.Domain
UserDataFile string
CloudConfigRoot string
LocalPorts string
UserDataFile string
CloudConfigRoot string
LocalPorts string
}

func (d *Driver) GetMachineName() string {
Expand Down Expand Up @@ -271,21 +270,25 @@ func parsePortRange(rawPortRange string) (int, int, error) {

portRange := strings.Split(rawPortRange, "-")

if len(portRange) < 2 {
return 0, 0, errors.New("Invalid port range, must be at least of length 2")
}

minPort, err := strconv.Atoi(portRange[0])
if err != nil {
return 0, 0, fmt.Errorf("invalid port range")
return 0, 0, errors.Wrap(err, "Invalid port range")
}
maxPort, err := strconv.Atoi(portRange[1])
sharifelgamal marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return 0, 0, fmt.Errorf("invalid port range")
return 0, 0, errors.Wrap(err, "Invalid port range")
}

if maxPort < minPort {
return 0, 0, fmt.Errorf("invalid port range")
return 0, 0, errors.New("Invalid port range")
}

if maxPort-minPort < 2 {
return 0, 0, fmt.Errorf("port range must be minimum 2 ports")
return 0, 0, errors.New("Port range must be minimum 2 ports")
}

return minPort, maxPort, nil
Expand Down Expand Up @@ -338,10 +341,6 @@ func (d *Driver) Start() error {

if d.MachineType != "" {
machineType := d.MachineType
if runtime.GOOS == "darwin" {
// highmem=off needed, see https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#23800615 for details
machineType += ",accel=hvf,highmem=off"
}
startCmd = append(startCmd,
"-M", machineType,
)
Expand Down Expand Up @@ -380,6 +379,13 @@ func (d *Driver) Start() error {
}
}

// hardware acceleration is important, it increases performance by 10x
if runtime.GOOS == "darwin" {
startCmd = append(startCmd, "-accel", "hvf")
} else if _, err := os.Stat("/dev/kvm"); err == nil && runtime.GOOS == "linux" {
startCmd = append(startCmd, "-accel", "kvm")
}

startCmd = append(startCmd,
"-m", fmt.Sprintf("%d", d.Memory),
"-smp", fmt.Sprintf("%d", d.CPU),
Expand Down Expand Up @@ -419,12 +425,6 @@ func (d *Driver) Start() error {

startCmd = append(startCmd, "-daemonize")

// other options
// "-enable-kvm" if its available
if _, err := os.Stat("/dev/kvm"); err == nil {
startCmd = append(startCmd, "-enable-kvm")
}

if d.CloudConfigRoot != "" {
startCmd = append(startCmd,
"-fsdev",
Expand Down
41 changes: 34 additions & 7 deletions pkg/minikube/registry/drvs/qemu2/qemu2.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ package qemu2

import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"

Expand Down Expand Up @@ -64,17 +66,35 @@ func qemuSystemProgram() (string, error) {

func qemuFirmwarePath() (string, error) {
arch := runtime.GOARCH
// For macOS, find the correct brew installation path for qemu firmware
if runtime.GOOS == "darwin" {
var p, fw string
switch arch {
case "amd64":
p = "/usr/local/Cellar/qemu"
fw = "share/qemu/edk2-x86_64-code.fd"
case "arm64":
p = "/opt/homebrew/Cellar/qemu"
fw = "share/qemu/edk2-aarch64-code.fd"
default:
return "", fmt.Errorf("unknown arch: %s", arch)
}

v, err := ioutil.ReadDir(p)
if err != nil {
return "", fmt.Errorf("lookup qemu: %v", err)
}
for _, version := range v {
if version.IsDir() {
return path.Join(p, version.Name(), fw), nil
}
}
}

switch arch {
case "amd64":
// on macOS, we assume qemu is installed via homebrew for simplicity
if runtime.GOOS == "darwin" {
return "/usr/local/Cellar/qemu/6.2.0_1/share/qemu/edk2-x86_64-code.fd", nil
}
return "/usr/share/OVMF/OVMF_CODE.fd", nil
case "arm64":
if runtime.GOOS == "darwin" {
return "/opt/homebrew/Cellar/qemu/6.2.0_1/share/qemu/edk2-aarch64-code.fd", nil
}
return "/usr/share/AAVMF/AAVMF_CODE.fd", nil
default:
return "", fmt.Errorf("unknown arch: %s", arch)
Expand All @@ -96,6 +116,13 @@ func configure(cc config.ClusterConfig, n config.Node) (interface{}, error) {
case "arm64":
qemuMachine = "virt"
qemuCPU = "cortex-a72"
// highmem=off needed, see https://patchwork.kernel.org/project/qemu-devel/patch/[email protected]/#23800615 for details
if runtime.GOOS == "darwin" {
qemuMachine = "virt,highmem=off"
} else if _, err := os.Stat("/dev/kvm"); err == nil {
qemuMachine = "virt,gic-version=3"
qemuCPU = "host"
}
default:
return nil, fmt.Errorf("unknown arch: %s", runtime.GOARCH)
}
Expand Down