diff --git a/cmd/start.go b/cmd/start.go index 0c2d3d9f..d9b55c74 100644 --- a/cmd/start.go +++ b/cmd/start.go @@ -334,6 +334,10 @@ func setFixedConfigs(conf *config.Config) { warnIfNotEqual("volume mount type", conf.MountType, fixedConf.MountType) conf.MountType = fixedConf.MountType } + if fixedConf.Network.Address && !conf.Network.Address { + log.Warnln("network address cannot be disabled once enabled") + conf.Network.Address = true + } } func prepareConfig(cmd *cobra.Command) { diff --git a/environment/container/incus/incus.go b/environment/container/incus/incus.go index aab5d587..71ee1d67 100644 --- a/environment/container/incus/incus.go +++ b/environment/container/incus/incus.go @@ -237,30 +237,30 @@ func (c incusRuntime) registerNetworks() error { return fmt.Errorf("error listing networks: %w", err) } - networks := map[string]networkInfo{} + var network networkInfo + var found bool + name := limautil.NetInterface { // decode and flatten for easy lookup var resp []networkInfo if err := json.NewDecoder(strings.NewReader(b)).Decode(&resp); err != nil { return fmt.Errorf("error decoding networks into struct: %w", err) } for _, n := range resp { - networks[n.Name] = n + if n.Name == name { + network = n + found = true + } } } - for i := 0; i < limautil.VZNetworksMaxNo; i++ { - name := limautil.NetInterfaceName(i) - network, ok := networks[name] - - // must be an unmanaged physical network - if !ok || network.Managed || network.Type != "physical" { - continue - } + // must be an unmanaged physical network + if !found || network.Managed || network.Type != "physical" { + return nil + } - err := c.guest.RunQuiet("sudo", "incus", "network", "create", name, "--type", "physical", "parent="+name) - if err != nil { - return fmt.Errorf("error creating managed network '%s': %w", name, err) - } + err = c.guest.RunQuiet("sudo", "incus", "network", "create", name, "--type", "macvlan", "parent="+name) + if err != nil { + return fmt.Errorf("error creating managed network '%s': %w", name, err) } return nil diff --git a/environment/vm/lima/daemon.go b/environment/vm/lima/daemon.go index b7141454..87c9de52 100644 --- a/environment/vm/lima/daemon.go +++ b/environment/vm/lima/daemon.go @@ -9,20 +9,21 @@ import ( "github.com/abiosoft/colima/daemon" "github.com/abiosoft/colima/daemon/process/inotify" "github.com/abiosoft/colima/daemon/process/vmnet" + "github.com/abiosoft/colima/environment/container/incus" "github.com/abiosoft/colima/environment/vm/lima/limaconfig" "github.com/abiosoft/colima/util" ) func (l *limaVM) startDaemon(ctx context.Context, conf config.Config) (context.Context, error) { - isQEMU := conf.VMType == limaconfig.QEMU - isVZ := conf.VMType == limaconfig.VZ + // vmnet is used by QEMU and always used by incus (even with VZ) + useVmnet := conf.VMType == limaconfig.QEMU || conf.Runtime == incus.Name - // network daemon is only needed for qemu - conf.Network.Address = conf.Network.Address && isQEMU + // network daemon is only needed for vmnet + conf.Network.Address = conf.Network.Address && useVmnet - // limited to macOS (with Qemu driver) - // or vz with inotify enabled - if !util.MacOS() || (isVZ && !conf.MountINotify) { + // limited to macOS (with vmnet required) + // or with inotify enabled + if !util.MacOS() || (!conf.MountINotify && !conf.Network.Address) { return ctx, nil } @@ -48,7 +49,7 @@ func (l *limaVM) startDaemon(ctx context.Context, conf config.Config) (context.C } // add network processes to daemon - if isQEMU { + if useVmnet { a.Add(func() error { if conf.Network.Address { a.Stage("preparing network") @@ -104,7 +105,7 @@ func (l *limaVM) startDaemon(ctx context.Context, conf config.Config) (context.C // network failure is not fatal if err := a.Exec(); err != nil { - if isQEMU { + if useVmnet { func() { installed, _ := ctx.Value(networkInstalledKey).(bool) if !installed { diff --git a/environment/vm/lima/limautil/network.go b/environment/vm/lima/limautil/network.go index 98fd54cb..32e69a22 100644 --- a/environment/vm/lima/limautil/network.go +++ b/environment/vm/lima/limautil/network.go @@ -2,22 +2,11 @@ package limautil import ( "bytes" - "fmt" "strings" ) // network interfaces for shared network in the virtual machine. -const ( - NetInterface = "col0" - netInterfacePrefix = "col" - - VZNetworksMaxNo = 3 -) - -// NetInterfaceName returns the name of the network interface for the specified index. -func NetInterfaceName(index int) string { - return fmt.Sprintf("%s%d", netInterfacePrefix, index) -} +const NetInterface = "col0" // 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 c0525531..a6c041dd 100644 --- a/environment/vm/lima/yaml.go +++ b/environment/vm/lima/yaml.go @@ -130,20 +130,12 @@ func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err reachableIPAddress := true if conf.Network.Address { - if l.VMType == limaconfig.VZ { + // incus always uses vmnet + if l.VMType == limaconfig.VZ && conf.Runtime != incus.Name { l.Networks = append(l.Networks, limaconfig.Network{ VZNAT: true, Interface: limautil.NetInterface, }) - // special case for incus runtime - if conf.Runtime == incus.Name { - for i := 1; i < limautil.VZNetworksMaxNo; i++ { - l.Networks = append(l.Networks, limaconfig.Network{ - VZNAT: true, - Interface: limautil.NetInterfaceName(i), - }) - } - } } else { reachableIPAddress, _ = ctx.Value(daemon.CtxKey(vmnet.Name)).(bool) @@ -161,16 +153,6 @@ func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err Interface: limautil.NetInterface, }) - // special case for incus runtime - if conf.Runtime == incus.Name { - for i := 1; i < limautil.VZNetworksMaxNo; i++ { - l.Networks = append(l.Networks, limaconfig.Network{ - Socket: socketFile, - Interface: limautil.NetInterfaceName(i), - }) - } - } - return nil }(); err != nil { reachableIPAddress = false