Skip to content

Feature: Registry for drivers and expose driver info in CLI #3692

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions cmd/limactl/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/lima-vm/lima/pkg/limatmpl"
"github.com/lima-vm/lima/pkg/limayaml"
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
"github.com/lima-vm/lima/pkg/registry"
"github.com/lima-vm/lima/pkg/store"
"github.com/lima-vm/lima/pkg/store/filenames"
"github.com/lima-vm/lima/pkg/templatestore"
Expand All @@ -32,6 +33,7 @@ func registerCreateFlags(cmd *cobra.Command, commentPrefix string) {
flags := cmd.Flags()
flags.String("name", "", commentPrefix+"Override the instance name")
flags.Bool("list-templates", false, commentPrefix+"List available templates and exit")
flags.Bool("list-drivers", false, commentPrefix+"List available drivers and exit")
editflags.RegisterCreate(cmd, commentPrefix)
}

Expand Down Expand Up @@ -393,6 +395,14 @@ func createStartActionCommon(cmd *cobra.Command, _ []string) (exit bool, err err
_, _ = fmt.Fprintln(w, f.Name)
}
return true, nil
} else if listDrivers, err := cmd.Flags().GetBool("list-drivers"); err != nil {
return true, err
} else if listDrivers {
w := cmd.OutOrStdout()
for k := range registry.List() {
_, _ = fmt.Fprintln(w, k)
}
return true, nil
}
return false, nil
}
Expand Down
22 changes: 0 additions & 22 deletions pkg/driverutil/driverutil.go

This file was deleted.

24 changes: 22 additions & 2 deletions pkg/limainfo/limainfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/sirupsen/logrus"

"github.com/lima-vm/lima/pkg/driverutil"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/registry"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/templatestore"
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
Expand All @@ -23,9 +23,14 @@ type LimaInfo struct {
DefaultTemplate *limayaml.LimaYAML `json:"defaultTemplate"`
LimaHome string `json:"limaHome"`
VMTypes []string `json:"vmTypes"` // since Lima v0.14.2
VMTypesEx map[string]DriverExt `json:"vmTypesEx"` // since Lima v2.0.0
GuestAgents map[limayaml.Arch]GuestAgent `json:"guestAgents"` // since Lima v1.1.0
}

type DriverExt struct {
Location string `json:"location,omitempty"` // since Lima v2.0.0
}

