2d7ffa
From ecd196732f36064aad675e18bf77b2b678673d63 Mon Sep 17 00:00:00 2001
2d7ffa
From: Kairui Song <kasong@redhat.com>
2d7ffa
Date: Wed, 4 Jul 2018 17:21:37 +0800
2d7ffa
Subject: [PATCH] Introduce tri-state hostonly mode
2d7ffa
2d7ffa
Add a new option --hostonly-mode which accept an <mode> parameter, so we have a tri-state hostonly mode:
2d7ffa
2d7ffa
    * generic: by passing "--no-hostonly" or not passing anything.
2d7ffa
               "--hostonly-mode" has no effect in such case.
2d7ffa
    * sloppy: by passing "--hostonly --hostonly-mode sloppy". This
2d7ffa
              is also the default mode when only "--hostonly" is given.
2d7ffa
    * strict: by passing "--hostonly --hostonly-mode strict".
2d7ffa
2d7ffa
Sloppy mode is the original hostonly mode, the new introduced strict
2d7ffa
mode will allow modules to ignore more drivers or do some extra job to
2d7ffa
save memory and disk space, while making the image less portable.
2d7ffa
2d7ffa
Also introduced a helper function "optional_hostonly" to make it
2d7ffa
easier for modules to leverage new hostonly mode.
2d7ffa
2d7ffa
To force install modules only in sloppy hostonly mode, use the form:
2d7ffa
2d7ffa
hostonly="$(optional_hostonly)" instmods <modules>
2d7ffa
2d7ffa
Signed-off-by: Kairui Song <kasong@redhat.com>
2d7ffa
2d7ffa
Cherry-picked from: a695250ec7db21359689e50733c6581a8d211215
2d7ffa
Resolves: #1599592
2d7ffa
---
2d7ffa
 dracut-functions.sh | 11 +++++++++++
2d7ffa
 dracut.sh           | 34 ++++++++++++++++++++++++++++++++++
2d7ffa
 2 files changed, 45 insertions(+)
2d7ffa
2d7ffa
diff --git a/dracut-functions.sh b/dracut-functions.sh
2d7ffa
index b7568bd9..4387168a 100755
2d7ffa
--- a/dracut-functions.sh
2d7ffa
+++ b/dracut-functions.sh
2d7ffa
@@ -1770,3 +1770,14 @@ lvm_internal_dev() {
2d7ffa
     [[ ${DM_LV_LAYER} ]] || [[ ! -L /dev/${DM_VG_NAME}/${DM_LV_NAME} ]]
2d7ffa
 }
2d7ffa
 
2d7ffa
+# Use with form hostonly="$(optional_hostonly)" inst_xxxx <args>
2d7ffa
+# If hosotnly mode is set to "strict", hostonly restrictions will still
2d7ffa
+# be applied, else will ignore hostonly mode and try to install all
2d7ffa
+# given modules.
2d7ffa
+optional_hostonly() {
2d7ffa
+    if [[ $hostonly_mode = "strict" ]]; then
2d7ffa
+        printf -- "$hostonly"
2d7ffa
+    else
2d7ffa
+        printf ""
2d7ffa
+    fi
2d7ffa
+}
2d7ffa
diff --git a/dracut.sh b/dracut.sh
2d7ffa
index 4f324439..52835872 100755
2d7ffa
--- a/dracut.sh
2d7ffa
+++ b/dracut.sh
2d7ffa
@@ -141,6 +141,21 @@ Creates initial ramdisk images for preloading modules
2d7ffa
   -H, --hostonly        Host-Only mode: Install only what is needed for
2d7ffa
                         booting the local host instead of a generic host.
2d7ffa
   -N, --no-hostonly     Disables Host-Only mode
