@@ -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
@@ -548,8 +554,6 @@ func (a *HostAgent) close() error {
548
554
}
549
555
550
556
func (a * HostAgent ) watchGuestAgentEvents (ctx context.Context ) {
551
- // TODO: use vSock (when QEMU for macOS gets support for vSock)
552
-
553
557
// Setup all socket forwards and defer their teardown
554
558
if * a .y .VMType != limayaml .WSL2 {
555
559
logrus .Debugf ("Forwarding unix sockets" )
@@ -561,6 +565,9 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
561
565
}
562
566
}
563
567
568
+ localUnix := filepath .Join (a .instDir , filenames .GuestAgentSock )
569
+ remoteUnix := "/run/lima-guestagent.sock"
570
+
564
571
a .onClose = append (a .onClose , func () error {
565
572
logrus .Debugf ("Stop forwarding unix sockets" )
566
573
var errs []error
@@ -573,9 +580,19 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
573
580
}
574
581
}
575
582
}
583
+ if a .driver .ForwardGuestAgent () {
584
+ if err := forwardSSH (context .Background (), a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
585
+ errs = append (errs , err )
586
+ }
587
+ }
576
588
return errors .Join (errs ... )
577
589
})
578
590
for {
591
+ if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
592
+ if a .driver .ForwardGuestAgent () {
593
+ _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
594
+ }
595
+ }
579
596
client , err := a .getOrCreateClient (ctx )
580
597
if err == nil {
581
598
if err := a .processGuestAgentEvents (ctx , client ); err != nil {
@@ -610,8 +627,18 @@ func (a *HostAgent) getOrCreateClient(ctx context.Context) (guestagentclient.Gue
610
627
return a .client , err
611
628
}
612
629
613
- func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient. GuestAgentClient , error ) {
630
+ func (a * HostAgent ) createConnection (ctx context.Context ) (net. Conn , error ) {
614
631
conn , err := a .driver .GuestAgentConn (ctx )
632
+ // default to forwarded sock
633
+ if conn == nil && err == nil {
634
+ var d net.Dialer
635
+ conn , err = d .DialContext (ctx , "unix" , filepath .Join (a .instDir , filenames .GuestAgentSock ))
636
+ }
637
+ return conn , err
638
+ }
639
+
640
+ func (a * HostAgent ) createClient (ctx context.Context ) (guestagentclient.GuestAgentClient , error ) {
641
+ conn , err := a .createConnection (ctx )
615
642
if err != nil {
616
643
return nil , err
617
644
}
0 commit comments