f28a670097
- What I did This fix removes the possibility of 'localhost' entry getting removed from /etc/hosts file by hostname-config service. Without this change, whenever we change the hostname from 'localhost' to any other name on the config_db.json and reload the config, /etc/hosts file will only have the new hostname on it. But there are multiple sonic utilities (eg: swssconfig) which relies on the hard coded 'localhost' name and they tend to stop working. - How I did it Added a new check on hostname-config.sh script to avid blindly deleting the line containing the old hostname from /etc/hosts file. Now it will delete the old hostname only if its not localhost or when the hostname is not changing. - How to verify it Bring up SONiC on a device with hostname as localhost Edit /etc/sonic/config_db.json to update the 'hostname' filed under DEVICE_METADATA from "hostname" : "localhost" --> "hostname" : "sonic" run config reload -y to reflect the hostname change done on config_db.json file. cat /etc/hosts and check whether both 127.0.0.1 localhost and 127.0.0.1 sonic entry are present on the file. ping localhost should work fine. - Description for the changelog Make hostname-config service more robust in handling SONiC hostname change from localhost to anything else.
17 lines
492 B
Bash
Executable File
17 lines
492 B
Bash
Executable File
#!/bin/bash -e
|
|
|
|
CURRENT_HOSTNAME=`hostname`
|
|
HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']`
|
|
|
|
echo $HOSTNAME > /etc/hostname
|
|
hostname -F /etc/hostname
|
|
|
|
# Remove the old hostname entry from hosts file.
|
|
# But, 'localhost' entry is used by multiple applications. Don't remove it altogether.
|
|
if [ $CURRENT_HOSTNAME != "localhost" ] || [ $CURRENT_HOSTNAME == $HOSTNAME ] ; then
|
|
sed -i "/\s$CURRENT_HOSTNAME$/d" /etc/hosts
|
|
fi
|
|
|
|
echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
|
|
|