@@ -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
@@ -117,6 +118,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
117
118
}
118
119
119
120
vSockPort := 0
121
+ virtioPort := ""
120
122
if * y .VMType == limayaml .VZ {
121
123
vSockPort = 2222
122
124
} else if * y .VMType == limayaml .WSL2 {
@@ -125,9 +127,11 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
125
127
logrus .WithError (err ).Error ("failed to get free VSock port" )
126
128
}
127
129
vSockPort = port
130
+ } else if * y .VMType == limayaml .QEMU {
131
+ virtioPort = filenames .VirtioPort
128
132
}
129
133
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 {
131
135
return nil , err
132
136
}
133
137
@@ -160,6 +164,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
160
164
Yaml : y ,
161
165
SSHLocalPort : sshLocalPort ,
162
166
VSockPort : vSockPort ,
167
+ VirtioPort : virtioPort ,
163
168
})
164
169
165
170
a := & HostAgent {
@@ -176,6 +181,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
176
181
sigintCh : sigintCh ,
177
182
eventEnc : json .NewEncoder (stdout ),
178
183
vSockPort : vSockPort ,
184
+ virtioPort : virtioPort ,
179
185
guestAgentAliveCh : make (chan struct {}),
180
186
}
181
187
return a , nil
@@ -561,6 +567,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
561
567
}
562
568
}
563
569
570
+ localUnix := filepath .Join (a .instDir , filenames .GuestAgentSock )
571
+ remoteUnix := "/run/lima-guestagent.sock"
572
+
564
573
a .onClose = append (a .onClose , func () error {
565
574
logrus .Debugf ("Stop forwarding unix sockets" )
566
575
var errs []error
@@ -573,9 +582,19 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
573
582
}
574
583
}
575
584
}
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
+ }
576
590
return errors .Join (errs ... )
577
591
})
578
592
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
+ }
579
598
client , err := a .getOrCreateClient (ctx )
580
599
if err == nil {
581
600
if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -610,8 +629,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
610
629
return a .client , err
611
630
}
612
631
613
- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
632
+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
614
633
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 )
615
644
if err != nil {
616
645
return nil , err
617
646
}
0 commit comments