diff --git a/cmd/start.go b/cmd/start.go index 40c9d4ee..b8a92f92 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -191,6 +191,7 @@ func init() { startCmd.Flags().BoolVar(&startCmdArgs.Network.Address, "network-address", false, "assign reachable IP address to the VM") startCmd.Flags().StringVar(&startCmdArgs.Network.Mode, "network-mode", "shared", "network mode (shared, bridged)") startCmd.Flags().StringVar(&startCmdArgs.Network.BridgeInterface, "network-interface", "en0", "host network interface to use for bridged mode") + startCmd.Flags().BoolVar(&startCmdArgs.Network.PreferredRoute, "network-preferred-route", false, "use the assigned IP address as the preferred route for the VM (implies --network-address)") // vm type if util.MacOS13OrNewer() { @@ -535,6 +536,9 @@ func prepareConfig(cmd *cobra.Command) { if !cmd.Flag("network-interface").Changed { startCmdArgs.Network.BridgeInterface = current.Network.BridgeInterface } + if !cmd.Flag("network-preferred-route").Changed { + startCmdArgs.Network.PreferredRoute = current.Network.PreferredRoute + } if util.MacOS13OrNewer() { if !cmd.Flag("vm-type").Changed { startCmdArgs.VMType = current.VMType diff --git a/config/config.go b/config/config.go index 52b35cef..71b2a487 100644 --- a/config/config.go +++ b/config/config.go @@ -84,6 +84,7 @@ type Network struct { HostAddresses bool `yaml:"hostAddresses"` Mode string `yaml:"mode"` // shared, bridged BridgeInterface string `yaml:"interface"` + PreferredRoute bool `yaml:"preferredRoute"` } // Mount is volume mount diff --git a/embedded/defaults/colima.yaml b/embedded/defaults/colima.yaml index 9257b446..8d1953b0 100644 --- a/embedded/defaults/colima.yaml +++ b/embedded/defaults/colima.yaml @@ -70,6 +70,11 @@ network: # Default: en0 interface: en0 + # Use the assigned IP address as the preferred route for the VM. + # Note: this only has an effect when `address` is set to true. + # Default: false + preferredRoute: false + # Custom DNS resolvers for the virtual machine. # # EXAMPLE diff --git a/environment/vm/lima/limautil/network.go b/environment/vm/lima/limautil/network.go index 9fe0839b..c2ceefac 100644 --- a/environment/vm/lima/limautil/network.go +++ b/environment/vm/lima/limautil/network.go @@ -9,7 +9,8 @@ import ( const NetInterface = "col0" // network metric for the route -const NetMetric = 300 +const NetMetric uint32 = 300 +const NetMetricPreferred uint32 = 100 // IPAddress returns the ip address for profile. // It returns the PTP address if networking is enabled or falls back to 127.0.0.1. diff --git a/environment/vm/lima/yaml.go b/environment/vm/lima/yaml.go index ed288f98..05de5e49 100644 --- a/environment/vm/lima/yaml.go +++ b/environment/vm/lima/yaml.go @@ -133,12 +133,16 @@ func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err reachableIPAddress := true if conf.Network.Address { + metric := limautil.NetMetric + if conf.Network.PreferredRoute { + metric = limautil.NetMetricPreferred + } // vmnet is always used for incus runtime or bridged mode if l.VMType == limaconfig.VZ && conf.Runtime != incus.Name && conf.Network.Mode != "bridged" { l.Networks = append(l.Networks, limaconfig.Network{ VZNAT: true, Interface: limautil.NetInterface, - Metric: limautil.NetMetric, + Metric: metric, }) } else { reachableIPAddress, _ = ctx.Value(daemon.CtxKey(vmnet.Name)).(bool) @@ -155,7 +159,7 @@ func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err l.Networks = append(l.Networks, limaconfig.Network{ Socket: socketFile, Interface: limautil.NetInterface, - Metric: limautil.NetMetric, + Metric: metric, }) return nil