@@ -11,7 +11,6 @@ import (
11
11
"errors"
12
12
"fmt"
13
13
"net"
14
- "os"
15
14
"path/filepath"
16
15
"runtime"
17
16
"time"
@@ -22,7 +21,6 @@ import (
22
21
23
22
"github.com/lima-vm/lima/v2/pkg/driver"
24
23
"github.com/lima-vm/lima/v2/pkg/limatype"
25
- "github.com/lima-vm/lima/v2/pkg/limatype/filenames"
26
24
"github.com/lima-vm/lima/v2/pkg/limayaml"
27
25
"github.com/lima-vm/lima/v2/pkg/osutil"
28
26
"github.com/lima-vm/lima/v2/pkg/ptr"
@@ -113,7 +111,7 @@ func (l *LimaVzDriver) Configure(inst *limatype.Instance) *driver.ConfiguredDriv
113
111
}
114
112
}
115
113
116
- func (l * LimaVzDriver ) FillConfig (cfg * limatype.LimaYAML , _ string ) error {
114
+ func (l * LimaVzDriver ) FillConfig (ctx context. Context , cfg * limatype.LimaYAML , _ string ) error {
117
115
if cfg .VMType == nil {
118
116
cfg .VMType = ptr .Of (limatype .VZ )
119
117
}
@@ -139,7 +137,7 @@ func (l *LimaVzDriver) FillConfig(cfg *limatype.LimaYAML, _ string) error {
139
137
cfg .VMOpts .VZ .Rosetta .BinFmt = ptr .Of (false )
140
138
}
141
139
142
- return nil
140
+ return validateConfig ( ctx , cfg )
143
141
}
144
142
145
143
func isEmpty (r limatype.Rosetta ) bool {
@@ -171,48 +169,14 @@ func (l *LimaVzDriver) BootScripts() (map[string][]byte, error) {
171
169
return scripts , nil
172
170
}
173
171
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 )
213
174
}
214
175
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
+ }
216
180
macOSProductVersion , err := osutil .ProductVersion ()
217
181
if err != nil {
218
182
return err
@@ -223,67 +187,86 @@ func (l *LimaVzDriver) Validate(_ context.Context) error {
223
187
if runtime .GOARCH == "amd64" && macOSProductVersion .LessThan (* semver .New ("15.5.0" )) {
224
188
logrus .Warnf ("vmType %s: On Intel Mac, macOS 15.5 or later is required to run Linux 6.12 or later. " +
225
189
"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 )
227
191
}
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 )
230
194
}
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 )
233
197
}
234
- for _ , f := range l . Instance . Config .Firmware .Images {
198
+ for _ , f := range cfg .Firmware .Images {
235
199
switch f .VMType {
236
200
case "" , limatype .VZ :
237
- if f .Arch == * l . Instance . Config .Arch {
201
+ if f .Arch == * cfg .Arch {
238
202
return errors .New ("`firmware.images` configuration is not supported for VZ driver" )
239
203
}
240
204
}
241
205
}
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 )
244
208
}
245
209
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 )
248
212
}
249
213
250
- for i , image := range l . Instance . Config .Images {
214
+ for i , image := range cfg .Images {
251
215
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 )
253
217
}
254
218
}
255
219
256
- for i , mount := range l . Instance . Config .Mounts {
220
+ for i , mount := range cfg .Mounts {
257
221
if unknown := reflectutil .UnknownNonEmptyFields (mount , "Location" ,
258
222
"MountPoint" ,
259
223
"Writable" ,
260
224
"SSHFS" ,
261
225
"NineP" ,
262
226
); 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 )
264
228
}
265
229
}
266
230
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" ,
269
233
"Lima" ,
270
234
"Socket" ,
271
235
"MACAddress" ,
272
236
"Metric" ,
273
237
"Interface" ,
274
238
); 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
+ }
276
259
}
277
260
}
278
261
279
- switch audioDevice := * l . Instance . Config .Audio .Device ; audioDevice {
262
+ switch audioDevice := * cfg .Audio .Device ; audioDevice {
280
263
case "" :
281
264
case "vz" , "default" , "none" :
282
265
default :
283
266
logrus .Warnf ("field `audio.device` must be \" vz\" , \" default\" , or \" none\" for VZ driver, got %q" , audioDevice )
284
267
}
285
268
286
- switch videoDisplay := * l . Instance . Config .Video .Display ; videoDisplay {
269
+ switch videoDisplay := * cfg .Video .Display ; videoDisplay {
287
270
case "vz" , "default" , "none" :
288
271
default :
289
272
logrus .Warnf ("field `video.display` must be \" vz\" , \" default\" , or \" none\" for VZ driver , got %q" , videoDisplay )
0 commit comments