LXC and macvlan, host to guest connection

By on 9 mai 2012, in Boulot, Informatique, Journal, Libre Software, Licorn®, LXC, System administration, Ubuntu

When you use the macvlan driver with LXC guests, they can communicate with each other with real ease. Great!

But in the default setup, the host won’t be able to communicate with the guests at all. Strangely enough, other machines on your LAN can communicate with the guests. All persons you ask will say the same thing to you: you have to create a macvlan interface on the host too, for it to be integrated with the bridge. Some make it rather complicated. Me, less. On Ubuntu 12.04 LTS, just edit /etc/network/interfaces, and add:

# you should already have an entry for eth0, I will not recopy it here.

auto macvlan0
iface macvlan0 inet dhcp
    # as eth0 and macvlan0 are on the same LAN, we must drop default route and LAN route
    # from eth0 configuration to avoid conflicts (this just slooooow down things).
    pre-up route del default
    # NOTE: adapt this line to your LAN address and netmask 
    pre-up route del -net 192.168.111.0 netmask 255.255.255.0
    pre-up ip link add link eth0 name macvlan0 type macvlan mode bridge

And then, sudo ifup macvlan0 if you don’t want to reboot. You can now ping your guests, provided they all have a network entry like this:

lxc.network.type=macvlan
lxc.network.macvlan.mode=bridge
lxc.network.link=eth0
lxc.network.flags=up
lxc.network.hwaddr = 00:16:xx:xx:xx:xx

Happy pinging, and other sort of network things ;-)

6
  • hayou

    Thanks for you help my friend !

  • S. H.

    This is what I had to do to get my routing sorted out. Hopefully it will help someone.

    auto macvlan0
    iface macvlan0 inet static
    address 10.20.4.7
    netmask 255.255.255.0
    network 10.20.4.0
    broadcast 10.20.4.255
    pre-up ip link add link eth0 name macvlan0 type macvlan mode bridge
    up route del -net 10.20.4.0 netmask 255.255.255.0 dev macvlan0
    up route add -host 10.20.4.8 dev macvlan0 # 10.20.4.8 is IP of LXC guest
    down route del -host 10.20.4.8 dev macvlan0 # 10.20.4.8 is IP of LXC guest
    post-down ip link del macvlan0

  • oxygen

    Thank you, simple and elegant

  • absolut

    Hi!
    Works ok but after sometime I loose connection.
    Any help please?
    Thanks

Trackbacks and Pingbacks

  • Configuration réseau de LXC | Linux Embedded

    […] macvlan en mode bridge : afin que les LXC puissent communiquer entre eux, il est possible de configurer le macvlan en mode bridge. Pour cela, il suffit d’ajouter l’option lxc.network.macvlan.mode = bridge dans le fichier de configuration. Ceci ne permettra toujours pas une communication des LXC avec la machine hôte. Pour cela, il faudra créer une interface macvlan sur l’hôte. Pour plus d’informations sur la marche à suivre, lisez cette article. […]

Reply