Skip to content

Commit 963f97c

Browse files
committed
driver(external): tweak some code and move the conenction proxy to server-level
Signed-off-by: Ansuman Sahoo <[email protected]>
1 parent ebbfcbd commit 963f97c

File tree

24 files changed

+302
-344
lines changed

24 files changed

+302
-344
lines changed

pkg/driver/qemu/cmd/lima-driver-qemu/main.go renamed to cmd/lima-driver-qemu/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,12 @@
44
package main
55

66
import (
7-
"github.com/lima-vm/lima/pkg/driver"
8-
97
"github.com/lima-vm/lima/pkg/driver/external/server"
108
"github.com/lima-vm/lima/pkg/driver/qemu"
119
)
1210

1311
// To be used as an external driver for Lima.
1412
func main() {
15-
driver := qemu.New(driver.DriverTypeExternal)
13+
driver := qemu.New()
1614
server.Serve(driver)
1715
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
1-
//go:build (darwin && amd64) || (darwin && arm64)
1+
//go:build darwin
22

33
// SPDX-FileCopyrightText: Copyright The Lima Authors
44
// SPDX-License-Identifier: Apache-2.0
55

66
package main
77

88
import (
9-
"github.com/lima-vm/lima/pkg/driver"
109
"github.com/lima-vm/lima/pkg/driver/external/server"
1110
"github.com/lima-vm/lima/pkg/driver/vz"
1211
)
1312

1413
// To be used as an external driver for Lima.
1514
func main() {
16-
driver := vz.New(driver.DriverTypeExternal)
15+
driver := vz.New()
1716
server.Serve(driver)
1817
}

pkg/driver/wsl2/cmd/lima-driver-wsl/main.go renamed to cmd/lima-driver-wsl2/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
package main
77

88
import (
9-
"github.com/lima-vm/lima/pkg/driver"
109
"github.com/lima-vm/lima/pkg/driver/external/server"
1110
"github.com/lima-vm/lima/pkg/driver/wsl2"
1211
)
1312

1413
// To be used as an external driver for Lima.
1514
func main() {
16-
driver := wsl2.New(driver.DriverTypeExternal)
15+
driver := wsl2.New()
1716
server.Serve(driver)
1817
}

cmd/limactl/main.go

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

1818
"github.com/lima-vm/lima/pkg/debugutil"
19-
// _ "github.com/lima-vm/lima/pkg/driver/qemu" // register qemu driver for all platforms
19+
"github.com/lima-vm/lima/pkg/driver/external/server"
20+
_ "github.com/lima-vm/lima/pkg/driver/qemu" // register qemu driver for all platforms
2021
"github.com/lima-vm/lima/pkg/fsutil"
2122
"github.com/lima-vm/lima/pkg/osutil"
22-
"github.com/lima-vm/lima/pkg/registry"
2323
"github.com/lima-vm/lima/pkg/store/dirnames"
2424
"github.com/lima-vm/lima/pkg/version"
2525
)
@@ -46,7 +46,7 @@ func main() {
4646
logrus.Fatal(err)
4747
}
4848

49-
defer registry.DefaultRegistry.StopAllExternalDrivers()
49+
defer server.StopAllExternalDrivers()
5050
}
5151

