@@ -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,12 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
541
545
}
542
546
}
543
547
548
+ // if needed, forward the guest agent unix socket
549
+ d := a .driver .(* driver.BaseDriver )
550
+ forwardUnix := d .VSockPort == 0 && d .VirtioPort == ""
551
+ localUnix := filepath .Join (a .instDir , filenames .GuestAgentSock )
552
+ remoteUnix := "/run/lima-guestagent.sock"
553
+
544
554
a .onClose = append (a .onClose , func () error {
545
555
logrus .Debugf ("Stop forwarding unix sockets" )
546
556
var errs []error
@@ -553,9 +563,19 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
553
563
}
554
564
}
555
565
}
566
+ if forwardUnix {
567
+ if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
568
+ errs = append (errs , err )
569
+ }
570
+ }
556
571
return errors .Join (errs ... )
557
572
})
558
573
for {
574
+ if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
575
+ if forwardUnix {
576
+ _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
577
+ }
578
+ }
559
579
client , err := a .getOrCreateClient (ctx )
560
580
if err == nil {
561
581
if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -590,8 +610,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
590
610
return a .client , err
591
611
}
592
612
593
- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
613
+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
594
614
conn , err := a .driver .GuestAgentConn (ctx )
615
+ // default to forwarded sock
616
+ if conn == nil && err == nil {
617
+ var d net.Dialer
618
+ conn , err = d .DialContext (ctx , "unix" , filepath .Join (a .instDir , filenames .GuestAgentSock ))
619
+ }
620
+ return conn , err
621
+ }
622
+
623
+ func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient.GuestAgentClient , error ) {
624
+ conn , err := a .createConnection (ctx )
595
625
if err != nil {
596
626
return nil , err
597
627
}
0 commit comments