Skip to content

Commit 76c670d

Browse files
committed
driver(external): tweak some code and move the conenction proxy to server-level
Signed-off-by: Ansuman Sahoo <anshumansahoo500@gmail.com>
1 parent fdd4e41 commit 76c670d

File tree

23 files changed

+153
-206
lines changed

23 files changed

+153
-206
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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

cmd/limactl/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +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/qemu" // register qemu driver for all platforms
19+
_ "github.com/lima-vm/lima/pkg/driver/qemu" // register qemu driver for all platforms
2020
"github.com/lima-vm/lima/pkg/fsutil"
2121
"github.com/lima-vm/lima/pkg/osutil"
2222
"github.com/lima-vm/lima/pkg/registry"
@@ -46,7 +46,7 @@ func main() {
4646
logrus.Fatal(err)
4747
}
4848

49-
defer registry.DefaultRegistry.StopAllExternalDrivers()
49+
defer registry.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
@@ -398,8 +398,12 @@ func createStartActionCommon(cmd *cobra.Command, _ []string) (exit bool, err err
398398
return true, err
399399
} else if listDrivers {
400400
w := cmd.OutOrStdout()
401-
for _, d := range registry.DefaultRegistry.List() {
402-
_, _ = fmt.Fprintln(w, d)
401+
for k, v := range registry.List() {
402+
if v != "internal" {
403+
_, _ = fmt.Fprintln(w, k+"(external)")
404+
continue
405+
}
406+
_, _ = fmt.Fprintln(w, k)
403407
}
404408
return true, nil
405409
}

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 driver.ConfiguredDriver{}
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 driver.ConfiguredDriver{}
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

pkg/driver/external/server/server.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func Serve(driver driver.Driver) {
3030
logger := logrus.New()
3131
logger.SetLevel(logrus.DebugLevel)
3232

33-
socketPath := filepath.Join(os.TempDir(), fmt.Sprintf("lima-driver-%s-%d.sock", driver.GetInfo().DriverName, os.Getpid()))
33+
socketPath := filepath.Join(os.TempDir(), fmt.Sprintf("lima-driver-%s-%d.sock", driver.Info().DriverName, os.Getpid()))
3434

3535
defer func() {
3636
if err := os.Remove(socketPath); err != nil && !os.IsNotExist(err) {
@@ -80,7 +80,7 @@ func Serve(driver driver.Driver) {
8080
os.Exit(0)
8181
}()
8282

83-
logger.Infof("Starting external driver server for %s", driver.GetInfo().DriverName)
83+
logger.Infof("Starting external driver server for %s", driver.Info().DriverName)
8484
logger.Infof("Server starting on Unix socket: %s", socketPath)
8585
if err := server.Serve(listener); err != nil {
8686
logger.Fatalf("Failed to serve: %v", err)

pkg/driver/qemu/qemu_driver.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ type LimaQemuDriver struct {
4242
VSockPort int
4343
VirtioPort string
4444

45-
DriverType driver.DriverType
46-
4745
qCmd *exec.Cmd
4846
qWaitCh chan error
4947

@@ -52,7 +50,7 @@ type LimaQemuDriver struct {
5250

5351
var _ driver.Driver = (*LimaQemuDriver)(nil)
5452

55-
func New(driverType driver.DriverType) *LimaQemuDriver {
53+
func New() *LimaQemuDriver {
5654
// virtserialport doesn't seem to work reliably: https://github.com/lima-vm/lima/issues/2064
5755
// but on Windows default Unix socket forwarding is not available
5856
var virtioPort string
@@ -63,13 +61,16 @@ func New(driverType driver.DriverType) *LimaQemuDriver {
6361
return &LimaQemuDriver{
6462
VSockPort: 0,
6563
VirtioPort: virtioPort,
66-
DriverType: driverType,
6764
}
6865
}
6966

70-
func (l *LimaQemuDriver) SetConfig(inst *store.Instance, sshLocalPort int) {
67+
func (l *LimaQemuDriver) Configure(inst *store.Instance, sshLocalPort int) driver.ConfiguredDriver {
7168
l.Instance = inst
7269
l.SSHLocalPort = sshLocalPort
70+
71+
return driver.ConfiguredDriver{
72+
Driver: l,
73+
}
7374
}
7475

7576
func (l *LimaQemuDriver) Validate() error {
@@ -244,7 +245,7 @@ func (l *LimaQemuDriver) ChangeDisplayPassword(_ context.Context, password strin
244245
return l.changeVNCPassword(password)
245246
}
246247

247-
func (l *LimaQemuDriver) GetDisplayConnection(_ context.Context) (string, error) {
248+
func (l *LimaQemuDriver) DisplayConnection(_ context.Context) (string, error) {
248249
return l.getVNCDisplayPort()
249250
}
250251

@@ -456,10 +457,10 @@ func (l *LimaQemuDriver) ListSnapshots(_ context.Context) (string, error) {
456457
return List(qCfg, l.Instance.Status == store.StatusRunning)
457458
}
458459

459-
func (l *LimaQemuDriver) GuestAgentConn(ctx context.Context) (net.Conn, error) {
460+
func (l *LimaQemuDriver) GuestAgentConn(ctx context.Context) (net.Conn, string, error) {
460461
var d net.Dialer
461462
dialContext, err := d.DialContext(ctx, "unix", filepath.Join(l.Instance.Dir, filenames.GuestAgentSock))
462-
return dialContext, err
463+
return dialContext, "unix", err
463464
}
464465

465466
type qArgTemplateApplier struct {
@@ -514,7 +515,7 @@ func (a *qArgTemplateApplier) applyTemplate(qArg string) (string, error) {
514515
return b.String(), nil
515516
}
516517

517-
func (l *LimaQemuDriver) GetInfo() driver.Info {
518+
func (l *LimaQemuDriver) Info() driver.Info {
518519
var info driver.Info
519520
if l.Instance != nil && l.Instance.Dir != "" {
520521
info.InstanceDir = l.Instance.Dir

0 commit comments

Comments
 (0)