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.
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}