2d7ffa
+  --hostonly-mode <mode>
2d7ffa
+                        Specify the hostonly mode to use. <mode> could be
2d7ffa
+                        one of "sloppy" or "strict". "sloppy" mode is used
2d7ffa
+                        by default.
2d7ffa
+                        In "sloppy" hostonly mode, extra drivers and modules
2d7ffa
+                        will be installed, so minor hardware change won't make
2d7ffa
+                        the image unbootable (eg. changed keyboard), and the
2d7ffa
+                        image is still portable among similar hosts.
2d7ffa
+                        With "strict" mode enabled, anything not necessary
2d7ffa
+                        for booting the local host in its current state will
2d7ffa
+                        not be included, and modules may do some extra job
2d7ffa
+                        to save more space. Minor change of hardware or
2d7ffa
+                        environment could make the image unbootable.
2d7ffa
+                        DO NOT use "strict" mode unless you know what you
2d7ffa
+                        are doing.
2d7ffa
   --hostonly-cmdline    Store kernel command line arguments needed
2d7ffa
                         in the initramfs
2d7ffa
   --no-hostonly-cmdline Do not store kernel command line arguments needed
2d7ffa
@@ -364,6 +379,7 @@ rearrange_params()
2d7ffa
         --long host-only \
2d7ffa
         --long no-hostonly \
2d7ffa
         --long no-host-only \
2d7ffa
+        --long hostonly-mode: \
2d7ffa
         --long hostonly-cmdline \
2d7ffa
         --long no-hostonly-cmdline \
2d7ffa
         --long no-hostonly-default-device \
2d7ffa
@@ -548,6 +564,8 @@ while :; do
2d7ffa
                        hostonly_l="yes" ;;
2d7ffa
         -N|--no-hostonly|--no-host-only)
2d7ffa
                        hostonly_l="no" ;;
2d7ffa
+        --hostonly-mode)
2d7ffa
+                       hostonly_mode_l="$2";           PARMS_TO_STORE+=" '$2'"; shift;;
2d7ffa
         --hostonly-cmdline)
2d7ffa
                        hostonly_cmdline_l="yes" ;;
2d7ffa
         --hostonly-i18n)
2d7ffa
@@ -803,6 +821,7 @@ stdloglvl=$((stdloglvl + verbosity_mod_l))
2d7ffa
 [[ $prefix = "/" ]] && unset prefix
2d7ffa
 [[ $hostonly_l ]] && hostonly=$hostonly_l
2d7ffa
 [[ $hostonly_cmdline_l ]] && hostonly_cmdline=$hostonly_cmdline_l
2d7ffa
+[[ $hostonly_mode_l ]] && hostonly_mode=$hostonly_mode_l
2d7ffa
 [[ $i18n_install_all_l ]] && i18n_install_all=$i18n_install_all_l
2d7ffa
 [[ $persistent_policy_l ]] && persistent_policy=$persistent_policy_l
2d7ffa
 [[ $use_fstab_l ]] && use_fstab=$use_fstab_l
2d7ffa
@@ -840,6 +859,21 @@ fi
2d7ffa
 [[ $hostonly = yes ]] && hostonly="-h"
2d7ffa
 [[ $hostonly != "-h" ]] && unset hostonly
2d7ffa
 
2d7ffa
+case $hostonly_mode in
2d7ffa
+    '')
2d7ffa
+        [[ $hostonly ]] && hostonly_mode="sloppy" ;;
2d7ffa
+    sloppy|strict)
2d7ffa
+        if [[ ! $hostonly ]]; then
2d7ffa
+            unset hostonly_mode
2d7ffa
+        fi
2d7ffa
+        ;;
2d7ffa
+    *)
2d7ffa
+        printf "%s\n" "dracut: Invalid hostonly mode '$hostonly_mode'." >&2
2d7ffa
+        exit 1
2d7ffa
+esac
2d7ffa
+
2d7ffa
+
2d7ffa
+
2d7ffa
 readonly TMPDIR="$tmpdir"
2d7ffa
 readonly DRACUT_TMPDIR="$(mktemp -p "$TMPDIR/" -d -t dracut.XXXXXX)"
2d7ffa
 [ -d "$DRACUT_TMPDIR" ] || {