5252
func newApp() *cobra.Command {

cmd/limactl/start.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,8 +400,12 @@ func createStartActionCommon(cmd *cobra.Command, _ []string) (exit bool, err err
400400
return true, err
401401
} else if listDrivers {
402402
w := cmd.OutOrStdout()
403-
for _, d := range registry.DefaultRegistry.List() {
404-
_, _ = fmt.Fprintln(w, d)
403+
for k, v := range registry.List() {
404+
if v != "internal" {
405+
_, _ = fmt.Fprintln(w, k+"(external)")
406+
continue
407+
}
408+
_, _ = fmt.Fprintln(w, k)
405409
}
406410
return true, nil
407411
}

pkg/driver/driver.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ type GUI interface {
4343
RunGUI() error
4444

4545
ChangeDisplayPassword(ctx context.Context, password string) error
46-
GetDisplayConnection(ctx context.Context) (string, error)
46+
DisplayConnection(ctx context.Context) (string, error)
4747
}
4848

4949
// SnapshotManager defines operations for managing snapshots.
@@ -66,7 +66,7 @@ type GuestAgent interface {
6666
ForwardGuestAgent() bool
6767

6868
// GuestAgentConn returns the guest agent connection, or nil (if forwarded by ssh).
69-
GuestAgentConn(_ context.Context) (net.Conn, error)
69+
GuestAgentConn(_ context.Context) (net.Conn, string, error)
7070
}
7171

7272
// Driver interface is used by hostagent for managing vm.
@@ -77,10 +77,14 @@ type Driver interface {
7777
Registration
7878
GuestAgent
7979

80-
GetInfo() Info
80+
Info() Info
8181

8282
// SetConfig sets the configuration for the instance.
83-
SetConfig(inst *store.Instance, sshLocalPort int)
83+
Configure(inst *store.Instance, sshLocalPort int) *ConfiguredDriver
84+
}
85+
86+
type ConfiguredDriver struct {
87+
Driver
8488
}
8589

8690
type Info struct {
@@ -90,10 +94,3 @@ type Info struct {
9094
VirtioPort string `json:"virtioPort"`
9195
InstanceDir string `json:"instanceDir,omitempty"`
9296
}
93-
94-
type DriverType = string
95-
96-
const (
97-
DriverTypeInternal DriverType = "internal"
98-
DriverTypeExternal DriverType = "external"
99-
)

pkg/driver/external/client/methods.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (d *DriverClient) ChangeDisplayPassword(ctx context.Context, password strin
136136
return nil
137137
}
138138

139-
func (d *DriverClient) GetDisplayConnection(ctx context.Context) (string, error) {
139+
func (d *DriverClient) DisplayConnection(ctx context.Context) (string, error) {
140140
d.logger.Debug("Getting display connection for the driver instance")
141141

142142
resp, err := d.DriverSvc.GetDisplayConnection(ctx, &emptypb.Empty{})
@@ -248,18 +248,18 @@ func (d *DriverClient) ForwardGuestAgent() bool {
248248
return resp.ShouldForward
249249
}
250250

251-
func (d *DriverClient) GuestAgentConn(ctx context.Context) (net.Conn, error) {
251+
func (d *DriverClient) GuestAgentConn(ctx context.Context) (net.Conn, string, error) {
252252
d.logger.Info("Getting guest agent connection")
253253
_, err := d.DriverSvc.GuestAgentConn(ctx, &emptypb.Empty{})
254254
if err != nil {
255255
d.logger.Errorf("Failed to get guest agent connection: %v", err)
256-
return nil, err
256+
return nil, "", err
257257
}
258258

259-
return nil, nil
259+
return nil, "", nil
260260
}
261261

262-
func (d *DriverClient) GetInfo() driver.Info {
262+
func (d *DriverClient) Info() driver.Info {
263263
d.logger.Debug("Getting driver info")
264264

265265
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -281,13 +281,13 @@ func (d *DriverClient) GetInfo() driver.Info {
281281
return info
282282
}
283283

284-
func (d *DriverClient) SetConfig(inst *store.Instance, sshLocalPort int) {
284+
func (d *DriverClient) Configure(inst *store.Instance, sshLocalPort int) *driver.ConfiguredDriver {
285285
d.logger.Debugf("Setting config for instance %s with SSH local port %d", inst.Name, sshLocalPort)
286286

287287
instJson, err := inst.MarshalJSON()
288288
if err != nil {
289289
d.logger.Errorf("Failed to marshal instance config: %v", err)
290-
return
290+
return nil
291291
}
292292

293293
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
@@ -299,8 +299,11 @@ func (d *DriverClient) SetConfig(inst *store.Instance, sshLocalPort int) {
299299
})
300300
if err != nil {
301301
d.logger.Errorf("Failed to set config: %v", err)
302-
return
302+
return nil
303303
}
304304

305305
d.logger.Debugf("Config set successfully for instance %s", inst.Name)
306+
return &driver.ConfiguredDriver{
307+
Driver: d,
308+
}
306309
}

pkg/driver/external/server/methods.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,20 @@ package server
66
import (
77
"context"
88
"encoding/json"
9+
"net"
10+
"path/filepath"
911

1012
"google.golang.org/grpc/codes"
1113
"google.golang.org/grpc/status"
1214

1315
// "google.golang.org/protobuf/proto"
1416
"google.golang.org/protobuf/types/known/emptypb"
1517

18+
"github.com/lima-vm/lima/pkg/bicopy"
1619
pb "github.com/lima-vm/lima/pkg/driver/external"
1720
"github.com/lima-vm/lima/pkg/store"
21+
"github.com/lima-vm/lima/pkg/store/filenames"
22+
"github.com/sirupsen/logrus"
1823
)
1924

2025
func (s *DriverServer) Start(empty *emptypb.Empty, stream pb.Driver_StartServer) error {
@@ -59,25 +64,48 @@ func (s *DriverServer) SetConfig(ctx context.Context, req *pb.SetConfigRequest)
5964
return &emptypb.Empty{}, err
6065
}
6166

62-
s.driver.SetConfig(&inst, int(req.SshLocalPort))
67+
_ = s.driver.Configure(&inst, int(req.SshLocalPort))
6368

6469
return &emptypb.Empty{}, nil
6570
}
6671

6772
func (s *DriverServer) GuestAgentConn(ctx context.Context, empty *emptypb.Empty) (*emptypb.Empty, error) {
6873
s.logger.Debug("Received GuestAgentConn request")
69-
_, err := s.driver.GuestAgentConn(ctx)
74+
conn, connType, err := s.driver.GuestAgentConn(ctx)
7075
if err != nil {
7176
s.logger.Errorf("GuestAgentConn failed: %v", err)
7277
return nil, err
7378
}
7479

80+
if connType != "unix" {
81+
proxySocketPath := filepath.Join(s.driver.Info().InstanceDir, filenames.GuestAgentSock)
82+
83+
listener, err := net.Listen("unix", proxySocketPath)
84+
if err != nil {
85+
logrus.Errorf("Failed to create proxy socket: %v", err)
86+
return nil, err
87+
}
88+
89+
go func() {
90+
defer listener.Close()
91+
defer conn.Close()
92+
93+
proxyConn, err := listener.Accept()
94+
if err != nil {
95+
logrus.Errorf("Failed to accept proxy connection: %v", err)
96+
return
97+
}
98+
99+
bicopy.Bicopy(conn, proxyConn, nil)
100+
}()
101+
}
102+
75103
return &emptypb.Empty{}, nil
76104
}
77105

78106
func (s *DriverServer) GetInfo(ctx context.Context, empty *emptypb.Empty) (*pb.InfoResponse, error) {
79107
s.logger.Debug("Received GetInfo request")
80-
info := s.driver.GetInfo()
108+
info := s.driver.Info()
81109

82110
infoJson, err := json.Marshal(info)
83111
if err != nil {
@@ -158,7 +186,7 @@ func (s *DriverServer) ChangeDisplayPassword(ctx context.Context, req *pb.Change
158186

159187
func (s *DriverServer) GetDisplayConnection(ctx context.Context, empty *emptypb.Empty) (*pb.GetDisplayConnectionResponse, error) {
160188
s.logger.Debug("Received GetDisplayConnection request")
161-
conn, err := s.driver.GetDisplayConnection(ctx)
189+
conn, err := s.driver.DisplayConnection(ctx)
162190
if err != nil {
163191
s.logger.Errorf("GetDisplayConnection failed: %v", err)
164192
return nil, err

0 commit comments

Comments
 (0)