Fix resolving FQDN given by DHCP
authorBenjamin Drung <benjamin.drung@profitbricks.com>
Tue, 24 Apr 2018 15:54:10 +0000 (17:54 +0200)
committerBenjamin Drung <benjamin.drung@profitbricks.com>
Mon, 30 Apr 2018 12:11:00 +0000 (14:11 +0200)
The DHCP can specify a host and domain name for the system. live-boot
fills `/etc/hostname` and `/etc/resolv.conf` with the data from the DHCP
server, but resolving the fully qualified domain name (FQDN) fails, when
the host is not specified in DNS:

```
(initramfs) hostname -f
hostname: example-host: Host name lookup failure
```

To make the FQDN resolvable without DNS, `libnss_files.so` needs to be
included in the initramfs, `/etc/nsswitch.conf` need to prefer `files`
over `dns`, and `/etc/hosts` needs to be filled with the data from the
DHCP server.

backend/initramfs-tools/live.hook
components/9990-networking.sh

index 5970e7d..1817814 100755 (executable)
@@ -240,13 +240,15 @@ fi
 # /lib/libnss_files.so.*:  /etc/hosts and /etc/passwd
 # /lib/libnss_compat.so.*: /etc/passwd
 
-for _SHLIB in $(find /lib -name 'libnss_dns.so.*')
+for _SHLIB in $(find /lib -name 'libnss_dns.so.*' -o -name 'libnss_files.so.*')
 do
        copy_exec "${_SHLIB}"
 done
 
-# might be needed if /etc/hosts is used
-#mkdir -p "${DESTDIR}/etc"
-#cp -p /etc/nsswitch.conf "${DESTDIR}/etc"
+if [ ! -e "${DESTDIR}/etc/nsswitch.conf" ]
+then
+       # Needed to make "hostname -f" honor the domainname provided by DHCP
+       echo "hosts: files dns" > "${DESTDIR}/etc/nsswitch.conf"
+fi
 
 [ "${QUIET}" ] || echo .
index 8e059c9..2927107 100755 (executable)
@@ -131,6 +131,22 @@ do_netsetup ()
                        HWADDR="$(cat /sys/class/net/${interface}/address)"
                fi
 
+               # Only create /etc/hosts if FQDN is known (to let 'hostname -f' query
+               # this file). Otherwise DNS will be queried to determine the FQDN.
+               if [ ! -e "/etc/hosts" ] && [ -n "${DNSDOMAIN}" ]
+               then
+                       echo "Creating /etc/hosts"
+                       cat > /etc/hosts <<EOF
+127.0.0.1      localhost
+127.0.1.1      ${HOSTNAME}.${DNSDOMAIN}        ${HOSTNAME}
+
+# The following lines are desirable for IPv6 capable hosts
+::1     localhost ip6-localhost ip6-loopback
+ff02::1 ip6-allnodes
+ff02::2 ip6-allrouters
+EOF
+               fi
+
                if [ ! -e "/etc/resolv.conf" ]
                then
                        echo "Creating /etc/resolv.conf"