@@ -244,7 +244,6 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
244244 instName : instName ,
245245 instSSHAddress : inst .SSHAddress ,
246246 sshConfig : sshConfig ,
247- portForwarder : newPortForwarder (sshConfig , sshLocalPort , rules , ignoreTCP , inst .VMType ),
248247 grpcPortForwarder : portfwd .NewPortForwarder (rules , ignoreTCP , ignoreUDP ),
249248 driver : limaDriver ,
250249 signalCh : signalCh ,
@@ -254,6 +253,7 @@ func New(ctx context.Context, instName string, stdout io.Writer, signalCh chan o
254253 guestAgentAliveCh : make (chan struct {}),
255254 showProgress : o .showProgress ,
256255 }
256+ a .portForwarder = newPortForwarder (sshConfig , a .sshAddressPort , rules , ignoreTCP , inst .VMType )
257257 return a , nil
258258}
259259
@@ -483,6 +483,12 @@ func (a *HostAgent) Info(_ context.Context) (*hostagentapi.Info, error) {
483483 return info , nil
484484}
485485
486+ func (a * HostAgent ) sshAddressPort () (sshAddress string , sshPort int ) {
487+ sshAddress = a .instSSHAddress
488+ sshPort = a .sshLocalPort
489+ return sshAddress , sshPort
490+ }
491+
486492func (a * HostAgent ) startHostAgentRoutines (ctx context.Context ) error {
487493 if * a .instConfig .Plain {
488494 msg := "Running in plain mode. Mounts, dynamic port forwarding, containerd, etc. will be ignored. Guest agent will not be running."
@@ -589,7 +595,8 @@ sudo chown -R "${USER}" /run/host-services`
589595 }
590596 // Copy all config files _after_ the requirements are done
591597 for _ , rule := range a .instConfig .CopyToHost {
592- if err := copyToHost (ctx , a .sshConfig , a .sshLocalPort , rule .HostFile , rule .GuestFile ); err != nil {
598+ sshAddress , sshPort := a .sshAddressPort ()
599+ if err := copyToHost (ctx , a .sshConfig , sshAddress , sshPort , rule .HostFile , rule .GuestFile ); err != nil {
593600 errs = append (errs , err )
594601 }
595602 }
@@ -636,10 +643,11 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
636643 // Setup all socket forwards and defer their teardown
637644 if ! (a .driver .Info ().Features .SkipSocketForwarding ) {
638645 logrus .Debugf ("Forwarding unix sockets" )
646+ sshAddress , sshPort := a .sshAddressPort ()
639647 for _ , rule := range a .instConfig .PortForwards {
640648 if rule .GuestSocket != "" {
641649 local := hostAddress (rule , & guestagentapi.IPPort {})
642- _ = forwardSSH (ctx , a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbForward , rule .Reverse )
650+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbForward , rule .Reverse )
643651 }
644652 }
645653 }
@@ -650,17 +658,18 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
650658 a .cleanUp (func () error {
651659 logrus .Debugf ("Stop forwarding unix sockets" )
652660 var errs []error
661+ sshAddress , sshPort := a .sshAddressPort ()
653662 for _ , rule := range a .instConfig .PortForwards {
654663 if rule .GuestSocket != "" {
655664 local := hostAddress (rule , & guestagentapi.IPPort {})
656665 // using ctx.Background() because ctx has already been cancelled
657- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
666+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , local , rule .GuestSocket , verbCancel , rule .Reverse ); err != nil {
658667 errs = append (errs , err )
659668 }
660669 }
661670 }
662671 if a .driver .ForwardGuestAgent () {
663- if err := forwardSSH (context .Background (), a .sshConfig , a . sshLocalPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
672+ if err := forwardSSH (context .Background (), a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbCancel , false ); err != nil {
664673 errs = append (errs , err )
665674 }
666675 }
@@ -671,7 +680,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
671680 if a .instConfig .MountInotify != nil && * a .instConfig .MountInotify {
672681 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
673682 if a .driver .ForwardGuestAgent () {
674- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
683+ sshAddress , sshPort := a .sshAddressPort ()
684+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
675685 }
676686 }
677687 err := a .startInotify (ctx )
@@ -687,7 +697,8 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
687697 for {
688698 if a .client == nil || ! isGuestAgentSocketAccessible (ctx , a .client ) {
689699 if a .driver .ForwardGuestAgent () {
690- _ = forwardSSH (ctx , a .sshConfig , a .sshLocalPort , localUnix , remoteUnix , verbForward , false )
700+ sshAddress , sshPort := a .sshAddressPort ()
701+ _ = forwardSSH (ctx , a .sshConfig , sshAddress , sshPort , localUnix , remoteUnix , verbForward , false )
691702 }
692703 }
693704 client , err := a .getOrCreateClient (ctx )
@@ -711,6 +722,7 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
711722}
712723
713724func (a * HostAgent ) addStaticPortForwardsFromList (ctx context.Context , staticPortForwards []limatype.PortForward ) {
725+ sshAddress , sshPort := a .sshAddressPort ()
714726 for _ , rule := range staticPortForwards {
715727 if rule .GuestSocket == "" {
716728 guest := & guestagentapi.IPPort {
@@ -721,7 +733,7 @@ func (a *HostAgent) addStaticPortForwardsFromList(ctx context.Context, staticPor
721733 local , remote := a .portForwarder .forwardingAddresses (guest )
722734 if local != "" {
723735 logrus .Infof ("Setting up static TCP forwarding from %s to %s" , remote , local )
724- if err := forwardTCP (ctx , a .sshConfig , a . sshLocalPort , local , remote , verbForward ); err != nil {
736+ if err := forwardTCP (ctx , a .sshConfig , sshAddress , sshPort , local , remote , verbForward ); err != nil {
725737 logrus .WithError (err ).Warnf ("failed to set up static TCP forwarding %s -> %s" , remote , local )
726738 }
727739 }
@@ -832,11 +844,11 @@ const (
832844 verbCancel = "cancel"
833845)
834846
835- func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , command ... string ) error {
847+ func executeSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , command ... string ) error {
836848 args := sshConfig .Args ()
837849 args = append (args ,
838- "-p" , strconv .Itoa (port ),
839- "127.0.0.1" ,
850+ "-p" , strconv .Itoa (sshPort ),
851+ sshAddress ,
840852 "--" ,
841853 )
842854 args = append (args , command ... )
@@ -847,7 +859,7 @@ func executeSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, command
847859 return nil
848860}
849861
850- func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote , verb string , reverse bool ) error {
862+ func forwardSSH (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote , verb string , reverse bool ) error {
851863 args := sshConfig .Args ()
852864 args = append (args ,
853865 "-T" ,
@@ -865,16 +877,16 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
865877 args = append (args ,
866878 "-N" ,
867879 "-f" ,
868- "-p" , strconv .Itoa (port ),
869- "127.0.0.1" ,
880+ "-p" , strconv .Itoa (sshPort ),
881+ sshAddress ,
870882 "--" ,
871883 )
872884 if strings .HasPrefix (local , "/" ) {
873885 switch verb {
874886 case verbForward :
875887 if reverse {
876888 logrus .Infof ("Forwarding %q (host) to %q (guest)" , local , remote )
877- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
889+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
878890 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) before setting up forwarding" , remote )
879891 }
880892 } else {
@@ -889,7 +901,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
889901 case verbCancel :
890902 if reverse {
891903 logrus .Infof ("Stopping forwarding %q (host) to %q (guest)" , local , remote )
892- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
904+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
893905 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after stopping forwarding" , remote )
894906 }
895907 } else {
@@ -910,7 +922,7 @@ func forwardSSH(ctx context.Context, sshConfig *ssh.SSHConfig, port int, local,
910922 if verb == verbForward && strings .HasPrefix (local , "/" ) {
911923 if reverse {
912924 logrus .WithError (err ).Warnf ("Failed to set up forward from %q (host) to %q (guest)" , local , remote )
913- if err := executeSSH (ctx , sshConfig , port , "rm" , "-f" , remote ); err != nil {
925+ if err := executeSSH (ctx , sshConfig , sshAddress , sshPort , "rm" , "-f" , remote ); err != nil {
914926 logrus .WithError (err ).Warnf ("Failed to clean up %q (guest) after forwarding failed" , remote )
915927 }
916928 } else {
@@ -944,10 +956,11 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
944956 Active : true ,
945957 })
946958
959+ sshAddress , sshPort := a .sshAddressPort ()
947960 args := a .sshConfig .Args ()
948961 args = append (args ,
949- "-p" , strconv .Itoa (a . sshLocalPort ),
950- "127.0.0.1" ,
962+ "-p" , strconv .Itoa (sshPort ),
963+ sshAddress ,
951964 "sh" , "-c" ,
952965 `"if command -v systemctl >/dev/null 2>&1 && systemctl is-enabled -q cloud-init-main.service; then
953966 sudo journalctl -u cloud-init-main.service -b -S @0 -o cat -f
@@ -1032,8 +1045,8 @@ func (a *HostAgent) watchCloudInitProgress(ctx context.Context) {
10321045
10331046 finalArgs := a .sshConfig .Args ()
10341047 finalArgs = append (finalArgs ,
1035- "-p" , strconv .Itoa (a . sshLocalPort ),
1036- "127.0.0.1" ,
1048+ "-p" , strconv .Itoa (sshPort ),
1049+ sshAddress ,
10371050 "sudo" , "tail" , "-n" , "20" , "/var/log/cloud-init-output.log" ,
10381051 )
10391052
@@ -1073,11 +1086,11 @@ func isDeactivatedCloudInitMainService(line string) bool {
10731086 return strings .HasPrefix (line , "cloud-init-main.service: consumed" )
10741087}
10751088
1076- func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , port int , local , remote string ) error {
1089+ func copyToHost (ctx context.Context , sshConfig * ssh.SSHConfig , sshAddress string , sshPort int , local , remote string ) error {
10771090 args := sshConfig .Args ()
10781091 args = append (args ,
1079- "-p" , strconv .Itoa (port ),
1080- "127.0.0.1" ,
1092+ "-p" , strconv .Itoa (sshPort ),
1093+ sshAddress ,
10811094 "--" ,
10821095 )
10831096 args = append (args ,
0 commit comments