Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 40459a5

Browse files
authored
B OpenNebula/one#6174: Consider /etc/hosts being empty (#297)
1 parent 87bb019 commit 40459a5

File tree

1 file changed

+7
-15
lines changed

1 file changed

+7
-15
lines changed

src/etc/one-context.d/net-15-hostname

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ function update_hosts() {
116116
elif grep -qE "[[:space:]]${name}([[:space:]]|#|\$)" /etc/hosts; then
117117
eval "${SED_I} -re \"s/^.*[[:space:]]${name}([[:space:]#].*|$)/${entry}/\" /etc/hosts"
118118
# create new entry
119-
elif [ -f /etc/hosts ]; then
119+
elif [ -s /etc/hosts ]; then
120120
# In FreeBSD, sed doesn't interpret \n. We put a real newline.
121121
eval "${SED_I} -e \"1s/^/${entry}\"$'\\\\\n/' /etc/hosts"
122122
else
@@ -151,15 +151,10 @@ if [ -n "${name}" ]; then
151151
# split host and domain names
152152
hostname=${name%%.*}
153153
domain=${name#*.}
154-
if [ "x${domain}" = "x${hostname}" ]; then
155-
domain=''
156-
fi
157154

158-
if [ -n "${domain}" ]; then
159-
set_domainname "${domain}"
160-
fi
155+
[ "$domain" = "$hostname" ] && domain=''
156+
[ -n "${domain}" ] && set_domainname "${domain}"
161157

162-
# FreeBSD
163158
if [ "${_kernel}" = 'FreeBSD' ]; then
164159
set_hostname "${name}"
165160
else
@@ -169,20 +164,17 @@ if [ -n "${name}" ]; then
169164
if [ -n "${DNS_HOSTNAME}" ]; then
170165
host_ip=$first_ip
171166
else
167+
name_ip=$(get_dns_name "${name}")
168+
172169
# If selected hostname resolves on first IP,
173170
# use first IP for local hostname in /etc/hosts.
174171
# Otherwise use loopback IP.
175-
name_ip=$(get_dns_name "${name}")
176-
if [ "x${first_ip}" = "x${name_ip}" ]; then
172+
if [ "$first_ip" = "$name_ip" ]; then
177173
host_ip=$first_ip
178-
elif [ -f /etc/debian_version ]; then
179-
host_ip='127.0.1.1'
180174
else
181175
host_ip='127.0.0.1'
182176
fi
183177
fi
184178

185-
if [ -n "${host_ip}" ]; then
186-
update_hosts "${host_ip}" "${name}" "${hostname}"
187-
fi
179+
[ -n "${host_ip}" ] && update_hosts "${host_ip}" "${name}" "${hostname}"
188180
fi

0 commit comments

Comments
 (0)