Skip to content

Commit

Permalink
incus: utilise macvlan network for multiple IP addresses
Browse files Browse the repository at this point in the history
Signed-off-by: Abiola Ibrahim <[email protected]>
  • Loading branch information
abiosoft committed Aug 26, 2024
1 parent c2595d4 commit 67330d0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
32 changes: 19 additions & 13 deletions environment/container/incus/incus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 1 addition & 12 deletions environment/vm/lima/limautil/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 0 additions & 19 deletions environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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
Expand Down

0 comments on commit 67330d0

Please sign in to comment.