systemd and search domains.

Ken D'Ambrosio ken at jots.org
Wed Jan 8 18:26:53 EST 2020


On 2020-01-08 17:58, Joshua Judson Rosen wrote:
>> Nutshell: clearly, it's time for
>> a self-written inotify daemon and call it a day.
>> Because it's stupid easy to prepend a line with my domain name every 
>> time the file changes,
>> whereas I'm gettin' old trying to figure this out through a more 
>> elegant mechanism.
> 
> Ha! An inotify monitor actually seems like a pretty elegant solution to 
> me!
> (though maybe I should point out that I got some of my aesthetic sense
>  from growing up watching The Red Green Show...).

I done did it.  (Yeah, I use Ruby for my CLI stuffs; it's pretty much 
replaced Perl as my go-to for any systemy stuff.  But this time, I 
decided not to make it a Ruby script and just threw in a -pie one-liner 
like the olden Perl days.  It's not particularly pretty, but hey...)
--------------------------
gozer # cat /usr/local/bin/dnssearch.sh
#!/bin/bash

# Prepends my domain to DNS search line in resolv.conf

resolv="/run/systemd/resolve/stub-resolv.conf"

while true
do
     grep jots.org /run/systemd/resolve/stub-resolv.conf || \
	echo "Prepending jots.org to domain search list"
     ruby -pi -e 'gsub(/search /, "search jots.org ")' "$resolv"
     inotifywait -e modify "$resolv" # Here and not at top to be sure 
fires once
     sleep 3
done
--------------------------
gozer # cat /etc/systemd/system/dnssearch.service
[Unit]
Description=Date to file on-disk
After=getty.target

[Service]
Type=simple
User=root
WorkingDirectory=/tmp
ExecStart=/usr/local/bin/dnssearch.sh
Restart=always

[Install]
WantedBy=multi-user.target
--------------------------
gozer systems # systemctl status dnssearch
● dnssearch.service - Date to file on-disk
    Loaded: loaded (/etc/systemd/system/dnssearch.service; enabled; 
vendor preset: enabled)
    Active: active (running) since Wed 2020-01-08 18:14:13 EST; 6min ago
  Main PID: 13429 (dnssearch.sh)
     Tasks: 2 (limit: 4915)
    CGroup: /system.slice/dnssearch.service
            ├─13429 /bin/bash /usr/local/bin/dnssearch.sh
            └─13700 inotifywait -e modify 
/run/systemd/resolve/stub-resolv.conf

Jan 08 18:14:16 gozer dnssearch.sh[13429]: Watches established.
Jan 08 18:14:28 gozer dnssearch.sh[13429]: 
/run/systemd/resolve/stub-resolv.conf MODIFY
Jan 08 18:14:28 gozer dnssearch.sh[13429]: # configured search jots.org 
domains.
Jan 08 18:14:28 gozer dnssearch.sh[13429]: search jots.org jots.org
Jan 08 18:14:31 gozer dnssearch.sh[13429]: Setting up watches.
Jan 08 18:14:31 gozer dnssearch.sh[13429]: Watches established.
Jan 08 18:14:53 gozer dnssearch.sh[13429]: 
/run/systemd/resolve/stub-resolv.conf MODIFY
Jan 08 18:14:53 gozer dnssearch.sh[13429]: Prepending jots.org to domain 
search list
Jan 08 18:14:56 gozer dnssearch.sh[13429]: Setting up watches.
Jan 08 18:14:56 gozer dnssearch.sh[13429]: Watches established.
--------------------------
First prepend was when I fired up the service and my domain wasn't 
already in there; second was when I launched the VPN.

> Watch out for the `inotify-handler writes and re-triggers itself
> resulting in an infinitely-long "search" line' problem,
> obviously? :)

Yeah, I've stubbed my toe on that w-a-y back when inotify first came 
out.  That, and just not firing too frequently should $stuff be 
happening, are the reasons for the three-second sleep.

-Ken


More information about the gnhlug-discuss mailing list