From 6be3e1787050f4dc338134c6204d2228e998d1c0 Mon Sep 17 00:00:00 2001
From: Shreenidhi Shedi <sshedi@vmware.com>
Date: Mon, 12 Jul 2021 22:37:18 +0530
Subject: [PATCH] Adjust host_only flag based on running environment

host_only decides the nature of initrd.
If initrd is getting generated in docker/chroot environemnt, it should
be generic. Otherwise cloud-images & ova will have a host specific
initrd which may fail to boot on a different host.

Added a command line option to manually set/unset host_only value.

Signed-off-by: Shreenidhi Shedi <sshedi@vmware.com>
---
 mkinitrd-dracut.sh | 33 +++++++++++++++++++++++++++++++--
 1 file changed, 31 insertions(+), 2 deletions(-)

diff --git a/mkinitrd-dracut.sh b/mkinitrd-dracut.sh
index c1bcf197..0ef65bd0 100755
--- a/mkinitrd-dracut.sh
+++ b/mkinitrd-dracut.sh
@@ -6,6 +6,8 @@ boot_dir="/boot"
 quiet=0
 host_only=0
 force=0
+set_hostonly=0
+no_hostonly=0
 
 error() { echo "$@" >&2; }
 
@@ -15,6 +17,8 @@ usage () {
     $cmd "usage: ${0##*/} [--version] [--help] [-v] [-f] [--preload <module>]"
     $cmd "       [--image-version] [--with=<module>]"
     $cmd "       [--nocompress]"
+    $cmd "       [--set-hostonly] - Generates host specific initrd.img"
+    $cmd "       [--no-hostonly] - Generates generic initrd.img"
     $cmd "       <initrd-image> <kernel-version>"
     $cmd ""
     $cmd "       (ex: ${0##*/} /boot/initramfs-$kver.img $kver)"
@@ -58,8 +62,6 @@ default_kernel_images() {
         kernels="$kernels $kernel_version"
         targets="$targets $boot_dir/initrd.img-$kernel_version"
     done
-    host_only=1
-    force=1
 }
 
 while (($# > 0)); do
@@ -82,6 +84,8 @@ while (($# > 0)); do
         --rootfs|-d) read_arg rootfs "$@" || shift $?
             dracut_args="${dracut_args} --filesystems $rootfs";;
         --nocompress) dracut_args="$dracut_args --no-compress";;
+        --set-hostonly) set_hostonly=1;;
+        --no-hostonly) no_hostonly=1;;
         --help) usage -n;;
         --builtin) ;;
         --without*) ;;
@@ -144,6 +148,31 @@ done
 targets=( $targets )
 [[ $kernels ]] && kernels=( $kernels )
 
+# don't set hostonly flag if running in docker env
+# if set initrd will be incomplete
+# https://fedoraproject.org/wiki/Features/DracutHostOnly#Detailed_Description
+# https://man7.org/linux/man-pages/man5/dracut.conf.5.html
+if [ ${set_hostonly} -eq 0 ]; then
+  if grep -qc docker /proc/self/cgroup; then
+    echo "--- Generating initrd under docker environment ---"
+    host_only=0
+  elif [ "$(stat -c %d:%i /)" != "$(stat -c %d:%i /proc/1/root/.)" ]; then
+    # set host_only=0 if initrd is generated in chroot
+    # otherwise cloud images generated in a Photon host will have bad initrd
+    echo "--- Generating initrd under chroot environment ---"
+    host_only=0
+  else
+    host_only=1
+  fi
+else
+  host_only=1
+fi
+force=1
+
+if [ ${no_hostonly} -eq 1 ]; then
+  host_only=0
+fi
+
 [[ $host_only == 1 ]] && dracut_args="${dracut_args} -H"
 [[ $force == 1 ]]     && dracut_args="${dracut_args} -f"
 
-- 
2.17.1