@@ -250,7 +250,6 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
250250 instName : instName ,
251251 instSSHAddress : inst .SSHAddress ,
252252 sshConfig : sshConfig ,
253- portForwarder : newPortForwarder (sshConfig , sshLocalPort , rules , ignoreTCP , inst .VMType ),
254253 grpcPortForwarder : portfwd .NewPortForwarder (rules , ignoreTCP , ignoreUDP ),
255254 driver : limaDriver ,
256255 signalCh : signalCh ,
@@ -260,6 +259,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
260259 guestAgentAliveCh : make (chan struct {}),
261260 showProgress : o .showProgress ,
262261 }
262+ a .portForwarder = newPortForwarder (sshConfig , a .sshAddressPort , rules , ignoreTCP , inst .VMType )
263263 return a , nil
264264}
265265
@@ -645,7 +645,8 @@ sudo chown -R "${USER}" /run/host-services`
645645 }
646646 // Copy all config files _after_ the requirements are done
647647 for _ , rule := range a .instConfig .CopyToHost {
648- if err := copyToHost (ctx , a .sshConfig , a .sshLocalPort , rule .HostFile , rule .GuestFile ); err != nil {
648+ sshAddress , sshPort := a .sshAddressPort ()
649+ if err := copyToHost (ctx , a .sshConfig , sshAddress , sshPort , rule .HostFile , rule .GuestFile ); err != nil {
649650 errs = append (errs , err )
650651 }
651652 }
@@ -692,10 +693,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
692693 // Setup all socket forwards and defer their teardown
693694 if ! (a .driver .Info ().Features .DynamicSSHAddress ) {
694695 logrus .Debugf ("Forwarding unix sockets" )
696+ sshAddress , sshPort := a .sshAddressPort ()
695697 for _ , rule := range a .instConfig .PortForwards {
696698 if rule .GuestSocket != "" {
697699 local := hostAddress (rule , & guestagentapi.IPPort {})
698- _ = forwardSSH (ctx , a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbForward , rule .Reverse )
700+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbForward , rule .Reverse )
699701 }
700702 }
701703 }
@@ -706,17 +708,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
706708 a .cleanUp (func () error {
707709 logrus .Debugf ("Stop forwarding unix sockets" )
708710 var errs []error
711+ sshAddress , sshPort := a .sshAddressPort ()
709712 for _ , rule := range a .instConfig .PortForwards {
710713 if rule .GuestSocket != "" {
711714 local := hostAddress (rule , & guestagentapi.IPPort {})
712715 // using ctx.Background() because ctx has already been cancelled
713- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
716+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
714717 errs = append (errs , err )
715718 }
716719 }
717720 }
718721 if a .driver .ForwardGuestAgent () {
719- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
722+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
720723 errs = append (errs , err )
721724 }
722725 }
@@ -727,7 +730,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
727730 if a .instConfig .MountInotify != nil && * a .instConfig .MountInotify {
728731 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
729732 if a .driver .ForwardGuestAgent () {
730- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
733+ sshAddress , sshPort := a .sshAddressPort ()
734+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
731735 }
732736 }
733737 err := a .startInotify (ctx )
@@ -743,7 +747,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
743747 for {
744748 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
745749 if a .driver .ForwardGuestAgent () {
746- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
750+ sshAddress , sshPort := a .sshAddressPort ()
751+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
747752 }
748753 }
749754 client , err := a .getOrCreateClient (ctx )
@@ -767,6 +772,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
767772}
768773
769774func (a * HostAgent ) addStaticPortForwardsFromList (ctx context.Context , staticPortForwards []limatype.PortForward ) {
775+ sshAddress , sshPort := a .sshAddressPort ()
770776 for _ , rule := range staticPortForwards {
771777 if rule .GuestSocket == "" {
772778 guest := & guestagentapi.IPPort {
@@ -777,7 +783,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
777783 local , remote := a .portForwarder .forwardingAddresses (guest )
778784 if local != "" {
779785 logrus .Infof ("Setting up static TCP forwarding from %s to %s" , remote , local )
780- if err := forwardTCP (ctx , a .sshConfig , a . sshLocalPort , local , remote , verbForward ); err != nil {
786+ if err := forwardTCP (ctx , a .sshConfig , sshAddress , sshPort , local , remote , verbForward ); err != nil {
781787 logrus .WithError (err ).Warnf ("failed to set up static TCP forwarding %s -> %s" , remote , local )
782788 }
783789 }
@@ -887,11 +893,11 @@ const (
887893 verbCancel = "cancel"
888894)
889895
890- func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , command ... string ) error {
896+ func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , command ... string ) error {
891897 args := sshConfig .Args ()
892898 args = append (args ,
893- "-p" , strconv .Itoa (port ),
894- "127.0.0.1" ,
899+ "-p" , strconv .Itoa (sshPort ),
900+ sshAddress ,
895901 "--" ,
896902 )
897903 args = append (args , command ... )
@@ -902,7 +908,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
902908 return nil
903909}
904910
905- func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote , verb string , reverse bool ) error {
911+ func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote , verb string , reverse bool ) error {
906912 args := sshConfig .Args ()
907913 args = append (args ,
908914 "-T" ,
@@ -920,16 +926,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
920926 args = append (args ,
921927 "-N" ,
922928 "-f" ,
923- "-p" , strconv .Itoa (port ),
924- "127.0.0.1" ,
929+ "-p" , strconv .Itoa (sshPort ),
930+ sshAddress ,
925931 "--" ,
926932 )
927933 if strings .HasPrefix (local , "/" ) {
928934 switch verb {
929935 case verbForward :
930936 if reverse {
931937 logrus .Infof ("Forwarding %q (host) to %q (guest)" , local , remote )
932- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
938+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
933939 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) before setting up forwarding" , remote )
934940 }
935941 } else {
@@ -944,7 +950,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
944950 case verbCancel :
945951 if reverse {
946952 logrus .Infof ("Stopping forwarding %q (host) to %q (guest)" , local , remote )
947- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
953+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
948954 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after stopping forwarding" , remote )
949955 }
950956 } else {
@@ -965,7 +971,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
965971 if verb == verbForward && strings .HasPrefix (local , "/" ) {
966972 if reverse {
967973 logrus .WithError (err ).Warnf ("Failed to set up forward from %q (host) to %q (guest)" , local , remote )
968- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
974+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
969975 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after forwarding failed" , remote )
970976 }
971977 } else {
@@ -999,10 +1005,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
9991005 Active : true ,
10001006 })
10011007
1008+ sshAddress , sshPort := a .sshAddressPort ()
10021009 args := a .sshConfig .Args ()
10031010 args = append (args ,
1004- "-p" , strconv .Itoa (a . sshLocalPort ),
1005- "127.0.0.1" ,
1011+ "-p" , strconv .Itoa (sshPort ),
1012+ sshAddress ,
10061013 "sh" , "-c" ,
10071014 `"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
10081015 sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1087,8 +1094,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10871094
10881095 finalArgs := a .sshConfig .Args ()
10891096 finalArgs = append (finalArgs ,
1090- "-p" , strconv .Itoa (a . sshLocalPort ),
1091- "127.0.0.1" ,
1097+ "-p" , strconv .Itoa (sshPort ),
1098+ sshAddress ,
10921099 "sudo" , "tail" , "-n" , "20" , "/var/log/cloud-init-output.log" ,
10931100 )
10941101
@@ -1128,11 +1135,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
11281135 return strings .HasPrefix (line , "cloud-init-main.service: consumed" )
11291136}
11301137
1131- func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote string ) error {
1138+ func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote string ) error {
11321139 args := sshConfig .Args ()
11331140 args = append (args ,
1134- "-p" , strconv .Itoa (port ),
1135- "127.0.0.1" ,
1141+ "-p" , strconv .Itoa (sshPort ),
1142+ sshAddress ,
11361143 "--" ,
11371144 )
11381145 args = append (args ,
0 commit comments