diff --git a/environment/vm/lima/lima.go b/environment/vm/lima/lima.go index c2980a436..d8c198e04 100644 --- a/environment/vm/lima/lima.go +++ b/environment/vm/lima/lima.go @@ -374,6 +374,14 @@ func (l *limaVM) addPostStartActions(a *cli.ActiveCommandChain, conf config.Conf return nil }) + // replicate addresses + a.Add(func() error { + if err := l.replicateHostAddresses(); err != nil { + logrus.Warnln(fmt.Errorf("unable to assign host IP addresses to the VM: %w", err)) + } + return nil + }) + // preserve state a.Add(func() error { if err := configmanager.SaveToFile(conf, config.CurrentProfile().StateFile()); err != nil { diff --git a/environment/vm/lima/network.go b/environment/vm/lima/network.go index 0d353a7e0..ce49046ad 100644 --- a/environment/vm/lima/network.go +++ b/environment/vm/lima/network.go @@ -7,6 +7,7 @@ import ( "github.com/abiosoft/colima/embedded" "github.com/abiosoft/colima/environment/vm/lima/limautil" + "github.com/abiosoft/colima/util" ) func (l *limaVM) writeNetworkFile() error { @@ -24,3 +25,12 @@ func (l *limaVM) writeNetworkFile() error { } return nil } + +func (l *limaVM) replicateHostAddresses() error { + for _, ip := range util.HostIPAddresses() { + if err := l.RunQuiet("sudo", "ip", "address", "add", ip.String()+"/24", "dev", "lo"); err != nil { + return err + } + } + return nil +} diff --git a/environment/vm/lima/yaml.go b/environment/vm/lima/yaml.go index 2c1acc3ea..ba3280b90 100644 --- a/environment/vm/lima/yaml.go +++ b/environment/vm/lima/yaml.go @@ -240,6 +240,19 @@ func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err Proto: limaconfig.TCP, }, ) + + // bind all host addresses + for _, ip := range util.HostIPAddresses() { + l.PortForwards = append(l.PortForwards, + limaconfig.PortForward{ + GuestIP: ip, + GuestPortRange: [2]int{1, 65535}, + HostIP: ip, + HostPortRange: [2]int{1, 65535}, + Proto: limaconfig.TCP, + }, + ) + } } switch strings.ToLower(conf.MountType) { diff --git a/util/util.go b/util/util.go index 3284f21cd..33823ea46 100644 --- a/util/util.go +++ b/util/util.go @@ -35,6 +35,25 @@ func RandomAvailablePort() int { return listener.Addr().(*net.TCPAddr).Port } +// HostIPAddresses returns all IPv4 addresses on the host. +func HostIPAddresses() []net.IP { + var addresses []net.IP + ints, err := net.InterfaceAddrs() + if err != nil { + return nil + } + for i := range ints { + split := strings.Split(ints[i].String(), "/") + addr := net.ParseIP(split[0]).To4() + // ignore default loopback + if addr != nil && addr.String() != "127.0.0.1" { + addresses = append(addresses, addr) + } + } + + return addresses +} + // ShellSplit splits cmd into arguments using. func ShellSplit(cmd string) []string { split, err := shlex.Split(cmd)