From 635d8b298848ff0e8e634c61d23fc318ab55d8d0 Mon Sep 17 00:00:00 2001 From: Chris Novakovic Date: Sun, 9 Jul 2017 00:04:01 +0100 Subject: [PATCH] initramfs: enable DNS if network is up In certain situations, it is desirable for hostnames to be resolvable in the initramfs (e.g., when a hostname is specified as the NFS server in the "boot" or "disk" kernel parameters). If the network is up when the init script is executed (e.g., when the "ip" kernel parameter is specified) and a DNS server is known, write it to /etc/resolv.conf so it can be used for DNS requests. --- packages/devel/glibc/config/nsswitch-init.conf | 3 +++ .../config/{nsswitch.conf => nsswitch-target.conf} | 0 packages/devel/glibc/package.mk | 10 +++++++++- packages/sysutils/busybox/scripts/init | 4 ++++ 4 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 packages/devel/glibc/config/nsswitch-init.conf rename packages/devel/glibc/config/{nsswitch.conf => nsswitch-target.conf} (100%) diff --git a/packages/devel/glibc/config/nsswitch-init.conf b/packages/devel/glibc/config/nsswitch-init.conf new file mode 100644 index 0000000000..eba159e8fc --- /dev/null +++ b/packages/devel/glibc/config/nsswitch-init.conf @@ -0,0 +1,3 @@ +# This is just enough to get DNS resolution working in the initramfs + +hosts: dns diff --git a/packages/devel/glibc/config/nsswitch.conf b/packages/devel/glibc/config/nsswitch-target.conf similarity index 100% rename from packages/devel/glibc/config/nsswitch.conf rename to packages/devel/glibc/config/nsswitch-target.conf diff --git a/packages/devel/glibc/package.mk b/packages/devel/glibc/package.mk index 31fb59aeda..a9d589f94e 100644 --- a/packages/devel/glibc/package.mk +++ b/packages/devel/glibc/package.mk @@ -151,7 +151,7 @@ post_makeinstall_target() { # create default configs mkdir -p $INSTALL/etc - cp $PKG_DIR/config/nsswitch.conf $INSTALL/etc + cp $PKG_DIR/config/nsswitch-target.conf $INSTALL/etc/nsswitch.conf cp $PKG_DIR/config/host.conf $INSTALL/etc cp $PKG_DIR/config/gai.conf $INSTALL/etc @@ -176,8 +176,16 @@ makeinstall_init() { cp -PR $PKG_BUILD/.$TARGET_NAME/math/libm.so* $INSTALL/usr/lib cp -PR $PKG_BUILD/.$TARGET_NAME/nptl/libpthread.so* $INSTALL/usr/lib cp -PR $PKG_BUILD/.$TARGET_NAME/rt/librt.so* $INSTALL/usr/lib + cp -PR $PKG_BUILD/.$TARGET_NAME/resolv/libnss_dns.so* $INSTALL/usr/lib + cp -PR $PKG_BUILD/.$TARGET_NAME/resolv/libresolv.so* $INSTALL/usr/lib if [ "$TARGET_ARCH" = "arm" -a "$TARGET_FLOAT" = "hard" ]; then ln -sf ld.so $INSTALL/usr/lib/ld-linux.so.3 fi } + +post_makeinstall_init() { +# create default configs + mkdir -p $INSTALL/etc + cp $PKG_DIR/config/nsswitch-init.conf $INSTALL/etc/nsswitch.conf +} diff --git a/packages/sysutils/busybox/scripts/init b/packages/sysutils/busybox/scripts/init index 20f8651fd7..b1bf4a93d6 100755 --- a/packages/sysutils/busybox/scripts/init +++ b/packages/sysutils/busybox/scripts/init @@ -1003,6 +1003,10 @@ [ -f "/sysroot/usr/lib/systemd/systemd" ] || error "final_check" "Could not find systemd!" } + # If the network is up (due to the use of the "ip" kernel parameter) and a DNS + # server is known, allow the libc resolver to use it + grep '^\(nameserver\|domain\) ' /proc/net/pnp | grep -v '^nameserver 0\.0\.0\.0$' > /etc/resolv.conf + if [ "${boot%%=*}" = "FILE" ]; then error "check arguments" "boot argument can't be FILE type..." fi