Skip to content

Instantly share code, notes, and snippets.

@michaelfung
Last active July 6, 2023 07:00
Show Gist options
  • Save michaelfung/fe5b46ae52e4821142af6e8008f155d2 to your computer and use it in GitHub Desktop.
Save michaelfung/fe5b46ae52e4821142af6e8008f155d2 to your computer and use it in GitHub Desktop.
macvlan on boot

Setup macvlan on boot

Tested to work on:

  • Ubuntu 23.04

Reference:

[https://fabianlee.org/2022/09/20/kvm-creating-a-bridged-network-with-netplan-on-ubuntu-22-04] [https://www.furorteutonicus.eu/2013-08-04-enabling-host-guest-networking-with-kvm-macvlan-and-macvtap]

Why

Make life easy for managing VMs and containers in my home lab.

All VMs and containers will get IP addresses of the same network segment as the host, and reachable from the host.

Switch to systemd networkd

If using NetworkManager, need to switch to systemd-networkd to manage network.

First, remove NetworkManager from netplan config in /etc/netplan/ folder. Then add a yaml config file to use networkd (e.g. /etc/netplan/01-networkd.yaml):

network:
  version: 2
  renderer: networkd

  ethernets:
    enp3s0:
      dhcp4: false
      dhcp6: false

Then use netplan apply to make changes effective.

Then perform the following 4 steps:

  1. enable as services on startup
    sudo systemctl enable systemd-resolved.service
    sudo systemctl enable systemd-networkd.service
  1. start services
    sudo systemctl start systemd-resolved.service
    sudo systemctl start systemd-networkd.service
  1. check status
    systemctl status systemd-resolved.service
    systemctl status systemd-networkd.service
  1. disable NetworkManager
    sudo systemctl disable NetworkManager.service
    sudo systemctl stop NetworkManager.service

Setup macvlan bridge with a script

Put the setup script under /etc/networkd-dispatcher/configured.d/:

#!/bin/bash
# filename: 10-setup-macvlan.sh

# basic net config, customize for my home network
HWLINK=enp3s0
MACVLN=macvlan0
IP=192.168.0.35/24
NETWORK=192.168.0.0/24
GATEWAY=192.168.0.254

# ------------
# setting up $MACVLN interface
# ------------
echo "# setting up $MACVLN interface..."
ip link add link $HWLINK $MACVLN type macvlan mode bridge
ip address add $IP dev $MACVLN
ip link set dev $MACVLN up

# ------------
# routing table
# ------------

# empty routes
echo "# empty routes..."
ip route flush dev $HWLINK
ip route flush dev $MACVLN

# add routes
echo "# add routes..."
ip route add $NETWORK dev $MACVLN metric 0

# add the default gateway
echo "# add the default gateway..."
ip route add default via $GATEWAY

Reboot to make effective.

If things are okay, you will get something like following:

xxx: $ ip link 
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
<snip>
6: macvlan0@enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mode DEFAULT group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
<snip>

xxx: $ ip a show macvlan0
6: macvlan0@enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.35/24 scope global macvlan0
       valid_lft forever preferred_lft forever
    inet6 xx--xx:9950/64 scope global temporary dynamic 
       valid_lft 86392sec preferred_lft 14392sec
    inet6 xx--xx:84f5/64 scope global dynamic mngtmpaddr 
       valid_lft 86392sec preferred_lft 14392sec
    inet6 fe80::xx--xx:84f5/64 scope link 
       valid_lft forever preferred_lft forever

LXD profile example

config:
  limits.cpu: "2"
  limits.memory: 4GiB
description: LXD profile that use macvlan
devices:
  eth0:
    nictype: macvlan
    parent: macvlan0
    type: nic
  root:
    path: /
    pool: pool2
    type: disk
name: macvlan-profile
used_by:
- /1.0/instances/win10
- /1.0/instances/d7vlan
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment