From: Jo Zzsi Date: Wed, 16 Jul 2025 14:21:01 +0000 (-0400) Subject: fix(dracut.sh): do not use uname to detect kernel version in a container X-Git-Tag: 108~31 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2b2debd;p=thirdparty%2Fdracut-ng.git 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. --- 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}