From 2b2debd7947b7d5a357c1a89691a75dfd3565747 Mon Sep 17 00:00:00 2001 From: Jo Zzsi Date: Wed, 16 Jul 2025 10:21:01 -0400 Subject: [PATCH] fix(dracut.sh): do not use uname to detect kernel version in a container A big general papercut with dracut right now in that it always assumes the kernel it's running on is the kernel to target. This commit lets dracut to detect that it's in a container (e.g. systemd-detect-virt -c), and check for a single /usr/lib/modules/$kver directory and automatically use that kernel. Currently this kernel version detection has three copies in the source (dracut.sh, lsinitrd.sh,test-functions). This commit keeps the logic for the three copies the same. As a follow-up commit, we should try to actually share the code for this logic instead of copying it. Fixes https://github.com/dracut-ng/dracut-ng/issues/1455. --- dracut.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dracut.sh b/dracut.sh index 387b880fe..e22892e62 100755 --- a/dracut.sh +++ b/dracut.sh @@ -1073,7 +1073,14 @@ if [[ $regenerate_all == "yes" ]]; then fi if ! [[ $kernel ]]; then - kernel=$(uname -r) + if type -P systemd-detect-virt &> /dev/null && container=$(systemd-detect-virt -c) &> /dev/null; then + dinfo "*** Detected container: $container ***" + # shellcheck disable=SC2012 + kernel="$(cd /lib/modules && ls -1 | tail -1)" + # shellcheck disable=SC2012 + [[ $kernel ]] || kernel="$(cd /usr/lib/modules && ls -1 | tail -1)" + fi + [[ $kernel ]] || kernel="$(uname -r)" fi DRACUT_PATH=${DRACUT_PATH:-/sbin /bin /usr/sbin /usr/bin} -- 2.47.3