Skip to content

Commit 3329b91

Browse files
authored
Merge pull request #3923 from AkihiroSuda/fix-3919
driver: remove AcceptConfig
2 parents ff5e337 + 49cc964 commit 3329b91

File tree

8 files changed

+121
-192
lines changed

8 files changed

+121
-192
lines changed

pkg/driver/driver.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ type Driver interface {
7979
Info() Info
8080

8181
// Configure sets the configuration for the instance.
82+
// TODO: merge Configure and FillConfig?
83+
// Or come up with a better name to clarify the difference.
8284
Configure(inst *limatype.Instance) *ConfiguredDriver
8385

84-
AcceptConfig(cfg *limatype.LimaYAML, filePath string) error
85-
FillConfig(cfg *limatype.LimaYAML, filePath string) error
86+
// FillConfig fills and validates the configuration for the instance.
87+
// The config is not set to the instance.
88+
FillConfig(ctx context.Context, cfg *limatype.LimaYAML, filePath string) error
8689

8790
SSHAddress(ctx context.Context) (string, error)
8891
}

pkg/driver/external/client/methods.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,7 @@ func (d *DriverClient) Delete(ctx context.Context) error {
115115
return nil
116116
}
117117

118-
func (d *DriverClient) AcceptConfig(_ *limatype.LimaYAML, _ string) error {
119-
return errors.New("pre-configured driver action not implemented in client driver")
120-
}
121-
122-
func (d *DriverClient) FillConfig(_ *limatype.LimaYAML, _ string) error {
118+
func (d *DriverClient) FillConfig(_ context.Context, _ *limatype.LimaYAML, _ string) error {
123119
return errors.New("pre-configured driver action not implemented in client driver")
124120
}
125121

pkg/driver/external/server/server.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func Serve(ctx context.Context, driver driver.Driver) {
5757
inspectStatus := flag.Bool("inspect-status", false, "Inspect status of the driver")
5858
flag.Parse()
5959
if *preConfiguredDriverAction {
60-
handlePreConfiguredDriverAction(driver)
60+
handlePreConfiguredDriverAction(ctx, driver)
6161
return
6262
}
6363
if *inspectStatus {
@@ -188,7 +188,7 @@ func handleInspectStatus(driver driver.Driver) {
188188
}
189189
}
190190

191-
func handlePreConfiguredDriverAction(driver driver.Driver) {
191+
func handlePreConfiguredDriverAction(ctx context.Context, driver driver.Driver) {
192192
decoder := json.NewDecoder(os.Stdin)
193193
encoder := json.NewEncoder(os.Stdout)
194194

@@ -198,11 +198,7 @@ func handlePreConfiguredDriverAction(driver driver.Driver) {
198198
}
199199

200200
config := &payload.Config
201-
if err := driver.AcceptConfig(config, payload.FilePath); err != nil {
202-
fmt.Fprintf(os.Stderr, "Failed to accept config: %v", err)
203-
}
204-
205-
if err := driver.FillConfig(config, payload.FilePath); err != nil {
201+
if err := driver.FillConfig(ctx, config, payload.FilePath); err != nil {
206202
fmt.Fprintf(os.Stderr, "Failed to fill config: %v", err)
207203
}
208204

pkg/driver/qemu/qemu_driver.go

Lines changed: 27 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -77,26 +77,37 @@ func (l *LimaQemuDriver) Configure(inst *limatype.Instance) *driver.ConfiguredDr
7777
}
7878

7979
func (l *LimaQemuDriver) Validate(ctx context.Context) error {
80+
return validateConfig(ctx, l.Instance.Config)
81+
}
82+
83+
func validateConfig(ctx context.Context, cfg *limatype.LimaYAML) error {
84+
if cfg == nil {
85+
return errors.New("configuration is nil")
86+
}
8087
if runtime.GOOS == "darwin" {
81-
if err := l.checkBinarySignature(ctx); err != nil {
88+
var vmArch string
89+
if cfg.Arch != nil {
90+
vmArch = *cfg.Arch
91+
}
92+
if err := checkBinarySignature(ctx, vmArch); err != nil {
8293
return err
8394
}
8495
}
85-
if err := l.validateMountType(); err != nil {
96+
if err := validateMountType(cfg); err != nil {
8697
return err
8798
}
8899

100+
if cfg.VMOpts.QEMU.MinimumVersion != nil {
101+
if _, err := semver.NewVersion(*cfg.VMOpts.QEMU.MinimumVersion); err != nil {
102+
return fmt.Errorf("field `vmOpts.qemu.minimumVersion` must be a semvar value, got %q: %w", *cfg.VMOpts.QEMU.MinimumVersion, err)
103+
}
104+
}
105+
89106
return nil
90107
}
91108

92109
// Helper method for mount type validation.
93-
func (l *LimaQemuDriver) validateMountType() error {
94-
if l.Instance == nil || l.Instance.Config == nil {
95-
return errors.New("instance configuration is not set")
96-
}
97-
98-
cfg := l.Instance.Config
99-
110+
func validateMountType(cfg *limatype.LimaYAML) error {
100111
if cfg.MountType != nil && *cfg.MountType == limatype.VIRTIOFS && runtime.GOOS != "linux" {
101112
return fmt.Errorf("field `mountType` must be %q or %q for QEMU driver on non-Linux, got %q",
102113
limatype.REVSSHFS, limatype.NINEP, *cfg.MountType)
@@ -111,7 +122,7 @@ func (l *LimaQemuDriver) validateMountType() error {
111122
return nil
112123
}
113124

114-
func (l *LimaQemuDriver) FillConfig(cfg *limatype.LimaYAML, filePath string) error {
125+
func (l *LimaQemuDriver) FillConfig(ctx context.Context, cfg *limatype.LimaYAML, filePath string) error {
115126
if cfg.VMType == nil {
116127
cfg.VMType = ptr.Of(limatype.QEMU)
117128
}
@@ -174,57 +185,7 @@ func (l *LimaQemuDriver) FillConfig(cfg *limatype.LimaYAML, filePath string) err
174185
return fmt.Errorf("mount type %q is explicitly unsupported", *cfg.MountType)
175186
}
176187

177-
return nil
178-
}
179-
180-
func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, _ string) error {
181-
if l.Instance == nil {
182-
l.Instance = &limatype.Instance{}
183-
}
184-
l.Instance.Config = cfg
185-
186-
if err := l.Validate(context.Background()); err != nil {
187-
return fmt.Errorf("config not supported by the QEMU driver: %w", err)
188-
}
189-
190-
if cfg.VMOpts.QEMU.MinimumVersion != nil {
191-
if _, err := semver.NewVersion(*cfg.VMOpts.QEMU.MinimumVersion); err != nil {
192-
return fmt.Errorf("field `vmOpts.qemu.minimumVersion` must be a semvar value, got %q: %w", *cfg.VMOpts.QEMU.MinimumVersion, err)
193-
}
194-
}
195-
196-
if runtime.GOOS == "darwin" {
197-
if cfg.Arch != nil && limayaml.IsNativeArch(*cfg.Arch) {
198-
logrus.Debugf("ResolveVMType: resolved VMType %q (non-native arch=%q is specified in []*LimaYAML{o,y,d})", "qemu", *cfg.Arch)
199-
return nil
200-
}
201-
if limayaml.ResolveArch(cfg.Arch) == limatype.X8664 && cfg.Firmware.LegacyBIOS != nil && *cfg.Firmware.LegacyBIOS {
202-
logrus.Debugf("ResolveVMType: resolved VMType %q (firmware.legacyBIOS is specified in []*LimaYAML{o,y,d} on x86_64)", "qemu")
203-
return nil
204-
}
205-
if cfg.MountType != nil && *cfg.MountType == limatype.NINEP {
206-
logrus.Debugf("ResolveVMType: resolved VMType %q (mountType=%q is specified in []*LimaYAML{o,y,d})", "qemu", limatype.NINEP)
207-
return nil
208-
}
209-
if cfg.Audio.Device != nil {
210-
switch *cfg.Audio.Device {
211-
case "", "none", "default", "vz":
212-
// NOP
213-
default:
214-
logrus.Debugf("ResolveVMType: resolved VMType %q (audio.device=%q is specified in []*LimaYAML{o,y,d})", "qemu", *cfg.Audio.Device)
215-
return nil
216-
}
217-
}
218-
if cfg.Video.Display != nil {
219-
display := *cfg.Video.Display
220-
if display != "" && display != "none" && display != "default" && display != "vz" {
221-
logrus.Debugf("ResolveVMType: resolved VMType %q (video.display=%q is specified in []*LimaYAML{o,y,d})", "qemu", display)
222-
return nil
223-
}
224-
}
225-
}
226-
227-
return nil
188+
return validateConfig(ctx, cfg)
228189
}
229190

230191
func (l *LimaQemuDriver) BootScripts() (map[string][]byte, error) {
@@ -414,18 +375,18 @@ func waitFileExists(path string, timeout time.Duration) error {
414375

415376
// Ask the user to sign the qemu binary with the "com.apple.security.hypervisor" if needed.
416377
// Workaround for https://github.com/lima-vm/lima/issues/1742
417-
func (l *LimaQemuDriver) checkBinarySignature(ctx context.Context) error {
378+
func checkBinarySignature(ctx context.Context, vmArch string) error {
418379
macOSProductVersion, err := osutil.ProductVersion()
419380
if err != nil {
420381
return err
421382
}
422383
// The codesign --xml option is only available on macOS Monterey and later
423-
if !macOSProductVersion.LessThan(*semver.New("12.0.0")) && l.Instance.Arch != "" {
424-
qExe, _, err := Exe(l.Instance.Arch)
384+
if !macOSProductVersion.LessThan(*semver.New("12.0.0")) && vmArch != "" {
385+
qExe, _, err := Exe(vmArch)
425386
if err != nil {
426-
return fmt.Errorf("failed to find the QEMU binary for the architecture %q: %w", l.Instance.Arch, err)
387+
return fmt.Errorf("failed to find the QEMU binary for the architecture %q: %w", vmArch, err)
427388
}
428-
if accel := Accel(l.Instance.Arch); accel == "hvf" {
389+
if accel := Accel(vmArch); accel == "hvf" {
429390
entitlementutil.AskToSignIfNotSignedProperly(ctx, qExe)
430391
}
431392
}

pkg/driver/vz/vz_driver_darwin.go

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"errors"
1212
"fmt"
1313
"net"
14-
"os"
1514
"path/filepath"
1615
"runtime"
1716
"time"
@@ -22,7 +21,6 @@ import (
2221

2322
"github.com/lima-vm/lima/v2/pkg/driver"
2423
"github.com/lima-vm/lima/v2/pkg/limatype"
25-
"github.com/lima-vm/lima/v2/pkg/limatype/filenames"
2624
"github.com/lima-vm/lima/v2/pkg/limayaml"
2725
"github.com/lima-vm/lima/v2/pkg/osutil"
2826
"github.com/lima-vm/lima/v2/pkg/ptr"
@@ -113,7 +111,7 @@ func (l *LimaVzDriver) Configure(inst *limatype.Instance) *driver.ConfiguredDriv
113111
}
114112
}
115113

116-
func (l *LimaVzDriver) FillConfig(cfg *limatype.LimaYAML, _ string) error {
114+
func (l *LimaVzDriver) FillConfig(ctx context.Context, cfg *limatype.LimaYAML, _ string) error {
117115
if cfg.VMType == nil {
118116
cfg.VMType = ptr.Of(limatype.VZ)
119117
}
@@ -139,7 +137,7 @@ func (l *LimaVzDriver) FillConfig(cfg *limatype.LimaYAML, _ string) error {
139137
cfg.VMOpts.VZ.Rosetta.BinFmt = ptr.Of(false)
140138
}
141139

142-
return nil
140+
return validateConfig(ctx, cfg)
143141
}
144142

145143
func isEmpty(r limatype.Rosetta) bool {
@@ -171,48 +169,14 @@ func (l *LimaVzDriver) BootScripts() (map[string][]byte, error) {
171169
return scripts, nil
172170
}
173171

174-
func (l *LimaVzDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) error {
175-
if dir, basename := filepath.Split(filePath); dir != "" && basename == filenames.LimaYAML && limayaml.IsExistingInstanceDir(dir) {
176-
vzIdentifier := filepath.Join(dir, filenames.VzIdentifier) // since Lima v0.14
177-
if _, err := os.Lstat(vzIdentifier); !errors.Is(err, os.ErrNotExist) {
178-
logrus.Debugf("ResolveVMType: resolved VMType %q (existing instance, with %q)", "vz", vzIdentifier)
179-
}
180-
}
181-
182-
for i, nw := range cfg.Networks {
183-
field := fmt.Sprintf("networks[%d]", i)
184-
switch {
185-
case nw.Lima != "":
186-
if nw.VZNAT != nil && *nw.VZNAT {
187-
return fmt.Errorf("field `%s.lima` and field `%s.vzNAT` are mutually exclusive", field, field)
188-
}
189-
case nw.Socket != "":
190-
if nw.VZNAT != nil && *nw.VZNAT {
191-
return fmt.Errorf("field `%s.socket` and field `%s.vzNAT` are mutually exclusive", field, field)
192-
}
193-
case nw.VZNAT != nil && *nw.VZNAT:
194-
if nw.Lima != "" {
195-
return fmt.Errorf("field `%s.vzNAT` and field `%s.lima` are mutually exclusive", field, field)
196-
}
197-
if nw.Socket != "" {
198-
return fmt.Errorf("field `%s.vzNAT` and field `%s.socket` are mutually exclusive", field, field)
199-
}
200-
}
201-
}
202-
203-
if l.Instance == nil {
204-
l.Instance = &limatype.Instance{}
205-
}
206-
l.Instance.Config = cfg
207-
208-
if err := l.Validate(context.Background()); err != nil {
209-
return fmt.Errorf("config not supported by the VZ driver: %w", err)
210-
}
211-
212-
return nil
172+
func (l *LimaVzDriver) Validate(ctx context.Context) error {
173+
return validateConfig(ctx, l.Instance.Config)
213174
}
214175

215-
func (l *LimaVzDriver) Validate(_ context.Context) error {
176+
func validateConfig(_ context.Context, cfg *limatype.LimaYAML) error {
177+
if cfg == nil {
178+
return errors.New("configuration is nil")
179+
}
216180
macOSProductVersion, err := osutil.ProductVersion()
217181
if err != nil {
218182
return err
@@ -223,67 +187,86 @@ func (l *LimaVzDriver) Validate(_ context.Context) error {
223187
if runtime.GOARCH == "amd64" && macOSProductVersion.LessThan(*semver.New("15.5.0")) {
224188
logrus.Warnf("vmType %s: On Intel Mac, macOS 15.5 or later is required to run Linux 6.12 or later. "+
225189
"Update macOS, or change vmType to \"qemu\" if the VM does not start up. (https://github.com/lima-vm/lima/issues/3334)",
226-
*l.Instance.Config.VMType)
190+
*cfg.VMType)
227191
}
228-
if l.Instance.Config.MountType != nil && *l.Instance.Config.MountType == limatype.NINEP {
229-
return fmt.Errorf("field `mountType` must be %q or %q for VZ driver , got %q", limatype.REVSSHFS, limatype.VIRTIOFS, *l.Instance.Config.MountType)
192+
if cfg.MountType != nil && *cfg.MountType == limatype.NINEP {
193+
return fmt.Errorf("field `mountType` must be %q or %q for VZ driver , got %q", limatype.REVSSHFS, limatype.VIRTIOFS, *cfg.MountType)
230194
}
231-
if *l.Instance.Config.Firmware.LegacyBIOS {
232-
logrus.Warnf("vmType %s: ignoring `firmware.legacyBIOS`", *l.Instance.Config.VMType)
195+
if *cfg.Firmware.LegacyBIOS {
196+
logrus.Warnf("vmType %s: ignoring `firmware.legacyBIOS`", *cfg.VMType)
233197
}
234-
for _, f := range l.Instance.Config.Firmware.Images {
198+
for _, f := range cfg.Firmware.Images {
235199
switch f.VMType {
236200
case "", limatype.VZ:
237-
if f.Arch == *l.Instance.Config.Arch {
201+
if f.Arch == *cfg.Arch {
238202
return errors.New("`firmware.images` configuration is not supported for VZ driver")
239203
}
240204
}
241205
}
242-
if unknown := reflectutil.UnknownNonEmptyFields(l.Instance.Config, knownYamlProperties...); l.Instance.Config.VMType != nil && len(unknown) > 0 {
243-
logrus.Warnf("vmType %s: ignoring %+v", *l.Instance.Config.VMType, unknown)
206+
if unknown := reflectutil.UnknownNonEmptyFields(cfg, knownYamlProperties...); cfg.VMType != nil && len(unknown) > 0 {
207+
logrus.Warnf("vmType %s: ignoring %+v", *cfg.VMType, unknown)
244208
}
245209

246-
if !limayaml.IsNativeArch(*l.Instance.Config.Arch) {
247-
return fmt.Errorf("unsupported arch: %q", *l.Instance.Config.Arch)
210+
if !limayaml.IsNativeArch(*cfg.Arch) {
211+
return fmt.Errorf("unsupported arch: %q", *cfg.Arch)
248212
}
249213

250-
for i, image := range l.Instance.Config.Images {
214+
for i, image := range cfg.Images {
251215
if unknown := reflectutil.UnknownNonEmptyFields(image, "File", "Kernel", "Initrd"); len(unknown) > 0 {
252-
logrus.Warnf("vmType %s: ignoring images[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
216+
logrus.Warnf("vmType %s: ignoring images[%d]: %+v", *cfg.VMType, i, unknown)
253217
}
254218
}
255219

256-
for i, mount := range l.Instance.Config.Mounts {
220+
for i, mount := range cfg.Mounts {
257221
if unknown := reflectutil.UnknownNonEmptyFields(mount, "Location",
258222
"MountPoint",
259223
"Writable",
260224
"SSHFS",
261225
"NineP",
262226
); len(unknown) > 0 {
263-
logrus.Warnf("vmType %s: ignoring mounts[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
227+
logrus.Warnf("vmType %s: ignoring mounts[%d]: %+v", *cfg.VMType, i, unknown)
264228
}
265229
}
266230

267-
for i, network := range l.Instance.Config.Networks {
268-
if unknown := reflectutil.UnknownNonEmptyFields(network, "VZNAT",
231+
for i, nw := range cfg.Networks {
232+
if unknown := reflectutil.UnknownNonEmptyFields(nw, "VZNAT",
269233
"Lima",
270234
"Socket",
271235
"MACAddress",
272236
"Metric",
273237
"Interface",
274238
); len(unknown) > 0 {
275-
logrus.Warnf("vmType %s: ignoring networks[%d]: %+v", *l.Instance.Config.VMType, i, unknown)
239+
logrus.Warnf("vmType %s: ignoring networks[%d]: %+v", *cfg.VMType, i, unknown)
240+
}
241+
242+
field := fmt.Sprintf("networks[%d]", i)
243+
switch {
244+
case nw.Lima != "":
245+
if nw.VZNAT != nil && *nw.VZNAT {
246+
return fmt.Errorf("field `%s.lima` and field `%s.vzNAT` are mutually exclusive", field, field)
247+
}
248+
case nw.Socket != "":
249+
if nw.VZNAT != nil && *nw.VZNAT {
250+
return fmt.Errorf("field `%s.socket` and field `%s.vzNAT` are mutually exclusive", field, field)
251+
}
252+
case nw.VZNAT != nil && *nw.VZNAT:
253+
if nw.Lima != "" {
254+
return fmt.Errorf("field `%s.vzNAT` and field `%s.lima` are mutually exclusive", field, field)
255+
}
256+
if nw.Socket != "" {
257+
return fmt.Errorf("field `%s.vzNAT` and field `%s.socket` are mutually exclusive", field, field)
258+
}
276259
}
277260
}
278261

279-
switch audioDevice := *l.Instance.Config.Audio.Device; audioDevice {
262+
switch audioDevice := *cfg.Audio.Device; audioDevice {
280263
case "":
281264
case "vz", "default", "none":
282265
default:
283266
logrus.Warnf("field `audio.device` must be \"vz\", \"default\", or \"none\" for VZ driver, got %q", audioDevice)
284267
}
285268

286-
switch videoDisplay := *l.Instance.Config.Video.Display; videoDisplay {
269+
switch videoDisplay := *cfg.Video.Display; videoDisplay {
287270
case "vz", "default", "none":
288271
default:
289272
logrus.Warnf("field `video.display` must be \"vz\", \"default\", or \"none\" for VZ driver , got %q", videoDisplay)

0 commit comments

Comments
 (0)