Skip to content

Commit 97299fb

Browse files
committed
driver(internal): change lima to support internal drivers
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent 171bd33 commit 97299fb

File tree

10 files changed

+85
-40
lines changed

10 files changed

+85
-40
lines changed

pkg/driver/driver.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,8 @@ type Plugin interface {
7676
// Name returns the name of the driver
7777
Name() string
7878

79-
// Enabled returns whether this driver is available on the current platform.
80-
// Also checks whether this driver can handle the given config
81-
Enabled() bool
82-
8379
// NewDriver returns a new driver instance. Only to be used to embed internal drivers
84-
NewDriver(ctx context.Context, inst *store.Instance, sshLocalPort int) Driver
80+
NewDriver(inst *store.Instance, sshLocalPort int) Driver
8581
}
8682

8783
// Driver interface is used by hostagent for managing vm.

pkg/driverutil/driverutil.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44
package driverutil
55

66
import (
7-
"github.com/lima-vm/lima/pkg/limayaml"
8-
"github.com/lima-vm/lima/pkg/vz"
9-
"github.com/lima-vm/lima/pkg/wsl2"
7+
"github.com/lima-vm/lima/pkg/registry"
108
)
119

12-
// Drivers returns the available drivers.
13-
func Drivers() []string {
14-
drivers := []string{limayaml.QEMU}
15-
if vz.Enabled {
16-
drivers = append(drivers, limayaml.VZ)
17-
}
18-
if wsl2.Enabled {
19-
drivers = append(drivers, limayaml.WSL2)
10+
// AvailableDrivers returns a list of available driver names
11+
func AvailableDrivers() []string {
12+
var available []string
13+
14+
for _, name := range registry.DefaultRegistry.List() {
15+
driver, _ := registry.DefaultRegistry.Get(name)
16+
if err := driver.Validate(); err == nil {
17+
return available
18+
}
19+
available = append(available, name)
2020
}
21-
return drivers
21+
22+
return available
2223
}

pkg/driverutil/instance.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44
package driverutil
55

66
import (
7+
"fmt"
8+
79
"github.com/lima-vm/lima/pkg/driver"
8-
"github.com/lima-vm/lima/pkg/limayaml"
9-
"github.com/lima-vm/lima/pkg/qemu"
10+
"github.com/lima-vm/lima/pkg/registry"
1011
"github.com/lima-vm/lima/pkg/store"
11-
"github.com/lima-vm/lima/pkg/vz"
12-
"github.com/lima-vm/lima/pkg/wsl2"
1312
)
1413

15-
func CreateTargetDriverInstance(inst *store.Instance, SSHLocalPort int) driver.Driver {
16-
limaDriver := base.Instance.Config.VMType
17-
if *limaDriver == limayaml.VZ {
18-
return vz.New(base)
14+
// CreateTargetDriverInstance creates the appropriate driver for an instance
15+
func CreateTargetDriverInstance(inst *store.Instance, sshLocalPort int) (driver.Driver, error) {
16+
limaDriver := inst.Config.VMType
17+
driver, exists := registry.DefaultRegistry.Get(string(*limaDriver))
18+
if !exists {
19+
return nil, fmt.Errorf("unknown or unsupported VM type: %s", *limaDriver)
1920
}
20-
if *limaDriver == limayaml.WSL2 {
21-
return wsl2.New(base)
21+
22+
if err := driver.Validate(); err != nil {
23+
return nil, fmt.Errorf("driver validation failed: %w", err)
2224
}
23-
return qemu.New(base)
25+
26+
return driver.NewDriver(inst, sshLocalPort), nil
2427
}

pkg/hostagent/hostagent.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ func New(instName string, stdout io.Writer, signalCh chan os.Signal, opts ...Opt
131131
}
132132
}
133133

134-
limaDriver := driverutil.CreateTargetDriverInstance(inst, sshLocalPort)
134+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, sshLocalPort)
135+
if err != nil {
136+
return nil, fmt.Errorf("failed to create driver instance: %w", err)
137+
}
138+
135139
var vSockPort int
136140
var virtioPort string
137141

pkg/instance/create.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ func Create(ctx context.Context, instName string, instConfig []byte, saveBrokenY
7575
return nil, err
7676
}
7777

78-
limaDriver := driverutil.CreateTargetDriverInstance(inst, 0)
78+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, 0)
79+
if err != nil {
80+
return nil, fmt.Errorf("failed to create driver instance: %w", err)
81+
}
7982

8083
if err := limaDriver.Register(ctx); err != nil {
8184
return nil, err

pkg/instance/delete.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ func Delete(ctx context.Context, inst *store.Instance, force bool) error {
3636
}
3737

3838
func unregister(ctx context.Context, inst *store.Instance) error {
39-
limaDriver := driverutil.CreateTargetDriverInstance(inst, 0)
39+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, 0)
40+
if err != nil {
41+
return fmt.Errorf("failed to create driver instance: %w", err)
42+
}
4043

4144
return limaDriver.Unregister(ctx)
4245
}

pkg/instance/start.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
9191
return nil, err
9292
}
9393
}
94-
limaDriver := driverutil.CreateTargetDriverInstance(inst, 0)
94+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, 0)
95+
if err != nil {
96+
return nil, fmt.Errorf("failed to create driver instance: %w", err)
97+
}
9598

