Resolve LAN addresses

So I want to mount a samba share at reboot on a device running OpenWrt 19.07.7 firmware and the authoritative DHCP server on the LAN.

I don't want to use the IP address of the samba server in /etc/fstab so I need dnsmasq up and running.

Where do I put mount -a? I've tried /etc/hotplug.d/iface but:

mount error: could not resolve address for vuplus: Unknown error

When in the boot process is dnsmasq capable of resolving LAN addresses and is there a hook that can be used e.g. to mount samba shares?

1 Like

Does /etc/rc.local suit your purposes?

2 Likes

With logread it turns out that some 4 hours after reboot vuplus asks for a new dhcp lease. I suspect that until then the router is unable to resolve vuplus. Is there a way around this so that immediately after reboot vuplus is resolved?

You could give vuplus a static IP address and a corresponding entry in /etc/hosts.

Exactly. That's what I did. Every reboot I recreated a /tmp/lanhosts hosts file with the hosts in /etc/config/dhcp referred to with:

list addnhosts '/tmp/lanhosts'

in /etc/config/dhcp to avoid having to maintain two files. I still think there should be a better method but I can't figure it out.

Configure both static leases and hostnames.

1 Like

So instead of entries in /etc/config/dhcp like this?

config host
	option name 'vuplus'
	option mac 'xx:xx:xx:xx:xx:xx'
	option ip '192.168.0.6'

@vgaetera The /etc/config/dhcp is equivalent to a static lease according to your second link.

The settings in both host and domain sections should match for each host.
Both sections are required since restarting the router removes the lease file.

To ensure consistency I created this sed script called joinhosts:

#!/bin/sed -nf
# Join host name and ip address in /etc/config/dhcp host records to /etc/hosts format

/^\toption name/h
/^\toption ip/{G;s/\toption ip '\(.*\)'\n\toption name '\(.*\)'/\1\t\2/;p}

I call it from /etc/rc.local like this:

/root/bin/joinhosts /etc/config/dhcp > /tmp/lanhosts

In /etc/config/dhcp I have:

list addnhosts '/tmp/lanhosts'

So after every reboot lan host names are resolved. But not immediately. A wait is needed and I haven't figured out yet for how long.

Actually, this line:

/etc/init.d/dnsmasq restart

is the solution to my problem! After this command added host names are resolved. No wait is needed anymore. So thank you for the link.

I find the way they retrieve the host names from uci more elegant than mine because it doesn't depend on the /etc/config/dhcp file syntax e.g. the use of single quotes which is optional.

Therefore I changed my script joinhosts like this:

#!/bin/sed -nf
# Join host name and ip address in uci host records to /etc/hosts format

/^dhcp.@host\[.*\].name=/h
/^dhcp.@host\[.*\].ip=/{G;s/^dhcp.@host\[.*\].ip='\(.*\)'\ndhcp.@host\[.*\].name='\(.*\)'/\1\t\2/;p}

I call it in /etc/rc.local like this:

uci show dhcp | /root/bin/joinhosts > /tmp/lanhosts
2 Likes

I simplified the sed command a bit and reduced it to one line. Now my /etc/rc.local looks like this:

# Put your custom commands here that should be executed once
# the system init finished. By default this file does nothing.

uci show dhcp | sed -n "/@host.*name=/h;/@host.*ip=/{G;s/.*='\(.*\)'\n.*='\(.*\)'/\1\t\2/;p}" > /tmp/lanhosts
/etc/init.d/dnsmasq restart
mount -a

exit 0

I consider this issue solved. Thanks everybody!

1 Like

There's actually a simpler method:
https://openwrt.org/docs/guide-user/base-system/dhcp?s=dns#static_leases

This is the solution I was looking for. Adding:

	option dns '1'

to a host section in /etc/config/dhcp adds static forward and reverse DNS entries for that host. I did that for all hosts and now they are all available right after boot completes. The only command remaining in /etc/rc.local is now:

mount -a

Only one method can be the simplest and this is the one. Thanks @vgaetera !

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.