@@ -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
145143func 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