@@ -57,6 +57,9 @@ type HostAgent struct {
5757
5858 clientMu sync.RWMutex
5959 client guestagentclient.GuestAgentClient
60+
61+ guestAgentAliveCh chan struct {} // closed on establishing the connection
62+ guestAgentAliveChOnce sync.Once
6063}
6164
6265type options struct {
@@ -160,19 +163,20 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
160163 })
161164
162165 a := & HostAgent {
163- y : y ,
164- sshLocalPort : sshLocalPort ,
165- udpDNSLocalPort : udpDNSLocalPort ,
166- tcpDNSLocalPort : tcpDNSLocalPort ,
167- instDir : inst .Dir ,
168- instName : instName ,
169- instSSHAddress : inst .SSHAddress ,
170- sshConfig : sshConfig ,
171- portForwarder : newPortForwarder (sshConfig , sshLocalPort , rules , inst .VMType ),
172- driver : limaDriver ,
173- sigintCh : sigintCh ,
174- eventEnc : json .NewEncoder (stdout ),
175- vSockPort : vSockPort ,
166+ y : y ,
167+ sshLocalPort : sshLocalPort ,
168+ udpDNSLocalPort : udpDNSLocalPort ,
169+ tcpDNSLocalPort : tcpDNSLocalPort ,
170+ instDir : inst .Dir ,
171+ instName : instName ,
172+ instSSHAddress : inst .SSHAddress ,
173+ sshConfig : sshConfig ,
174+ portForwarder : newPortForwarder (sshConfig , sshLocalPort , rules , inst .VMType ),
175+ driver : limaDriver ,
176+ sigintCh : sigintCh ,
177+ eventEnc : json .NewEncoder (stdout ),
178+ vSockPort : vSockPort ,
179+ guestAgentAliveCh : make (chan struct {}),
176180 }
177181 return a , nil
178182}
@@ -492,6 +496,21 @@ sudo chown -R "${USER}" /run/host-services`
492496 if err := a .waitForRequirements ("optional" , a .optionalRequirements ()); err != nil {
493497 errs = append (errs , err )
494498 }
499+ if ! * a .y .Plain {
500+ logrus .Info ("Waiting for the guest agent to be running" )
501+ select {
502+ case <- a .guestAgentAliveCh :
503+ // NOP
504+ case <- time .After (time .Minute ):
505+ err := errors .New ("guest agent does not seem to be running; port forwards will not work" )
506+ if * a .y .VMType == limayaml .WSL2 {
507+ // geustagent is currently not available for WSL2: https://github.com/lima-vm/lima/issues/2025
508+ logrus .Warn (err )
509+ } else {
510+ errs = append (errs , err )
511+ }
512+ }
513+ }
495514 if err := a .waitForRequirements ("final" , a .finalRequirements ()); err != nil {
496515 errs = append (errs , err )
497516 }
@@ -605,6 +624,10 @@ func (a *HostAgent) processGuestAgentEvents(ctx context.Context, client guestage
605624 if err != nil {
606625 return err
607626 }
627+ logrus .Info ("Guest agent is running" )
628+ a .guestAgentAliveChOnce .Do (func () {
629+ close (a .guestAgentAliveCh )
630+ })
608631
609632 logrus .Debugf ("guest agent info: %+v" , info )
610633
0 commit comments