ossobv / sonic-buildimage

Scripts which perform an installable binary image build for SONiC
Other
0 stars 0 forks source link

runtime: /etc/hosts keeps growing #2

Open wdoekes opened 2 weeks ago

wdoekes commented 2 weeks ago

Upstream: #-20404 #-20579

On SONiC-OS-202405-osso.0-dirty-20241102.171738 I see this:

--- a/hosts
+++ b/hosts
@@ -3,3 +3,7 @@
 127.0.0.1 leaf1.dostno.systems
 127.0.0.1 leaf1.dostno.systems
 127.0.0.1 leaf1.dostno.systems
+127.0.0.1 leaf1.dostno.systems
+127.0.0.1 leaf1.dostno.systems
+127.0.0.1 leaf1.dostno.systems
+127.0.0.1 leaf1.dostno.systems

Something is writing extra entries all the time.

Low prio, but could use some investigation.

wdoekes commented 2 weeks ago

The cause: /etc/dhcp/dhclient-exit-hooks.d/sethostname (./files/dhcp/sethostname):

case $reason in
    BOUND|RENEW|REBIND|REBOOT)
        current_host_name=`hostname -s`
        if [ "$current_host_name" != "$new_host_name" ] && [ -n "$new_host_name" ]
        then
            echo $new_host_name > /etc/hostname
            hostname -F /etc/hostname
            sed -i "/\s$current_host_name$/d" /etc/hosts
            echo "127.0.0.1 $new_host_name" >> /etc/hosts
        fi
        ;;
esac

We have fqdn's, so we get this:

# reason=RENEW new_host_name=leaf1.dostno.systems sh -x /etc/dhcp/dhclient-exit-hooks.d/sethostname
+ hostname -s
+ current_host_name=leaf1
+ [ leaf1 != leaf1.dostno.systems ]
+ [ -n leaf1.dostno.systems ]
+ echo leaf1.dostno.systems
+ hostname -F /etc/hostname
+ sed -i /\sleaf1$/d /etc/hosts
+ echo 127.0.0.1 leaf1.dostno.systems

It would stay fixed for short hostnames:

# reason=RENEW new_host_name=leaf1 sh -x /etc/dhcp/dhclient-exit-hooks.d/sethostname
+ hostname -s
+ current_host_name=leaf1
+ [ leaf1 != leaf1 ]

Fix:

case $reason in
    BOUND|RENEW|REBIND|REBOOT)
        if [ -n "$new_host_name" ]; then
            current_host_long=$(hostname -f)
            current_host_short=$(hostname -s)

            case "$new_host_name" in
            *.*)
                current_host_name=$current_host_long
                new_host_short=${new_host_name%%.*}
                new_host_long=$new_host_name
                ;;
            *)
                current_host_name=$current_host_short
                new_host_short=$new_host_name
                new_host_long=$new_host_short.$(hostname -d 2>/dev/null)
                new_host_long=${new_host_long%.}
                ;;
            esac
            if [ "$current_host_name" != "$new_host_name" ]; then
                echo $new_host_name > /etc/hostname
                hostname -F /etc/hostname
                echo "127.0.0.1 $new_host_long $new_host_short" >> /etc/hosts
                sed -i "/\s$current_host_long\s\+$current_host_short$/d" /etc/hosts
            fi
        fi
        ;;
esac

Test:

# reason=RENEW new_host_name=test1 sh -x /etc/dhcp/dhclient-exit-hooks.d/sethostname
+ set -u
+ [ -n test1 ]
+ hostname -f
+ current_host_long=leaf2.dostno.systems
+ hostname -s
+ current_host_short=leaf2
+ current_host_name=leaf2
+ new_host_short=test1
+ hostname -d
+ new_host_long=test1.dostno.systems
+ new_host_long=test1.dostno.systems
+ [ leaf2 != test1 ]
+ echo test1
+ hostname -F /etc/hostname
+ echo 127.0.0.1 test1.dostno.systems test1
+ sed -i /\sleaf2.dostno.systems\s\+leaf2$/d /etc/hosts
# cat /etc/hosts
127.0.0.1       localhost
127.0.0.1 test1.dostno.systems test1

^- change: both fqdn and short hostname

# reason=RENEW new_host_name=test1 sh -x /etc/dhcp/dhclient-exit-hooks.d/sethostname
+ set -u
+ [ -n test1 ]
+ hostname -f
+ current_host_long=test1.dostno.systems
+ hostname -s
+ current_host_short=test1
+ current_host_name=test1
+ new_host_short=test1
+ hostname -d
+ new_host_long=test1.dostno.systems
+ new_host_long=test1.dostno.systems
+ [ test1 != test1 ]

^- no change, as expected