I am using namespaces to separate a few physical interfaces on a server. The routing works perfectly. Also I have a folder for each namespace in /etc/netns/ e.g. /etc/netns/namespaceA/resolv.conf so that DNS works fine as well.
The problem arises when using DHCP with dhclient. I am running dhclient from inside a namespace and am getting this error.
(namespaceA)root@tc-vm:~#dhclient
RTNETLINK answers: File exists
mv: cannot move '/etc/resolv.conf.dhclient-new.2740' to '/etc/resolv.conf': Device or resource busy
I found out that the mv in /etc/resolvconf/update.d/libc contains a mv which might cause the problem.
How can dhclient be made namespace aware?
I looked into the issue myself.
What happens is that when you create a network namespace, you see
/etc/resolv.confof the host machine unless you create explicitly/etc/netns/<namespace_name>/resolv.conf, which will bind mount automatically to/etc/resolv.confwhen looked up inside the network namespace. Therefore, by simply creating that path, theresolv.confof the host won't be visibile any more on the network namespace, which will have its ownresolv.conf.The manual page of
ip netnsexplains this:As far as updating
resolv.conf,dhclientdoesn't work in network namespaces out of the box when/etc/netns/<namespace_name>/resolv.confexists (on the other hand, when it doesn't exist, it will overwrite theresolv.confof the host machine, since it's the only one available, but that's not really desirable). As the error in the question above shows, what happens is thatdhclientprepares a temporary file with the new nameserver details in/etc/resolv.conf.dhclient-new.2740and then tries to rename it as/etc/resolv.conf. It generates an error because/etc/resolv.confis already bind-mounted and apparentlymvisn't allowed to do this trick.In order to make
dhclientwork in network namespaces,/sbin/dhclient-scriptshould be modified. I removed this:And replaced it with:
Otherwise,
dhcpcdseems to do this job correctly.