Skip to content

vz: trivial changes #1968

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 4 commits into from
Oct 30, 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
3 changes: 2 additions & 1 deletion cmd/limactl/factory-reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func factoryResetAction(_ *cobra.Command, args []string) error {
}
for _, f := range fi {
path := filepath.Join(inst.Dir, f.Name())
if !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") {
if !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") && f.Name() != filenames.VzIdentifier {
logrus.Infof("Removing %q", path)
if err := os.Remove(path); err != nil {
logrus.Error(err)
Expand Down
12 changes: 12 additions & 0 deletions pkg/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ type Driver interface {
// Validate returns error if the current driver isn't support for given config
Validate() error

// Initialize is called on creating the instance for initialization.
// (e.g., creating "vz-identifier" file)
//
// Initialize MUST return nil when it is called against an existing instance.
//
// Initialize does not create the disks.
Initialize(_ context.Context) error

// CreateDisk returns error if the current driver fails in creating disk
CreateDisk() error

Expand Down Expand Up @@ -69,6 +77,10 @@ func (d *BaseDriver) Validate() error {
return nil
}

func (d *BaseDriver) Initialize(_ context.Context) error {
return nil
}

func (d *BaseDriver) CreateDisk() error {
return nil
}
Expand Down
29 changes: 29 additions & 0 deletions pkg/osutil/osversion_darwin.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"
)

// ProductVersion returns the macOS product version like "12.3.1".
func ProductVersion() (*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
}
14 changes: 14 additions & 0 deletions pkg/osutil/osversion_others.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build !darwin

package osutil

import (
"errors"

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

// ProductVersion returns the OS product version, not the kernel version.
func ProductVersion() (*semver.Version, error) {
return nil, errors.New("not implemented")
}
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.ProductVersion()
if err != nil {
logrus.Warn(err)
return memBytes
Expand Down
7 changes: 6 additions & 1 deletion pkg/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Prepared struct {
}

// Prepare ensures the disk, the nerdctl archive, etc.
func Prepare(_ context.Context, inst *store.Instance) (*Prepared, error) {
func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
y, err := inst.LoadYAML()
if err != nil {
return nil, err
Expand All @@ -87,6 +87,10 @@ func Prepare(_ context.Context, inst *store.Instance) (*Prepared, error) {
return nil, err
}

if err := limaDriver.Initialize(ctx); err != nil {
return nil, err
}

// Check if the instance has been created (the base disk already exists)
created := false
baseDisk := filepath.Join(inst.Dir, filenames.BaseDisk)
Expand Down Expand Up @@ -114,6 +118,7 @@ func Start(ctx context.Context, inst *store.Instance) error {
if _, err := os.Stat(haPIDPath); !errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("instance %q seems running (hint: remove %q if the instance is not actually running)", inst.Name, haPIDPath)
}
logrus.Infof("Starting the instance %q with VM driver %q", inst.Name, inst.VMType)

haSockPath := filepath.Join(inst.Dir, filenames.HostAgentSock)

Expand Down
5 changes: 5 additions & 0 deletions pkg/vz/vz_driver_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func (l *LimaVzDriver) Validate() error {
return nil
}

func (l *LimaVzDriver) Initialize(_ context.Context) error {
_, err := getMachineIdentifier(l.BaseDriver)
return err
}

func (l *LimaVzDriver) CreateDisk() error {
return EnsureDisk(l.BaseDriver)
}
Expand Down