9699
if err := limaDriver.Validate(); err != nil {
97100
return nil, err
@@ -103,7 +106,7 @@ func Prepare(ctx context.Context, inst *store.Instance) (*Prepared, error) {
103106

104107
// Check if the instance has been created (the base disk already exists)
105108
baseDisk := filepath.Join(inst.Dir, filenames.BaseDisk)
106-
_, err := os.Stat(baseDisk)
109+
_, err = os.Stat(baseDisk)
107110
created := err == nil
108111

109112
if err := limaDriver.CreateDisk(ctx); err != nil {

pkg/limainfo/limainfo.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func New() (*LimaInfo, error) {
4444
info := &LimaInfo{
4545
Version: version.Version,
4646
DefaultTemplate: y,
47-
VMTypes: driverutil.Drivers(),
47+
VMTypes: driverutil.AvailableDrivers(),
4848
GuestAgents: make(map[limayaml.Arch]GuestAgent),
4949
}
5050
info.Templates, err = templatestore.Templates()

pkg/registry/registry.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sync"
88

99
"github.com/lima-vm/lima/pkg/driver"
10+
"github.com/lima-vm/lima/pkg/store"
1011
)
1112

1213
type Registry struct {
@@ -20,7 +21,7 @@ func NewRegistry() *Registry {
2021
}
2122
}
2223

23-
func (r *Registry) Register(driver driver.Driver) {
24+
func (r *Registry) Register(driver driver.Driver, inst *store.Instance, sshLocalPort int) {
2425
r.mu.Lock()
2526
defer r.mu.Unlock()
2627

@@ -32,6 +33,17 @@ func (r *Registry) Register(driver driver.Driver) {
3233
r.drivers[name] = driver
3334
}
3435

36+
func (r *Registry) List() []string {
37+
r.mu.RLock()
38+
defer r.mu.RUnlock()
39+
40+
var names []string
41+
for name := range r.drivers {
42+
names = append(names, name)
43+
}
44+
return names
45+
}
46+
3547
func (r *Registry) Get(name string) (driver.Driver, bool) {
3648
r.mu.RLock()
3749
defer r.mu.RUnlock()
@@ -44,6 +56,11 @@ var DefaultRegistry *Registry
4456

4557
func Register(driver driver.Driver) {
4658
if DefaultRegistry != nil {
47-
DefaultRegistry.Register(driver)
59+
name := driver.Name()
60+
if _, exists := DefaultRegistry.drivers[name]; exists {
61+
return
62+
}
63+
64+
DefaultRegistry.drivers[name] = driver
4865
}
4966
}

pkg/snapshot/snapshot.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,42 @@ package snapshot
55

66
import (
77
"context"
8+
"fmt"
89

910
"github.com/lima-vm/lima/pkg/driverutil"
1011
"github.com/lima-vm/lima/pkg/store"
1112
)
1213

1314
func Del(ctx context.Context, inst *store.Instance, tag string) error {
14-
limaDriver := driverutil.CreateTargetDriverInstance(inst, 0)
15+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, 0)
16+
if err != nil {
17+
return fmt.Errorf("failed to create driver instance: %w", err)
18+
}
19+
1520
return limaDriver.DeleteSnapshot(ctx, tag)
1621
}
1722

1823
func Save(ctx context.Context, inst *store.Instance, tag string) error {
19-
limaDriver := driverutil.CreateTargetDriverInstance(inst, 0)
24+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, 0)
25+
if err != nil {
26+
return fmt.Errorf("failed to create driver instance: %w", err)
27+
}
2028
return limaDriver.CreateSnapshot(ctx, tag)
2129
}
2230

2331
func Load(ctx context.Context, inst *store.Instance, tag string) error {
24-
limaDriver := driverutil.CreateTargetDriverInstance(inst, 0)
32+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, 0)
33+
if err != nil {
34+
return fmt.Errorf("failed to create driver instance: %w", err)
35+
}
2536
return limaDriver.ApplySnapshot(ctx, tag)
2637
}
2738

2839
func List(ctx context.Context, inst *store.Instance) (string, error) {
29-
limaDriver := driverutil.CreateTargetDriverInstance(inst, 0)
40+
limaDriver, err := driverutil.CreateTargetDriverInstance(inst, 0)
41+
if err != nil {
42+
return "", fmt.Errorf("failed to create driver instance: %w", err)
43+
}
44+
3045
return limaDriver.ListSnapshots(ctx)
3146
}

0 commit comments

Comments
 (0)