Skip to content

Commit 50bb714

Browse files
authored
Merge pull request #3694 from unsuman/feature/external-driver-system
Feature: External driver plugin system and update Makefile
2 parents 14dcebd + 7bd7066 commit 50bb714

File tree

18 files changed

+2508
-42
lines changed

18 files changed

+2508
-42
lines changed

Makefile

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,48 @@ endif
243243
# calls the native resolver library and not the simplistic version in the Go library.
244244
ENVS__output/bin/limactl$(exe) = CGO_ENABLED=1 GOOS="$(GOOS)" GOARCH="$(GOARCH)" CC="$(CC)"
245245

246+
LIMACTL_DRIVER_TAGS :=
247+
ifneq (,$(findstring vz,$(ADDITIONAL_DRIVERS)))
248+
LIMACTL_DRIVER_TAGS += external_vz
249+
endif
250+
ifneq (,$(findstring qemu,$(ADDITIONAL_DRIVERS)))
251+
LIMACTL_DRIVER_TAGS += external_qemu
252+
endif
253+
ifneq (,$(findstring wsl2,$(ADDITIONAL_DRIVERS)))
254+
LIMACTL_DRIVER_TAGS += external_wsl2
255+
endif
256+
257+
GO_BUILDTAGS ?=
258+
GO_BUILDTAGS_LIMACTL := $(strip $(GO_BUILDTAGS) $(LIMACTL_DRIVER_TAGS))
259+
246260
_output/bin/limactl$(exe): $(LIMACTL_DEPS) $$(call force_build,$$@)
247-
# If the previous cross-compilation was for GOOS=windows, limactl.exe might still be present.
248261
ifneq ($(GOOS),windows) #
249262
@rm -rf _output/bin/limactl.exe
250263
else
251264
@rm -rf _output/bin/limactl
252265
endif
253-
$(ENVS_$@) $(GO_BUILD) -o $@ ./cmd/limactl
266+
$(ENVS_$@) $(GO_BUILD) -tags '$(GO_BUILDTAGS_LIMACTL)' -o $@ ./cmd/limactl
254267
ifeq ($(GOOS),darwin)
255268
codesign -f -v --entitlements vz.entitlements -s - $@
256269
endif
257270

271+
DRIVER_INSTALL_DIR := _output/libexec/lima
272+
273+
.PHONY: additional-drivers
274+
additional-drivers:
275+
@mkdir -p $(DRIVER_INSTALL_DIR)
276+
@for drv in $(ADDITIONAL_DRIVERS); do \
277+
echo "Building $$drv as external"; \
278+
if [ "$(GOOS)" = "windows" ]; then \
279+
$(GO_BUILD) -o $(DRIVER_INSTALL_DIR)/lima-driver-$$drv.exe ./cmd/lima-driver-$$drv; \
280+
else \
281+
$(GO_BUILD) -o $(DRIVER_INSTALL_DIR)/lima-driver-$$drv ./cmd/lima-driver-$$drv; \
282+
fi; \
283+
if [ "$$drv" = "vz" ] && [ "$(GOOS)" = "darwin" ]; then \
284+
codesign -f -v --entitlements vz.entitlements -s - $(DRIVER_INSTALL_DIR)/lima-driver-vz; \
285+
fi; \
286+
done
287+
258288
LIMA_CMDS = $(sort lima lima$(bat)) # $(sort ...) deduplicates the list
259289
LIMA_DEPS = $(addprefix _output/bin/,$(LIMA_CMDS))
260290
lima: $(LIMA_DEPS)

cmd/lima-driver-qemu/main.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"github.com/lima-vm/lima/pkg/driver/external/server"
8+
"github.com/lima-vm/lima/pkg/driver/qemu"
9+
)
10+
11+
// To be used as an external driver for Lima.
12+
func main() {
13+
server.Serve(qemu.New())
14+
}

cmd/lima-driver-vz/main_darwin.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"github.com/lima-vm/lima/pkg/driver/external/server"
8+
"github.com/lima-vm/lima/pkg/driver/vz"
9+
)
10+
11+
// To be used as an external driver for Lima.
12+
func main() {
13+
server.Serve(vz.New())
14+
}

cmd/lima-driver-wsl2/main_windows.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package main
5+
6+
import (
7+
"github.com/lima-vm/lima/pkg/driver/external/server"
8+
"github.com/lima-vm/lima/pkg/driver/wsl2"
9+
)
10+
11+
// To be used as an external driver for Lima.
12+
func main() {
13+
server.Serve(wsl2.New())
14+
}

cmd/limactl/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/spf13/cobra"
1717

1818
"github.com/lima-vm/lima/pkg/debugutil"
19+
"github.com/lima-vm/lima/pkg/driver/external/server"
1920
"github.com/lima-vm/lima/pkg/fsutil"
2021
"github.com/lima-vm/lima/pkg/osutil"
2122
"github.com/lima-vm/lima/pkg/store/dirnames"
@@ -43,6 +44,8 @@ func main() {
4344
handleExitCoder(err)
4445
logrus.Fatal(err)
4546
}
47+
48+
server.StopAllExternalDrivers()
4649
}
4750

4851
func newApp() *cobra.Command {

pkg/driver/external/client/client.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-FileCopyrightText: Copyright The Lima Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package client
5+
6+
import (
7+
"context"
8+
"net"
9+
10+
"github.com/sirupsen/logrus"
11+
"google.golang.org/grpc"
12+
"google.golang.org/grpc/credentials/insecure"
13+
14+
pb "github.com/lima-vm/lima/pkg/driver/external"
15+
)
16+
17+
type DriverClient struct {
18+
socketPath string
19+
Conn *grpc.ClientConn
20+
DriverSvc pb.DriverClient
21+
logger *logrus.Logger
22+
}
23+
24+
func NewDriverClient(socketPath string, logger *logrus.Logger) (*DriverClient, error) {
25+
opts := []grpc.DialOption{
26+
grpc.WithDefaultCallOptions(grpc.MaxCallRecvMsgSize(16 << 20)),
27+
grpc.WithDefaultCallOptions(grpc.MaxCallSendMsgSize(16 << 20)),
28+
grpc.WithContextDialer(func(_ context.Context, _ string) (net.Conn, error) {
29+
return net.Dial("unix", socketPath)
30+
}),
31+
grpc.WithTransportCredentials(insecure.NewCredentials()),
32+
}
33+
34+
//nolint:staticcheck // grpc.Dial is used for compatibility reasons
35+
conn, err := grpc.Dial("unix://"+socketPath, opts...)
36+
if err != nil {
37+
logger.Errorf("failed to dial gRPC driver client connection: %v", err)
38+
return nil, err
39+
}
40+
41+
driverSvc := pb.NewDriverClient(conn)
42+
43+
return &DriverClient{
44+
socketPath: socketPath,
45+
Conn: conn,
46+
DriverSvc: driverSvc,
47+
logger: logger,
48+
}, nil
49+
}

0 commit comments

Comments
 (0)