-
-
Notifications
You must be signed in to change notification settings - Fork 395
/
yaml.go
547 lines (477 loc) · 16.6 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package lima
import (
"context"
"fmt"
"net"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/abiosoft/colima/daemon"
"github.com/abiosoft/colima/daemon/process/gvproxy"
"github.com/abiosoft/colima/daemon/process/vmnet"
"github.com/abiosoft/colima/config"
"github.com/abiosoft/colima/embedded"
"github.com/abiosoft/colima/environment"
"github.com/abiosoft/colima/environment/container/docker"
"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 Config, err error) {
l.Arch = environment.Arch(conf.Arch).Value()
// VM type is qemu except in few scenarios
l.VMType = QEMU
sameArchitecture := environment.HostArch() == l.Arch
// when vz is chosen and OS version supports it
if util.MacOS13OrNewer() && conf.VMType == VZ && sameArchitecture {
l.VMType = VZ
// Rosetta is only available on M1
if conf.VZRosetta && util.MacOS13OrNewerOnM1() {
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 conf.CPUType != "" && conf.CPUType != "host" {
l.CPUType = map[environment.Arch]string{
l.Arch: conf.CPUType,
}
}
l.Images = append(l.Images,
File{Arch: environment.AARCH64, Location: "https://github.com/abiosoft/alpine-lima/releases/download/colima-v0.5.5/alpine-lima-clm-3.18.0-aarch64.iso", Digest: "sha512:84c93e8aaa09446618bf87daa993e260da69b50e95670aed5df6671b2cff9464810752cbf70f6ee5ddf9d3e1c91d98104b3c573cc024c5f0687ad3f4d2e93ebc"},
File{Arch: environment.X8664, Location: "https://github.com/abiosoft/alpine-lima/releases/download/colima-v0.5.5/alpine-lima-clm-3.18.0-x86_64.iso", Digest: "sha512:f761b807fe9ba345968df72c07f8c5abcae0c4a44976fe5595c0ff748ef693841221a70e663986c700b027cea32b7cac24d5490d4c721593c39f2b8840c362a2"},
)
if conf.CPU > 0 {
l.CPUs = &conf.CPU
}
if conf.Memory > 0 {
l.Memory = fmt.Sprintf("%dGiB", conf.Memory)
}
if conf.Disk > 0 {
l.Disk = fmt.Sprintf("%dGiB", conf.Disk)
}
l.SSH = SSH{LocalPort: 0, LoadDotSSHPubKeys: false, ForwardAgent: conf.ForwardAgent}
l.Containerd = 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"
}
if len(l.DNS) == 0 {
gvProxyEnabled, _ := ctx.Value(daemon.CtxKey(gvproxy.Name)).(bool)
if gvProxyEnabled {
l.DNS = append(l.DNS, net.ParseIP(gvproxy.GatewayIP))
l.HostResolver.Enabled = false
}
reachableIPAddress, _ := ctx.Value(daemon.CtxKey(vmnet.Name)).(bool)
if reachableIPAddress {
if gvProxyEnabled {
l.DNS = append(l.DNS, net.ParseIP(vmnet.NetGateway))
}
}
}
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, Provision{
Mode: ProvisionModeSystem,
Script: "sysctl -w fs.inotify.max_user_watches=1048576",
})
// add user to docker group
// "sudo", "usermod", "-aG", "docker", user
l.Provision = append(l.Provision, Provision{
Mode: ProvisionModeUser,
Script: "sudo usermod -aG docker $USER",
})
// allow env vars propagation for services
l.Provision = append(l.Provision, Provision{
Mode: ProvisionModeSystem,
Script: `grep -q "^rc_env_allow" /etc/rc.conf || echo 'rc_env_allow="*"' >> /etc/rc.conf`,
})
}
// network setup
{
reachableIPAddress := true
if conf.Network.Address {
if l.VMType == VZ {
l.Networks = append(l.Networks, Network{
VZNAT: true,
Interface: vmnet.NetInterface,
})
} 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, Network{
Socket: socketFile,
Interface: vmnet.NetInterface,
})
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,
PortForward{
GuestIP: net.ParseIP("0.0.0.0"),
GuestPort: 80,
GuestIPMustBeZero: true,
Ignore: true,
Proto: TCP,
},
PortForward{
GuestIP: net.ParseIP("0.0.0.0"),
GuestPort: 443,
GuestIPMustBeZero: true,
Ignore: true,
Proto: TCP,
},
)
}
}
// gvproxy is cross-platform but not needed on Linux as slirp is only erratic on macOS.
gvProxyEnabled, _ := ctx.Value(daemon.CtxKey(gvproxy.Name)).(bool)
if gvProxyEnabled && util.MacOS() {
var values struct {
Vmnet struct {
Enabled bool
Interface string
}
GVProxy struct {
Enabled bool
MacAddress string
IPAddress net.IP
Gateway net.IP
}
}
if reachableIPAddress {
values.Vmnet.Enabled = true
values.Vmnet.Interface = vmnet.NetInterface
}
gvProxyEnabled, _ := ctx.Value(daemon.CtxKey(gvproxy.Name)).(bool)
if gvProxyEnabled {
values.GVProxy.Enabled = true
values.GVProxy.MacAddress = strings.ToUpper(gvproxy.MacAddress())
values.GVProxy.IPAddress = net.ParseIP(gvproxy.DeviceIP)
values.GVProxy.Gateway = net.ParseIP(gvproxy.GatewayIP)
if err := func() error {
tpl, err := embedded.ReadString("network/ifaces.sh")
if err != nil {
return err
}
script, err := util.ParseTemplate(tpl, values)
if err != nil {
return fmt.Errorf("error parsing template for network script: %w", err)
}
l.Provision = append(l.Provision, Provision{
Mode: ProvisionModeSystem,
Script: string(script),
})
return nil
}(); err != nil {
logrus.Warn(fmt.Errorf("error setting up gvproxy network: %w", err))
}
}
}
}
// port forwarding
if conf.Layer {
port := util.RandomAvailablePort()
// set port for future retrieval
l.Env[limautil.LayerEnvVar] = strconv.Itoa(port)
// forward port
l.PortForwards = append(l.PortForwards,
PortForward{
GuestPort: 23,
HostPort: port,
})
}
// ports and sockets
{
// docker socket
if conf.Runtime == docker.Name {
l.PortForwards = append(l.PortForwards,
PortForward{
GuestSocket: "/var/run/docker.sock",
HostSocket: docker.HostSocketFile(),
Proto: TCP,
})
if config.CurrentProfile().ShortName == "default" {
// for backward compatibility, will be removed in future releases
l.PortForwards = append(l.PortForwards,
PortForward{
GuestSocket: "/var/run/docker.sock",
HostSocket: docker.LegacyDefaultHostSocketFile(),
Proto: TCP,
})
}
}
// handle port forwarding to allow listening on 0.0.0.0
// bind 0.0.0.0
l.PortForwards = append(l.PortForwards,
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: TCP,
},
)
// bind 127.0.0.1
l.PortForwards = append(l.PortForwards,
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: TCP,
},
)
}
switch strings.ToLower(conf.MountType) {
case "ssh", "sshfs", "reversessh", "reverse-ssh", "reversesshfs", REVSSHFS:
l.MountType = REVSSHFS
default:
if l.VMType == VZ {
l.MountType = VIRTIOFS
} else { // qemu
l.MountType = NINEP
}
}
l.Provision = append(l.Provision, Provision{
Mode: ProvisionModeSystem,
Script: "mkmntdirs && mount -a",
})
// trim mounted drive to recover disk space
l.Provision = append(l.Provision, Provision{
Mode: ProvisionModeSystem,
Script: `readlink /sbin/fstrim || fstrim -a`,
})
// workaround for slow virtiofs https://github.com/drud/ddev/issues/4466#issuecomment-1361261185
// TODO: remove when fixed upstream
if l.MountType == VIRTIOFS {
l.Provision = append(l.Provision, Provision{
Mode: ProvisionModeSystem,
Script: `stat /sys/class/block/vda/queue/write_cache && echo 'write through' > /sys/class/block/vda/queue/write_cache`,
})
}
if len(conf.Mounts) == 0 {
l.Mounts = append(l.Mounts,
Mount{Location: "~", Writable: true},
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, 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 := 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, Provision{
Mode: script.Mode,
Script: script.Script,
})
}
return
}
type Arch = environment.Arch
// Config is lima config. Code copied from lima and modified.
type Config struct {
VMType VMType `yaml:"vmType,omitempty" json:"vmType,omitempty"`
Arch Arch `yaml:"arch,omitempty"`
Images []File `yaml:"images"`
CPUs *int `yaml:"cpus,omitempty"`
Memory string `yaml:"memory,omitempty"`
Disk string `yaml:"disk,omitempty"`
Mounts []Mount `yaml:"mounts,omitempty"`
MountType MountType `yaml:"mountType,omitempty" json:"mountType,omitempty"`
SSH SSH `yaml:"ssh,omitempty"`
Containerd Containerd `yaml:"containerd"`
Env map[string]string `yaml:"env,omitempty"`
DNS []net.IP `yaml:"dns"`
Firmware Firmware `yaml:"firmware"`
HostResolver HostResolver `yaml:"hostResolver"`
PortForwards []PortForward `yaml:"portForwards,omitempty"`
Networks []Network `yaml:"networks,omitempty"`
Provision []Provision `yaml:"provision,omitempty" json:"provision,omitempty"`
CPUType map[Arch]string `yaml:"cpuType,omitempty" json:"cpuType,omitempty"`
Rosetta Rosetta `yaml:"rosetta,omitempty" json:"rosetta,omitempty"`
}
type File struct {
Location string `yaml:"location"` // REQUIRED
Arch Arch `yaml:"arch,omitempty"`
Digest string `yaml:"digest,omitempty"`
}
type Mount struct {
Location string `yaml:"location"` // REQUIRED
MountPoint string `yaml:"mountPoint,omitempty"`
Writable bool `yaml:"writable"`
NineP NineP `yaml:"9p,omitempty" json:"9p,omitempty"`
}
type SSH struct {
LocalPort int `yaml:"localPort"`
LoadDotSSHPubKeys bool `yaml:"loadDotSSHPubKeys"`
ForwardAgent bool `yaml:"forwardAgent"` // default: false
}
type Containerd struct {
System bool `yaml:"system"` // default: false
User bool `yaml:"user"` // default: true
}
type Firmware struct {
// LegacyBIOS disables UEFI if set.
// LegacyBIOS is ignored for aarch64.
LegacyBIOS bool `yaml:"legacyBIOS"`
}
type (
Proto = string
MountType = string
VMType = string
)
const (
TCP Proto = "tcp"
REVSSHFS MountType = "reverse-sshfs"
NINEP MountType = "9p"
VIRTIOFS MountType = "virtiofs"
QEMU VMType = "qemu"
VZ VMType = "vz"
)
type PortForward struct {
GuestIPMustBeZero bool `yaml:"guestIPMustBeZero,omitempty" json:"guestIPMustBeZero,omitempty"`
GuestIP net.IP `yaml:"guestIP,omitempty" json:"guestIP,omitempty"`
GuestPort int `yaml:"guestPort,omitempty" json:"guestPort,omitempty"`
GuestPortRange [2]int `yaml:"guestPortRange,omitempty" json:"guestPortRange,omitempty"`
GuestSocket string `yaml:"guestSocket,omitempty" json:"guestSocket,omitempty"`
HostIP net.IP `yaml:"hostIP,omitempty" json:"hostIP,omitempty"`
HostPort int `yaml:"hostPort,omitempty" json:"hostPort,omitempty"`
HostPortRange [2]int `yaml:"hostPortRange,omitempty" json:"hostPortRange,omitempty"`
HostSocket string `yaml:"hostSocket,omitempty" json:"hostSocket,omitempty"`
Proto Proto `yaml:"proto,omitempty" json:"proto,omitempty"`
Ignore bool `yaml:"ignore,omitempty" json:"ignore,omitempty"`
}
type HostResolver struct {
Enabled bool `yaml:"enabled" json:"enabled"`
IPv6 bool `yaml:"ipv6,omitempty" json:"ipv6,omitempty"`
Hosts map[string]string `yaml:"hosts,omitempty" json:"hosts,omitempty"`
}
type Network struct {
// `Lima`, `Socket`, and `VNL` are mutually exclusive; exactly one is required
Lima string `yaml:"lima,omitempty" json:"lima,omitempty"`
// Socket is a QEMU-compatible socket
Socket string `yaml:"socket,omitempty" json:"socket,omitempty"`
// VZNAT uses VZNATNetworkDeviceAttachment. Needs VZ. No root privilege is required.
VZNAT bool `yaml:"vzNAT,omitempty" json:"vzNAT,omitempty"`
// VNLDeprecated is a Virtual Network Locator (https://github.com/rd235/vdeplug4/commit/089984200f447abb0e825eb45548b781ba1ebccd).
// On macOS, only VDE2-compatible form (optionally with vde:// prefix) is supported.
// VNLDeprecated is deprecated. Use Socket.
VNLDeprecated string `yaml:"vnl,omitempty" json:"vnl,omitempty"`
SwitchPortDeprecated uint16 `yaml:"switchPort,omitempty" json:"switchPort,omitempty"` // VDE Switch port, not TCP/UDP port (only used by VDE networking)
MACAddress string `yaml:"macAddress,omitempty" json:"macAddress,omitempty"`
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
}
type ProvisionMode = string
const (
ProvisionModeSystem ProvisionMode = "system"
ProvisionModeUser ProvisionMode = "user"
)
type Provision struct {
Mode ProvisionMode `yaml:"mode" json:"mode"` // default: "system"
Script string `yaml:"script" json:"script"`
}
type NineP struct {
SecurityModel string `yaml:"securityModel,omitempty" json:"securityModel,omitempty"`
ProtocolVersion string `yaml:"protocolVersion,omitempty" json:"protocolVersion,omitempty"`
Msize string `yaml:"msize,omitempty" json:"msize,omitempty"`
Cache string `yaml:"cache,omitempty" json:"cache,omitempty"`
}
type Rosetta struct {
Enabled bool `yaml:"enabled" json:"enabled"`
BinFmt bool `yaml:"binfmt" json:"binfmt"`
}
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
}