@@ -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
@@ -657,7 +657,8 @@ sudo chown -R "${USER}" /run/host-services`
657657 }
658658 // Copy all config files _after_ the requirements are done
659659 for _ , rule := range a .instConfig .CopyToHost {
660- if err := copyToHost (ctx , a .sshConfig , a .sshLocalPort , rule .HostFile , rule .GuestFile ); err != nil {
660+ sshAddress , sshPort := a .sshAddressPort ()
661+ if err := copyToHost (ctx , a .sshConfig , sshAddress , sshPort , rule .HostFile , rule .GuestFile ); err != nil {
661662 errs = append (errs , err )
662663 }
663664 }
@@ -704,10 +705,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
704705 // Setup all socket forwards and defer their teardown
705706 if ! (a .driver .Info ().Features .SkipSocketForwarding ) {
706707 logrus .Debugf ("Forwarding unix sockets" )
708+ sshAddress , sshPort := a .sshAddressPort ()
707709 for _ , rule := range a .instConfig .PortForwards {
708710 if rule .GuestSocket != "" {
709711 local := hostAddress (rule , & guestagentapi.IPPort {})
710- _ = forwardSSH (ctx , a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbForward , rule .Reverse )
712+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbForward , rule .Reverse )
711713 }
712714 }
713715 }
@@ -718,17 +720,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
718720 a .cleanUp (func () error {
719721 logrus .Debugf ("Stop forwarding unix sockets" )
720722 var errs []error
723+ sshAddress , sshPort := a .sshAddressPort ()
721724 for _ , rule := range a .instConfig .PortForwards {
722725 if rule .GuestSocket != "" {
723726 local := hostAddress (rule , & guestagentapi.IPPort {})
724727 // using ctx.Background() because ctx has already been cancelled
725- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
728+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
726729 errs = append (errs , err )
727730 }
728731 }
729732 }
730733 if a .driver .ForwardGuestAgent () {
731- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
734+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
732735 errs = append (errs , err )
733736 }
734737 }
@@ -739,7 +742,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
739742 if a .instConfig .MountInotify != nil && * a .instConfig .MountInotify {
740743 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
741744 if a .driver .ForwardGuestAgent () {
742- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
745+ sshAddress , sshPort := a .sshAddressPort ()
746+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
743747 }
744748 }
745749 err := a .startInotify (ctx )
@@ -755,7 +759,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
755759 for {
756760 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
757761 if a .driver .ForwardGuestAgent () {
758- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
762+ sshAddress , sshPort := a .sshAddressPort ()
763+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
759764 }
760765 }
761766 client , err := a .getOrCreateClient (ctx )
@@ -779,6 +784,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
779784}
780785
781786func (a * HostAgent ) addStaticPortForwardsFromList (ctx context.Context , staticPortForwards []limatype.PortForward ) {
787+ sshAddress , sshPort := a .sshAddressPort ()
782788 for _ , rule := range staticPortForwards {
783789 if rule .GuestSocket == "" {
784790 guest := & guestagentapi.IPPort {
@@ -789,7 +795,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
789795 local , remote := a .portForwarder .forwardingAddresses (guest )
790796 if local != "" {
791797 logrus .Infof ("Setting up static TCP forwarding from %s to %s" , remote , local )
792- if err := forwardTCP (ctx , a .sshConfig , a . sshLocalPort , local , remote , verbForward ); err != nil {
798+ if err := forwardTCP (ctx , a .sshConfig , sshAddress , sshPort , local , remote , verbForward ); err != nil {
793799 logrus .WithError (err ).Warnf ("failed to set up static TCP forwarding %s -> %s" , remote , local )
794800 }
795801 }
@@ -899,11 +905,11 @@ const (
899905 verbCancel = "cancel"
900906)
901907
902- func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , command ... string ) error {
908+ func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , command ... string ) error {
903909 args := sshConfig .Args ()
904910 args = append (args ,
905- "-p" , strconv .Itoa (port ),
906- "127.0.0.1" ,
911+ "-p" , strconv .Itoa (sshPort ),
912+ sshAddress ,
907913 "--" ,
908914 )
909915 args = append (args , command ... )
@@ -914,7 +920,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
914920 return nil
915921}
916922
917- func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote , verb string , reverse bool ) error {
923+ func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote , verb string , reverse bool ) error {
918924 args := sshConfig .Args ()
919925 args = append (args ,
920926 "-T" ,
@@ -932,16 +938,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
932938 args = append (args ,
933939 "-N" ,
934940 "-f" ,
935- "-p" , strconv .Itoa (port ),
936- "127.0.0.1" ,
941+ "-p" , strconv .Itoa (sshPort ),
942+ sshAddress ,
937943 "--" ,
938944 )
939945 if strings .HasPrefix (local , "/" ) {
940946 switch verb {
941947 case verbForward :
942948 if reverse {
943949 logrus .Infof ("Forwarding %q (host) to %q (guest)" , local , remote )
944- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
950+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
945951 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) before setting up forwarding" , remote )
946952 }
947953 } else {
@@ -956,7 +962,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
956962 case verbCancel :
957963 if reverse {
958964 logrus .Infof ("Stopping forwarding %q (host) to %q (guest)" , local , remote )
959- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
965+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
960966 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after stopping forwarding" , remote )
961967 }
962968 } else {
@@ -977,7 +983,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
977983 if verb == verbForward && strings .HasPrefix (local , "/" ) {
978984 if reverse {
979985 logrus .WithError (err ).Warnf ("Failed to set up forward from %q (host) to %q (guest)" , local , remote )
980- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
986+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
981987 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after forwarding failed" , remote )
982988 }
983989 } else {
@@ -1011,10 +1017,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10111017 Active : true ,
10121018 })
10131019
1020+ sshAddress , sshPort := a .sshAddressPort ()
10141021 args := a .sshConfig .Args ()
10151022 args = append (args ,
1016- "-p" , strconv .Itoa (a . sshLocalPort ),
1017- "127.0.0.1" ,
1023+ "-p" , strconv .Itoa (sshPort ),
1024+ sshAddress ,
10181025 "sh" , "-c" ,
10191026 `"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
10201027 sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1099,8 +1106,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10991106
11001107 finalArgs := a .sshConfig .Args ()
11011108 finalArgs = append (finalArgs ,
1102- "-p" , strconv .Itoa (a . sshLocalPort ),
1103- "127.0.0.1" ,
1109+ "-p" , strconv .Itoa (sshPort ),
1110+ sshAddress ,
11041111 "sudo" , "tail" , "-n" , "20" , "/var/log/cloud-init-output.log" ,
11051112 )
11061113
@@ -1140,11 +1147,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
11401147 return strings .HasPrefix (line , "cloud-init-main.service: consumed" )
11411148}
11421149
1143- func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote string ) error {
1150+ func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote string ) error {
11441151 args := sshConfig .Args ()
11451152 args = append (args ,
1146- "-p" , strconv .Itoa (port ),
1147- "127.0.0.1" ,
1153+ "-p" , strconv .Itoa (sshPort ),
1154+ sshAddress ,
11481155 "--" ,
11491156 )
11501157 args = append (args ,
0 commit comments