What I Want
I want to be able to refer to systems on both my home and work networks by their hostnames rather than their fully-qualified domain names, so, ‘prey’ instead of ‘prey.thestaticvoid.com’ and ‘acad2’ instead of ‘acad2.es.gwu.edu’.
The Problem
I would typically set my home and work domains as the search setting in /etc/resolv.conf. Unfortunately, either NWAM or the Solaris DHCP client (I haven’t decided which) overwrites resolv.conf on every new connection. DHCP on Linux does the same thing, but I can configure it by editing dhclient.conf (or whatever is being used these days, it’s been a while. I think I just set my domains in the NetworkManager GUI and forget about it).
The Solaris DHCP client configuration is not nearly as flexible, and neither is NWAM which gives you the option of replacing resolv.conf with information supplied by the DHCP server, or provided by you, but not a mix of both. I do like having the nameservers set by the DHCP server, so supplying a manual configuration is not an option.
What I Tried
The first thing I tried was setting the LOCALDOMAIN environmental variable in /etc/profile. From the resolv.conf man page:
You can override the search keyword of the system
resolv.conf file on a per-process basis by setting the
environment variable LOCALDOMAIN to a space-separated list
of search domains.
I thought, great, a way to manage domain search settings without worrying about what’s doing what to resolv.conf. It didn’t work as advertised:
% LOCALDOMAIN=thestaticvoid.com ping prey ping: unknown host prey % s touch /etc/resolv.conf % LOCALDOMAIN=thestaticvoid.com ping prey prey is alive % LOCALDOMAIN=thestaticvoid.com ping prey ping: unknown host prey
Next, I considered adding an NWAM Network Modifier to set my search string in resolv.conf after a new connection is established. This worked reasonably well, but didn’t handle the case when you switch from one network to another, for example, from wireless to wired. The only events in NWAM that can trigger a script when the network connection changes happens before DHCP messes up resolv.conf.
Finally, in the course of my testing, I discovered that the svc:/network/dns/client service was restarting with every network connection change. I looked into its manifest and saw that it was designed to wait for changes to resolv.conf:
Wait for potential DHCP modification of resolv.conf.
-->
<dependency
name='net'
grouping='require_all'
restart_on='none'
type='service'>
<service_fmri value='svc:/network/service' />
</dependency>
So I could write another service which depends on dns/client and restarts whenever dns/client does and I would have the last word about what goes into my configuration file!
My Solution
I wrote a service, svc:/network/dns/resolv-conf, with the following manifest:
<!DOCTYPE service_bundle SYSTEM "/usr/share/lib/xml/dtd/service_bundle.dtd.1">
<service_bundle type="manifest" name="dns-resolv-conf">
<service name="network/dns/resolv-conf"
type="service"
version="1">
<create_default_instance enabled="false" />
<single_instance />
<dependency name="dns-client"
grouping="require_all"
restart_on="restart"
type="service">
<service_fmri value="svc:/network/dns/client" />
</dependency>
<dependent name="resolv-conf"
grouping="optional_all"
restart_on="restart">
<service_fmri value="svc:/milestone/name-services" />
</dependent>
<exec_method type="method"
name="start"
exec="/lib/svc/method/dns-resolv-conf start"
timeout_seconds="60" />
<exec_method type="method"
name="stop"
exec="/lib/svc/method/dns-resolv-conf stop"
timeout_seconds="60" />
<property_group name="options" type="application">
<propval name="search" type="astring" value="" />
</property_group>
<property_group name="startd" type="framework">
<propval name="duration" type="astring" value="transient" />
</property_group>
<stability value="Unstable" />
<template>
<common_name>
<loctext xml:lang="C">resolv.conf Settings</loctext>
</common_name>
<documentation>
<manpage title="resolv.conf" section="4"
manpath="/usr/share/man" />
</documentation>
</template>
</service>
</service_bundle>
which calls the script, /lib/svc/method/dns-resolv-conf containing:
. /lib/svc/share/smf_include.sh
search=$(svcprop -p options/search $SMF_FMRI)
case "$1" in
"start")
# Don't do anything if search option not provided.
[ "$search" == '""' ] && exit $SMF_EXIT_OK
# Reverse the lines because we either want to:
# add the search line after the *last* domain line or
# add it to the very top of the file if there is no domain line
tac /etc/resolv.conf | grep -v "^search" | gawk '
/^domain/ {
if (!isset) {
print "search", $2, search
isset=1
}
}
END {
if (!isset) {
print "search", search
}
}
1
' search="$search" | tac > /etc/resolv.conf.new && mv -f /etc/resolv.conf.new /etc/resolv.conf
;;
"stop")
# Just get rid of any search lines, I guess.
grep -v "^search" /etc/resolv.conf > /etc/resolv.conf.new && mv -f /etc/resolv.conf.new /etc/resolv.conf
;;
*)
echo "Usage: $0 { start | stop }"
exit $SMF_EXIT_ERR_CONFIG
esac
exit $SMF_EXIT_OK
So now I can set my search options like:
% svccfg -s resolv-conf setprop 'options/search="thestaticvoid.com es.gwu.edu"' % svcadm refresh resolv-conf % svcadm enable resolv-conf % cat /etc/resolv.conf domain iss.gwu.edu search iss.gwu.edu thestaticvoid.com es.gwu.edu nameserver 161.253.152.50 nameserver 128.164.141.12
Problem solved! Or at least worked-around in the least hacky way I can!