Fix for myplc-native:
[myplc.git] / plc.d / network
index b412d18..5d3d443 100755 (executable)
@@ -7,34 +7,57 @@
 # Mark Huang <mlhuang@cs.princeton.edu>
 # Copyright (C) 2006 The Trustees of Princeton University
 #
-# $Id: guest.init,v 1.12 2006/04/04 22:09:47 mlhuang Exp $
+# $Id$
 #
 
 # Source function library and configuration
 . /etc/plc.d/functions
+. /etc/planetlab/plc_config
+
+# Be verbose
+set -x
 
 case "$1" in
     start)
        MESSAGE=$"Generating network files"
        dialog "$MESSAGE"
 
+       hfile=$(mktemp)
        # Minimal /etc/hosts
-       echo "127.0.0.1 localhost.localdomain localhost" >/etc/hosts
        (
-           for server in API BOOT WWW ; do
+           for server in DB API BOOT WWW ; do
                hostname=PLC_${server}_HOST
-               ip=$(gethostbyname ${!hostname})
-               if [ -n "$ip" ] ; then
-                   echo "$ip   $hostname"
+               ip=PLC_${server}_IP
+               if [ -n "${!ip}" ] ; then
+                   echo "${!ip}        ${!hostname}"
+               else
+                   ip=$(gethostbyname ${!hostname})
+                   if [ -n "$ip" ] ; then
+                       echo "$ip       ${!hostname}"
+                   fi
                fi
            done
-       ) >>/etc/hosts
+       ) > $hfile
+       grep -v "^#" /etc/hosts >> $hfile
+       echo "#generated by /etc/plc.d/network" >/etc/hosts
+       echo "127.0.0.1 localhost.localdomain localhost" >> $hfile
+       sort -u $hfile >> /etc/hosts
+       chmod +r /etc/hosts
+       rm -f $hfile
 
        # Set up nameservers
+       rfile=$(mktemp)
        (
+           echo "; generated by /etc/plc.d/network"
            [ -n "$PLC_NET_DNS1" ] && echo "nameserver $PLC_NET_DNS1"
-           [ -n "$PLC_NET_DNS2" ] && echo "nameserver $PLC_NET_DNS2"
-       ) >/etc/resolv.conf
+           [ -n "$PLC_NET_DNS2" -a "$PLC_NET_DNS1" != "$PLC_NET_DNS2" ] && echo "nameserver $PLC_NET_DNS2"
+       ) > $rfile
+       if [ -n "$PLC_NET_DNS1" -o -n "$PLC_NET_DNS2" ] ; then
+           mv $rfile /etc/resolv.conf
+           chmod 444 /etc/resolv.conf
+       else
+           rm -f $rfile
+       fi
 
        result "$MESSAGE"
        ;;