@@ -54,6 +54,7 @@ type HostAgent struct {
54
54
eventEncMu sync.Mutex
55
55
56
56
vSockPort int
57
+ virtioPort string
57
58
58
59
clientMu sync.RWMutex
59
60
client guestagentclient.GuestAgentClient
@@ -114,6 +115,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
114
115
}
115
116
116
117
vSockPort := 0
118
+ virtioPort := ""
117
119
if * y .VMType == limayaml .VZ {
118
120
vSockPort = 2222
119
121
} else if * y .VMType == limayaml .WSL2 {
@@ -122,9 +124,11 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
122
124
logrus .WithError (err ).Error ("failed to get free VSock port" )
123
125
}
124
126
vSockPort = port
127
+ } else if * y .VMType == limayaml .QEMU {
128
+ virtioPort = filenames .VirtioPort
125
129
}
126
130
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 {
128
132
return nil , err
129
133
}
130
134
@@ -157,6 +161,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
157
161
Yaml : y ,
158
162
SSHLocalPort : sshLocalPort ,
159
163
VSockPort : vSockPort ,
164
+ VirtioPort : virtioPort ,
160
165
})
161
166
162
167
a := & HostAgent {
@@ -173,6 +178,7 @@ func New(instName string, stdout io.Writer, sigintCh chan os.Signal, opts ...Opt
173
178
sigintCh : sigintCh ,
174
179
eventEnc : json .NewEncoder (stdout ),
175
180
vSockPort : vSockPort ,
181
+ virtioPort : virtioPort ,
176
182
}
177
183
return a , nil
178
184
}
@@ -528,7 +534,6 @@ func (a *HostAgent) close() error {
528
534
}
529
535
530
536
func (a * HostAgent ) watchGuestAgentEvents (ctx context.Context ) {
531
- // TODO: use vSock (when QEMU for macOS gets support for vSock)
532
537
533
538
// Setup all socket forwards and defer their teardown
534
539
if * a .y .VMType != limayaml .WSL2 {
@@ -541,6 +546,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
541
546
}
542
547
}
543
548
549
+ localUnix := filepath .Join (a .instDir , filenames .GuestAgentSock )
550
+ remoteUnix := "/run/lima-guestagent.sock"
551
+
544
552
a .onClose = append (a .onClose , func () error {
545
553
logrus .Debugf ("Stop forwarding unix sockets" )
546
554
var errs []error
@@ -553,9 +561,20 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
553
561
}
554
562
}
555
563
}
564
+ if a .driver .ForwardUnixGuestAgent () {
565
+ if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
566
+ errs = append (errs , err )
567
+ }
568
+ }
569
+
556
570
return errors .Join (errs ... )
557
571
})
558
572
for {
573
+ if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
574
+ if a .driver .ForwardUnixGuestAgent () {
575
+ _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
576
+ }
577
+ }
559
578
client , err := a .getOrCreateClient (ctx )
560
579
if err == nil {
561
580
if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -590,8 +609,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
590
609
return a .client , err
591
610
}
592
611
593
- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
612
+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
594
613
conn , err := a .driver .GuestAgentConn (ctx )
614
+ // default to forwarded sock
615
+ if conn == nil && err == nil {
616
+ var d net.Dialer
617
+ conn , err = d .DialContext (ctx , "unix" , filepath .Join (a .instDir , filenames .GuestAgentSock ))
618
+ }
619
+ return conn , err
620
+ }
621
+
622
+ func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient.GuestAgentClient , error ) {
623
+ conn , err := a .createConnection (ctx )
595
624
if err != nil {
596
625
return nil , err
597
626
}
0 commit comments