]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
LXC implement missing macaddr assignment feature
authorRyota Ozaki <ozaki.ryota@gmail.com>
Wed, 21 Oct 2009 10:04:02 +0000 (12:04 +0200)
committerDaniel Veillard <veillard@redhat.com>
Wed, 21 Oct 2009 10:04:02 +0000 (12:04 +0200)
Currently MAC address configuration of container veth is just ignored.
This patch implements the missing feature.

* src/lxc/veth.c, src/lxc/veth.h: add setMacAddr
* src/lxc/lxc_driver.c: set macaddr of container veth if specified

src/lxc/lxc_driver.c
src/lxc/veth.c
src/lxc/veth.h

index 783dfccbfbe3ae657fc289cb5cd1c58f0354d206..ef97364068bc923f4cf1e0b39f1cc3adf760069c 100644 (file)
@@ -786,6 +786,17 @@ static int lxcSetupInterfaces(virConnectPtr conn,
             goto error_exit;
         }
 
+        if (def->nets[i]->mac) {
+            char macaddr[VIR_MAC_STRING_BUFLEN];
+            virFormatMacAddr(def->nets[i]->mac, macaddr);
+            if (0 != (rc = setMacAddr(containerVeth, macaddr))) {
+                virReportSystemError(conn, rc,
+                                     _("failed to set %s to %s"),
+                                     macaddr, containerVeth);
+                goto error_exit;
+            }
+        }
+
         if (0 != (rc = brAddInterface(brctl, bridge, parentVeth))) {
             virReportSystemError(conn, rc,
                                  _("failed to add %s device to %s"),
index 71d7de680eaef7b3424d2e12713b33d64eb5aca3..b15df8db6199d57214d260984765be0aec29830e 100644 (file)
@@ -216,3 +216,34 @@ error_out:
     VIR_FREE(pid);
     return rc;
 }
+
+/**
+ * setMacAddr
+ * @iface: name of device
+ * @macaddr: MAC address to be assigned
+ *
+ * Changes the MAC address of the given device with the
+ * given address using this command:
+ *     ip link set @iface address @macaddr
+ *
+ * Returns 0 on success or -1 in case of error
+ */
+int setMacAddr(const char* iface, const char* macaddr)
+{
+    int rc = -1;
+    const char *argv[] = {
+        "ip", "link", "set", iface, "address", macaddr, NULL
+    };
+    int cmdResult;
+
+    if (NULL == iface) {
+        goto error_out;
+    }
+
+    rc = virRun(NULL, argv, &cmdResult);
+    if (0 == rc)
+        rc = cmdResult;
+
+error_out:
+    return rc;
+}
index 429eb3d410e9c5671ca307afad546ba03e289c73..8f2f514e329ae784ab8ac0d6c29a54cb7de204bc 100644 (file)
@@ -20,5 +20,6 @@ int vethCreate(char* veth1, int veth1MaxLen, char* veth2,
 int vethDelete(const char* veth);
 int vethInterfaceUpOrDown(const char* veth, int upOrDown);
 int moveInterfaceToNetNs(const char *iface, int pidInNs);
+int setMacAddr(const char* iface, const char* macaddr);
 
 #endif /* VETH_H */