@@ -53,7 +53,8 @@ type HostAgent struct {
53
53
eventEnc * json.Encoder
54
54
eventEncMu sync.Mutex
55
55
56
- vSockPort int
56
+ vSockPort int
57
+ virtioPort string
57
58
58
59
clientMu sync.RWMutex
59
60
client guestagentclient.GuestAgentClient
@@ -114,6 +115,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
114
115
}
115
116
116
117
vSockPort := 0
118
+ virtioPort := ""
117
119
if * y .VMType == limayaml .VZ {
118
120
vSockPort = 2222
119
121
} else if * y .VMType == limayaml .WSL2 {
@@ -122,9 +124,11 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
122
124
logrus .WithError (err ).Error ("failed to get free VSock port" )
123
125
}
124
126
vSockPort = port
127
+ } else if * y .VMType == limayaml .QEMU {
128
+ virtioPort = filenames .VirtioPort
125
129
}
126
130
127
- if err := cidata .GenerateISO9660 (inst .Dir , instName , y , udpDNSLocalPort , tcpDNSLocalPort , o .nerdctlArchive , vSockPort ); err != nil {
131
+ if err := cidata .GenerateISO9660 (inst .Dir , instName , y , udpDNSLocalPort , tcpDNSLocalPort , o .nerdctlArchive , vSockPort , virtioPort ); err != nil {
128
132
return nil , err
129
133
}
130
134
@@ -157,6 +161,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
157
161
Yaml : y ,
158
162
SSHLocalPort : sshLocalPort ,
159
163
VSockPort : vSockPort ,
164
+ VirtioPort : virtioPort ,
160
165
})
161
166
162
167
a := & HostAgent {
@@ -173,6 +178,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
173
178
sigintCh : sigintCh ,
174
179
eventEnc : json .NewEncoder (stdout ),
175
180
vSockPort : vSockPort ,
181
+ virtioPort : virtioPort ,
176
182
}
177
183
return a , nil
178
184
}
@@ -528,8 +534,6 @@ func (a *HostAgent) close() error {
528
534
}
529
535
530
536
func (a * HostAgent ) watchGuestAgentEvents (ctx context.Context ) {
531
- // TODO: use vSock (when QEMU for macOS gets support for vSock)
532
-
533
537
// Setup all socket forwards and defer their teardown
534
538
if * a .y .VMType != limayaml .WSL2 {
535
539
logrus .Debugf ("Forwarding unix sockets" )
@@ -541,6 +545,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
541
545
}
542
546
}
543
547
548
+ localUnix := filepath .Join (a .instDir , filenames .GuestAgentSock )
549
+ remoteUnix := "/run/lima-guestagent.sock"
550
+
544
551
a .onClose = append (a .onClose , func () error {
545
552
logrus .Debugf ("Stop forwarding unix sockets" )
546
553
var errs []error
@@ -553,9 +560,19 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
553
560
}
554
561
}
555
562
}
563
+ if a .driver .ForwardGuestAgent () {
564
+ if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
565
+ errs = append (errs , err )
566
+ }
567
+ }
556
568
return errors .Join (errs ... )
557
569
})
558
570
for {
571
+ if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
572
+ if a .driver .ForwardGuestAgent () {
573
+ _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
574
+ }
575
+ }
559
576
client , err := a .getOrCreateClient (ctx )
560
577
if err == nil {
561
578
if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -590,8 +607,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
590
607
return a .client , err
591
608
}
592
609
593
- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
610
+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
594
611
conn , err := a .driver .GuestAgentConn (ctx )
612
+ // default to forwarded sock
613
+ if conn == nil && err == nil {
614
+ var d net.Dialer
615
+ conn , err = d .DialContext (ctx , "unix" , filepath .Join (a .instDir , filenames .GuestAgentSock ))
616
+ }
617
+ return conn , err
618
+ }
619
+
620
+ func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient.GuestAgentClient , error ) {
621
+ conn , err := a .createConnection (ctx )
595
622
if err != nil {
596
623
return nil , err
597
624
}
0 commit comments