Skip to content

Commit 1ed89ab

Browse files
authored
Merge pull request #217 from scaleway/fix_empty_etc_hosts
Fix empty /etc/hosts from images
2 parents 819e186 + 83e5e3c commit 1ed89ab

2 files changed

Lines changed: 8 additions & 3 deletions

File tree

bases/overlay-systemd/etc/systemd/system/scw-set-hostname.service

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Description=SCW set hostname from metadata on first boot
33
Requires=network-online.target
44
After=network.target network-online.target
5-
ConditionFileNotEmpty=!/etc/hostname
65

76
[Service]
87
Type=oneshot
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
#! /bin/bash
22

33
old_hostname=$(hostnamectl status | grep 'hostname' | cut -d ":" -f2 | tr -d " ")
4-
new_hostname=$(scw-metadata --cached HOSTNAME)
4+
new_hostname=$(scw-metadata HOSTNAME)
5+
6+
if ! [ -s /etc/hosts ]; then
7+
cp /etc/hosts.default /etc/hosts
8+
sed -i "s/\bserver\b/$new_hostname/g" /etc/hosts
9+
fi
10+
511
hostnamectl set-hostname $new_hostname
612
if grep -q "$old_hostname" /etc/hosts; then
7-
sed -i "/$old_hostname/s/.*/127.0.0.1\t$new_hostname/" /etc/hosts
13+
sed -i "/$old_hostname/s/\b$old_hostname\b/$new_hostname/g" /etc/hosts
814
else
915
echo -e "127.0.0.1\t$new_hostname" >> /etc/hosts
1016
fi

0 commit comments

Comments
 (0)