-
-
Notifications
You must be signed in to change notification settings - Fork 402
/
yaml.go
407 lines (360 loc) · 11.3 KB
/
yaml.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package lima
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"strings"
"github.com/abiosoft/colima/daemon"
"github.com/abiosoft/colima/daemon/process/vmnet"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/environment"
"github.com/abiosoft/colima/environment/container/containerd"
"github.com/abiosoft/colima/environment/container/docker"
"github.com/abiosoft/colima/environment/container/incus"
"github.com/abiosoft/colima/environment/vm/lima/limaconfig"
"github.com/abiosoft/colima/environment/vm/lima/limautil"
"github.com/abiosoft/colima/util"
"github.com/sirupsen/logrus"
)
func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err error) {
l.Arch = environment.Arch(conf.Arch).Value()
// VM type is qemu except in few scenarios
l.VMType = limaconfig.QEMU
sameArchitecture := environment.HostArch() == l.Arch
// when vz is chosen and OS version supports it
if util.MacOS13OrNewer() && conf.VMType == limaconfig.VZ && sameArchitecture {
l.VMType = limaconfig.VZ
// Rosetta is only available on M1
if conf.VZRosetta && util.MacOS13OrNewerOnArm() {
if util.RosettaRunning() {
l.Rosetta.Enabled = true
l.Rosetta.BinFmt = true
} else {
logrus.Warnln("Unable to enable Rosetta: Rosetta2 is not installed")
logrus.Warnln("Run 'softwareupdate --install-rosetta' to install Rosetta2")
}
}
if util.MacOSNestedVirtualizationSupported() {
l.NestedVirtualization = conf.NestedVirtualization
}
}
if conf.CPUType != "" && conf.CPUType != "host" {
l.CPUType = map[environment.Arch]string{
l.Arch: conf.CPUType,
}
}
if conf.CPU > 0 {
l.CPUs = &conf.CPU
}
if conf.Memory > 0 {
l.Memory = fmt.Sprintf("%dMiB", uint32(conf.Memory*1024))
}
if conf.Disk > 0 {
l.Disk = fmt.Sprintf("%dGiB", conf.Disk)
}
l.SSH = limaconfig.SSH{LocalPort: conf.SSHPort, LoadDotSSHPubKeys: false, ForwardAgent: conf.ForwardAgent}
l.Containerd = limaconfig.Containerd{System: false, User: false}
l.DNS = conf.Network.DNSResolvers
l.HostResolver.Enabled = len(conf.Network.DNSResolvers) == 0
l.HostResolver.Hosts = conf.Network.DNSHosts
if l.HostResolver.Hosts == nil {
l.HostResolver.Hosts = make(map[string]string)
}
if _, ok := l.HostResolver.Hosts["host.docker.internal"]; !ok {
l.HostResolver.Hosts["host.docker.internal"] = "host.lima.internal"
}
l.Env = conf.Env
if l.Env == nil {
l.Env = make(map[string]string)
}
// extra required provision commands
{
// fix inotify
l.Provision = append(l.Provision, limaconfig.Provision{
Mode: limaconfig.ProvisionModeSystem,
Script: "sysctl -w fs.inotify.max_user_watches=1048576",
})
// add user to docker group
// "sudo", "usermod", "-aG", "docker", user
if conf.Runtime == docker.Name {
l.Provision = append(l.Provision, limaconfig.Provision{
Mode: limaconfig.ProvisionModeDependency,
Script: "groupadd -f docker && usermod -aG docker {{ .User }}",
})
}
// add user to incus-admin group
// "sudo", "usermod", "-aG", "incus-admin", user
if conf.Runtime == incus.Name {
l.Provision = append(l.Provision, limaconfig.Provision{
Mode: limaconfig.ProvisionModeDependency,
Script: "groupadd -f incus-admin && usermod -aG incus-admin {{ .User }}",
})
}
// set hostname
hostname := config.CurrentProfile().ID
if conf.Hostname != "" {
hostname = conf.Hostname
}
l.Provision = append(l.Provision, limaconfig.Provision{
Mode: limaconfig.ProvisionModeSystem,
Script: "hostnamectl set-hostname " + hostname,
})
}
// network setup
{
l.Networks = append(l.Networks, limaconfig.Network{
Lima: "user-v2",
})
reachableIPAddress := true
if conf.Network.Address {
// incus always uses vmnet
if l.VMType == limaconfig.VZ && conf.Runtime != incus.Name {
l.Networks = append(l.Networks, limaconfig.Network{
VZNAT: true,
Interface: limautil.NetInterface,
Metric: limautil.NetMetric,
})
} else {
reachableIPAddress, _ = ctx.Value(daemon.CtxKey(vmnet.Name)).(bool)
// network is currently limited to macOS.
if util.MacOS() && reachableIPAddress {
if err := func() error {
socketFile := vmnet.Info().Socket.File()
// ensure the socket file exists
if _, err := os.Stat(socketFile); err != nil {
return fmt.Errorf("vmnet socket file not found: %w", err)
}
l.Networks = append(l.Networks, limaconfig.Network{
Socket: socketFile,
Interface: limautil.NetInterface,
Metric: limautil.NetMetric,
})
return nil
}(); err != nil {
reachableIPAddress = false
logrus.Warn(fmt.Errorf("error setting up reachable IP address: %w", err))
}
}
}
// disable ports 80 and 443 when k8s is enabled and there is a reachable IP address
// to prevent ingress (traefik) from occupying relevant host ports.
if reachableIPAddress && conf.Kubernetes.Enabled && !ingressDisabled(conf.Kubernetes.K3sArgs) {
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestIP: net.ParseIP("0.0.0.0"),
GuestPort: 80,
GuestIPMustBeZero: true,
Ignore: true,
Proto: limaconfig.TCP,
},
limaconfig.PortForward{
GuestIP: net.ParseIP("0.0.0.0"),
GuestPort: 443,
GuestIPMustBeZero: true,
Ignore: true,
Proto: limaconfig.TCP,
},
)
}
// disable port forwarding for Incus when there is a reachable IP address for consistent behaviour
if reachableIPAddress && conf.Runtime == incus.Name {
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestIP: net.ParseIP("0.0.0.0"),
GuestIPMustBeZero: true,
GuestPortRange: [2]int{1, 65535},
HostPortRange: [2]int{1, 65535},
Ignore: true,
Proto: limaconfig.TCP,
},
limaconfig.PortForward{
GuestIP: net.ParseIP("127.0.0.1"),
GuestPortRange: [2]int{1, 65535},
HostPortRange: [2]int{1, 65535},
Ignore: true,
Proto: limaconfig.TCP,
},
)
}
}
}
// ports and sockets
{
// docker socket
if conf.Runtime == docker.Name {
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestSocket: "/var/run/docker.sock",
HostSocket: docker.HostSocketFile(),
Proto: limaconfig.TCP,
})
if config.CurrentProfile().ShortName == "default" {
// for backward compatibility, will be removed in future releases
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestSocket: "/var/run/docker.sock",
HostSocket: docker.LegacyDefaultHostSocketFile(),
Proto: limaconfig.TCP,
})
}
}
// containerd socket
if conf.Runtime == containerd.Name {
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestSocket: "/var/run/containerd.sock",
HostSocket: containerd.HostSocketFile(),
Proto: limaconfig.TCP,
})
}
if conf.Runtime == incus.Name {
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestSocket: "/var/lib/incus/unix.socket",
HostSocket: incus.HostSocketFile(),
Proto: limaconfig.TCP,
})
}
// handle port forwarding to allow listening on 0.0.0.0
// bind 0.0.0.0
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestIPMustBeZero: true,
GuestIP: net.ParseIP("0.0.0.0"),
GuestPortRange: [2]int{1, 65535},
HostIP: net.ParseIP("0.0.0.0"),
HostPortRange: [2]int{1, 65535},
Proto: limaconfig.TCP,
},
)
// bind 127.0.0.1
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestIP: net.ParseIP("127.0.0.1"),
GuestPortRange: [2]int{1, 65535},
HostIP: net.ParseIP("127.0.0.1"),
HostPortRange: [2]int{1, 65535},
Proto: limaconfig.TCP,
},
)
// bind all host addresses when network address is not enabled
if !conf.Network.Address && conf.Network.HostAddresses {
for _, ip := range util.HostIPAddresses() {
l.PortForwards = append(l.PortForwards,
limaconfig.PortForward{
GuestIP: ip,
GuestPortRange: [2]int{1, 65535},
HostIP: ip,
HostPortRange: [2]int{1, 65535},
Proto: limaconfig.TCP,
},
)
}
}
}
switch strings.ToLower(conf.MountType) {
case "ssh", "sshfs", "reversessh", "reverse-ssh", "reversesshfs", limaconfig.REVSSHFS:
l.MountType = limaconfig.REVSSHFS
default:
if l.VMType == limaconfig.VZ {
l.MountType = limaconfig.VIRTIOFS
} else { // qemu
l.MountType = limaconfig.NINEP
}
}
l.Provision = append(l.Provision, limaconfig.Provision{
Mode: limaconfig.ProvisionModeSystem,
Script: "mount -a",
})
// trim mounted drive to recover disk space
if conf.Runtime != incus.Name {
l.Provision = append(l.Provision, limaconfig.Provision{
Mode: limaconfig.ProvisionModeSystem,
Script: `readlink /usr/sbin/fstrim || fstrim -a`,
})
}
if len(conf.Mounts) == 0 {
l.Mounts = append(l.Mounts,
limaconfig.Mount{Location: "~", Writable: true},
limaconfig.Mount{Location: filepath.Join("/tmp", config.CurrentProfile().ID), Writable: true},
)
} else {
// overlapping mounts are problematic in Lima https://github.com/lima-vm/lima/issues/302
if err = checkOverlappingMounts(conf.Mounts); err != nil {
err = fmt.Errorf("overlapping mounts not supported: %w", err)
return
}
l.Mounts = append(l.Mounts, limaconfig.Mount{Location: config.CacheDir(), Writable: false})
cacheOverlapFound := false
for _, m := range conf.Mounts {
var location, mountPoint string
location, err = util.CleanPath(m.Location)
if err != nil {
return
}
mountPoint, err = util.CleanPath(m.MountPoint)
if err != nil {
return
}
mount := limaconfig.Mount{Location: location, MountPoint: mountPoint, Writable: m.Writable}
l.Mounts = append(l.Mounts, mount)
// check if cache directory has been mounted by other mounts, and remove cache directory from mounts
if strings.HasPrefix(config.CacheDir(), location) && !cacheOverlapFound {
l.Mounts = l.Mounts[1:]
cacheOverlapFound = true
}
}
}
// provision scripts
for _, script := range conf.Provision {
l.Provision = append(l.Provision, limaconfig.Provision{
Mode: script.Mode,
Script: script.Script,
})
}
return
}
type Arch = environment.Arch
func checkOverlappingMounts(mounts []config.Mount) error {
for i := 0; i < len(mounts)-1; i++ {
for j := i + 1; j < len(mounts); j++ {
a, err := util.CleanPath(mounts[i].Location)
if err != nil {
return err
}
b, err := util.CleanPath(mounts[j].Location)
if err != nil {
return err
}
if strings.HasPrefix(a, b) || strings.HasPrefix(b, a) {
return fmt.Errorf("'%s' overlaps '%s'", a, b)
}
}
}
return nil
}
// disableHas checks if the provided feature is indeed found in the disable configuration slice.
func ingressDisabled(disableFlags []string) bool {
disabled := func(s string) bool { return s == "traefik" || s == "ingress" }
for i, f := range disableFlags {
if f == "--disable" {
if len(disableFlags)-1 <= i {
return false
}
if disabled(disableFlags[i+1]) {
return true
}
continue
}
str := strings.SplitN(f, "=", 2)
if len(str) < 2 || str[0] != "--disable" {
continue
}
if disabled(str[1]) {
return true
}
}
return false
}