@@ -251,7 +251,6 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
251251 instName : instName ,
252252 instSSHAddress : inst .SSHAddress ,
253253 sshConfig : sshConfig ,
254- portForwarder : newPortForwarder (sshConfig , sshLocalPort , rules , ignoreTCP , inst .VMType ),
255254 grpcPortForwarder : portfwd .NewPortForwarder (rules , ignoreTCP , ignoreUDP ),
256255 driver : limaDriver ,
257256 signalCh : signalCh ,
@@ -261,6 +260,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
261260 guestAgentAliveCh : make (chan struct {}),
262261 showProgress : o .showProgress ,
263262 }
263+ a .portForwarder = newPortForwarder (sshConfig , a .sshAddressPort , rules , ignoreTCP , inst .VMType )
264264 return a , nil
265265}
266266
@@ -671,7 +671,8 @@ sudo chown -R "${USER}" /run/host-services`
671671 }
672672 // Copy all config files _after_ the requirements are done
673673 for _ , rule := range a .instConfig .CopyToHost {
674- if err := copyToHost (ctx , a .sshConfig , a .sshLocalPort , rule .HostFile , rule .GuestFile ); err != nil {
674+ sshAddress , sshPort := a .sshAddressPort ()
675+ if err := copyToHost (ctx , a .sshConfig , sshAddress , sshPort , rule .HostFile , rule .GuestFile ); err != nil {
675676 errs = append (errs , err )
676677 }
677678 }
@@ -718,10 +719,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
718719 // Setup all socket forwards and defer their teardown
719720 if ! (a .driver .Info ().Features .DynamicSSHAddress ) {
720721 logrus .Debugf ("Forwarding unix sockets" )
722+ sshAddress , sshPort := a .sshAddressPort ()
721723 for _ , rule := range a .instConfig .PortForwards {
722724 if rule .GuestSocket != "" {
723725 local := hostAddress (rule , & guestagentapi.IPPort {})
724- _ = forwardSSH (ctx , a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbForward , rule .Reverse )
726+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbForward , rule .Reverse )
725727 }
726728 }
727729 }
@@ -732,17 +734,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
732734 a .cleanUp (func () error {
733735 logrus .Debugf ("Stop forwarding unix sockets" )
734736 var errs []error
737+ sshAddress , sshPort := a .sshAddressPort ()
735738 for _ , rule := range a .instConfig .PortForwards {
736739 if rule .GuestSocket != "" {
737740 local := hostAddress (rule , & guestagentapi.IPPort {})
738741 // using ctx.Background() because ctx has already been cancelled
739- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
742+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
740743 errs = append (errs , err )
741744 }
742745 }
743746 }
744747 if a .driver .ForwardGuestAgent () {
745- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
748+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
746749 errs = append (errs , err )
747750 }
748751 }
@@ -753,7 +756,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
753756 if a .instConfig .MountInotify != nil && * a .instConfig .MountInotify {
754757 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
755758 if a .driver .ForwardGuestAgent () {
756- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
759+ sshAddress , sshPort := a .sshAddressPort ()
760+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
757761 }
758762 }
759763 err := a .startInotify (ctx )
@@ -769,7 +773,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
769773 for {
770774 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
771775 if a .driver .ForwardGuestAgent () {
772- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
776+ sshAddress , sshPort := a .sshAddressPort ()
777+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
773778 }
774779 }
775780 client , err := a .getOrCreateClient (ctx )
@@ -793,6 +798,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
793798}
794799
795800func (a * HostAgent ) addStaticPortForwardsFromList (ctx context.Context , staticPortForwards []limatype.PortForward ) {
801+ sshAddress , sshPort := a .sshAddressPort ()
796802 for _ , rule := range staticPortForwards {
797803 if rule .GuestSocket == "" {
798804 guest := & guestagentapi.IPPort {
@@ -803,7 +809,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
803809 local , remote := a .portForwarder .forwardingAddresses (guest )
804810 if local != "" {
805811 logrus .Infof ("Setting up static TCP forwarding from %s to %s" , remote , local )
806- if err := forwardTCP (ctx , a .sshConfig , a . sshLocalPort , local , remote , verbForward ); err != nil {
812+ if err := forwardTCP (ctx , a .sshConfig , sshAddress , sshPort , local , remote , verbForward ); err != nil {
807813 logrus .WithError (err ).Warnf ("failed to set up static TCP forwarding %s -> %s" , remote , local )
808814 }
809815 }
@@ -913,11 +919,11 @@ const (
913919 verbCancel = "cancel"
914920)
915921
916- func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , command ... string ) error {
922+ func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , command ... string ) error {
917923 args := sshConfig .Args ()
918924 args = append (args ,
919- "-p" , strconv .Itoa (port ),
920- "127.0.0.1" ,
925+ "-p" , strconv .Itoa (sshPort ),
926+ sshAddress ,
921927 "--" ,
922928 )
923929 args = append (args , command ... )
@@ -928,7 +934,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
928934 return nil
929935}
930936
931- func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote , verb string , reverse bool ) error {
937+ func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote , verb string , reverse bool ) error {
932938 args := sshConfig .Args ()
933939 args = append (args ,
934940 "-T" ,
@@ -946,16 +952,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
946952 args = append (args ,
947953 "-N" ,
948954 "-f" ,
949- "-p" , strconv .Itoa (port ),
950- "127.0.0.1" ,
955+ "-p" , strconv .Itoa (sshPort ),
956+ sshAddress ,
951957 "--" ,
952958 )
953959 if strings .HasPrefix (local , "/" ) {
954960 switch verb {
955961 case verbForward :
956962 if reverse {
957963 logrus .Infof ("Forwarding %q (host) to %q (guest)" , local , remote )
958- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
964+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
959965 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) before setting up forwarding" , remote )
960966 }
961967 } else {
@@ -970,7 +976,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
970976 case verbCancel :
971977 if reverse {
972978 logrus .Infof ("Stopping forwarding %q (host) to %q (guest)" , local , remote )
973- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
979+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
974980 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after stopping forwarding" , remote )
975981 }
976982 } else {
@@ -991,7 +997,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
991997 if verb == verbForward && strings .HasPrefix (local , "/" ) {
992998 if reverse {
993999 logrus .WithError (err ).Warnf ("Failed to set up forward from %q (host) to %q (guest)" , local , remote )
994- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
1000+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
9951001 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after forwarding failed" , remote )
9961002 }
9971003 } else {
@@ -1025,10 +1031,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10251031 Active : true ,
10261032 })
10271033
1034+ sshAddress , sshPort := a .sshAddressPort ()
10281035 args := a .sshConfig .Args ()
10291036 args = append (args ,
1030- "-p" , strconv .Itoa (a . sshLocalPort ),
1031- "127.0.0.1" ,
1037+ "-p" , strconv .Itoa (sshPort ),
1038+ sshAddress ,
10321039 "sh" , "-c" ,
10331040 `"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
10341041 sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1113,8 +1120,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
11131120
11141121 finalArgs := a .sshConfig .Args ()
11151122 finalArgs = append (finalArgs ,
1116- "-p" , strconv .Itoa (a . sshLocalPort ),
1117- "127.0.0.1" ,
1123+ "-p" , strconv .Itoa (sshPort ),
1124+ sshAddress ,
11181125 "sudo" , "tail" , "-n" , "20" , "/var/log/cloud-init-output.log" ,
11191126 )
11201127
@@ -1154,11 +1161,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
11541161 return strings .HasPrefix (line , "cloud-init-main.service: consumed" )
11551162}
11561163
1157- func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote string ) error {
1164+ func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote string ) error {
11581165 args := sshConfig .Args ()
11591166 args = append (args ,
1160- "-p" , strconv .Itoa (port ),
1161- "127.0.0.1" ,
1167+ "-p" , strconv .Itoa (sshPort ),
1168+ sshAddress ,
11621169 "--" ,
11631170 )
11641171 args = append (args ,
0 commit comments