diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 241934549402a40cad54686ddd22d792f901b1a5..97b7903a577c3ca6ef7c01de7b2ef8367392662d 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -225,26 +225,38 @@ static int lxcContainerWaitForContinue(int control) /** - * lxcContainerEnableInterfaces: + * lxcContainerRenameAndEnableInterfaces: * @nveths: number of interfaces * @veths: interface names * - * This function will enable the interfaces for this container. + * This function will rename the interfaces to ethN + * with id ascending order from zero and enable the + * renamed interfaces for this container. * * Returns 0 on success or nonzero in case of error */ -static int lxcContainerEnableInterfaces(unsigned int nveths, - char **veths) +static int lxcContainerRenameAndEnableInterfaces(unsigned int nveths, + char **veths) { int rc = 0; unsigned int i; + char *newname = NULL; for (i = 0 ; i < nveths ; i++) { - DEBUG("Enabling %s", veths[i]); - rc = vethInterfaceUpOrDown(veths[i], 1); - if (0 != rc) { + rc = virAsprintf(&newname, "eth%d", i); + if (rc < 0) goto error_out; - } + + DEBUG("Renaming %s to %s", veths[i], newname); + rc = setInterfaceName(veths[i], newname); + if (0 != rc) + goto error_out; + + DEBUG("Enabling %s", newname); + rc = vethInterfaceUpOrDown(newname, 1); + if (0 != rc) + goto error_out; + VIR_FREE(newname); } /* enable lo device only if there were other net devices */ @@ -252,6 +264,7 @@ static int lxcContainerEnableInterfaces(unsigned int nveths, rc = vethInterfaceUpOrDown("lo", 1); error_out: + VIR_FREE(newname); return rc; } @@ -757,8 +770,9 @@ static int lxcContainerChild( void *data ) if (lxcContainerWaitForContinue(argv->monitor) < 0) return -1; - /* enable interfaces */ - if (lxcContainerEnableInterfaces(argv->nveths, argv->veths) < 0) + /* rename and enable interfaces */ + if (lxcContainerRenameAndEnableInterfaces(argv->nveths, + argv->veths) < 0) return -1; /* drop a set of root capabilities */ diff --git a/src/lxc/veth.c b/src/lxc/veth.c index 8617cf74a03490f331ba33e61b6f2d4afecad1ca..ede85cee177e4191e4748fad3bdc4b40c397f29c 100644 --- a/src/lxc/veth.c +++ b/src/lxc/veth.c @@ -247,3 +247,34 @@ int setMacAddr(const char* iface, const char* macaddr) error_out: return rc; } + +/** + * setInterfaceName + * @iface: name of device + * @new: new name of @iface + * + * Changes the name of the given device with the + * given new name using this command: + * ip link set @iface name @new + * + * Returns 0 on success or -1 in case of error + */ +int setInterfaceName(const char* iface, const char* new) +{ + int rc = -1; + const char *argv[] = { + "ip", "link", "set", iface, "name", new, NULL + }; + int cmdResult; + + if (NULL == iface || NULL == new) { + 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 8f2f514e329ae784ab8ac0d6c29a54cb7de204bc..8075a5ee735292ec293530d07f39d12ccaf2f8ba 100644 --- a/src/lxc/veth.h +++ b/src/lxc/veth.h @@ -21,5 +21,6 @@ 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); +int setInterfaceName(const char* iface, const char* new); #endif /* VETH_H */