@@ -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,20 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
553560 }
554561 }
555562 }
563+ if a .driver .ForwardUnixGuestAgent () {
564+ if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
565+ errs = append (errs , err )
566+ }
567+ }
568+
556569 return errors .Join (errs ... )
557570 })
558571 for {
572+ if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
573+ if a .driver .ForwardUnixGuestAgent () {
574+ _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
575+ }
576+ }
559577 client , err := a .getOrCreateClient (ctx )
560578 if err == nil {
561579 if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -590,8 +608,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
590608 return a .client , err
591609}
592610
593- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
611+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
594612 conn , err := a .driver .GuestAgentConn (ctx )
613+ // default to forwarded sock
614+ if conn == nil && err == nil {
615+ var d net.Dialer
616+ conn , err = d .DialContext (ctx , "unix" , filepath .Join (a .instDir , filenames .GuestAgentSock ))
617+ }
618+ return conn , err
619+ }
620+
621+ func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient.GuestAgentClient , error ) {
622+ conn , err := a .createConnection (ctx )
595623 if err != nil {
596624 return nil , err
597625 }
0 commit comments