Skip to content

Commit 8b370b9

Browse files
committed
support for qemu driver
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent 8657586 commit 8b370b9

File tree

4 files changed

+68
-67
lines changed

4 files changed

+68
-67
lines changed

pkg/driver/qemu/qemu_driver.go

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,34 @@ func (l *LimaQemuDriver) Configure(inst *limatype.Instance, sshLocalPort int) *d
7070
l.Instance = inst
7171
l.SSHLocalPort = sshLocalPort
7272

73-
if l.Instance.Config.Video.VNC.Display == nil || *l.Instance.Config.Video.VNC.Display == "" {
74-
l.Instance.Config.Video.VNC.Display = ptr.Of("127.0.0.1:0,to=9")
73+
return &driver.ConfiguredDriver{
74+
Driver: l,
75+
}
76+
}
77+
78+
func (l *LimaQemuDriver) Validate() error {
79+
if runtime.GOOS == "darwin" {
80+
if err := l.checkBinarySignature(); err != nil {
81+
return err
82+
}
83+
}
84+
85+
if *l.Instance.Config.MountType == limatype.VIRTIOFS && runtime.GOOS != "linux" {
86+
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)
88+
}
89+
return nil
90+
}
91+
92+
func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, filePath string) error {
93+
instDir := filepath.Dir(filePath)
94+
95+
if cfg.Video.VNC.Display == nil || *cfg.Video.VNC.Display == "" {
96+
cfg.Video.VNC.Display = ptr.Of("127.0.0.1:0,to=9")
7597
}
7698

7799
mountTypesUnsupported := make(map[string]struct{})
78-
for _, f := range l.Instance.Config.MountTypesUnsupported {
100+
for _, f := range cfg.MountTypesUnsupported {
79101
mountTypesUnsupported[f] = struct{}{}
80102
}
81103

@@ -84,49 +106,29 @@ func (l *LimaQemuDriver) Configure(inst *limatype.Instance, sshLocalPort int) *d
84106
mountTypesUnsupported[limatype.NINEP] = struct{}{}
85107
}
86108

87-
if l.Instance.Config.MountType == nil || *l.Instance.Config.MountType == "" || *l.Instance.Config.MountType == "default" {
88-
l.Instance.Config.MountType = ptr.Of(limatype.NINEP)
109+
if cfg.MountType == nil || *cfg.MountType == "" || *cfg.MountType == "default" {
110+
cfg.MountType = ptr.Of(limatype.NINEP)
89111
if _, ok := mountTypesUnsupported[limatype.NINEP]; ok {
90112
// Use REVSSHFS if the instance does not support 9p
91-
l.Instance.Config.MountType = ptr.Of(limatype.REVSSHFS)
92-
} else if limayaml.IsExistingInstanceDir(l.Instance.Dir) && !versionutil.GreaterEqual(limayaml.ExistingLimaVersion(l.Instance.Dir), "1.0.0") {
113+
cfg.MountType = ptr.Of(limatype.REVSSHFS)
114+
} else if limayaml.IsExistingInstanceDir(instDir) && !versionutil.GreaterEqual(limayaml.ExistingLimaVersion(instDir), "1.0.0") {
93115
// Use REVSSHFS if the instance was created with Lima prior to v1.0
94-
l.Instance.Config.MountType = ptr.Of(limatype.REVSSHFS)
116+
cfg.MountType = ptr.Of(limatype.REVSSHFS)
95117
}
96118
}
97119

98-
if _, ok := mountTypesUnsupported[*l.Instance.Config.MountType]; ok {
120+
if _, ok := mountTypesUnsupported[*cfg.MountType]; ok {
99121
// We cannot return an error here, but Validate() will return it.
100-
logrus.Warnf("Unsupported mount type: %q", *l.Instance.Config.MountType)
122+
logrus.Warnf("Unsupported mount type: %q", *cfg.MountType)
101123
}
102124

103-
for i := range l.Instance.Config.Mounts {
104-
mount := &l.Instance.Config.Mounts[i]
105-
if mount.Virtiofs.QueueSize == nil && *l.Instance.Config.MountType == limatype.VIRTIOFS {
106-
l.Instance.Config.Mounts[i].Virtiofs.QueueSize = ptr.Of(limayaml.DefaultVirtiofsQueueSize)
125+
for i := range cfg.Mounts {
126+
mount := &cfg.Mounts[i]
127+
if mount.Virtiofs.QueueSize == nil && *cfg.MountType == limatype.VIRTIOFS {
128+
cfg.Mounts[i].Virtiofs.QueueSize = ptr.Of(limayaml.DefaultVirtiofsQueueSize)
107129
}
108130
}
109131

110-
return &driver.ConfiguredDriver{
111-
Driver: l,
112-
}
113-
}
114-
115-
func (l *LimaQemuDriver) Validate() error {
116-
if runtime.GOOS == "darwin" {
117-
if err := l.checkBinarySignature(); err != nil {
118-
return err
119-
}
120-
}
121-
122-
if *l.Instance.Config.MountType == limatype.VIRTIOFS && runtime.GOOS != "linux" {
123-
return fmt.Errorf("field `mountType` must be %q or %q for QEMU driver on non-Linux, got %q",
124-
limatype.REVSSHFS, limatype.NINEP, *l.Instance.Config.MountType)
125-
}
126-
return nil
127-
}
128-
129-
func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, filepath string) error {
130132
if runtime.GOOS == "darwin" {
131133
if cfg.Arch != nil && !limayaml.IsNativeArch(*cfg.Arch) {
132134
logrus.Debugf("ResolveVMType: resolved VMType %q (non-native arch=%q is specified in []*LimaYAML{o,y,d})", "qemu", *cfg.Arch)
@@ -162,9 +164,6 @@ func (l *LimaQemuDriver) AcceptConfig(cfg *limatype.LimaYAML, filepath string) e
162164
l.Instance = &limatype.Instance{}
163165
}
164166
l.Instance.Config = cfg
165-
defer func() {
166-
l.Instance.Config = nil
167-
}()
168167

169168
if err := l.Validate(); err != nil {
170169
return fmt.Errorf("config not supported by the QEMU driver: %w", err)
@@ -361,7 +360,7 @@ func (l *LimaQemuDriver) checkBinarySignature() error {
361360
return err
362361
}
363362
// The codesign --xml option is only available on macOS Monterey and later
364-
if !macOSProductVersion.LessThan(*semver.New("12.0.0")) {
363+
if !macOSProductVersion.LessThan(*semver.New("12.0.0")) && l.Instance.Arch != "" {
365364
qExe, _, err := Exe(l.Instance.Arch)
366365
if err != nil {
367366
return fmt.Errorf("failed to find the QEMU binary for the architecture %q: %w", l.Instance.Arch, err)

pkg/limayaml/defaults.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/lima-vm/lima/v2/pkg/networks"
3535
"github.com/lima-vm/lima/v2/pkg/osutil"
3636
"github.com/lima-vm/lima/v2/pkg/ptr"
37+
"github.com/lima-vm/lima/v2/pkg/registry"
3738
"github.com/lima-vm/lima/v2/pkg/store/dirnames"
3839
"github.com/lima-vm/lima/v2/pkg/store/filenames"
3940
"github.com/lima-vm/lima/v2/pkg/version"
@@ -208,6 +209,14 @@ func FillDefault(y, d, o *limatype.LimaYAML, filePath string, warn bool) {
208209
if o.VMType != nil {
209210
y.VMType = o.VMType
210211
}
212+
if y.VMType != nil && *y.VMType != "" && *y.VMType != "default" {
213+
logrus.Debugf("ResolveVMType: VMType %q is explicitly specified in %q", *y.VMType, filePath)
214+
_, _, exists := registry.Get(*y.VMType)
215+
if !exists {
216+
logrus.Warnf("ResolveVMType: VMType %q is not registered", *y.VMType)
217+
}
218+
*y.VMType = limatype.NewVMType(*y.VMType)
219+
}
211220

212221
if y.OS == nil {
213222
y.OS = d.OS

pkg/limayaml/defaults_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,7 @@ func TestFillDefault(t *testing.T) {
3131
logrus.SetLevel(logrus.DebugLevel)
3232
var d, y, o limatype.LimaYAML
3333

34-
defaultVMType, err := ResolveVMType(&y, "")
35-
if err != nil {
36-
t.Fatalf("Failed to resolve default VMType: %v", err)
37-
}
38-
34+
defaultVMType := limatype.QEMU
3935
opts := []cmp.Option{
4036
// Consider nil slices and empty slices to be identical
4137
cmpopts.EquateEmpty(),

pkg/limayaml/load.go

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -71,50 +71,47 @@ func load(b []byte, filePath string, warn bool) (*limatype.LimaYAML, error) {
7171
}
7272

7373
FillDefault(&y, &d, &o, filePath, warn)
74-
vmType, err := CheckExplicitVM(&y, filePath)
75-
if err != nil {
76-
logrus.WithError(err).Warnf("Failed to resolve VMType for %q", filePath)
77-
}
78-
y.VMType = ptr.Of(vmType)
7974

80-
if err := AcceptConfig(&y, filePath); err != nil {
75+
if err := ResolveVMType(&y, filePath); err != nil {
8176
logrus.WithError(err).Warnf("Failed to accept config for %q", filePath)
8277
return nil, fmt.Errorf("failed to accept config for %q: %w", filePath, err)
8378
}
8479

8580
return &y, nil
8681
}
8782

88-
// Check if the VMType is explicitly specified
89-
func CheckExplicitVM(y *limatype.LimaYAML, filePath string) (limatype.VMType, error) {
90-
if y.VMType != nil && *y.VMType != "" && *y.VMType != "default" {
91-
logrus.Debugf("ResolveVMType: VMType %q is explicitly specified in %q", *y.VMType, filePath)
92-
_, _, exists := registry.Get(*y.VMType)
83+
func ResolveVMType(y *limatype.LimaYAML, filePath string) error {
84+
if y.VMType != nil && *y.VMType != "" {
85+
vmType := *y.VMType
86+
_, intDriver, exists := registry.Get(vmType)
9387
if !exists {
94-
logrus.Debugf("ResolveVMType: VMType %q is not registered, using default VMType %q", *y.VMType, limatype.QEMU)
95-
return "", fmt.Errorf("VMType %q is not registered", *y.VMType)
88+
return fmt.Errorf("specified vmType %q is not a registered driver", vmType)
89+
}
90+
if intDriver == nil {
91+
// For now we only support internal drivers.
92+
return fmt.Errorf("specified vmType %q is not an internal driver", vmType)
93+
}
94+
if err := intDriver.AcceptConfig(y, filePath); err != nil {
95+
return fmt.Errorf("vmType %q is not compatible with the configuration: %w", vmType, err)
9696
}
97-
return limatype.NewVMType(*y.VMType), nil
97+
logrus.Debugf("ResolveVMType: using explicitly specified VMType %q", vmType)
98+
return nil
9899
}
99100

100-
return "", nil
101-
}
102-
103-
func AcceptConfig(y *limatype.LimaYAML, filePath string) error {
101+
// If VMType is not specified, we try to resolve it by checking config with all the registered drivers.
104102
candidates := registry.List()
105103
for vmType, location := range candidates {
106-
if location != registry.External && vmType == limatype.VZ {
107-
// For now we only support internal drivers.
104+
// For now we only support internal drivers.
105+
if location == registry.Internal {
108106
_, intDriver, _ := registry.Get(vmType)
109107
if err := intDriver.AcceptConfig(y, filePath); err == nil {
110-
logrus.Debugf("ResolveVMType: resolved VMType %q (from %q)", vmType, location)
108+
logrus.Debugf("ResolveVMType: resolved VMType %q", vmType)
111109
y.VMType = ptr.Of(vmType)
112110
return nil
113-
} else {
114-
logrus.Debugf("ResolveVMType: VMType %q is not accepted by the driver: %v", vmType, err)
115-
return fmt.Errorf("VMType %q is not accepted by the driver: %w", vmType, err)
116111
}
117112
}
113+
118114
}
115+
119116
return fmt.Errorf("no VMType found for %q", filePath)
120117
}

0 commit comments

Comments
 (0)