[libvirt] [PATCH] Network: LXC generic ethernet interface

Takayuki Usui takayuki at midokura.jp
Mon Sep 24 01:48:44 UTC 2012


The patch adds generic ethernet type to LXC, that allows you to make
an arbitrary virtual network interface and associate it to an LXC
domain. That does not, however, provide a means to execute a script,
that is still available only for QEMU.

    1. Create a virtual ethernet device pair

        # ip link add type veth (e.g. veth0 and veth1 by default)

    2. Define a LXC domain and associate it to one end of the pair

        [...]
        <interface type='ethernet'>
          <target dev='veth1'/>
        </interface>

    3. Do whatever you like, for example, add another end of the pair
       to a bridge (e.g. br0)

        # brctr addif br0 veth0
        # ip link set dev veth0 up

    4. Start a LXC domain. The interface shows up as eth0 in the domain.

Signed-off-by: Takayuki Usui <takayuki at midokura.jp>
---
 src/lxc/lxc_process.c |   50 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/src/lxc/lxc_process.c b/src/lxc/lxc_process.c
index 84128d1..0b86241 100644
--- a/src/lxc/lxc_process.c
+++ b/src/lxc/lxc_process.c
@@ -419,6 +419,46 @@ cleanup:
 }
 
 
+static int lxcSetupInterfaceEthernet(virConnectPtr conn ATTRIBUTE_UNUSED,
+                                     virDomainDefPtr def ATTRIBUTE_UNUSED,
+                                     virDomainNetDefPtr net,
+                                     unsigned int *nveths,
+                                     char ***veths)
+{
+    int ret = -1;
+    char *containerVeth = NULL;
+    
+    if (net->ifname == 0 || strlen(net->ifname) == 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
+                       _("Ethernet target device is required"));
+        goto cleanup;
+    }
+
+    if (VIR_ALLOC_N(containerVeth, strlen(net->ifname) + 1) < 0) {
+        virReportOOMError();
+        goto cleanup;
+    }
+    if (VIR_REALLOC_N(*veths, (*nveths)+1) < 0) {
+        virReportOOMError();
+        VIR_FREE(containerVeth);
+        goto cleanup;
+    }
+    if (virStrcpy(containerVeth, net->ifname,
+                  strlen(net->ifname) + 1) == NULL) {
+        virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
+                       _("Ethernet target device name is too long"));
+        VIR_FREE(containerVeth);
+        goto cleanup;
+    }
+    (*veths)[(*nveths)] = containerVeth;
+    (*nveths)++;
+
+    ret = 0;
+
+cleanup:
+    return ret;
+}
+
 /**
  * virLXCProcessSetupInterfaces:
  * @conn: pointer to connection
@@ -522,8 +562,16 @@ static int virLXCProcessSetupInterfaces(virConnectPtr conn,
                 goto cleanup;
             break;
 
-        case VIR_DOMAIN_NET_TYPE_USER:
         case VIR_DOMAIN_NET_TYPE_ETHERNET:
+            if (lxcSetupInterfaceEthernet(conn,
+                                          def,
+                                          def->nets[i],
+                                          nveths,
+                                          veths) < 0)
+                 goto cleanup;
+            break;
+
+        case VIR_DOMAIN_NET_TYPE_USER:
         case VIR_DOMAIN_NET_TYPE_SERVER:
         case VIR_DOMAIN_NET_TYPE_CLIENT:
         case VIR_DOMAIN_NET_TYPE_MCAST:
-- 
1.7.9.5




More information about the libvir-list mailing list