Generalized logic for setting up /etc/resolv.conf and /etc/hosts
Marc Fiuczynski [Fri, 1 Feb 2008 21:03:12 +0000 (21:03 +0000)]
depending on whether myplc has DNS enabled or not.

If PLC's DNS (dnsmasq) is enabled, then:

1) /etc/resolv.conf will point at the local host and
   /etc/plc_resolv.conf points either at:

  a) the plc-config-tty configured DNS servers, or
  b) the servers specific in the original /etc/resolv.conf

2) /etc/plc_hosts will contain a list of the currently configured
   nodes. (This needs to be fixed, as the set of nodes and their
   network settings can change).

If PLC's DNS (dnsmasq) is disabled, then /etc/resolv.conf will either :
  a) remain untouched if PLC_NET_DNS{1,2} are not specified, or
  b) be backed up to /etc/resolv_plc.backup and a new /etc/resolv.conf
     will be generated with the PLC_NET_DNS{1,2} servers configured by
     plc-config-tty

plc.d/dns

index b819e32..b195809 100755 (executable)
--- a/plc.d/dns
+++ b/plc.d/dns
 . /etc/plc.d/functions
 . /etc/planetlab/plc_config
 
+SIGNATURE="; generated by /etc/plc.d/dns --- DO NOT REMOVE THIS TEXT!"
+
 # Be verbose
 set -x
 
 case "$1" in
     start)
-       [ $PLC_DNS_ENABLED -ne 1 ] && exit 0
+       if [ $PLC_DNS_ENABLED -eq 1 ] ; then
+           MESSAGE=$"Starting DNS server"
+           dialog "$MESSAGE"
 
-       MESSAGE=$"Starting DNS server"
-       dialog "$MESSAGE"
+           dns-config
+           check
+       fi
 
-       dns-config
-       check
+       # Set up nameservers
+       if [ -n "$PLC_NET_DNS1" -o -n "$PLC_NET_DNS2" ] ; then
+           MESSAGE=$"Configuring resolv.conf"
+           dialog "$MESSAGE"
 
-       plc_daemon dnsmasq
-       check
+           rfile=$(mktemp)
+           (
+               echo $SIGNATURE
+               [ -n "$PLC_NET_DNS1" ] && echo "nameserver $PLC_NET_DNS1"
+               [ -n "$PLC_NET_DNS2" -a "$PLC_NET_DNS1" != "$PLC_NET_DNS2" ] && echo "nameserver $PLC_NET_DNS2"
+           ) > $rfile
+           
+           if [ $PLC_DNS_ENABLED -eq 1 ] ; then
+               ourfile=$(grep "$SIGNATURE" /etc/resolv.conf| wc -l)
+               if [ $ourfile -eq 0 ] ; then
+                   cp /etc/resolv.conf /etc/plc_resolv.conf
+               fi
+           else
+               cp /etc/resolv.conf /etc/resolv_plc.backup
+           fi
+           mv -f $rfile /etc/resolv.conf
+           chmod 444 /etc/resolv.conf
+       fi
+
+       if [ $PLC_DNS_ENABLED -eq 1 ] ; then
+           plc_daemon dnsmasq
+           check
+       fi
 
        result "$MESSAGE"
        ;;
 
     stop)
-       [ $PLC_DNS_ENABLED -ne 1 ] && exit 0
-       MESSAGE=$"Stopping DNS server"
+       MESSAGE=$"Reverting /etc/resolv.conf and /etc/hosts"
        dialog "$MESSAGE"
+       if [ $PLC_DNS_ENABLED -eq 1 ] ; then
+           if [ -f "/etc/plc_resolv.conf" ] ; then
+               ourfile=$(grep "$SIGNATURE" /etc/plc_resolv.conf| wc -l)
+               if [ $ourfile -ne 0 ] ; then 
+                   dialog "WARNING: original /etc/resolv.conf was generated by etc/plc.d/dns"          
+               fi
+               mv -f /etc/plc_resolv.conf /etc/resolv.conf
+           fi
+           [ -f "/etc/plc_hosts" ] && rm -f /etc/plc_hosts
+       fi
+       if [ $PLC_DNS_ENABLED -eq 0 -a -f "/etc/resolv_plc.backup" ] ; then
+           mv -f /etc/resolv_plc.backup /etc/resolv.conf
+       fi
+
+       if [ $PLC_DNS_ENABLED -eq 1 ] ; then
+           MESSAGE=$"Stopping DNS server"
+           dialog "$MESSAGE"
 
-       killproc plc_dnsmasq
-       check
+           killproc plc_dnsmasq
+           check
+       fi
 
        result "$MESSAGE"
        ;;