Skip to content

Commit 8355d5e

Browse files
authored
Merge pull request #2316 from afbjorklund/cputype
Refactor CPUType to add defaultCPUType
2 parents 103a9e4 + afafcd4 commit 8355d5e

File tree

3 files changed

+63
-74
lines changed

3 files changed

+63
-74
lines changed

pkg/limayaml/defaults.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,35 @@ const (
3838

3939
var IPv4loopback1 = net.IPv4(127, 0, 0, 1)
4040

41+
func defaultCPUType() CPUType {
42+
cpuType := map[Arch]string{
43+
AARCH64: "cortex-a72",
44+
ARMV7L: "cortex-a7",
45+
// Since https://github.com/lima-vm/lima/pull/494, we use qemu64 cpu for better emulation of x86_64.
46+
X8664: "qemu64",
47+
RISCV64: "rv64", // FIXME: what is the right choice for riscv64?
48+
}
49+
for arch := range cpuType {
50+
if IsNativeArch(arch) && IsAccelOS() {
51+
if HasHostCPU() {
52+
cpuType[arch] = "host"
53+
} else if HasMaxCPU() {
54+
cpuType[arch] = "max"
55+
}
56+
}
57+
if arch == X8664 && runtime.GOOS == "darwin" {
58+
switch cpuType[arch] {
59+
case "host", "max":
60+
// Disable pdpe1gb on Intel Mac
61+
// https://github.com/lima-vm/lima/issues/1485
62+
// https://stackoverflow.com/a/72863744/5167443
63+
cpuType[arch] += ",-pdpe1gb"
64+
}
65+
}
66+
}
67+
return cpuType
68+
}
69+
4170
func defaultContainerdArchives() []File {
4271
const nerdctlVersion = "1.7.6"
4372
location := func(goos string, goarch string) string {
@@ -185,31 +214,7 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
185214
}
186215
}
187216

188-
cpuType := map[Arch]string{
189-
AARCH64: "cortex-a72",
190-
ARMV7L: "cortex-a7",
191-
// Since https://github.com/lima-vm/lima/pull/494, we use qemu64 cpu for better emulation of x86_64.
192-
X8664: "qemu64",
193-
RISCV64: "rv64", // FIXME: what is the right choice for riscv64?
194-
}
195-
for arch := range cpuType {
196-
if IsNativeArch(arch) && IsAccelOS() {
197-
if HasHostCPU() {
198-
cpuType[arch] = "host"
199-
} else if HasMaxCPU() {
200-
cpuType[arch] = "max"
201-
}
202-
}
203-
if arch == X8664 && runtime.GOOS == "darwin" {
204-
switch cpuType[arch] {
205-
case "host", "max":
206-
// Disable pdpe1gb on Intel Mac
207-
// https://github.com/lima-vm/lima/issues/1485
208-
// https://stackoverflow.com/a/72863744/5167443
209-
cpuType[arch] += ",-pdpe1gb"
210-
}
211-
}
212-
}
217+
cpuType := defaultCPUType()
213218
var overrideCPUType bool
214219
for k, v := range d.CPUType {
215220
if len(v) > 0 {

pkg/limayaml/defaults_test.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,10 @@ func TestFillDefault(t *testing.T) {
5959

6060
// Builtin default values
6161
builtin := LimaYAML{
62-
VMType: ptr.Of("qemu"),
63-
OS: ptr.Of(LINUX),
64-
Arch: ptr.Of(arch),
65-
CPUType: map[Arch]string{
66-
AARCH64: "cortex-a72",
67-
ARMV7L: "cortex-a7",
68-
X8664: "qemu64",
69-
RISCV64: "rv64",
70-
},
62+
VMType: ptr.Of("qemu"),
63+
OS: ptr.Of(LINUX),
64+
Arch: ptr.Of(arch),
65+
CPUType: defaultCPUType(),
7166
CPUs: ptr.Of(defaultCPUs()),
7267
Memory: ptr.Of(defaultMemoryAsString()),
7368
Disk: ptr.Of(defaultDiskSizeAsString()),
@@ -108,19 +103,6 @@ func TestFillDefault(t *testing.T) {
108103
},
109104
Plain: ptr.Of(false),
110105
}
111-
if IsAccelOS() {
112-
if HasHostCPU() {
113-
builtin.CPUType[arch] = "host"
114-
} else if HasMaxCPU() {
115-
builtin.CPUType[arch] = "max"
116-
}
117-
if arch == X8664 && runtime.GOOS == "darwin" {
118-
switch builtin.CPUType[arch] {
119-
case "host", "max":
120-
builtin.CPUType[arch] += ",-pdpe1gb"
121-
}
122-
}
123-
}
124106

125107
defaultPortForward := PortForward{
126108
GuestIP: IPv4loopback1,
@@ -298,7 +280,7 @@ func TestFillDefault(t *testing.T) {
298280
VMType: ptr.Of("vz"),
299281
OS: ptr.Of("unknown"),
300282
Arch: ptr.Of("unknown"),
301-
CPUType: map[Arch]string{
283+
CPUType: CPUType{
302284
AARCH64: "arm64",
303285
ARMV7L: "armhf",
304286
X8664: "amd64",
@@ -487,7 +469,7 @@ func TestFillDefault(t *testing.T) {
487469
VMType: ptr.Of("qemu"),
488470
OS: ptr.Of(LINUX),
489471
Arch: ptr.Of(arch),
490-
CPUType: map[Arch]string{
472+
CPUType: CPUType{
491473
AARCH64: "uber-arm",
492474
ARMV7L: "armv8",
493475
X8664: "pentium",

pkg/limayaml/limayaml.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,31 @@ import (
77
)
88

99
type LimaYAML struct {
10-
VMType *VMType `yaml:"vmType,omitempty" json:"vmType,omitempty"`
11-
OS *OS `yaml:"os,omitempty" json:"os,omitempty"`
12-
Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
13-
Images []Image `yaml:"images" json:"images"` // REQUIRED
14-
CPUType map[Arch]string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
15-
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
16-
Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes
17-
Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes
18-
AdditionalDisks []Disk `yaml:"additionalDisks,omitempty" json:"additionalDisks,omitempty"`
19-
Mounts []Mount `yaml:"mounts,omitempty" json:"mounts,omitempty"`
20-
MountType *MountType `yaml:"mountType,omitempty" json:"mountType,omitempty"`
21-
MountInotify *bool `yaml:"mountInotify,omitempty" json:"mountInotify,omitempty"`
22-
SSH SSH `yaml:"ssh,omitempty" json:"ssh,omitempty"` // REQUIRED (FIXME)
23-
Firmware Firmware `yaml:"firmware,omitempty" json:"firmware,omitempty"`
24-
Audio Audio `yaml:"audio,omitempty" json:"audio,omitempty"`
25-
Video Video `yaml:"video,omitempty" json:"video,omitempty"`
26-
Provision []Provision `yaml:"provision,omitempty" json:"provision,omitempty"`
27-
UpgradePackages *bool `yaml:"upgradePackages,omitempty" json:"upgradePackages,omitempty"`
28-
Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"`
29-
GuestInstallPrefix *string `yaml:"guestInstallPrefix,omitempty" json:"guestInstallPrefix,omitempty"`
30-
Probes []Probe `yaml:"probes,omitempty" json:"probes,omitempty"`
31-
PortForwards []PortForward `yaml:"portForwards,omitempty" json:"portForwards,omitempty"`
32-
CopyToHost []CopyToHost `yaml:"copyToHost,omitempty" json:"copyToHost,omitempty"`
33-
Message string `yaml:"message,omitempty" json:"message,omitempty"`
34-
Networks []Network `yaml:"networks,omitempty" json:"networks,omitempty"`
10+
VMType *VMType `yaml:"vmType,omitempty" json:"vmType,omitempty"`
11+
OS *OS `yaml:"os,omitempty" json:"os,omitempty"`
12+
Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
13+
Images []Image `yaml:"images" json:"images"` // REQUIRED
14+
CPUType CPUType `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
15+
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
16+
Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes
17+
Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes
18+
AdditionalDisks []Disk `yaml:"additionalDisks,omitempty" json:"additionalDisks,omitempty"`
19+
Mounts []Mount `yaml:"mounts,omitempty" json:"mounts,omitempty"`
20+
MountType *MountType `yaml:"mountType,omitempty" json:"mountType,omitempty"`
21+
MountInotify *bool `yaml:"mountInotify,omitempty" json:"mountInotify,omitempty"`
22+
SSH SSH `yaml:"ssh,omitempty" json:"ssh,omitempty"` // REQUIRED (FIXME)
23+
Firmware Firmware `yaml:"firmware,omitempty" json:"firmware,omitempty"`
24+
Audio Audio `yaml:"audio,omitempty" json:"audio,omitempty"`
25+
Video Video `yaml:"video,omitempty" json:"video,omitempty"`
26+
Provision []Provision `yaml:"provision,omitempty" json:"provision,omitempty"`
27+
UpgradePackages *bool `yaml:"upgradePackages,omitempty" json:"upgradePackages,omitempty"`
28+
Containerd Containerd `yaml:"containerd,omitempty" json:"containerd,omitempty"`
29+
GuestInstallPrefix *string `yaml:"guestInstallPrefix,omitempty" json:"guestInstallPrefix,omitempty"`
30+
Probes []Probe `yaml:"probes,omitempty" json:"probes,omitempty"`
31+
PortForwards []PortForward `yaml:"portForwards,omitempty" json:"portForwards,omitempty"`
32+
CopyToHost []CopyToHost `yaml:"copyToHost,omitempty" json:"copyToHost,omitempty"`
33+
Message string `yaml:"message,omitempty" json:"message,omitempty"`
34+
Networks []Network `yaml:"networks,omitempty" json:"networks,omitempty"`
3535
// `network` was deprecated in Lima v0.7.0, removed in Lima v0.14.0. Use `networks` instead.
3636
Env map[string]string `yaml:"env,omitempty" json:"env,omitempty"`
3737
DNS []net.IP `yaml:"dns,omitempty" json:"dns,omitempty"`
@@ -51,6 +51,8 @@ type (
5151
VMType = string
5252
)
5353

54+
type CPUType = map[Arch]string
55+
5456
const (
5557
LINUX OS = "Linux"
5658

0 commit comments

Comments
 (0)