Package: dracut
Version: 113-1
Severity: important
Tags: patch
Forwarded: https://github.com/dracut-ng/dracut/pull/2845
User: [email protected]
Usertags: origin-ubuntu tenacious ubuntu-patch

Dear Maintainer,

The 61-chrony autopkgtest added in dracut 112-8 / 113-1 fails on Debian 
unstable and Ubuntu tenacious with a chrony-wait.service timeout (upstream 
issue https://github.com/dracut-ng/dracut/issues/2541, Ubuntu bug 
https://bugs.launchpad.net/ubuntu/+source/dracut/+bug/2178220).

Root cause:
On Debian and Ubuntu, dracut-network uses the 11systemd-networkd module by 
default:
1. modules.d/11systemd-networkd/module-setup.sh does not install 
libnss_dns.so.* into the initramfs (unlike 35network-manager and 
35network-legacy).
2. modules.d/11systemd-networkd/networkd-config.sh does not register an 
initqueue/finished hook when rd.neednet=1 is set, so dracut-initqueue exits 
immediately when the root filesystem is local without executing 
initqueue/settled/99-networkd-run.sh.
3. modules.d/11systemd-networkd/networkd-run.sh does not write DHCP DNS servers 
(or kernel command-line nameserver= options) into /etc/resolv.conf, so chronyd 
cannot resolve pool.ntp.org.

The attached patch fixes all three issues in 11systemd-networkd and makes 
debian/tests/61-chrony and debian/tests/50-network pass.
Upstream PR: https://github.com/dracut-ng/dracut/pull/2845
From 3d06cd9764c0a96e5e6c202841fd5729c5c56b45 Mon Sep 17 00:00:00 2001
From: Viktor Pashaiev <[email protected]>
Date: Fri, 27 Nov 2026 13:49:49 +0100
Subject: [PATCH] fix(systemd-networkd): populate /etc/resolv.conf and wait for
 online

On distributions where dracut-network defaults to 11systemd-networkd,
the 61-chrony test times out in chrony-wait.service because:

1. module-setup.sh does not install libnss_dns.so.* into the initramfs.
2. networkd-config.sh does not register an initqueue/finished hook when
   rd.neednet=1 is set, so dracut-initqueue exits immediately when the
   root device is local without running networkd-run.sh.
3. networkd-run.sh does not populate /etc/resolv.conf from DHCP or
   kernel command-line DNS servers, leaving chronyd unable to resolve
   pool.ntp.org.

Install libnss_dns.so.* in 11systemd-networkd, register the
initqueue/finished/networkd.sh hook when rd.neednet=1, and populate
/etc/resolv.conf from kernel command-line nameserver= arguments and
/run/systemd/netif/state DNS options in networkd-run.sh.

Fixes: https://github.com/dracut-ng/dracut/issues/2541
---
 modules.d/11systemd-networkd/module-setup.sh  |   2 +
 .../11systemd-networkd/networkd-config.sh     |   2 +
 modules.d/11systemd-networkd/networkd-run.sh  |  30 ++++
 test/TEST-61-CHRONY/test.sh                   | 142 ++++++++++++++++++
 4 files changed, 176 insertions(+)

diff --git a/modules.d/11systemd-networkd/module-setup.sh b/modules.d/11systemd-networkd/module-setup.sh
index 484ccd3..9f38a06 100755
--- a/modules.d/11systemd-networkd/module-setup.sh
+++ b/modules.d/11systemd-networkd/module-setup.sh
@@ -72,6 +72,8 @@ install() {
     inst_hook cmdline 99 "$moddir"/networkd-config.sh
     inst_hook initqueue/settled 99 "$moddir"/networkd-run.sh
 
+    inst_libdir_file {"tls/$DRACUT_ARCH/",tls/,"$DRACUT_ARCH/",}"libnss_dns.so.*"
+
     # Enable systemd type units
     for i in \
         systemd-networkd.service \
diff --git a/modules.d/11systemd-networkd/networkd-config.sh b/modules.d/11systemd-networkd/networkd-config.sh
index b6f1ae5..bfdb120 100755
--- a/modules.d/11systemd-networkd/networkd-config.sh
+++ b/modules.d/11systemd-networkd/networkd-config.sh
@@ -38,6 +38,8 @@ if [ -n "$netroot" ] || [ -e /tmp/net.ifaces ]; then
 fi
 
 if getargbool 0 rd.neednet; then
+    # shellcheck disable=SC2016
+    echo 'for i in /tmp/networkd.*.done; do [ -f "$i" ] && exit 0; done; exit 1' > "$hookdir"/initqueue/finished/networkd.sh
     mkdir -p /run/networkd/initrd
     : > /run/networkd/initrd/neednet
 fi
diff --git a/modules.d/11systemd-networkd/networkd-run.sh b/modules.d/11systemd-networkd/networkd-run.sh
index 791b091..0a56838 100755
--- a/modules.d/11systemd-networkd/networkd-run.sh
+++ b/modules.d/11systemd-networkd/networkd-run.sh
@@ -74,6 +74,36 @@ for ifpath in /sys/class/net/*; do
             fi
         fi
 
+        if [ ! -L /etc/resolv.conf ] && [ ! -s /etc/resolv.conf ]; then
+            dns_servers=""
+            if [ -r /run/systemd/netif/state ]; then
+                dns_servers=$(sed -n "s/^DNS=//p" /run/systemd/netif/state)
+            fi
+            nl="
+"
+            resolv=""
+            set -f
+            for s in $(getargs nameserver) $dns_servers; do
+                s=${s%%#*}
+                case "$s" in
+                    \[*\]*)
+                        s=${s#\[}
+                        s=${s%%\]*}
+                        ;;
+                    *.*.*.*:*)
+                        s=${s%:*}
+                        ;;
+                esac
+                [ -n "$s" ] || continue
+                case "${nl}${resolv}" in
+                    *"${nl}nameserver ${s}${nl}"*) ;;
+                    *) resolv="${resolv}nameserver ${s}${nl}" ;;
+                esac
+            done
+            set +f
+            [ -n "$resolv" ] && printf '%s' "$resolv" > /etc/resolv.conf
+        fi
+
         source_hook initqueue/online "$ifname"
         /sbin/netroot "$ifname"
 
diff --git a/test/TEST-61-CHRONY/test.sh b/test/TEST-61-CHRONY/test.sh
index 4fc53ac..de3e1b2 100755
--- a/test/TEST-61-CHRONY/test.sh
+++ b/test/TEST-61-CHRONY/test.sh
@@ -18,6 +18,134 @@ test_check() {
     require_binaries_for_test chronyd openssl python3 systemctl
 }
 
+test_networkd_unit() {
+    local work rc1=0 rc2=0
+    work=$(mktemp -d "$TESTDIR/networkd-unit.XXXXXX")
+
+    # 1. Verify networkd-config.sh rd.neednet=0 (negative) vs rd.neednet=1 (positive) finished hook
+    (
+        set +u
+        netroot=""
+        hookdir="$work/hooks"
+        mkdir -p "$hookdir/initqueue/finished" "$work/run/systemd/network" "$work/etc/cmdline.d" "$work/run/networkd/initrd"
+        sed -e "s|/tmp/|$work/tmp/|g" \
+            -e "s|/run/systemd/network|$work/run/systemd/network|g" \
+            -e "s|/etc/cmdline.d|$work/etc/cmdline.d|g" \
+            -e "s|/run/networkd/initrd|$work/run/networkd/initrd|g" \
+            -e "s|/usr/lib/dracut/default.network|$PKGLIBDIR/modules.d/11systemd-networkd/dracut-default.network|g" \
+            "$PKGLIBDIR/modules.d/11systemd-networkd/networkd-config.sh" > "$work/networkd-config-test.sh"
+
+        # shellcheck disable=SC2317,SC2329
+        getcmdline() { echo "$FAKE_CMDLINE"; }
+        # shellcheck disable=SC2317,SC2329
+        systemctl() { :; }
+        # shellcheck disable=SC2317,SC2329
+        getargbool() {
+            case "$FAKE_CMDLINE" in
+                *rd.neednet=1*) return 0 ;;
+                *) return 1 ;;
+            esac
+        }
+
+        # Negative case: rd.neednet=0 must not create initqueue/finished/networkd.sh
+        FAKE_CMDLINE="rd.neednet=0"
+        # shellcheck disable=SC1091
+        . "$work/networkd-config-test.sh"
+        [ ! -e "$hookdir/initqueue/finished/networkd.sh" ] || exit 1
+
+        # Positive case: rd.neednet=1 creates networkd.sh and exits 1 before .done, 0 after .done
+        FAKE_CMDLINE="rd.neednet=1"
+        # shellcheck disable=SC1091
+        . "$work/networkd-config-test.sh"
+        [ -s "$hookdir/initqueue/finished/networkd.sh" ] || exit 1
+        if sh "$hookdir/initqueue/finished/networkd.sh"; then
+            exit 1
+        fi
+        mkdir -p "$work/tmp"
+        : > "$work/tmp/networkd.eth0.done"
+        sh "$hookdir/initqueue/finished/networkd.sh" || exit 1
+    ) || rc1=$?
+
+    # 2. Verify networkd-run.sh /etc/resolv.conf generation from /run/systemd/netif/state and nameserver=
+    (
+        set +u
+        mkdir -p "$work/sys/class/net/eth0" "$work/run/systemd/netif/leases" "$work/tmp" "$work/etc"
+        echo 2 > "$work/sys/class/net/eth0/ifindex"
+
+        sed -e "s|/tmp/|$work/tmp/|g" \
+            -e "s|/sys/class/net|$work/sys/class/net|g" \
+            -e "s|/run/systemd/netif|$work/run/systemd/netif|g" \
+            -e "s|/etc/resolv.conf|$work/etc/resolv.conf|g" \
+            -e "s|/usr/lib/systemd/systemd-networkd-wait-online|true|g" \
+            -e "s|/sbin/netroot|true|g" \
+            "$PKGLIBDIR/modules.d/11systemd-networkd/networkd-run.sh" > "$work/networkd-run-test.sh"
+
+        # shellcheck disable=SC2317,SC2329
+        source_hook() { :; }
+        # shellcheck disable=SC2317,SC2329
+        escape() { printf "%s" "$1"; }
+        # shellcheck disable=SC2317,SC2329
+        trim() { printf "%s" "$1" | sed "s/^[[:space:]]*//;s/[[:space:]]*$//"; }
+        # shellcheck disable=SC2317,SC2329
+        getargs() { printf "%s\n" "${FAKE_NAMESERVERS-}"; }
+        # shellcheck disable=SC2317,SC2329
+        networkctl() { return 1; }
+
+        # Case A (negative): no state file and no cmdline nameserver -> /etc/resolv.conf not created
+        rm -f "$work/tmp/networkd.eth0.done"
+        FAKE_NAMESERVERS=""
+        # shellcheck disable=SC1091
+        . "$work/networkd-run-test.sh"
+        [ ! -e "$work/etc/resolv.conf" ] || exit 1
+        [ -f "$work/tmp/networkd.eth0.done" ] || exit 1
+
+        # Case A2: no state file, cmdline nameserver only -> written from cmdline
+        rm -f "$work/tmp/networkd.eth0.done" "$work/etc/resolv.conf"
+        FAKE_NAMESERVERS="192.0.2.53 [2001:db8::53]:53"
+        # shellcheck disable=SC1091
+        . "$work/networkd-run-test.sh"
+        [ "$(cat "$work/etc/resolv.conf")" = "$(printf "%s\n" "nameserver 192.0.2.53" "nameserver 2001:db8::53")" ] || exit 1
+        rm -f "$work/etc/resolv.conf"
+
+        # Case B: state file DNS= (with :port#sni, %scope, [ipv6]:port) + cmdline nameserver= deduplication
+        rm -f "$work/tmp/networkd.eth0.done"
+        printf "DNS=10.0.2.3:53#dns.example 10.0.2.30:5353 [2606:4700:4700::1111]:53#one.one fe80::1%%2\n" > "$work/run/systemd/netif/state"
+        FAKE_NAMESERVERS="10.0.2.30 10.0.2.3"
+        # shellcheck disable=SC1091
+        . "$work/networkd-run-test.sh"
+        expected_b=$(printf "%s\n" "nameserver 10.0.2.30" "nameserver 10.0.2.3" "nameserver 2606:4700:4700::1111" "nameserver fe80::1%2")
+        [ "$(cat "$work/etc/resolv.conf")" = "$expected_b" ] || exit 1
+
+        # Case B2: existing empty /etc/resolv.conf is overwritten (! -s vs ! -e)
+        rm -f "$work/tmp/networkd.eth0.done"
+        : > "$work/etc/resolv.conf"
+        printf "DNS=10.0.2.3\n" > "$work/run/systemd/netif/state"
+        FAKE_NAMESERVERS=""
+        # shellcheck disable=SC1091
+        . "$work/networkd-run-test.sh"
+        [ "$(cat "$work/etc/resolv.conf")" = "nameserver 10.0.2.3" ] || exit 1
+
+        # Case C (negative): existing non-empty /etc/resolv.conf is preserved
+        rm -f "$work/tmp/networkd.eth0.done"
+        printf "DNS=9.9.9.9\n" > "$work/run/systemd/netif/state"
+        FAKE_NAMESERVERS="8.8.8.8"
+        # shellcheck disable=SC1091
+        . "$work/networkd-run-test.sh"
+        [ "$(cat "$work/etc/resolv.conf")" = "nameserver 10.0.2.3" ] || exit 1
+
+        # Case D (negative): symlinked /etc/resolv.conf is not overwritten
+        rm -f "$work/etc/resolv.conf" "$work/tmp/networkd.eth0.done"
+        ln -s "$work/etc/stub-resolv.conf" "$work/etc/resolv.conf"
+        # shellcheck disable=SC1091
+        . "$work/networkd-run-test.sh"
+        [ -L "$work/etc/resolv.conf" ] || exit 1
+        [ ! -e "$work/etc/stub-resolv.conf" ] || exit 1
+    ) || rc2=$?
+
+    rm -rf "$work"
+    [ "$rc1" -eq 0 ] && [ "$rc2" -eq 0 ]
+}
+
 client_run() {
     local nook="$1"
     local test_name="$2"
@@ -51,6 +179,10 @@ client_run() {
 test_run() {
     declare -a disk_args=()
 
+    if [[ -d "$PKGLIBDIR/modules.d/11systemd-networkd" ]]; then
+        test_networkd_unit || return 1
+    fi
+
     qemu_add_drive disk_args "$TESTDIR"/marker.img marker
     qemu_add_drive disk_args "$TESTDIR"/root.img root
 
@@ -109,6 +241,12 @@ test_setup() {
         echo "#!/bin/sh"
         echo "command -v warn > /dev/null || . /lib/dracut-lib.sh"
         echo 'warn "$(systemctl status chronyd.service)"'
+        echo 'if [ -f /usr/lib/systemd/systemd-networkd ]; then'
+        echo '    if ! grep -qx "nameserver 10.0.2.3" /etc/resolv.conf; then'
+        echo '        warn "Missing DHCP nameserver 10.0.2.3 in /etc/resolv.conf"'
+        echo '        umount /sysroot'
+        echo '    fi'
+        echo 'fi'
         echo "remote_file_content=\$(curl --cacert /etc/$SSL_CERT \"https://10.0.2.2:4443/remote-file.txt\";)"
         echo 'if [ "$remote_file_content" = "dracut-chrony-success" ]; then'
         echo '    warn "$remote_file_content"'
@@ -138,6 +276,10 @@ test_setup() {
         -i "$TESTDIR/fetch-remote-file.sh" "/var/lib/dracut/hooks/pre-pivot/10-fetch-remote-file.sh" \
         -i "$TESTDIR/chrony-less-timeout.conf" "/usr/lib/systemd/system/chrony-wait.service.d/chrony-less-timeout.conf" \
         -a "chrony url-lib ${USE_NETWORK}"
+
+    if lsinitrd "$TESTDIR/initramfs.testing" | grep -q "usr/lib/systemd/systemd-networkd$"; then
+        lsinitrd "$TESTDIR/initramfs.testing" | grep -q "libnss_dns\.so\." || return 1
+    fi
 }
 
 test_cleanup() {
-- 
2.43.0

Reply via email to