[ NOTE: SEE POST BELOW FOR A MUCH MORE ELEGANT SOLUTION. This post shows essentially a brute-force solution. I’ve figure out a much more elegant solution below. ]
If you’re tired of dnsmasq wiping the cachefile every time it has to restart (usually because your cable provider has set DHCP lease time to a very short 1 or 2 hours, and you get a new IP address each time), so you can never really build up a good-sized cachefile, this will set it up to repopulate the dnsmasq cachefile on each restart of dnsmasq.
You’re going to need an external USB drive to save dnsmasq logfiles to. Mine is /dev/sda2, and the logfile sits at /mnt/sda2/dns/dnsmasq.log.
In a terminal window connected to the router, issue:
mkdir /mnt/sda2/dns touch /mnt/sda2/dns/dnsmasq.log chown -R dnsmasq:dnsmasq /mnt/sda2/dns chmod 755 /mnt/sda2/dns
In the LuCI interface, under Networking >> DNS >> Log tab, check the Log queries checkbox, and under Log Facility, enter the path to your logfile: /mnt/sda2/dns/dnsmasq.log
Click Save & Apply. You’ll see the /mnt/sda2/dns/dnsmasq.log file size growing.
Of course, we don’t want it to grow forever, so we have to trim it.
Under System >> Scheduled Tasks, enter:
# Trim DNS Pre-Warming Log 0 4 * * * tail -n 100000 /mnt/sda2/dns/dnsmasq.log > /mnt/sda2/dns/dnsmasq.log.tmp && cat /mnt/sda2/dns/dnsmasq.log.tmp > /mnt/sda2/dns/dnsmasq.log && rm /mnt/sda2/dns/dnsmasq.log.tmp
You can set that 100000 to whatever number you want. You’ll have to experiment to get the right number.
Now, we’ve got the logfile growing, we’ve got the logfile trimmer set up. Now we have to set up the code to repopulate the cachefile, and a way to trigger it.
Create a file and set permissions:
touch /etc/dns_cache_prewarm.sh chmod +x /etc/dns_cache_prewarm.sh
Populate that file with this code:
#!/bin/sh # SETTINGS # == == == == == == == == == == == == == == == LOGFILE="/mnt/sda2/dns/dnsmasq.log" LOGGING=1 # Options: 0-off, 1-concise, 2-verbose # == == == == == == == == == == == == == == == PIDFILE="/tmp/dns_cache_prewarm.pid" TMP_DOMAINS="/tmp/dns_cache_prewarm.domains" # If ${LOGFILE} doesn't exist, exit. [ -f "${LOGFILE}" ] || exit 0 # Kill any previous instance (and its child processes) # == == == == == == == == == == == == == == == if [ -f "${PIDFILE}" ]; then OLD_PID=$(cat "${PIDFILE}") if [ -n "${OLD_PID}" ] && [ -d "/proc/${OLD_PID}" ]; then [ ${LOGGING} -gt 0 ] && echo "Existing dns_cache_prewarm process (PID ${OLD_PID}) found. Terminating PID ${OLD_PID}..." | tee >(logger -t dns_cache_prewarm) for p in /proc/[0-9]*/stat; do [ -r "$p" ] || continue read -r pid comm state ppid _ < "$p" if [ "$ppid" = "${OLD_PID}" ]; then kill -9 "${pid}" 2>/dev/null fi done kill -9 "${OLD_PID}" 2>/dev/null fi fi # Register current PID and clean up temp files on exit echo $$ > "${PIDFILE}" trap 'rm -f "${PIDFILE}" "${TMP_DOMAINS}"' EXIT # == == == == == == == == == == == == == == == [ ${LOGGING} -gt 0 ] && echo "DNS cachefile repopulate started." | tee >(logger -t dns_cache_prewarm) # Wait for dnsmasq to finish initializing sleep 10 # Extract unique FQDNs into a tmp file awk '{ for(i=1; i<NF; i++) { if($i == "forwarded" && $(i+1) ~ /^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/) { print $(i+1) } } }' "${LOGFILE}" \ | grep -vE '\.arpa$' \ | sort -u > "${TMP_DOMAINS}" COUNT=0 # Loop over the file, count the domains while read -r domain; do [ -z "${domain}" ] && continue # Abort if a newer instance took over the PID file [ -f "${PIDFILE}" ] && [ "$(cat "${PIDFILE}")" = "$$" ] || exit 0 nslookup "${domain}" 127.0.0.1 >/dev/null 2>&1 & COUNT=$((COUNT + 1)) sleep 0.3 # Terminal-only creeper dot (prints a dot every 5 domains when LOGGING=1) if [ -t 1 ] && [ "${LOGGING}" -lt 2 ] && [ $((COUNT % 5)) -eq 0 ]; then printf "." fi [ ${LOGGING} -eq 2 ] && echo "${domain} repopulated" | tee >(logger -t dns_cache_prewarm) done < "${TMP_DOMAINS}" if [ -t 1 ]; then printf "\n" fi [ ${LOGGING} -gt 0 ] && echo "DNS cachefile repopulate complete. ${COUNT} domains repopulated." | tee >(logger -t dns_cache_prewarm)
If your OpenWRT throws an error, it’s going to most likely be that sleep 0.3… BusyBox’s sleep command only takes integer values. Either change it to sleep 1 or install the coreutils-sleep package.
The code above kills any prior instance of itself, so if dnsmasq is restarted before this script can finish, this script will start over repopulating the dnsmasq cachefile.
Note the sort -u in the code above. It’ll only attempt to re-resolve each domain once, no matter how many times a domain occurs in your dnsmasq logfile.
Note the creeper dot when you’re running the script in a terminal and you’ve got LOGGING=1 (concise)… that’s so you know the script is still running alright.
Ok, now we have to trigger that code. In /etc/init.d/dnsmasq, you’ll see the code:
start_service() { local instance="$1" local instance_found=0 local first_instance="" . /lib/functions/network.sh config_cb() { local type="$1" local name="$2" if [ "$type" = "dnsmasq" ]; then if [ -n "$instance" ] && [ "$instance" = "$name" ]; then instance_found=1 fi if [ -z "$DEFAULT_INSTANCE" ]; then local disabled config_get_bool disabled "$name" disabled 0 if [ "$disabled" -eq 0 ]; then # First enabled section will be assigned default instance name. # Unnamed sections get precedence over named sections. if expr "$cfg" : 'cfg[0-9a-f]*$' >/dev/null = "9"; then # See uci_fixup_section. DEFAULT_INSTANCE="$name" # Unnamed config section. elif [ -z "$first_instance" ]; then first_instance="$name" fi fi fi fi } DEFAULT_INSTANCE="" config_load dhcp if [ -z "$DEFAULT_INSTANCE" ]; then DEFAULT_INSTANCE="$first_instance" # No unnamed config section was found. fi if [ -n "$instance" ]; then [ "$instance_found" -gt 0 ] || return dnsmasq_start "$instance" else config_foreach dnsmasq_start dnsmasq fi }
Change that to:
start_service() { local instance="$1" local instance_found=0 local first_instance="" . /lib/functions/network.sh config_cb() { local type="$1" local name="$2" if [ "$type" = "dnsmasq" ]; then if [ -n "$instance" ] && [ "$instance" = "$name" ]; then instance_found=1 fi if [ -z "$DEFAULT_INSTANCE" ]; then local disabled config_get_bool disabled "$name" disabled 0 if [ "$disabled" -eq 0 ]; then # First enabled section will be assigned default instance name. # Unnamed sections get precedence over named sections. if expr "$cfg" : 'cfg[0-9a-f]*$' >/dev/null = "9"; then # See uci_fixup_section. DEFAULT_INSTANCE="$name" # Unnamed config section. elif [ -z "$first_instance" ]; then first_instance="$name" fi fi fi fi } DEFAULT_INSTANCE="" config_load dhcp if [ -z "$DEFAULT_INSTANCE" ]; then DEFAULT_INSTANCE="$first_instance" # No unnamed config section was found. fi if [ -n "$instance" ]; then [ "$instance_found" -gt 0 ] || return dnsmasq_start "$instance" else config_foreach dnsmasq_start dnsmasq fi (sleep 2 && /etc/dns_cache_prewarm.sh) & }
Now, when dnsmasq restarts, in your logs (with LOGGING=2), you’ll see entries like:
user.notice: dns_cache_prewarm: DNS cachefile repopulate started. ... ... (snipped for brevity) user.notice: dns_cache_prewarm: url8947.nightly.pray.com repopulated user.notice: dns_cache_prewarm: use1-nf.naver.com.akadns.net repopulated user.notice: dns_cache_prewarm: v10.events.data.microsoft.com repopulated user.notice: dns_cache_prewarm: v20.events.data.microsoft.com repopulated user.notice: dns_cache_prewarm: valid.apple.com repopulated user.notice: dns_cache_prewarm: variations.brave.com repopulated user.notice: dns_cache_prewarm: video-fa.scdn.co repopulated user.notice: dns_cache_prewarm: www.bing.com repopulated user.notice: dns_cache_prewarm: www.collegeboard.org repopulated user.notice: dns_cache_prewarm: www.facebook.com repopulated user.notice: dns_cache_prewarm: www.google.com repopulated user.notice: dns_cache_prewarm: www.googleapis.com repopulated user.notice: dns_cache_prewarm: www.gstatic.com repopulated user.notice: dns_cache_prewarm: www.msftncsi.com repopulated user.notice: dns_cache_prewarm: www.spotify.com repopulated user.notice: dns_cache_prewarm: www.youtube.com repopulated user.notice: dns_cache_prewarm: DNS cachefile repopulate complete. 1121 domains repopulated.
If you run it manually in the terminal (with LOGGING=1), you’ll see something like:
root@OpenWRT:~# sh /etc/dns_cache_prewarm.sh
DNS cachefile repopulate started. ............................................................................................................................................................. ................................................................... DNS cachefile repopulate complete. 1121 domains repopulated.
All you’ll see in the logs when LOGGING=1 is something like:
DNS cachefile repopulate started. DNS cachefile repopulate complete. 1121 domains repopulated.

