Fix to improve hostname handling (#12064)

* Fix to improve hostname handling
If config_db.json is missing hostname entry, hostname-config.sh ends
up deleting existing entry too and hostname changes to default 'localhost'

* default hostname to 'sonic` if missing in config file
This commit is contained in:
Devesh Pathak 2022-10-25 14:51:02 -07:00 committed by Ying Xie
parent e9737814f8
commit fa609b80f1

View File

@ -3,6 +3,11 @@
CURRENT_HOSTNAME=`hostname` CURRENT_HOSTNAME=`hostname`
HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']` HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']`
if [ -z "$HOSTNAME" ] ; then
echo "Missing hostname in the config file, setting to default 'sonic'"
HOSTNAME='sonic'
fi
echo $HOSTNAME > /etc/hostname echo $HOSTNAME > /etc/hostname
hostname -F /etc/hostname hostname -F /etc/hostname