LXC allow container to have ethN interfaces

Current implementation of lxc driver creates vethN named
interface(s) in the host and passes as it is to a container.
The reason why it doesn't use ethN is due to the limitation
that one namespace cannot have multiple iterfaces that have
an identical name so that we give up creating ethN named
interface in the host for the container.

However, we should be able to allow the container to have
ethN by changing the name after clone(CLONE_NEWNET).

* src/lxc/lxc_container.c src/lxc/veth.c src/lxc/veth.h: do the clone
  and then renames interfaces eth0 ... ethN to keep the interface names
  familiar in the domain
This commit is contained in:
Ryota Ozaki 2009-11-05 14:11:30 +01:00 committed by Daniel Veillard
parent a9cb354833
commit 8db32571ba
3 changed files with 56 additions and 10 deletions

View File

@ -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 */

View File

@ -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;
}

View File

@ -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 */