From: Benjamin Drung <benjamin.drung@canonical.com>
Date: Mon, 4 Aug 2025 14:41:41 +0200
Subject: fix(dracut): do not call uname -r in chroot environments

Running dracut in a schroot environment (even with `systemd-detect-virt`
installed) will pick `uname -r` as kernel version despite that kernel
not being available.

So also check `systemd-detect-virt --chroot` before relying on
`uname -r`.

Forwarded: https://github.com/dracut-ng/dracut-ng/pull/1483
---
 dracut.sh   | 2 +-
 lsinitrd.sh | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/dracut.sh b/dracut.sh
index e6adffe..d0d8bf2 100755
--- a/dracut.sh
+++ b/dracut.sh
@@ -969,7 +969,7 @@ export DRACUT_LOG_LEVEL=warning
 export add_dlopen_features="" omit_dlopen_features=""
 
 if ! [[ $kernel ]] && [[ $regenerate_all_l != "yes" ]]; then
-    if type -P systemd-detect-virt &> /dev/null && ! systemd-detect-virt -c &> /dev/null; then
+    if type -P systemd-detect-virt &> /dev/null && ! systemd-detect-virt -c &> /dev/null && ! systemd-detect-virt -r &> /dev/null; then
         kernel="$(uname -r)"
     else
         # shellcheck disable=SC2012
diff --git a/lsinitrd.sh b/lsinitrd.sh
index 86b2c5a..af7233a 100755
--- a/lsinitrd.sh
+++ b/lsinitrd.sh
@@ -97,7 +97,7 @@ while (($# > 0)); do
 done
 
 if ! [[ $KERNEL_VERSION ]]; then
-    if type -P systemd-detect-virt &> /dev/null && ! systemd-detect-virt -c &> /dev/null; then
+    if type -P systemd-detect-virt &> /dev/null && ! systemd-detect-virt -c &> /dev/null && ! systemd-detect-virt -r &> /dev/null; then
         KERNEL_VERSION="$(uname -r)"
     else
         # shellcheck disable=SC2012
