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