Skip to content

Commit 59438a1

Browse files
authored
Merge pull request #656 from rancher-sandbox/cputype-list
Make cpuType a map instead of a single value
2 parents ae9733c + 4c9b713 commit 59438a1

File tree

7 files changed

+69
-42
lines changed

7 files changed

+69
-42
lines changed

pkg/limayaml/default.yaml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,6 @@ images:
2727
- location: "https://cloud-images.ubuntu.com/impish/current/impish-server-cloudimg-arm64.img"
2828
arch: "aarch64"
2929

30-
# Specify QEMU CPU emulation, e.g., "host", "qemu64", "Haswell".
31-
# You can see what options are available for host emulation with: `qemu-system-$(arch) -cpu help`.
32-
# Setting of instructions is supported like this: "qemu64,+ssse3".
33-
# Default: "host" (native arch), "qemu64" (Intel on ARM), or "cortex-a72" (ARM on Intel)
34-
cpuType: null
35-
3630
# CPUs: if you see performance issues, try limiting cpus to 1.
3731
# Default: 4
3832
cpus: null
@@ -141,6 +135,15 @@ containerd:
141135
# FURTHER ADVANCED CONFIGURATION
142136
# ===================================================================== #
143137

138+
# Specify desired QEMU CPU type for each arch.
139+
# You can see what options are available for host emulation with: `qemu-system-$(arch) -cpu help`.
140+
# Setting of instructions is supported like this: "qemu64,+ssse3".
141+
cpuType:
142+
# Default: "cortex-a72" (or "host" when running on arm64)
143+
aarch64: null
144+
# Default: "qemu64" (or "host" when running on amd64)
145+
x86_64: null
146+
144147
firmware:
145148
# Use legacy BIOS instead of UEFI.
146149
# Default: false

pkg/limayaml/defaults.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,32 @@ func FillDefault(y, d, o *LimaYAML, filePath string) {
8181
}
8282
}
8383

