From 67330d019ce958fa91bc1a2f3df8e8eaf7feec73 Mon Sep 17 00:00:00 2001 From: Abiola Ibrahim Date: Sun, 25 Aug 2024 22:26:59 +0100 Subject: [PATCH] incus: utilise macvlan network for multiple IP addresses Signed-off-by: Abiola Ibrahim --- environment/container/incus/incus.go | 32 +++++++++++++++---------- environment/vm/lima/limautil/network.go | 13 +--------- environment/vm/lima/yaml.go | 19 --------------- 3 files changed, 20 insertions(+), 44 deletions(-) diff --git a/environment/container/incus/incus.go b/environment/container/incus/incus.go index aab5d587..0497193e 100644 --- a/environment/container/incus/incus.go +++ b/environment/container/incus/incus.go @@ -237,30 +237,36 @@ 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 !found || network.Managed || network.Type != "physical" { + return nil + } - // must be an unmanaged physical network - if !ok || network.Managed || network.Type != "physical" { - continue - } + 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) + } - 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) - } + // set as default network + err = c.guest.RunQuiet("sudo", "incus", "profile", "device", "set", "default", "eth0", "network="+name) + if err != nil { + return fmt.Errorf("error setting managed network '%s' as default: %w", name, err) } return nil 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..5d45ae8a 100644 --- a/environment/vm/lima/yaml.go +++ b/environment/vm/lima/yaml.go @@ -135,15 +135,6 @@ func newConf(ctx context.Context, conf config.Config) (l limaconfig.Config, err 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 +152,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