From 38ccccf09526bdd125c717beb13769aa2904e751 Mon Sep 17 00:00:00 2001 From: Ryota Ozaki Date: Mon, 19 Oct 2009 22:13:18 +0900 Subject: [PATCH] LXC implement missing macaddr assignment feature Currently MAC address configuration for lxc 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 | 11 +++++++++++ src/lxc/veth.c | 31 +++++++++++++++++++++++++++++++ src/lxc/veth.h | 1 + 3 files changed, 43 insertions(+), 0 deletions(-) diff --git a/src/lxc/lxc_driver.c b/src/lxc/lxc_driver.c index 783dfcc..ef97364 100644 --- a/src/lxc/lxc_driver.c +++ b/src/lxc/lxc_driver.c @@ -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"), diff --git a/src/lxc/veth.c b/src/lxc/veth.c index 71d7de6..b15df8d 100644 --- a/src/lxc/veth.c +++ b/src/lxc/veth.c @@ -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; +} diff --git a/src/lxc/veth.h b/src/lxc/veth.h index 429eb3d..8f2f514 100644 --- a/src/lxc/veth.h +++ b/src/lxc/veth.h @@ -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 */ -- 1.6.2.5