Skip to content

Commit

Permalink
Merge pull request #22636 from jakecorrenti/krunkit-detection
Browse files Browse the repository at this point in the history
machine: Add LibKrun provider detection
  • Loading branch information
openshift-merge-bot[bot] authored May 8, 2024
2 parents 092d040 + ee5153c commit 1ee9014
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
74 changes: 55 additions & 19 deletions pkg/machine/provider/platform_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package provider

import (
"bytes"
"errors"
"fmt"
"os"
"os/exec"
"strconv"
"runtime"
"strings"

"github.com/blang/semver/v4"
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v5/pkg/machine/applehv"
"github.com/containers/podman/v5/pkg/machine/define"
Expand Down Expand Up @@ -44,42 +44,78 @@ func Get() (vmconfigs.VMProvider, error) {

// SupportedProviders returns the providers that are supported on the host operating system
func SupportedProviders() []define.VMType {
return []define.VMType{define.AppleHvVirt}
supported := []define.VMType{define.AppleHvVirt}
if runtime.GOARCH == "arm64" {
return append(supported, define.LibKrun)
}
return supported
}

// InstalledProviders returns the supported providers that are installed on the host
func InstalledProviders() ([]define.VMType, error) {
installed := []define.VMType{}

appleHvInstalled, err := appleHvInstalled()
if err != nil {
return nil, err
}
if appleHvInstalled {
installed = append(installed, define.AppleHvVirt)
}

libKrunInstalled, err := libKrunInstalled()
if err != nil {
return nil, err
}
if libKrunInstalled {
installed = append(installed, define.LibKrun)
}

return installed, nil
}

func appleHvInstalled() (bool, error) {
var outBuf bytes.Buffer
// Apple's Virtualization.Framework is only supported on MacOS 11.0+
const SupportedMacOSVersion = 11
// Apple's Virtualization.Framework is only supported on MacOS 11.0+,
// but to use EFI MacOS 13.0+ is required
expectedVer, err := semver.Make("13.0.0")
if err != nil {
return false, err
}

cmd := exec.Command("sw_vers", "--productVersion")
cmd.Stdout = &outBuf
if err := cmd.Run(); err != nil {
return nil, fmt.Errorf("unable to check current macOS version using `sw_vers --productVersion`: %s", err)
return false, fmt.Errorf("unable to check current macOS version using `sw_vers --productVersion`: %s", err)
}

// the output will be in the format of MAJOR.MINOR.PATCH
output := outBuf.String()
idx := strings.Index(output, ".")
if idx < 0 {
return nil, errors.New("invalid output provided by sw_vers --productVersion")
}
majorString := output[:idx]
majorInt, err := strconv.Atoi(majorString)
output := strings.TrimSuffix(outBuf.String(), "\n")
currentVer, err := semver.Make(output)
if err != nil {
return nil, err
return false, err
}

if majorInt >= SupportedMacOSVersion {
return []define.VMType{define.AppleHvVirt}, nil
return currentVer.GTE(expectedVer), nil
}

func libKrunInstalled() (bool, error) {
if runtime.GOARCH != "arm64" {
return false, nil
}

// need to verify that krunkit, virglrenderer, and libkrun-efi are installed
cfg, err := config.Default()
if err != nil {
return false, err
}

return []define.VMType{}, nil
_, err = cfg.FindHelperBinary("krunkit", false)
return err == nil, nil
}

// HasPermsForProvider returns whether the host operating system has the proper permissions to use the given provider
func HasPermsForProvider(provider define.VMType) bool {
// there are no permissions required for AppleHV
return provider == define.AppleHvVirt
// there are no permissions required for AppleHV or LibKrun
return provider == define.AppleHvVirt || provider == define.LibKrun
}
13 changes: 12 additions & 1 deletion pkg/machine/provider/platform_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ import (
func TestSupportedProviders(t *testing.T) {
switch runtime.GOOS {
case "darwin":
assert.Equal(t, []define.VMType{define.AppleHvVirt}, SupportedProviders())
if runtime.GOARCH == "arm64" {
assert.Equal(t, []define.VMType{define.AppleHvVirt, define.LibKrun}, SupportedProviders())
} else {
assert.Equal(t, []define.VMType{define.AppleHvVirt}, SupportedProviders())
}
case "windows":
assert.Equal(t, []define.VMType{define.WSLVirt, define.HyperVVirt}, SupportedProviders())
case "linux":
Expand All @@ -24,6 +28,7 @@ func TestInstalledProviders(t *testing.T) {
assert.Nil(t, err)
switch runtime.GOOS {
case "darwin":
// TODO: need to verify if an arm64 machine reports {applehv, libkrun}
assert.Equal(t, []define.VMType{define.AppleHvVirt}, installed)
case "windows":
provider, err := Get()
Expand Down Expand Up @@ -55,6 +60,9 @@ func TestBadSupportedProviders(t *testing.T) {
switch runtime.GOOS {
case "darwin":
assert.NotEqual(t, []define.VMType{define.QemuVirt}, SupportedProviders())
if runtime.GOARCH != "arm64" {
assert.NotEqual(t, []define.VMType{define.AppleHvVirt, define.LibKrun}, SupportedProviders())
}
case "windows":
assert.NotEqual(t, []define.VMType{define.QemuVirt}, SupportedProviders())
case "linux":
Expand All @@ -68,6 +76,9 @@ func TestBadInstalledProviders(t *testing.T) {
switch runtime.GOOS {
case "darwin":
assert.NotEqual(t, []define.VMType{define.QemuVirt}, installed)
if runtime.GOARCH != "arm64" {
assert.NotEqual(t, []define.VMType{define.AppleHvVirt, define.LibKrun}, installed)
}
case "windows":
assert.NotContains(t, installed, define.QemuVirt)
case "linux":
Expand Down

1 comment on commit 1ee9014

@packit-as-a-service
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

podman-next COPR build failed. @containers/packit-build please check.

Please sign in to comment.