Intended to use ovs with lxc before, but lxc only support linux bridge:
int lxc_bridge_attach(const char *bridge, const char *ifname)
{
int fd, index, err;
struct ifreq ifr;if (strlen(ifname) >= IFNAMSIZ)
return -EINVAL;index = if_nametoindex(ifname);
if (!index)
return -EINVAL;process_lock();
fd = socket(AF_INET, SOCK_STREAM, 0);
process_unlock();
if (fd < 0)
return -errno;strncpy(ifr.ifr_name, bridge, IFNAMSIZ);
ifr.ifr_ifindex = index;
err = ioctl(fd, SIOCBRADDIF, &ifr);
process_lock();
close(fd);
process_unlock();
if (err)
err = -errno;return err;
}
here we can see only linux bridg supported, if you try to attach to ovs, it fails.
with help from a friend, had some changes here:
--- lxc-1.0.0~alpha1.orig/src/lxc/network.c 2013-09-11 06:22:00.000000000 +0800
+++ lxc-1.0.0~alpha1/src/lxc/network.c 2013-10-12 22:01:28.926679880 +0800
@@ -986,6 +986,9 @@
{
int fd, index, err;
struct ifreq ifr;
+ struct stat sb;
+ char brdirname[22 + IFNAMSIZ + 1] = {0};
+ char ovscmd[20 + IFNAMSIZ * 2 + 1] = {0};if (strlen(ifname) >= IFNAMSIZ)
return -EINVAL;
@@ -993,6 +996,15 @@
index = if_nametoindex(ifname);
if (!index)
return -EINVAL;
+ snprintf(brdirname, 22 +IFNAMSIZ + 1, "/sys/class/net/%s/bridge", bridge);
+ if (stat(brdirname, &sb) == -1) {
+ if (errno == ENOENT) {
+ snprintf(ovscmd, 20 + IFNAMSIZ * 2 + 1, "ovs-vsctl add-port %s %s", bridge, ifname);
+ err = system(ovscmd);
+ } else {
+ err = -EINVAL;
+ }
+ } else if (S_ISDIR(sb.st_mode)) {fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0)
@@ -1004,7 +1016,9 @@
close(fd);
if (err)
err = -errno;
-
+ } else {
+ err = -EINVAL;
+ }
return err;
}
This patch was made for lxc in ubuntu 13.10, I have it build in my ppa. binary package can be found from my ppa
ppa:zhengpeng-hou/dev.
So now, I have all my containers bridged to my home network directly.
https://docs.google.com/file/d/0BwNEP6aM800AMUhHcDdQb3B1NEU/edit?usp=sharing