Skip to content

Commit 617b3e4

Browse files
authored
Add service to config hostname based on configdb (#1174)
1 parent 6d0329a commit 617b3e4

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

files/build_templates/sonic_debian_extension.j2

+5
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ sudo cp $IMAGE_CONFIGS/interfaces/*.j2 $FILESYSTEM_ROOT/usr/share/sonic/template
120120
# Copy initial interfaces configuration file, will be overwritten on first boot
121121
sudo cp $IMAGE_CONFIGS/interfaces/init_interfaces $FILESYSTEM_ROOT/etc/network
122122

123+
# Copy hostname configuration scripts
124+
sudo cp $IMAGE_CONFIGS/hostname/hostname-config.service $FILESYSTEM_ROOT/etc/systemd/system/
125+
sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable hostname-config.service
126+
sudo cp $IMAGE_CONFIGS/hostname/hostname-config.sh $FILESYSTEM_ROOT/usr/bin/
127+
123128
# Copy updategraph script and service file
124129
sudo cp $IMAGE_CONFIGS/updategraph/updategraph.service $FILESYSTEM_ROOT/etc/systemd/system/
125130
sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable updategraph.service
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Update hostname based on configdb
3+
Requires=database.service
4+
After=database.service
5+
6+
[Service]
7+
Type=oneshot
8+
ExecStart=/usr/bin/hostname-config.sh
9+
10+
[Install]
11+
WantedBy=multi-user.target
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash -e
2+
3+
CURRENT_HOSTNAME=`hostname`
4+
HOSTNAME=`sonic-cfggen -d -v DEVICE_METADATA[\'localhost\'][\'hostname\']`
5+
6+
echo $HOSTNAME > /etc/hostname
7+
hostname -F /etc/hostname
8+
9+
sed -i "/\s$CURRENT_HOSTNAME$/d" /etc/hosts
10+
echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
11+

0 commit comments

Comments
 (0)