Skip to content

Commit 0e78773

Browse files
committed
external driver plugin system and update Makefile to build exteral bins
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent deaa857 commit 0e78773

File tree

18 files changed

+2479
-38
lines changed

18 files changed

+2479
-38
lines changed

Makefile

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

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

269+
DRIVER_INSTALL_DIR := _output/libexec/lima
270+
271+
.PHONY: additional-drivers
272+
additional-drivers:
273+
@mkdir -p $(DRIVER_INSTALL_DIR)
274+
@for drv in $(ADDITIONAL_DRIVERS); do \
275+
echo "Building $$drv as external"; \
276+
$(GO_BUILD) -o $(DRIVER_INSTALL_DIR)/lima-driver-$$drv ./cmd/lima-driver-$$drv; \
277+
if [ "$$drv" = "vz" ]; then \
278+
codesign -f -v --entitlements vz.entitlements -s - $(DRIVER_INSTALL_DIR)/lima-driver-vz; \
279+
fi; \
280+
done
281+
256282
LIMA_CMDS = $(sort lima lima$(bat)) # $(sort ...) deduplicates the list
257283
LIMA_DEPS = $(addprefix _output/bin/,$(LIMA_CMDS))
258284
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.go

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

cmd/lima-driver-wsl2/main.go

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

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

0 commit comments

Comments
 (0)