[Ovirt-devel] [PATCH node] Change DNS setup to match NTP. rhbz#508677

Darryl L. Pierce dpierce at redhat.com
Thu Jul 9 14:18:54 UTC 2009


Rather than expecting the DNS entries to be on one line, they are now
entered as two separate entries.

Also fixes a bug that caused the first DNS entry to be lost when a
second entry was provided.

In both cases, the input is collected in a colon-delimited string that
is then processed. This is to provide consistency between the
interactive setup and the automated setup.

Signed-off-by: Darryl L. Pierce <dpierce at redhat.com>
---
 scripts/ovirt-config-networking |   53 +++++++++++++++++++++++++-------------
 scripts/ovirt-functions         |   18 +++++++++++++
 2 files changed, 53 insertions(+), 18 deletions(-)

diff --git a/scripts/ovirt-config-networking b/scripts/ovirt-config-networking
index aa04a7c..730381e 100755
--- a/scripts/ovirt-config-networking
+++ b/scripts/ovirt-config-networking
@@ -212,17 +212,30 @@ function configure_dns
 
     if [ -z "$AUTO" ]; then
         while true; do
-            printf "\n"
-            echo "Enter up to two DNS servers separated by commas:"
-            if [ -n "$OVIRT_DNS" ]; then
-                echo "Press Enter for defaults: ($OVIRT_DNS)"
-            fi
-            read -ep ": "
-            DNS=$REPLY
-
-            if [ -z "$DNS" ]; then
-                DNS=$OVIRT_DNS
-            fi
+            for dns in first second; do
+                while true; do
+                    printf "\n"
+                    read -ep "Please enter the ${dns} DNS server (or ENTER to exit): "
+                    if [[ -z "${REPLY}" ]]; then
+                        if [[ -z "${DNS}" ]]; then
+                            printf "\nAborted...\n"
+                            return
+                        else
+                            break
+                        fi
+                    fi
+                    if is_valid_ipv4 $REPLY; then
+                        if [[ -z "${DNS}" ]]; then
+                            DNS="${REPLY}"
+                        elif [[ -n "${REPLY}" ]]; then
+                            DNS="${DNS}:${REPLY}"
+                        fi
+                        break
+                    else
+                        printf "${REPLY} is an invalid address.\n"
+                    fi
+                done
+            done
 
             printf "\n"
             ask_yes_or_no "Is this correct (y/n/a)?" true true
@@ -235,12 +248,11 @@ function configure_dns
     fi
 
     if [ -n "$DNS" ]; then
-        DNS1=$(echo "$DNS" | awk -F, '{print $1}')
-        DNS2=$(echo "$DNS" | awk -F, '{print $2}')
+        DNS1=$(echo "$DNS" | awk -F\: '{print $1}')
+        DNS2=$(echo "$DNS" | awk -F\: '{print $2}')
 
-        test -n "$DNS1" && IF_CONFIG="set $IF_ROOT/DNS1 $DNS1"
-        test -n "$DNS2" && IF_CONFIG="set $IF_ROOT/DNS2 $DNS2"
-        printf "$IF_CONFIG\n" >> $IF_FILENAME
+        test -n "$DNS1" && printf "set $IF_ROOT/DNS1 $DNS1\n" >> $IF_FILENAME
+        test -n "$DNS2" && printf "set $IF_ROOT/DNS2 $DNS2\n" >> $IF_FILENAME
     fi
 }
 
@@ -259,7 +271,11 @@ function configure_ntp
 
 	    if [ -z "$REPLY" ]; then break; fi
 
-	    NTPSERVERS="$NTPSERVERS $REPLY"
+            if is_valid_ipv4 $REPLY; then
+	        NTPSERVERS="${NTPSERVERS}:${REPLY}"
+            else
+                printf "${REPLY} is an invalid address.\n"
+            fi
 	done
     fi
 }
@@ -278,7 +294,8 @@ save\n" > $ntpconf
 
     if [ -n "$NTPSERVERS" ]; then
         offset=1
-	for server in $NTPSERVERS; do
+        SERVERS=$(echo $NTPSERVERS | awk 'BEGIN{FS=":"}{for (i=1; i<=NF; i++) print $i}')
+	for server in $SERVERS; do
             printf "set /files/etc/ntp.conf/server[${offset}] ${server}\n" >> $ntpconf
             offset=$(echo "$offset+1" | bc)
 	done
diff --git a/scripts/ovirt-functions b/scripts/ovirt-functions
index ecde762..81db36b 100755
--- a/scripts/ovirt-functions
+++ b/scripts/ovirt-functions
@@ -654,6 +654,24 @@ ask_yes_or_no () {
     done
 }
 
+# Verifies the address entered is a valid IPv4 address.
+is_valid_ipv4 () {
+    local address=${1}
+    local result=1
+
+    if [[ "$address" =~ "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" ]]; then
+        OIFS=$IFS
+        IFS='.'
+        ip=($address)
+        IFS=$OIFS
+        [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
+            && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
+        result=$?
+    fi
+
+    return $result
+}
+
 # execute a function if called as a script, e.g.
 #   ovirt-functions ovirt_store_config /etc/hosts
 
-- 
1.6.2.5




More information about the ovirt-devel mailing list