]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
fix(dracut.sh): do not use uname to detect kernel version in a container
authorJo Zzsi <jozzsicsataban@gmail.com>
Wed, 16 Jul 2025 14:21:01 +0000 (10:21 -0400)
committerLaszlo <laszlo.gombos@gmail.com>
Fri, 18 Jul 2025 15:37:47 +0000 (11:37 -0400)
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

index 387b880febee90bd774ab735a77b319c74a339c4..e22892e627c6e7e6c91aff692dcbf8de5604563a 100755 (executable)
--- 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}