17094c
From 90d96c4f6a44d895f8999da8c4721a04612e6379 Mon Sep 17 00:00:00 2001
17094c
From: Kairui Song <kasong@redhat.com>
17094c
Date: Tue, 29 Oct 2019 16:29:14 +0800
17094c
Subject: [PATCH] 90kernel-modules: don't install any block driver if not
17094c
 needed
17094c
17094c
If hostonly mode is in use, and there isn't any block device included
17094c
as hostonly device, then the initramfs won't need any block driver.
17094c
17094c
Eg. for kdump built image, there could be only one nfs mount point,
17094c
or the initramfs may only execute some network operation then reboot.
17094c
17094c
In such case, skip block driver installation to save space and time.
17094c
17094c
Signed-off-by: Kairui Song <kasong@redhat.com>
17094c
(cherry picked from commit f4ad2e5c8964b6c7b50420299a0096bade6a40bd)
17094c
17094c
Resolves: #1776659
17094c
---
17094c
 modules.d/90kernel-modules/module-setup.sh | 32 +++++++++++++++++++++++-------
17094c
 1 file changed, 25 insertions(+), 7 deletions(-)
17094c
17094c
diff --git a/modules.d/90kernel-modules/module-setup.sh b/modules.d/90kernel-modules/module-setup.sh
17094c
index 3dfceff0..c91ba10c 100755
17094c
--- a/modules.d/90kernel-modules/module-setup.sh
17094c
+++ b/modules.d/90kernel-modules/module-setup.sh
17094c
@@ -2,6 +2,8 @@
17094c
 
17094c
 # called by dracut
17094c
 installkernel() {
17094c
+    local _blockfuncs='ahci_platform_get_resources|ata_scsi_ioctl|scsi_add_host|blk_cleanup_queue|register_mtd_blktrans|scsi_esp_register|register_virtio_device|usb_stor_disconnect|mmc_add_host|sdhci_add_host|scsi_add_host_with_dma'
17094c
+
17094c
     find_kernel_modules_external () {
17094c
         local _OLDIFS
17094c
         local external_pattern="^/"
17094c
@@ -16,11 +18,25 @@ installkernel() {
17094c
         done < "$srcmods/modules.dep"
17094c
         IFS=$_OLDIFS
17094c
     }
17094c
-    local _blockfuncs='ahci_platform_get_resources|ata_scsi_ioctl|scsi_add_host|blk_cleanup_queue|register_mtd_blktrans|scsi_esp_register|register_virtio_device|usb_stor_disconnect|mmc_add_host|sdhci_add_host|scsi_add_host_with_dma'
17094c
+
17094c
+    is_block_dev() {
17094c
+        [ -e /sys/dev/block/$1 ] && return 0
17094c
+        return 1
17094c
+    }
17094c
+
17094c
+    install_block_modules () {
17094c
+        hostonly='' instmods sr_mod sd_mod scsi_dh ata_piix
17094c
+        instmods \
17094c
+            scsi_dh_rdac scsi_dh_emc scsi_dh_alua \
17094c
+            =ide nvme vmd nfit \
17094c
+            virtio_blk
17094c
+
17094c
+        dracut_instmods -o -s "${_blockfuncs}" "=drivers"
17094c
+    }
17094c
 
17094c
     if [[ -z $drivers ]]; then
17094c
         hostonly='' instmods \
17094c
-            sr_mod sd_mod scsi_dh ata_piix hid_generic unix \
17094c
+            hid_generic unix \
17094c
             ehci-hcd ehci-pci ehci-platform \
17094c
             ohci-hcd ohci-pci \
17094c
             uhci-hcd \
17094c
@@ -38,10 +54,10 @@ installkernel() {
17094c
             ${NULL}
17094c
 
17094c
         instmods \
17094c
-            yenta_socket scsi_dh_rdac scsi_dh_emc scsi_dh_alua \
17094c
+            yenta_socket \
17094c
             atkbd i8042 usbhid firewire-ohci pcmcia hv-vmbus \
17094c
-            virtio virtio_blk virtio_ring virtio_pci virtio_scsi \
17094c
-            "=drivers/pcmcia" =ide nvme vmd nfit
17094c
+            virtio virtio_ring virtio_pci virtio_scsi \
17094c
+            "=drivers/pcmcia"
17094c
 
17094c
         if [[ "$(uname -m)" == arm* || "$(uname -m)" == aarch64 ]]; then
17094c
             # arm/aarch64 specific modules
17094c
@@ -72,10 +88,12 @@ installkernel() {
17094c
                 ${NULL}
17094c
         fi
17094c
 
17094c
-        dracut_instmods -o -s "${_blockfuncs}" "=drivers"
17094c
-
17094c
         find_kernel_modules_external | instmods
17094c
 
17094c
+        if ! [[ $hostonly ]] || for_each_host_dev_and_slaves is_block_dev; then
17094c
+            install_block_modules
17094c
+        fi
17094c
+
17094c
         # if not on hostonly mode, install all known filesystems,
17094c
         # if the required list is not set via the filesystems variable
17094c
         if ! [[ $hostonly ]]; then
17094c