9abf8c
From 3085acbec79b3649ac906529778f86a14f330e62 Mon Sep 17 00:00:00 2001
9abf8c
From: Kairui Song <kasong@redhat.com>
9abf8c
Date: Wed, 21 Oct 2020 16:18:07 +0800
9abf8c
Subject: [PATCH] Add a --hostonly-nics option
9abf8c
9abf8c
Currently when network is enabled, dracut will install all network
9abf8c
drivers that are currently loaded, but some time only one NIC is needed
9abf8c
for the initramfs.
9abf8c
9abf8c
So for strict hostonly mode, add a --hostonly-nics option, user can
9abf8c
provide a list of NICs to be enabled, and only needed drivers for
9abf8c
specifed NICs will be installed so save space.
9abf8c
9abf8c
Signed-off-by: Kairui Song <kasong@redhat.com>
9abf8c
(cherry picked from commit 1e92f7285d42fbd3bc01f534f9bd646f6fdd9504)
9abf8c
9abf8c
Cherry-picked from: 1e92f7285d42fbd3bc01f534f9bd646f6fdd9504
9abf8c
Resolves: #1890041
9abf8c
9abf8c
Cherry-picked from: c69961b403c1c83bb4717e1df1470e71cd0245d1
9abf8c
Resolves: #1890041
9abf8c
---
9abf8c
 dracut.sh                                          |  6 ++++++
9abf8c
 modules.d/90kernel-network-modules/module-setup.sh | 18 +++++++++++++++---
9abf8c
 2 files changed, 21 insertions(+), 3 deletions(-)
9abf8c
9abf8c
diff --git a/dracut.sh b/dracut.sh
9abf8c
index e70dab16..01abe7a4 100755
9abf8c
--- a/dracut.sh
9abf8c
+++ b/dracut.sh
9abf8c
@@ -173,6 +173,8 @@ Creates initial ramdisk images for preloading modules
9abf8c
   --hostonly-i18n       Install only needed keyboard and font files according
9abf8c
                         to the host configuration (default).
9abf8c
   --no-hostonly-i18n    Install all keyboard and font files available.
9abf8c
+  --hostonly-nics [LIST]
9abf8c
+                        Only enable listed NICs in the initramfs.
9abf8c
   --persistent-policy [POLICY]
9abf8c
                         Use [POLICY] to address disks and partitions.
9abf8c
                         POLICY can be any directory name found in /dev/disk.
9abf8c
@@ -400,6 +402,7 @@ rearrange_params()
9abf8c
         --long kernel-image: \
9abf8c
         --long no-hostonly-i18n \
9abf8c
         --long hostonly-i18n \
9abf8c
+        --long hostonly-nics: \
9abf8c
         --long no-machineid \
9abf8c
         -- "$@")
9abf8c
 
9abf8c
@@ -561,6 +564,8 @@ while :; do
9abf8c
                        hostonly_cmdline_l="yes" ;;
9abf8c
         --hostonly-i18n)
9abf8c
                        i18n_install_all_l="no" ;;
9abf8c
+        --hostonly-nics)
9abf8c
+                       hostonly_nics_l+=("$2");           PARMS_TO_STORE+=" '$2'"; shift;;
9abf8c
         --no-hostonly-i18n)
9abf8c
                        i18n_install_all_l="yes" ;;
9abf8c
         --no-hostonly-cmdline)
9abf8c
@@ -720,6 +725,7 @@ unset NPATH
9abf8c
 (( ${#fstab_lines_l[@]} )) && fstab_lines+=( "${fstab_lines_l[@]}" )
9abf8c
 (( ${#install_items_l[@]} )) && install_items+=" ${install_items_l[@]} "
9abf8c
 (( ${#install_optional_items_l[@]} )) && install_optional_items+=" ${install_optional_items_l[@]} "
9abf8c
+(( ${#hostonly_nics_l[@]} )) && hostonly_nics+=" ${hostonly_nics_l[@]} "
9abf8c
 
9abf8c
 # these options override the stuff in the config file
9abf8c
 (( ${#dracutmodules_l[@]} )) && dracutmodules="${dracutmodules_l[@]}"
9abf8c
diff --git a/modules.d/90kernel-network-modules/module-setup.sh b/modules.d/90kernel-network-modules/module-setup.sh
9abf8c
index 600ef112..f36d31f7 100755
9abf8c
--- a/modules.d/90kernel-network-modules/module-setup.sh
9abf8c
+++ b/modules.d/90kernel-network-modules/module-setup.sh
9abf8c
@@ -14,15 +14,27 @@ depends() {
9abf8c
 installkernel() {
9abf8c
     # Include wired net drivers, excluding wireless
9abf8c
     local _arch=$(uname -m)
9abf8c
-    local _net_drivers='eth_type_trans|register_virtio_device|usbnet_open'
9abf8c
+    local _net_symbols='eth_type_trans|register_virtio_device|usbnet_open'
9abf8c
     local _unwanted_drivers='/(wireless|isdn|uwb|net/ethernet|net/phy|net/team)/'
9abf8c
+    local _net_drivers
9abf8c
 
9abf8c
     if [ "$_arch" = "s390" -o "$_arch" = "s390x" ]; then
9abf8c
-        _s390drivers="=drivers/s390/net"
9abf8c
+        dracut_instmods -o -P ".*${_unwanted_drivers}.*" -s "$_net_symbols" "=drivers/s390/net"
9abf8c
     fi
9abf8c
 
9abf8c
-    dracut_instmods -o -P ".*${_unwanted_drivers}.*" -s "$_net_drivers" "=drivers/net" ${_s390drivers:+"$_s390drivers"}
9abf8c
+    if [[ $hostonly_mode == 'strict' ]] && [[ $hostonly_nics ]]; then
9abf8c
+        for _nic in $hostonly_nics; do
9abf8c
+            _net_drivers=$(get_dev_module /sys/class/net/$_nic)
9abf8c
+            if ! [[ $_net_drivers ]]; then
9abf8c
+                derror "--hostonly-nics contains invalid NIC '$_nic'"
9abf8c
+                continue
9abf8c
+            fi
9abf8c
+            hostonly="" instmods $_net_drivers
9abf8c
+        done
9abf8c
+        return 0
9abf8c
+    fi
9abf8c
 
9abf8c
+    dracut_instmods -o -P ".*${_unwanted_drivers}.*" -s "$_net_symbols" "=drivers/net"
9abf8c
     #instmods() will take care of hostonly
9abf8c
     instmods \
9abf8c
         =drivers/net/phy \
9abf8c