Skip to content

Commit

Permalink
pkg/osutil: move MacOSProductVersion() from pkg/qemu
Browse files Browse the repository at this point in the history
Signed-off-by: Akihiro Suda <[email protected]>
  • Loading branch information
AkihiroSuda committed Oct 26, 2023
1 parent 52c68fa commit 2c2c4d5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
29 changes: 29 additions & 0 deletions pkg/osutil/osversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package osutil

import (
"fmt"
"os/exec"
"strings"

"github.com/coreos/go-semver/semver"
)

// MacOSProductVersion returns the macOS product version like "12.3.1".
func MacOSProductVersion() (*semver.Version, error) {
cmd := exec.Command("sw_vers", "-productVersion")
// output is like "12.3.1\n"
b, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to execute %v: %w", cmd.Args, err)
}
verTrimmed := strings.TrimSpace(string(b))
// macOS 12.4 returns just "12.4\n"
for strings.Count(verTrimmed, ".") < 2 {
verTrimmed += ".0"
}
verSem, err := semver.NewVersion(verTrimmed)
if err != nil {
return nil, fmt.Errorf("failed to parse macOS version %q: %w", verTrimmed, err)
}
return verSem, nil
}
22 changes: 2 additions & 20 deletions pkg/qemu/qemu.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/lima-vm/lima/pkg/networks/usernet"
"github.com/lima-vm/lima/pkg/osutil"

"github.com/coreos/go-semver/semver"
"github.com/digitalocean/go-qemu/qmp"
Expand Down Expand Up @@ -398,25 +399,6 @@ func showDarwinARM64HVFQEMU620Warning(exe, accel string, features *features) {
logrus.Warn(w)
}

func getMacOSProductVersion() (*semver.Version, error) {
cmd := exec.Command("sw_vers", "-productVersion")
// output is like "12.3.1\n"
b, err := cmd.Output()
if err != nil {
return nil, fmt.Errorf("failed to execute %v: %w", cmd.Args, err)
}
verTrimmed := strings.TrimSpace(string(b))
// macOS 12.4 returns just "12.4\n"
for strings.Count(verTrimmed, ".") < 2 {
verTrimmed += ".0"
}
verSem, err := semver.NewVersion(verTrimmed)
if err != nil {
return nil, fmt.Errorf("failed to parse macOS version %q: %w", verTrimmed, err)
}
return verSem, nil
}

// adjustMemBytesDarwinARM64HVF adjusts the memory to be <= 3 GiB, only when the following conditions are met:
//
// - Host OS < macOS 12.4
Expand All @@ -443,7 +425,7 @@ func adjustMemBytesDarwinARM64HVF(memBytes int64, accel string, features *featur
if !features.VersionGEQ7 {
return memBytes
}
macOSProductVersion, err := getMacOSProductVersion()
macOSProductVersion, err := osutil.MacOSProductVersion()
if err != nil {
logrus.Warn(err)
return memBytes
Expand Down

0 comments on commit 2c2c4d5

Please sign in to comment.