type GuestAgent struct {
Location string `json:"location"` // since Lima v1.1.0
}
Expand All @@ -42,10 +47,25 @@ func New() (*LimaInfo, error) {
if err != nil {
return nil, err
}

reg := registry.List()
if len(reg) == 0 {
return nil, errors.New("no VM types found; ensure that the drivers are properly registered")
}
vmTypesEx := make(map[string]DriverExt)
var vmTypes []string
for name, path := range reg {
vmTypesEx[name] = DriverExt{
Location: path,
}
vmTypes = append(vmTypes, name)
}

info := &LimaInfo{
Version: version.Version,
DefaultTemplate: y,
VMTypes: driverutil.Drivers(),
VMTypes: vmTypes,
VMTypesEx: vmTypesEx,
GuestAgents: make(map[limayaml.Arch]GuestAgent),
}
info.Templates, err = templatestore.Templates()
Expand Down
183 changes: 183 additions & 0 deletions pkg/registry/registry.go
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add unit tests for this newly created package?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I add it under #3554 and another PR because it will require a mock diver and Register() function(see this) in order to test functions like Get(), registerDriverFile() etc?

Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
// SPDX-FileCopyrightText: Copyright The Lima Authors
// SPDX-License-Identifier: Apache-2.0

package registry

import (
"context"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"

"github.com/lima-vm/lima/pkg/driver"
"github.com/lima-vm/lima/pkg/driver/vz"
"github.com/lima-vm/lima/pkg/driver/wsl2"
"github.com/lima-vm/lima/pkg/limayaml"
"github.com/lima-vm/lima/pkg/usrlocalsharelima"
)

const (
Internal = "internal"
)

type ExternalDriver struct {
Name string
InstanceName string
Command *exec.Cmd
SocketPath string
Path string
Ctx context.Context
Logger *logrus.Logger
CancelFunc context.CancelFunc
}

var (
internalDrivers = make(map[string]driver.Driver)
ExternalDrivers = make(map[string]*ExternalDriver)
)

func List() map[string]string {
if err := discoverDrivers(); err != nil {
logrus.Warnf("Error discovering drivers: %v", err)
}

vmTypes := make(map[string]string)
for name := range internalDrivers {
vmTypes[name] = Internal
}
for name, d := range ExternalDrivers {
vmTypes[name] = d.Path
}

// This block will be removed while merging the internal driver pull request(#3693).
if len(vmTypes) == 0 {
vmTypes[limayaml.QEMU] = Internal
if vz.Enabled {
vmTypes[limayaml.VZ] = Internal
}
if wsl2.Enabled {
vmTypes[limayaml.WSL2] = Internal
}
}

return vmTypes
}

func Get(name string) (*ExternalDriver, driver.Driver, bool) {
if err := discoverDrivers(); err != nil {
logrus.Warnf("Error discovering drivers: %v", err)
}

internalDriver, exists := internalDrivers[name]
if !exists {
externalDriver, exists := ExternalDrivers[name]
if exists {
return externalDriver, nil, exists
}
}
return nil, internalDriver, exists
}

func registerExternalDriver(name, path string) {
if _, exists := ExternalDrivers[name]; exists {
return
}

if _, exists := internalDrivers[name]; exists {
logrus.Debugf("Driver %q is already registered as an internal driver, skipping external registration", name)
return
}

ExternalDrivers[name] = &ExternalDriver{
Name: name,
Path: path,
Logger: logrus.New(),
}
}

func discoverDrivers() error {
prefix, err := usrlocalsharelima.Prefix()
if err != nil {
return err
}
stdDriverDir := filepath.Join(prefix, "libexec", "lima")

logrus.Debugf("Discovering external drivers in %s", stdDriverDir)
if _, err := os.Stat(stdDriverDir); err == nil {
if err := discoverDriversInDir(stdDriverDir); err != nil {
logrus.Warnf("Error discovering external drivers in %q: %v", stdDriverDir, err)
}
}

if driverPaths := os.Getenv("LIMA_DRIVERS_PATH"); driverPaths != "" {
paths := filepath.SplitList(driverPaths)
for _, path := range paths {
if path == "" {
continue
}

info, err := os.Stat(path)
if err != nil {
logrus.Warnf("Error accessing external driver path %q: %v", path, err)
continue
}

if info.IsDir() {
if err := discoverDriversInDir(path); err != nil {
logrus.Warnf("Error discovering external drivers in %q: %v", path, err)
}
} else if isExecutable(info.Mode()) {
registerDriverFile(path)
}
}
}

return nil
}

func discoverDriversInDir(dir string) error {
entries, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("failed to read driver directory %q: %w", dir, err)
}

for _, entry := range entries {
if entry.IsDir() {
continue
}

info, err := entry.Info()
if err != nil {
logrus.Warnf("Failed to get info for %q: %v", entry.Name(), err)
continue
}

if !isExecutable(info.Mode()) {
continue
}

driverPath := filepath.Join(dir, entry.Name())
registerDriverFile(driverPath)
}

return nil
}

func registerDriverFile(path string) {
base := filepath.Base(path)
if !strings.HasPrefix(base, "lima-driver-") {
return
}

name := strings.TrimPrefix(base, "lima-driver-")
Comment on lines +172 to +176
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we can simplify (but I'm not sure without any tests):

Suggested change
if !strings.HasPrefix(base, "lima-driver-") {
return
}
name := strings.TrimPrefix(base, "lima-driver-")
name, ok := strings.CutPrefix(base, "lima-driver-")
if !ok {
return
}


registerExternalDriver(name, path)
}

func isExecutable(mode os.FileMode) bool {
return mode&0o111 != 0
}
9 changes: 9 additions & 0 deletions pkg/usrlocalsharelima/usrlocalsharelima.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,12 @@ func chooseGABinary(candidates []string) (string, error) {
return entries[0], nil
}
}

// Prefix returns the <PREFIX> directory, which is two levels above the lima share directory.
func Prefix() (string, error) {
dir, err := Dir()
if err != nil {
return "", err
}
return filepath.Dir(filepath.Dir(dir)), nil
}