Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
Bug 38172 - hostname is overitten even if set correctly by dhcp
Summary: hostname is overitten even if set correctly by dhcp
Status: RESOLVED FIXED
Alias: None
Product: Gentoo Linux
Classification: Unclassified
Component: [OLD] Core system (show other bugs)
Hardware: All Linux
: High normal
Assignee: Gentoo's Team for Core System packages
URL:
Whiteboard:
Keywords:
: 41888 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-01-14 07:14 UTC by Jochen Radmacher
Modified: 2006-02-24 11:14 UTC (History)
2 users (show)

See Also:
Package list:
Runtime testing required: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jochen Radmacher 2004-01-14 07:14:59 UTC
We are usning pxeboot+nfs+dhcp to boot our machines. The hostname is assigned by dhcp (kernel-dhcp). So the hostname is correct direcly after the kernel has started the network. Your script checks /etc/hostname and sets hostname to localhost since /etc/hostname does not exist. (We use the same /etc to boot multiple machines). With the attached patch this works correctly.

WARNING: i could not yet test if this breaks on a localy booting machine.

patch:
--- /etc/init.d/hostname.org    2004-01-14 15:10:43.433484680 +0100
+++ /etc/init.d/hostname        2004-01-14 15:13:48.000000000 +0100
@@ -19,7 +19,10 @@
 checkconfig() {
        if [ ! -f /etc/hostname ] || [ -z "$(< /etc/hostname)" ]
        then
-               eerror "You need to set /etc/hostname to a valid hostname"
+               if [ -n"`/bin/hostname`" ]
+               then
+                       eerror "You need to set /etc/hostname to a valid hostname"
+               fi
                return 1
        fi
 }
@@ -43,8 +46,13 @@
        if checkconfig
        then
                myhost="$(< /etc/hostname)"
+       else
+        if [ -n"`/bin/hostname`" ]
+        then
+         myhost="`/bin/hostname`"
+        fi
        fi
-
+
        ebegin "Setting hostname to ${myhost}"
        /bin/hostname "${myhost}"
        retval=$?
Comment 1 Jochen Radmacher 2004-01-16 08:30:31 UTC
I discovered a small typo:
+               if [ -n"`/bin/hostname`" ]
+               then
+                       eerror "You need to set /etc/hostname to a valid hostname"
+               fi
must read:
+               if [ -z"`/bin/hostname`" ]
+               then
+                       eerror "You need to set /etc/hostname to a valid hostname"
+               fi
Comment 2 SpanKY gentoo-dev 2004-10-03 01:31:47 UTC
*** Bug 41888 has been marked as a duplicate of this bug. ***
Comment 3 SpanKY gentoo-dev 2004-10-09 16:35:35 UTC
the behavior in cvs is now:

if /etc/hostname is setup, then that value will be used

if /etc/hostname is not setup, and /bin/hostname returns "" or "(none)", then the hostname will be set to 'localhost', otherwise if /bin/hostname returns something, that value wont be screwed around with
Comment 4 Adam Carheden 2006-02-24 11:14:50 UTC
Are you guys sure this bug is resolved? Below is the code from stage3-pentium4-2005.1-r1.tar.bz2:/etc/init.d/hostname

Problem:
Looks like hostname is set to ${myhost} no matter what on line 27. myhost is set on lines 10, 16, 21, and 23. But the last two reside within opposite parts of an if-else clause, so myhost will ALWAYS be set to one of those two values, ignoring the values set on lines 10 or 16.

Fix:
Lines 10-17 should be removed. Alternativly, insert at line 25 the following:       local kernelhost=$(/bin/hostname 2>/dev/null)
if [[ -n ${kernelhost} ]] && [[ ! ${myhost} == "(none)" ]] ; then
  myhost="${kernelhost}"
fi
if [ -z "${myhost}" ] ; then
  myhost="localhost"
fi

stage3-pentium4-2005.1-r1.tar.bz2:/etc/init.d/hostname:
09 start() {
10      local myhost=$(/bin/hostname 2>/dev/null)
11      local retval=0
12 
13       # If the hostname is already set via the kernel, and /etc/hostname
14       # isn't setup, then we shouldn't go reseting the configuration #38172.
15       if [[ -z ${myhost} ]] || [[ ${myhost} == "(none)" ]] ; then
16               myhost="localhost"
17       fi
18
19       if [[ -f /etc/hostname ]] ; then
20              ewarn "You should stop using /etc/hostname and use /etc/conf.d/hostname"
21               myhost=$(</etc/hostname)
22       else
23               myhost=${HOSTNAME}
24       fi
25
26       ebegin "Setting hostname to ${myhost}"
27       /bin/hostname "${myhost}"
28       retval=$?
29       eend ${retval} "Failed to set the hostname"
30
31       if [[ ${retval} -eq 0 ]] ; then
32               # setup $HOSTNAME, ignore errors in case /etc is readonly.
33               echo "HOSTNAME=\"${myhost}\"" 2>/dev/null > /etc/env.d/01hostname
34       fi
35
36        return ${retval}
37 }