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