User Tools

Site Tools


linux:macvlan

macvlan

With macvlan you can create virtual instances on top of your regular NIC with different MAC-Addresses.
macvlan is less documented but it works good and is easy to implement.

You can use macvlan in scenarios where you don't need the hole complexity of a bridged device,like networking in KVM environments.
macvlan is much more lightweight than a bridge.

macvlan is part of the GNU/Linux Kernel since version 3.0

Module

load macvlan module

  modprobe macvlan

If you want to load the module on every boot

  echo "macvlan" >> /etc/modules

Create a new macvlan

For this part you need iproute2 / (sometimes just called iproute) package installed

You can create the new interface with

  ip link add name $NAME_OF_VIRTUAL_NIC address $MAC_ADDRESS link $NAME_OF_TARGET_NIC type macvlan
This looks like 
  ip link add name eth0.1 address 52:54:00:cc:ee:aa link enp0s31f6 type macvlan

Lets have a look on our interfaces now

  [root@alphaframe ~]$ ip a
  [..]
  2: enp0s31f6: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether aa:bb:cc:dd:ee:ff brd ff:ff:ff:ff:ff:ff
    inet 172.20.0.10/24 brd 172.20.11.127 scope global dynamic enp0s31f6
    [...]
  3: eth0.1@enp0s31f6: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 52:54:00:cc:ee:aa brd ff:ff:ff:ff:ff:ff
  [...]

Make changes persisent

This part is based and working on CentOS 7.3.1611

Create interface on boot

  echo "ip link add name eth0.1 address 52:54:00:cc:ee:aa link enp0s31f6 type macvlan" > /sbin/ifup-pre-local2

Destroy interface on shutdown

  echo "ip link delete dev eth0.1" > /sbin/ifdown-local

Make changes persisent (systemd based systems)

Create a new Service File which contain the following:

[Unit]
Description=Start eth0.1 MACVLAN 
Wants=network.target
After=syslog.target network.target
 
[Service]
Type=oneshot
ExecStart=/sbin/ip link add name eth0.1 address 52:54:00:cc:ee:aa link enp0s31f6 type macvlan 
ExecStop=/sbin/ip link delete dev eth0.1
[Install]
WantedBy=multi-user.target

Save it as /etc/systemd/system/macvlan.service and enable it systemctl enable macvlan

Configure interface in KVM

In this case I use “Virtual Machine Manager” for handling my virtual machines
Now you can configure the “Virtual Network Interface”
select Host device eth0.1:macvtap as Network source, set the Source mode to Passthrough.

Know problems

if you boot your Virtual Machine and shut it down again, you can not turn it on again. Because the interface is already up and running.

You need to set the interface down, than you can boot your VM again.

  ip link set eth0.1 down
linux/macvlan.txt · Last modified: 2017/10/04 14:25 (external edit)