Skip to content

Commit 447b100

Browse files
committed
add FillConfig() function to the driver interface
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent a570232 commit 447b100

File tree

6 files changed

+66
-25
lines changed

6 files changed

+66
-25
lines changed

pkg/driver/driver.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ type Driver interface {
8383
Configure(inst *limatype.Instance, sshLocalPort int) *ConfiguredDriver
8484

8585
AcceptConfig(cfg *limatype.LimaYAML, filepath string) error
86+
FillConfig(cfg *limatype.LimaYAML, filePath string) error
8687
}
8788

8889
type ConfiguredDriver struct {

pkg/driver/external/client/methods.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,7 @@ func (d *DriverClient) Configure(inst *store.Instance) *driver.ConfiguredDriver
310310
func (d *DriverClient) AcceptConfig(cfg *limatype.LimaYAML, filepath string) error {
311311
return errors.New("AcceptConfig not implemented in DriverClient")
312312
}
313+
314+
func (d *DriverClient) FillConfig(cfg *limatype.LimaYAML, filepath string) error {
315+
return errors.New("AcceptConfig not implemented in DriverClient")
316+
}

pkg/driver/qemu/qemu_driver.go

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"os/exec"
1717
"path/filepath"
1818
"runtime"
19+
"slices"
1920
"strconv"
2021
"strings"
2122
"text/template"
@@ -81,15 +82,40 @@ func (l *LimaQemuDriver) Validate() error {
8182
return err
8283
}
8384
}
85+
if err := l.validateMountType(); err != nil {
86+
return err
87+
}
88+
89+
return nil
90+
}
8491

85-
if *l.Instance.Config.MountType == limatype.VIRTIOFS && runtime.GOOS != "linux" {
92+
// Helper method for mount type validation
93+
func (l *LimaQemuDriver) validateMountType() error {
94+
if l.Instance == nil || l.Instance.Config == nil {
95+
return fmt.Errorf("instance configuration is not set")
96+
}
97+
98+
cfg := l.Instance.Config
99+
100+
if cfg.MountType != nil && *cfg.MountType == limatype.VIRTIOFS && runtime.GOOS != "linux" {
86101
return fmt.Errorf("field `mountType` must be %q or %q for QEMU driver on non-Linux, got %q",
87-
limatype.REVSSHFS, limatype.NINEP, *l.Instance.Config.MountType)
102+
limatype.REVSSHFS, limatype.NINEP, *cfg.MountType)
103+
}
104+
if slices.Contains(cfg.MountTypesUnsupported, *cfg.MountType) {
105+
return fmt.Errorf("mount type %q is explicitly unsupported", *cfg.MountType)
88106
}
107+
if runtime.GOOS == "windows" && cfg.MountType != nil && *cfg.MountType == limatype.NINEP {
108+
return fmt.Errorf("mount type %q is not supported on Windows", limatype.NINEP)
109+
}
110+
89111
return nil
90112
}
91113

92-
func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) error {
114+
func (l *LimaQemuDriver) FillConfig(cfg *limatype.LimaYAML, filePath string) error {
115+
if cfg.VMType == nil {
116+
cfg.VMType = ptr.Of(limatype.QEMU)
117+
}
118+
93119
instDir := filepath.Dir(filePath)
94120

95121
if cfg.Video.VNC.Display == nil || *cfg.Video.VNC.Display == "" {
@@ -117,20 +143,32 @@ func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) e
117143
}
118144
}
119145

120-
if _, ok := mountTypesUnsupported[*cfg.MountType]; ok {
121-
// We cannot return an error here, but Validate() will return it.
122-
logrus.Warnf("Unsupported mount type: %q", *cfg.MountType)
123-
}
124-
125146
for i := range cfg.Mounts {
126147
mount := &cfg.Mounts[i]
127148
if mount.Virtiofs.QueueSize == nil && *cfg.MountType == limatype.VIRTIOFS {
128149
cfg.Mounts[i].Virtiofs.QueueSize = ptr.Of(limayaml.DefaultVirtiofsQueueSize)
129150
}
130151
}
131152

153+
if _, ok := mountTypesUnsupported[*cfg.MountType]; ok {
154+
return fmt.Errorf("mount type %q is explicitly unsupported", *cfg.MountType)
155+
}
156+
157+
return nil
158+
}
159+
160+
func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) error {
161+
if l.Instance == nil {
162+
l.Instance = &limatype.Instance{}
163+
}
164+
l.Instance.Config = cfg
165+
166+
if err := l.Validate(); err != nil {
167+
return fmt.Errorf("config not supported by the QEMU driver: %w", err)
168+
}
169+
132170
if runtime.GOOS == "darwin" {
133-
if cfg.Arch != nil && !limayaml.IsNativeArch(*cfg.Arch) {
171+
if cfg.Arch != nil && limayaml.IsNativeArch(*cfg.Arch) {
134172
logrus.Debugf("ResolveVMType: resolved VMType %q (non-native arch=%q is specified in []*LimaYAML{o,y,d})", "qemu", *cfg.Arch)
135173
return nil
136174
}
@@ -160,15 +198,6 @@ func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) e
160198
}
161199
}
162200

163-
if l.Instance == nil {
164-
l.Instance = &limatype.Instance{}
165-
}
166-
l.Instance.Config = cfg
167-
168-
if err := l.Validate(); err != nil {
169-
return fmt.Errorf("config not supported by the QEMU driver: %w", err)
170-
}
171-
172201
return nil
173202
}
174203

pkg/driver/vz/vz_driver_darwin.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ func (l *LimaVzDriver) Configure(inst *limatype.Instance, sshLocalPort int) *dri
115115
Driver: l,
116116
}
117117
}
118-
119-
func (l *LimaVzDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) error {
118+
func (l *LimaVzDriver) FillConfig(cfg *limatype.LimaYAML, filePath string) error {
120119
if cfg.VMType == nil {
121120
cfg.VMType = ptr.Of(limatype.VZ)
122121
}
@@ -125,6 +124,10 @@ func (l *LimaVzDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) err
125124
cfg.MountType = ptr.Of(limatype.VIRTIOFS)
126125
}
127126

127+
return nil
128+
}
129+
130+
func (l *LimaVzDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) error {
128131
if dir, basename := filepath.Split(filePath); dir != "" && basename == filenames.LimaYAML && limayaml.IsExistingInstanceDir(dir) {
129132
vzIdentifier := filepath.Join(dir, filenames.VzIdentifier) // since Lima v0.14
130133
if _, err := os.Lstat(vzIdentifier); !errors.Is(err, os.ErrNotExist) {

pkg/limayaml/load.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"github.com/sirupsen/logrus"
1414

1515
"github.com/lima-vm/lima/v2/pkg/limatype"
16-
"github.com/lima-vm/lima/v2/pkg/ptr"
1716
"github.com/lima-vm/lima/v2/pkg/registry"
1817
"github.com/lima-vm/lima/v2/pkg/store/dirnames"
1918
"github.com/lima-vm/lima/v2/pkg/store/filenames"
@@ -73,15 +72,14 @@ func load(b []byte, filePath string, warn bool) (*limatype.LimaYAML, error) {
7372

7473
FillDefault(&y, &d, &o, filePath, warn)
7574

76-
if err := ValidateVMType(&y, filePath); err != nil {
77-
logrus.WithError(err).Warnf("Failed to accept config for %q", filePath)
75+
if err := ResolveVMType(&y, filePath); err != nil {
7876
return nil, fmt.Errorf("failed to accept config for %q: %w", filePath, err)
7977
}
8078

8179
return &y, nil
8280
}
8381

84-
func ValidateVMType(y *limatype.LimaYAML, filePath string) error {
82+
func ResolveVMType(y *limatype.LimaYAML, filePath string) error {
8583
if y.VMType != nil && *y.VMType != "" {
8684
vmType := *y.VMType
8785
_, intDriver, exists := registry.Get(vmType)
@@ -95,6 +93,9 @@ func ValidateVMType(y *limatype.LimaYAML, filePath string) error {
9593
if err := intDriver.AcceptConfig(y, filePath); err != nil {
9694
return fmt.Errorf("vmType %q is not compatible with the configuration: %w", vmType, err)
9795
}
96+
if err := intDriver.FillConfig(y, filePath); err != nil {
97+
return fmt.Errorf("unable to fill config for vmType %q: %w", vmType, err)
98+
}
9899
logrus.Debugf("ResolveVMType: using explicitly specified VMType %q", vmType)
99100
return nil
100101
}
@@ -113,7 +114,9 @@ func ValidateVMType(y *limatype.LimaYAML, filePath string) error {
113114
_, intDriver, _ := registry.Get(vmType)
114115
if err := intDriver.AcceptConfig(y, filePath); err == nil {
115116
logrus.Debugf("ResolveVMType: resolved VMType %q", vmType)
116-
y.VMType = ptr.Of(vmType)
117+
if err := intDriver.FillConfig(y, filePath); err != nil {
118+
return fmt.Errorf("unable to fill config for VMType %q: %w", vmType, err)
119+
}
117120
return nil
118121
}
119122
}

pkg/registry/registry_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (m *mockDriver) GuestAgentConn(_ context.Context) (net.Conn, string, error)
4646
func (m *mockDriver) Info() driver.Info { return driver.Info{DriverName: m.Name} }
4747
func (m *mockDriver) Configure(_ *limatype.Instance, _ int) *driver.ConfiguredDriver { return nil }
4848
func (m *mockDriver) AcceptConfig(_ *limatype.LimaYAML, _ string) error { return nil }
49+
func (m *mockDriver) FillConfig(_ *limatype.LimaYAML, _ string) error { return nil }
4950

5051
func TestRegister(t *testing.T) {
5152
BackupRegistry(t)

0 commit comments

Comments
 (0)