Skip to content

Commit de88a43

Browse files
authored
Merge pull request #1968 from AkihiroSuda/vz-trivials
vz: trivial changes
2 parents 21976a9 + 017a0d7 commit de88a43

File tree

7 files changed

+70
-22
lines changed

7 files changed

+70
-22
lines changed

cmd/limactl/factory-reset.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"strings"
88

99
"github.com/lima-vm/lima/pkg/store"
10+
"github.com/lima-vm/lima/pkg/store/filenames"
1011
"github.com/sirupsen/logrus"
1112
"github.com/spf13/cobra"
1213
)
@@ -45,7 +46,7 @@ func factoryResetAction(_ *cobra.Command, args []string) error {
4546
}
4647
for _, f := range fi {
4748
path := filepath.Join(inst.Dir, f.Name())
48-
if !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") {
49+
if !strings.HasSuffix(path, ".yaml") && !strings.HasSuffix(path, ".yml") && f.Name() != filenames.VzIdentifier {
4950
logrus.Infof("Removing %q", path)
5051
if err := os.Remove(path); err != nil {
5152
logrus.Error(err)

pkg/driver/driver.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ type Driver interface {
1616
// Validate returns error if the current driver isn't support for given config
1717
Validate() error
1818

19+
// Initialize is called on creating the instance for initialization.
20+
// (e.g., creating "vz-identifier" file)
21+
//
22+
// Initialize MUST return nil when it is called against an existing instance.
23+
//
24+
// Initialize does not create the disks.
25+
Initialize(_ context.Context) error
26+
1927
// CreateDisk returns error if the current driver fails in creating disk
2028
CreateDisk() error
2129

@@ -69,6 +77,10 @@ func (d *BaseDriver) Validate() error {
6977
return nil
7078
}
7179

80+
func (d *BaseDriver) Initialize(_ context.Context) error {
81+
return nil
82+
}
83+
7284
func (d *BaseDriver) CreateDisk() error {
7385
return nil
7486
}

pkg/osutil/osversion_darwin.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package osutil
2+
3+
import (
4+
"fmt"
5+
"os/exec"
6+
"strings"
7+
8+
"github.com/coreos/go-semver/semver"
9+
)
10+
11+
// ProductVersion returns the macOS product version like "12.3.1".
12+
func ProductVersion() (*semver.Version, error) {
13+
cmd := exec.Command("sw_vers", "-productVersion")
14+
// output is like "12.3.1\n"
15+
b, err := cmd.Output()
16+
if err != nil {
17+
return nil, fmt.Errorf("failed to execute %v: %w", cmd.Args, err)
18+
}
19+
verTrimmed := strings.TrimSpace(string(b))
20+
// macOS 12.4 returns just "12.4\n"
21+
for strings.Count(verTrimmed, ".") < 2 {
22+
verTrimmed += ".0"
23+
}
24+
verSem, err := semver.NewVersion(verTrimmed)
25+
if err != nil {
26+
return nil, fmt.Errorf("failed to parse macOS version %q: %w", verTrimmed, err)
27+
}
28+
return verSem, nil
29+
}

pkg/osutil/osversion_others.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//go:build !darwin
2+
3+
package osutil
4+
5+
import (
6+
"errors"
7+
8+
"github.com/coreos/go-semver/semver"
9+
)
10+
11+
// ProductVersion returns the OS product version, not the kernel version.
12+
func ProductVersion() (*semver.Version, error) {
13+
return nil, errors.New("not implemented")
14+
}

pkg/qemu/qemu.go

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"time"
1818

1919
"github.com/lima-vm/lima/pkg/networks/usernet"
20+
"github.com/lima-vm/lima/pkg/osutil"
2021

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

401-
func getMacOSProductVersion() (*semver.Version, error) {
402-
cmd := exec.Command("sw_vers", "-productVersion")
403-
// output is like "12.3.1\n"
404-
b, err := cmd.Output()
405-
if err != nil {
406-
return nil, fmt.Errorf("failed to execute %v: %w", cmd.Args, err)
407-
}
408-
verTrimmed := strings.TrimSpace(string(b))
409-
// macOS 12.4 returns just "12.4\n"
410-
for strings.Count(verTrimmed, ".") < 2 {
411-
verTrimmed += ".0"
412-
}
413-
verSem, err := semver.NewVersion(verTrimmed)
414-
if err != nil {
415-
return nil, fmt.Errorf("failed to parse macOS version %q: %w", verTrimmed, err)
416-
}
417-
return verSem, nil
418-
}
419-
420402
// adjustMemBytesDarwinARM64HVF adjusts the memory to be <= 3 GiB, only when the following conditions are met:
421403
//
422404
// - Host OS < macOS 12.4
@@ -443,7 +425,7 @@ func adjustMemBytesDarwinARM64HVF(memBytes int64, accel string, features *featur
443425
if !features.VersionGEQ7 {
444426
return memBytes
445427
}
446-
macOSProductVersion, err := getMacOSProductVersion()
428+
macOSProductVersion, err := osutil.ProductVersion()
447429
if err != nil {
448430
logrus.Warn(err)
449431
return memBytes

pkg/start/start.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Prepared struct {
7272
}
7373

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

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

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

pkg/vz/vz_driver_darwin.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ func (l *LimaVzDriver) Validate() error {
129129
return nil
130130
}
131131

132+
func (l *LimaVzDriver) Initialize(_ context.Context) error {
133+
_, err := getMachineIdentifier(l.BaseDriver)
134+
return err
135+
}
136+
132137
func (l *LimaVzDriver) CreateDisk() error {
133138
return EnsureDisk(l.BaseDriver)
134139
}

0 commit comments

Comments
 (0)