@@ -53,7 +53,8 @@ type HostAgent struct {
5353 eventEnc * json.Encoder
5454 eventEncMu sync.Mutex
5555
56- vSockPort int
56+ vSockPort int
57+ virtioPort string
5758
5859 clientMu sync.RWMutex
5960 client guestagentclient.GuestAgentClient
@@ -114,6 +115,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
114115 }
115116
116117 vSockPort := 0
118+ virtioPort := ""
117119 if * y .VMType == limayaml .VZ {
118120 vSockPort = 2222
119121 } else if * y .VMType == limayaml .WSL2 {
@@ -122,9 +124,11 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
122124 logrus .WithError (err ).Error ("failed to get free VSock port" )
123125 }
124126 vSockPort = port
127+ } else if * y .VMType == limayaml .QEMU {
128+ virtioPort = filenames .VirtioPort
125129 }
126130
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 {
128132 return nil , err
129133 }
130134
@@ -157,6 +161,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
157161 Yaml : y ,
158162 SSHLocalPort : sshLocalPort ,
159163 VSockPort : vSockPort ,
164+ VirtioPort : virtioPort ,
160165 })
161166
162167 a := & HostAgent {
@@ -173,6 +178,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
173178 sigintCh : sigintCh ,
174179 eventEnc : json .NewEncoder (stdout ),
175180 vSockPort : vSockPort ,
181+ virtioPort : virtioPort ,
176182 }
177183 return a , nil
178184}
@@ -528,8 +534,6 @@ func (a *HostAgent) close() error {
528534}
529535
530536func (a * HostAgent ) watchGuestAgentEvents (ctx context.Context ) {
531- // TODO: use vSock (when QEMU for macOS gets support for vSock)
532-
533537 // Setup all socket forwards and defer their teardown
534538 if * a .y .VMType != limayaml .WSL2 {
535539 logrus .Debugf ("Forwarding unix sockets" )
@@ -541,6 +545,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
541545 }
542546 }
543547
548+ localUnix := filepath .Join (a .instDir , filenames .GuestAgentSock )
549+ remoteUnix := "/run/lima-guestagent.sock"
550+
544551 a .onClose = append (a .onClose , func () error {
545552 logrus .Debugf ("Stop forwarding unix sockets" )
546553 var errs []error
@@ -553,9 +560,19 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
553560 }
554561 }
555562 }
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+ }
556568 return errors .Join (errs ... )
557569 })
558570 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+ }
559576 client , err := a .getOrCreateClient (ctx )
560577 if err == nil {
561578 if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -590,8 +607,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
590607 return a .client , err
591608}
592609
593- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
610+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
594611 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 )
595622 if err != nil {
596623 return nil , err
597624 }
0 commit comments