-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathlima_config_applier.go
286 lines (248 loc) · 9.41 KB
/
lima_config_applier.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package config
import (
"fmt"
"strings"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/spf13/afero"
"github.com/xorcare/pointer"
"golang.org/x/exp/slices"
"gopkg.in/yaml.v3"
"github.com/runfinch/finch/pkg/command"
"github.com/runfinch/finch/pkg/system"
)
const (
sociVersion = "0.4.0"
sociInstallationProvisioningScriptHeader = "# soci installation and configuring"
sociFileNameFormat = "soci-snapshotter-%s-linux-%s.tar.gz"
sociDownloadURLFormat = "https://github.com/awslabs/soci-snapshotter/releases/download/v%s/%s"
sociInstallationScriptFormat = `%s
if [ ! -f /usr/local/bin/soci ]; then
# download soci
set -e
curl --retry 2 --retry-max-time 120 -OL "%s"
# move to usr/local/bin
tar -C /usr/local/bin -xvf %s soci soci-snapshotter-grpc
fi
# changing containerd config
export config=etc/containerd/config.toml
echo " [proxy_plugins.soci]
type = \"snapshot\"
address = \"/run/soci-snapshotter-grpc/soci-snapshotter-grpc.sock\" " >> $config
sudo systemctl restart containerd.service
sudo soci-snapshotter-grpc &> ~/soci-snapshotter-logs &
`
userModeEmulationProvisioningScriptHeader = "# cross-arch tools"
)
// LimaConfigApplierSystemDeps contains the system dependencies for LimaConfigApplier.
//
//go:generate mockgen -copyright_file=../../copyright_header -destination=../mocks/pkg_config_lima_config_applier_system_deps.go -package=mocks -mock_names LimaConfigApplierSystemDeps=LimaConfigApplierSystemDeps . LimaConfigApplierSystemDeps
type LimaConfigApplierSystemDeps interface {
system.RuntimeArchGetter
system.RuntimeOSGetter
}
type limaConfigApplier struct {
cfg *Finch
cmdCreator command.Creator
fs afero.Fs
limaConfigPath string
systemDeps LimaConfigApplierSystemDeps
}
var _ LimaConfigApplier = (*limaConfigApplier)(nil)
// NewLimaApplier creates a new LimaConfigApplier that
// applies lima configuration changes by writing to the lima config file on the disk.
func NewLimaApplier(
cfg *Finch,
cmdCreator command.Creator,
fs afero.Fs,
limaConfigPath string,
systemDeps LimaConfigApplierSystemDeps,
) LimaConfigApplier {
return &limaConfigApplier{
cfg: cfg,
cmdCreator: cmdCreator,
fs: fs,
limaConfigPath: limaConfigPath,
systemDeps: systemDeps,
}
}
// Apply writes Lima-specific config values from Finch's config to the supplied lima config file path.
// Apply will create a lima config file at the path if it does not exist.
func (lca *limaConfigApplier) Apply(isInit bool) error {
if cfgExists, err := afero.Exists(lca.fs, lca.limaConfigPath); err != nil {
return fmt.Errorf("error checking if file at path %s exists, error: %w", lca.limaConfigPath, err)
} else if !cfgExists {
if err := afero.WriteFile(lca.fs, lca.limaConfigPath, []byte(""), 0o644); err != nil {
return fmt.Errorf("failed to create the an empty lima config file: %w", err)
}
}
b, err := afero.ReadFile(lca.fs, lca.limaConfigPath)
if err != nil {
return fmt.Errorf("failed to load the lima config file: %w", err)
}
var limaCfg limayaml.LimaYAML
if err := yaml.Unmarshal(b, &limaCfg); err != nil {
return fmt.Errorf("failed to unmarshal the lima config file: %w", err)
}
limaCfg.CPUs = lca.cfg.CPUs
limaCfg.Memory = lca.cfg.Memory
limaCfg.Mounts = []limayaml.Mount{}
for _, ad := range lca.cfg.AdditionalDirectories {
limaCfg.Mounts = append(limaCfg.Mounts, limayaml.Mount{
Location: *ad.Path, Writable: pointer.Bool(true),
})
}
if limaCfg.Rosetta.Enabled == nil {
limaCfg.Rosetta.Enabled = pointer.Bool(false)
limaCfg.Rosetta.BinFmt = pointer.Bool(false)
}
if isInit {
cfgAfterInit, err := lca.applyInit(&limaCfg)
if err != nil {
return fmt.Errorf("failed to apply init-only config values: %w", err)
}
limaCfg = *cfgAfterInit
}
supportedSnapshotters := []string{"overlayfs", "soci"}
snapshotters := make(map[string][2]bool)
for i, snapshotter := range lca.cfg.Snapshotters {
if !slices.Contains(supportedSnapshotters, snapshotter) {
return fmt.Errorf("invalid snapshotter config value: %s", snapshotter)
}
isDefaultSnapshotter := false
if i == 0 {
isDefaultSnapshotter = true
}
isEnabled := true
snapshotters[snapshotter] = [2]bool{isEnabled, isDefaultSnapshotter}
}
toggleSnaphotters(&limaCfg, snapshotters)
limaCfgBytes, err := yaml.Marshal(limaCfg)
if err != nil {
return fmt.Errorf("failed to marshal the lima config file: %w", err)
}
if err := afero.WriteFile(lca.fs, lca.limaConfigPath, limaCfgBytes, 0o644); err != nil {
return fmt.Errorf("failed to write to the lima config file: %w", err)
}
return nil
}
// applyInit changes settings that will only apply to the VM after a new init.
func (lca *limaConfigApplier) applyInit(limaCfg *limayaml.LimaYAML) (*limayaml.LimaYAML, error) {
hasSupport, hasSupportErr := SupportsVirtualizationFramework(lca.cmdCreator)
if *lca.cfg.Rosetta &&
lca.systemDeps.OS() == "darwin" &&
lca.systemDeps.Arch() == "arm64" {
if hasSupportErr != nil {
return nil, fmt.Errorf("failed to check for virtualization framework support: %w", hasSupportErr)
}
if !hasSupport {
return nil, fmt.Errorf(`system does not have virtualization framework support, change vmType to "qemu"`)
}
limaCfg.Rosetta.Enabled = pointer.Bool(true)
limaCfg.Rosetta.BinFmt = pointer.Bool(true)
limaCfg.VMType = pointer.String("vz")
limaCfg.MountType = pointer.String("virtiofs")
toggleUserModeEmulationInstallationScript(limaCfg, false)
} else {
if *lca.cfg.VMType == "vz" {
if hasSupportErr != nil {
return nil, fmt.Errorf("failed to check for virtualization framework support: %w", hasSupportErr)
}
if !hasSupport {
return nil, fmt.Errorf(`system does not have virtualization framework support, change vmType to "qemu"`)
}
limaCfg.MountType = pointer.String("virtiofs")
} else if *lca.cfg.VMType == "qemu" {
limaCfg.MountType = pointer.String("reverse-sshfs")
}
limaCfg.Rosetta.Enabled = pointer.Bool(false)
limaCfg.Rosetta.BinFmt = pointer.Bool(false)
limaCfg.VMType = lca.cfg.VMType
toggleUserModeEmulationInstallationScript(limaCfg, true)
}
return limaCfg, nil
}
func toggleUserModeEmulationInstallationScript(limaCfg *limayaml.LimaYAML, enabled bool) {
idx, hasScript := hasUserModeEmulationInstallationScript(limaCfg)
if !hasScript && enabled {
limaCfg.Provision = append(limaCfg.Provision, limayaml.Provision{
Mode: "system",
Script: fmt.Sprintf(`%s
#!/bin/bash
qemu_pkgs=""
if [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-aarch64"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-arm"
elif [ ! -f /usr/bin/qemu-aarch64-static ]; then
qemu_pkgs="$qemu_pkgs qemu-user-static-x86"
fi
if [[ $qemu_pkgs ]]; then
dnf install -y --setopt=install_weak_deps=False ${qemu_pkgs}
fi
`, userModeEmulationProvisioningScriptHeader),
})
} else if hasScript && !enabled {
if len(limaCfg.Provision) > 0 {
limaCfg.Provision = append(limaCfg.Provision[:idx], limaCfg.Provision[idx+1:]...)
}
}
}
func hasUserModeEmulationInstallationScript(limaCfg *limayaml.LimaYAML) (int, bool) {
hasCrossArchToolInstallationScript := false
var scriptIdx int
for idx, prov := range limaCfg.Provision {
trimmed := strings.Trim(prov.Script, " ")
if !hasCrossArchToolInstallationScript && strings.HasPrefix(trimmed, userModeEmulationProvisioningScriptHeader) {
hasCrossArchToolInstallationScript = true
scriptIdx = idx
}
}
return scriptIdx, hasCrossArchToolInstallationScript
}
// toggles snapshotters and sets default snapshotter.
func toggleSnaphotters(limaCfg *limayaml.LimaYAML, snapshotters map[string][2]bool) {
toggleOverlayFs(limaCfg, snapshotters["overlayfs"][1])
toggleSoci(limaCfg, snapshotters["soci"][0], snapshotters["soci"][1], sociVersion)
}
// sets overlayfs as the default snapshotter.
func toggleOverlayFs(limaCfg *limayaml.LimaYAML, isDefault bool) {
if isDefault {
limaCfg.Env = map[string]string{"CONTAINERD_SNAPSHOTTER": ""}
}
}
func toggleSoci(limaCfg *limayaml.LimaYAML, enabled bool, isDefault bool, sociVersion string) {
idx, hasScript := findSociInstallationScript(limaCfg)
sociFileName := fmt.Sprintf(sociFileNameFormat, sociVersion, system.NewStdLib().Arch())
sociDownloadURL := fmt.Sprintf(sociDownloadURLFormat, sociVersion, sociFileName)
sociInstallationScript := fmt.Sprintf(sociInstallationScriptFormat, sociInstallationProvisioningScriptHeader, sociDownloadURL, sociFileName)
if !hasScript && enabled {
limaCfg.Provision = append(limaCfg.Provision, limayaml.Provision{
Mode: "system",
Script: sociInstallationScript,
})
} else if hasScript && !enabled {
if len(limaCfg.Provision) > 0 {
limaCfg.Provision = append(limaCfg.Provision[:idx], limaCfg.Provision[idx+1:]...)
}
}
if isDefault {
limaCfg.Env = map[string]string{"CONTAINERD_SNAPSHOTTER": "soci"}
} else {
limaCfg.Env = map[string]string{"CONTAINERD_SNAPSHOTTER": ""}
}
}
func findSociInstallationScript(limaCfg *limayaml.LimaYAML) (int, bool) {
hasSociInstallationScript := false
var scriptIdx int
for idx, prov := range limaCfg.Provision {
trimmed := strings.Trim(prov.Script, " ")
if !hasSociInstallationScript && strings.HasPrefix(trimmed, sociInstallationProvisioningScriptHeader) {
hasSociInstallationScript = true
scriptIdx = idx
break
}
}
return scriptIdx, hasSociInstallationScript
}