Skip to content

Commit

Permalink
checking etc/hosts logic
Browse files Browse the repository at this point in the history
  • Loading branch information
palashvij-msft committed Aug 25, 2023
1 parent eb3e23d commit a12d9da
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
28 changes: 22 additions & 6 deletions src/aznfswatchdog
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ next_ip_change_detection_epoch=0
# For controlled warnings if Blob fqdn to IPv4 address added in etc/hosts after mount
#
WARN_COUNTER=0
WARNING_GENERATED=false

# Load common aznfs helpers.
. /opt/microsoft/aznfs/common.sh
Expand Down Expand Up @@ -313,14 +314,29 @@ while :; do
continue
fi

#
# Warn if the desired resolution entry is added in /etc/hosts after mount is success
if grep -q "$hname" /etc/hosts; then
((WARN_COUNTER++))
if ((WARN_COUNTER % 2 == 1)); then
wecho "[WARNING] Detected hard-coded resolution entry for "$hname" in /etc/hosts."
wecho "[Action Required]: To avoid conflicts and to ensure correctly handling endpoint IP address changes by AZNFS,
remove the entry for "$hname" in /etc/hosts."
# Read each line from /etc/hosts
#
while IFS= read -r line; do
# Check if the line is not commented
if ! [[ "$line" =~ ^[[:space:]]*# ]]; then
# Check if corresponding entry for nfs_host is present
if echo "$line" | grep -q "$l_host"; then
((WARN_COUNTER++))
if ((WARN_COUNTER % 2 == 1)); then
WARNING_GENERATED=true
wecho "[WARNING] Detected static resolution entry for "$l_host" in /etc/hosts."
wecho "[Action Required]: Remove the entry for "$l_host" in /etc/hosts to ensure correctly handling endpoint IP address changes by AZNFS"
break
fi
fi
fi
done < /etc/hosts

# Reset the WARN_COUNTER if no warnings were generated
if ! $WARNING_GENERATED; then
WARN_COUNTER=0
fi

#
Expand Down
21 changes: 15 additions & 6 deletions src/mountscript.sh
Original file line number Diff line number Diff line change
Expand Up @@ -636,13 +636,22 @@ if ! is_valid_blob_fqdn "$nfs_host"; then
exit 1
fi

#
# fail if the desired entry is present in /etc/hosts
if grep -q "$nfs_host" /etc/hosts; then
eecho "Detected static resolution entry for "$nfs_host" in /etc/hosts."
eecho "[Action Required]: To avoid conflicts and to ensure correctly handling endpoint IP address changes by AZNFS,
remove the entry for "$nfs_host" in /etc/hosts."
exit 1
fi
# Read each line from /etc/hosts
#
while IFS= read -r line; do
# Check if the line is not commented
if ! [[ "$line" =~ ^[[:space:]]*# ]]; then
# Check if corresponding entry for nfs_host is present
if echo "$line" | grep -q "$nfs_host"; then
eecho "Mount failed!"
eecho "Detected entry for $nfs_host in /etc/hosts."
eecho "[Action Required]: Remove or comment out the entry for $nfs_host in /etc/hosts for MOUNT to work."
exit 1
fi
fi
done < /etc/hosts

nfs_ip=$(resolve_ipv4 "$nfs_host")
if [ $? -ne 0 ]; then
Expand Down

0 comments on commit a12d9da

Please sign in to comment.