@@ -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
@@ -117,6 +118,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
117118 }
118119
119120 vSockPort := 0
121+ virtioPort := ""
120122 if * y .VMType == limayaml .VZ {
121123 vSockPort = 2222
122124 } else if * y .VMType == limayaml .WSL2 {
@@ -125,9 +127,11 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
125127 logrus .WithError (err ).Error ("failed to get free VSock port" )
126128 }
127129 vSockPort = port
130+ } else if * y .VMType == limayaml .QEMU {
131+ virtioPort = filenames .VirtioPort
128132 }
129133
130- if err := cidata .GenerateISO9660 (inst .Dir , instName , y , udpDNSLocalPort , tcpDNSLocalPort , o .nerdctlArchive , vSockPort ); err != nil {
134+ if err := cidata .GenerateISO9660 (inst .Dir , instName , y , udpDNSLocalPort , tcpDNSLocalPort , o .nerdctlArchive , vSockPort , virtioPort ); err != nil {
131135 return nil , err
132136 }
133137
@@ -160,6 +164,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
160164 Yaml : y ,
161165 SSHLocalPort : sshLocalPort ,
162166 VSockPort : vSockPort ,
167+ VirtioPort : virtioPort ,
163168 })
164169
165170 a := & HostAgent {
@@ -176,6 +181,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
176181 sigintCh : sigintCh ,
177182 eventEnc : json .NewEncoder (stdout ),
178183 vSockPort : vSockPort ,
184+ virtioPort : virtioPort ,
179185 guestAgentAliveCh : make (chan struct {}),
180186 }
181187 return a , nil
@@ -561,6 +567,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
561567 }
562568 }
563569
570+ localUnix := filepath .Join (a .instDir , filenames .GuestAgentSock )
571+ remoteUnix := "/run/lima-guestagent.sock"
572+
564573 a .onClose = append (a .onClose , func () error {
565574 logrus .Debugf ("Stop forwarding unix sockets" )
566575 var errs []error
@@ -573,9 +582,19 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
573582 }
574583 }
575584 }
585+ if a .driver .ForwardGuestAgent () {
586+ if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
587+ errs = append (errs , err )
588+ }
589+ }
576590 return errors .Join (errs ... )
577591 })
578592 for {
593+ if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
594+ if a .driver .ForwardGuestAgent () {
595+ _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
596+ }
597+ }
579598 client , err := a .getOrCreateClient (ctx )
580599 if err == nil {
581600 if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -610,8 +629,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
610629 return a .client , err
611630}
612631
613- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
632+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
614633 conn , err := a .driver .GuestAgentConn (ctx )
634+ // default to forwarded sock
635+ if conn == nil && err == nil {
636+ var d net.Dialer
637+ conn , err = d .DialContext (ctx , "unix" , filepath .Join (a .instDir , filenames .GuestAgentSock ))
638+ }
639+ return conn , err
640+ }
641+
642+ func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient.GuestAgentClient , error ) {
643+ conn , err := a .createConnection (ctx )
615644 if err != nil {
616645 return nil , err
617646 }
0 commit comments