Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to wait until /etc/resolv.conf is fully written before forking into the background #258

Open
rwmjones opened this issue Oct 30, 2023 · 4 comments

Comments

@rwmjones
Copy link

The situation is we have an appliance-type environment where we run dhcpcd eth0 to bring up the interface and set /etc/resolv.conf:

https://github.com/libguestfs/libguestfs/blob/0e37e5feeaf4380bc971a3105ead8e779a94fa36/appliance/init#L123

This generally works well except that /etc/resolv.conf does not have a nameserver record added until one or two seconds after dhcpcd forks into the background. In the init script I added:

cat /etc/resolv.conf
sleep 2
cat /etc/resolv.conf

and the output was:

+ cat /etc/resolv.conf
# Generated by dhcpcd from eth0.dhcp6
# /etc/resolv.conf.head can replace this line
search home.annexia.org
# /etc/resolv.conf.tail can replace this line
+ sleep 2
+ cat /etc/resolv.conf
# Generated by dhcpcd from eth0.dhcp, eth0.dhcp6
# /etc/resolv.conf.head can replace this line
search home.annexia.org
nameserver 192.168.0.254
# /etc/resolv.conf.tail can replace this line

I'd like dhcpcd (or some method) to wait before going into the background until /etc/resolv.conf has a nameserver record.

Otherwise later commands (which we run immediately after init, no sleeping) fail because they cannot do DNS resolution.

I tried adding the -w flag but it didn't make a difference.

@rsmarples
Copy link
Member

-w only waits until an address can be used.
DNS is only one facet of setup (albeit more important than most others)

Looks to me like you have a few options on how to resolve this.

  • Fix your DHCPv6 server to hand out DNS servers alongside it's DNS search
  • Use --waitip=4 to force dhcpcd to wait until an IPv4 address has been obtained as it's more likely to have working DNS
  • Write /etc/dhcpcd.exit-hook to perform things that require DNS resolution that can't be done dynamically

Any solution needs to cope with carrier droppage or the DHCP server not responding, etc so if you really need DNS working for stuff in a scripted fashion then using the dhcpcd hooks system is really the best option here.

@rwmjones
Copy link
Author

rwmjones commented Nov 7, 2023

Any clues on how to make option 3 work?

Note the DHCP server is built into the hypervisor (qemu) so to some extent we control it or at least understand how it behaves.

@rsmarples
Copy link
Member

man 8 dhcpcd-run-hooks

Also, look at the supplied 20-resolv.conf hook as it shows how to test if we got a DNS server from either DHCP, DHCPv6 or Router Advertisement. The last one is fairly complex to work out but it's also valid.

@rwmjones
Copy link
Author

Is there an example of an exit hook? The man page doesn't really explain anything about it.

Anyway I'm probably just going to add a loop which busy waits looking for nameserver to appear in /etc/resolv.conf, but it would be nice if there was an easier way to do this.

rwmjones added a commit to libguestfs/libguestfs that referenced this issue Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants