Skip to content

Instantly share code, notes, and snippets.

@llazzaro
Created July 13, 2016 04:00
Show Gist options
  • Save llazzaro/c0232e70ae95786079b11485b9966ea2 to your computer and use it in GitHub Desktop.
Save llazzaro/c0232e70ae95786079b11485b9966ea2 to your computer and use it in GitHub Desktop.
Assigning VLANs to LXC containers connected to OpenVswitch

I followed the instructions from http://blog.scottlowe.org/2014/01/23/automatically-connecting-lxc-to-open-vswitch/ but wasn't happy with the script

# pip install shyaml
# cat /var/lib/lxc/vm1/config
# Distribution configuration
lxc.include = /usr/share/lxc/config/ubuntu.common.conf
lxc.arch = x86_64

# Container specific configuration
lxc.rootfs = /var/lib/lxc/vm1/rootfs
lxc.utsname = vm1

# Network configuration
lxc.network.type = veth
lxc.network.veth.pair = veth-vmone

lxc.network.script.up = /etc/lxc/ovsup
lxc.network.script.down = /etc/lxc/ovsdown
# cat /etc/lxc/ovsup
#!/bin/bash

BRIDGE=`cat /etc/lxc/ovs_cfg | shyaml get-value $1.bridge`
VLAN=`cat /etc/lxc/ovs_cfg | shyaml get-value $1.vlan`

ovs-vsctl --may-exist add-br $BRIDGE
ovs-vsctl --if-exists del-port $BRIDGE $5
ovs-vsctl --may-exist add-port $BRIDGE $5
ovs-vsctl set port $5 tag=$VLAN
# cat /etc/lxc/ovsdown
#!/bin/bash

BRIDGE=`cat /etc/lxc/ovs_cfg | shyaml get-value $1.bridge`

ovs-vsctl --if-exists del-port $BRIDGE $5
# cat /etc/lxc/cat ovs_cfg
vm1:
  bridge: containers0
  vlan: 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment