diff --git a/network/network.go b/network/network.go index c7560c04..014ba743 100644 --- a/network/network.go +++ b/network/network.go @@ -68,6 +68,14 @@ func SetDefaultGateway(ip, ifaceName string) error { return netlink.AddDefaultGw(ip, ifaceName) } +func SetInterfaceMac(name string, macaddr string) error { + iface, err := net.InterfaceByName(name) + if err != nil { + return err + } + return netlink.NetworkSetMacAddress(iface, macaddr) +} + func SetInterfaceIp(name string, rawIp string) error { iface, err := net.InterfaceByName(name) if err != nil { diff --git a/network/types.go b/network/types.go index ae098981..41b7aadb 100644 --- a/network/types.go +++ b/network/types.go @@ -17,6 +17,9 @@ type Network struct { // Prefix for the veth interfaces. VethPrefix string `json:"veth_prefix,omitempty"` + // MACAddress contains the MAC address to set on the network interface + MACAddress string `json:"mac_address,omitempty"` + // Address contains the IPv4 and mask to set on the network interface Address string `json:"address,omitempty"` diff --git a/network/veth.go b/network/veth.go index c38fe3d2..b9533c58 100644 --- a/network/veth.go +++ b/network/veth.go @@ -60,6 +60,11 @@ func (v *Veth) Initialize(config *Network, networkState *NetworkState) error { if err := ChangeInterfaceName(vethChild, defaultDevice); err != nil { return fmt.Errorf("change %s to %s %s", vethChild, defaultDevice, err) } + if config.MACAddress != "" { + if err := SetInterfaceMac(defaultDevice, config.MACAddress); err != nil { + return fmt.Errorf("set %s mac %s", defaultDevice, err) + } + } if err := SetInterfaceIp(defaultDevice, config.Address); err != nil { return fmt.Errorf("set %s ip %s", defaultDevice, err) }