84-
if y.CPUType == nil {
85-
y.CPUType = d.CPUType
86-
}
87-
if o.CPUType != nil {
88-
y.CPUType = o.CPUType
89-
}
90-
if y.CPUType == nil || *y.CPUType == "" {
91-
if IsNativeArch(*y.Arch) {
92-
y.CPUType = pointer.String("host")
93-
} else if *y.Arch == X8664 {
94-
// Intel on ARM
95-
// Since https://github.com/lima-vm/lima/pull/494, we use qemu64 cpu for better emulation of x86_64.
96-
y.CPUType = pointer.String("qemu64")
97-
} else {
98-
// ARM on Intel
99-
y.CPUType = pointer.String("cortex-a72")
84+
cpuType := map[Arch]string{
85+
AARCH64: "cortex-a72",
86+
// Since https://github.com/lima-vm/lima/pull/494, we use qemu64 cpu for better emulation of x86_64.
87+
X8664: "qemu64",
88+
}
89+
for arch := range cpuType {
90+
if IsNativeArch(arch) {
91+
cpuType[arch] = "host"
92+
}
93+
}
94+
for k, v := range d.CPUType {
95+
if len(v) > 0 {
96+
cpuType[k] = v
97+
}
98+
}
99+
for k, v := range y.CPUType {
100+
if len(v) > 0 {
101+
cpuType[k] = v
102+
}
103+
}
104+
for k, v := range o.CPUType {
105+
if len(v) > 0 {
106+
cpuType[k] = v
100107
}
101108
}
109+
y.CPUType = cpuType
102110

103111
if y.CPUs == nil {
104112
y.CPUs = d.CPUs

pkg/limayaml/defaults_test.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ func TestFillDefault(t *testing.T) {
4747

4848
// Builtin default values
4949
builtin := LimaYAML{
50-
Arch: pointer.String(arch),
51-
CPUType: pointer.String("host"),
52-
CPUs: pointer.Int(4),
53-
Memory: pointer.String("4GiB"),
54-
Disk: pointer.String("100GiB"),
50+
Arch: pointer.String(arch),
51+
CPUType: map[Arch]string{
52+
AARCH64: "cortex-a72",
53+
X8664: "qemu64",
54+
},
55+
CPUs: pointer.Int(4),
56+
Memory: pointer.String("4GiB"),
57+
Disk: pointer.String("100GiB"),
5558
Containerd: Containerd{
5659
System: pointer.Bool(false),
5760
User: pointer.Bool(true),
@@ -74,6 +77,7 @@ func TestFillDefault(t *testing.T) {
7477
},
7578
PropagateProxyEnv: pointer.Bool(true),
7679
}
80+
builtin.CPUType[arch] = "host"
7781

7882
defaultPortForward := PortForward{
7983
GuestIP: api.IPv4loopback1,
@@ -169,11 +173,14 @@ func TestFillDefault(t *testing.T) {
169173

170174
// Choose values that are different from the "builtin" defaults
171175
d = LimaYAML{
172-
Arch: pointer.String("unknown"),
173-
CPUType: pointer.String("host"),
174-
CPUs: pointer.Int(7),
175-
Memory: pointer.String("5GiB"),
176-
Disk: pointer.String("105GiB"),
176+
Arch: pointer.String("unknown"),
177+
CPUType: map[Arch]string{
178+
AARCH64: "arm64",
179+
X8664: "amd64",
180+
},
181+
CPUs: pointer.Int(7),
182+
Memory: pointer.String("5GiB"),
183+
Disk: pointer.String("105GiB"),
177184
Containerd: Containerd{
178185
System: pointer.Bool(true),
179186
User: pointer.Bool(false),
@@ -282,11 +289,14 @@ func TestFillDefault(t *testing.T) {
282289
// User-provided overrides should override user-provided config settings
283290

284291
o = LimaYAML{
285-
Arch: pointer.String(arch),
286-
CPUType: pointer.String("host"),
287-
CPUs: pointer.Int(12),
288-
Memory: pointer.String("7GiB"),
289-
Disk: pointer.String("117GiB"),
292+
Arch: pointer.String(arch),
293+
CPUType: map[Arch]string{
294+
AARCH64: "uber-arm",
295+
X8664: "pentium",
296+
},
297+
CPUs: pointer.Int(12),
298+
Memory: pointer.String("7GiB"),
299+
Disk: pointer.String("117GiB"),
290300
Containerd: Containerd{
291301
System: pointer.Bool(true),
292302
User: pointer.Bool(false),

pkg/limayaml/limayaml.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
type LimaYAML struct {
1010
Arch *Arch `yaml:"arch,omitempty" json:"arch,omitempty"`
1111
Images []File `yaml:"images" json:"images"` // REQUIRED
12-
CPUType *string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
12+
CPUType map[Arch]string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
1313
CPUs *int `yaml:"cpus,omitempty" json:"cpus,omitempty"`
1414
Memory *string `yaml:"memory,omitempty" json:"memory,omitempty"` // go-units.RAMInBytes
1515
Disk *string `yaml:"disk,omitempty" json:"disk,omitempty"` // go-units.RAMInBytes

pkg/limayaml/validate.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ func Validate(y LimaYAML, warn bool) error {
5050
}
5151
}
5252

53-
if *y.CPUType == "" {
54-
return fmt.Errorf("field `cpuType` must be set")
53+
for arch := range y.CPUType {
54+
switch arch {
55+
case AARCH64, X8664:
56+
// these are the only supported architectures
57+
default:
58+
return fmt.Errorf("field `cpuType` uses unsupported arch %q", arch)
59+
}
5560
}
5661

5762
if *y.CPUs == 0 {

pkg/qemu/qemu.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ func Cmdline(cfg Config) (string, []string, error) {
207207
return "", nil, errors.New(errStr)
208208
}
209209

210-
cpu := *y.CPUType
210+
cpu := y.CPUType[*y.Arch]
211211
args = appendArgsIfNoConflict(args, "-cpu", cpu)
212212
switch *y.Arch {
213213
case limayaml.X8664:

pkg/store/instance.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ func Inspect(instName string) (*Instance, error) {
7676
}
7777
inst.Dir = instDir
7878
inst.Arch = *y.Arch
79-
inst.CPUType = *y.CPUType
79+
inst.CPUType = y.CPUType[*y.Arch]
80+
8081
inst.CPUs = *y.CPUs
8182
memory, err := units.RAMInBytes(*y.Memory)
8283
if err == nil {

0 commit comments

Comments
 (0)