Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

incus: utilise macvlan network for multiple IP addresses #1124

Merged
merged 3 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 14 additions & 14 deletions environment/container/incus/incus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions environment/vm/lima/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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")
Expand Down Expand Up @@ -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 {
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
22 changes: 2 additions & 20 deletions environment/vm/lima/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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