1This script can be used to implement persistent leases on openWRT, DD-WRT 2etc. Persistent leases are good: if the lease database is lost on a 3reboot, then it will eventually be restored as hosts renew their 4leases. Until a host renews (which may take hours/days) it will 5not exist in the DNS if dnsmasq's DDNS function is in use. 6 7*WRT systems remount all non-volatile fileystems read-only after boot, 8so the normal leasefile will not work. They do, however have NV 9storage, accessed with the nvram command: 10 11/usr/lib # nvram 12usage: nvram [get name] [set name=value] [unset name] [show] 13 14The principle is that leases are kept in NV variable with data 15corresponding to the line in a leasefile: 16 17dnsmasq_lease_192.168.1.56=3600 00:41:4a:05:80:74 192.168.1.56 * * 18 19By giving dnsmasq the leasefile-ro command, it no longer creates or writes a 20leasefile; responsibility for maintaining the lease database transfers 21to the lease change script. At startup, in leasefile-ro mode, 22dnsmasq will run 23 24"<lease_change_script> init" 25 26and read whatever that command spits out, expecting it to 27be in dnsmasq leasefile format. 28 29So the lease change script, given "init" as argv[1] will 30suck existing leases out of the NVRAM and emit them from 31stdout in the correct format. 32 33The second part of the problem is keeping the NVRAM up-to-date: this 34is done by the lease-change script which dnsmasq runs when a lease is 35updated. When it is called with argv[1] as "old", "add", or "del" 36it updates the relevant nvram entry. 37 38So, dnsmasq should be run as : 39 40dnsmasq --leasefile-ro --dhcp-script=/path/to/lease_update.sh 41 42or the same flags added to /etc/dnsmasq.conf 43 44 45 46Notes: 47 48This needs dnsmasq-2.33 or later to work. 49 50This technique will work with, or without, compilation with 51HAVE_BROKEN_RTC. Compiling with HAVE_BROKEN_RTC is 52_highly_recommended_ for this application since is avoids problems 53with the system clock being warped by NTP, and it vastly reduces the 54number of writes to the NVRAM. With HAVE_BROKEN_RTC, NVRAM is updated 55only when a lease is created or destroyed; without it, a write occurs 56every time a lease is renewed. 57 58It probably makes sense to restrict the number of active DHCP leases 59to an appropriate number using dhcp-lease-max. On a new DD_WRT system, 60there are about 10K bytes free in the NVRAM. Each lease record is 61about 100 bytes, so restricting the number of leases to 50 will limit 62use to half that. (The default limit in the distributed source is 150) 63 64Any UI script which reads the dnsmasq leasefile will have to be 65ammended, probably by changing it to read the output of 66`lease_update init` instead. 67 68 69Thanks: 70 71To Steve Horbachuk for checks on the script and debugging beyond the 72call of duty. 73 74 75Simon Kelley 76Fri Jul 28 11:51:13 BST 2006 77 78 79 80 81 82