From 43b5c8a4df961f5346e2ce176d6efe7495ddff14 Mon Sep 17 00:00:00 2001 From: Benjamin Drung Date: Tue, 24 Apr 2018 17:54:10 +0200 Subject: [PATCH] Fix resolving FQDN given by DHCP 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 | 10 ++++++---- components/9990-networking.sh | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/backend/initramfs-tools/live.hook b/backend/initramfs-tools/live.hook index 5970e7d..1817814 100755 --- a/backend/initramfs-tools/live.hook +++ b/backend/initramfs-tools/live.hook @@ -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 . diff --git a/components/9990-networking.sh b/components/9990-networking.sh index 8e059c9..2927107 100755 --- a/components/9990-networking.sh +++ b/components/9990-networking.sh @@ -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 <