Is it a good idea to move leasefile into a non-temp directory?

I see that by default the dnsmasq leasefile is in /tmp/leasefile. This means that on a router restart (which happens rarely when I'm troubleshooting, but it's still something that does happen), because the file is lost, all wifi devices renew dhcp leases but wired ones don't. This has the sideeffect that ethers defined dns addresses such as my-host.lan no longer resolves to the correct ip, even if the host is still holding to that ip, because the leasetime isn't over and it does not need to renew yet.

Moving the leasefile to a non-temporary directory should mean on a reboot it is read, and state is not lost. However, I'm here going, if it was this simple, I feel that'd be the default behavior. Am I missing something here? Would this be a good idea or come with serious downsides? Perhaps another question is, what's the justification for the current setup being the default?

it's updates frequently, and would wear out you flash mem, which isn't designed for this.
static leases would probably solve the issue for you.

or pull the ethernet cable (power cycle the switch, etc) for all wired devices, once the router's back up.

1 Like

Yes, I am using static leases, as I had mentioned as the ethers file. The issue is that, on a reboot, openwrt loses track of the active dhcp lease, and doesn't actually resolve my-host.lan to the static lease ip, until the host renews the dhcp lease.

Flash memory life is a good point! Thank you.

but ethers only map MAC to host name ? not DNS ?

For anyone running on platforms with more durable storage (e.g. x86 or rpi), it is entirely possible to move the dhcp.leases file to /etc and then symlink that file to /tmp/dhcp.leases.

I do this on my x86_64 platform so my leases file survives reboots. I have also added /etc/dhcp.leases to my /etc/sysupgrade.conf so it survives upgrades as well.

To create the necessary symlink upon every reboot, you’d want to add this to your /etc/rc.local file:

# Create a symlink to /etc/dhcp.leases if it doesn't exist already...
[ ! -f /tmp/dhcp.leases ] && ln -s /etc/dhcp.leases /tmp/dhcp.leases || echo "INFO: /tmp/dhcp.leases already exists."

A static lease, defined in the ethers file, maps a mac address to a custom name and a static ip. This way, name.lan gets mapped to the ip (as dnsmasq handles both dns and dhcp, it can easily integrate these), as long as there is an active dhcp lease (which is what I discovered at least, and the root of my problem).

ah, you can actually put IPs in there, I've only used hosts names :wink:

This is not applicable for me, due to not having durable storage, but still wanted to ask why you go with a symlink setup at startup rather than just change the relevant config option?

Option name: leasefile

1 Like

The documentation for uci based configuration actually exposes all of these options. Maybe interesting to check :slight_smile: (tho it's just examples, not explained in detail. For that, it's best to look into further documentation of the underlying tooling and the generated files)

Because I have some other tools that are looking at /tmp/dhcp.leases and are not set up to follow that config option. But if that same situation doesn’t apply to another, then by all means the config option would be the way to go.

1 Like

I use option dhcphostsfile '/etc/config/dhcp.hosts' , and when I edit this file, I run a script that copy the entries to /etc/hosts file . This way my dns always translate, with or without an active dhcp lease

Edit:

In dhcp.hosts, the entry is something like this:
XX:XX:XX:XX:XX:XX,192.168.21.141,note

In hosts, the entry becomes:
192.168.21.141 note

1 Like

Oh that's smart! I already provision via script anyways, so I could definitely add a post-uci section where I generate the hosts file.

One thing I'm curious about is why you use dhcphostsfile rather than ethers for this? Are there any pros/cons to either approach?

honestly ?
I don't know :sweat_smile:

Can I do something like...

/etc/hosts:
192.168.21.141 note
/etc/ethers:
XX:XX:XX:XX:XX:XX note

or something like that?

On a small network it doesn't hurt to use short lease times like 10 minutes. Should the router reboot the clients will send renewals which will repopulate the RAM disk table within a few minutes.

Add hostnames to /etc/hosts if the are static