Harald Hoyer ff2fda
From 7047294617bbdd3ffb2466c73db56fda4e6156db Mon Sep 17 00:00:00 2001
Harald Hoyer ff2fda
From: Kairui Song <kasong@redhat.com>
Harald Hoyer ff2fda
Date: Tue, 17 Jul 2018 17:16:07 +0800
Harald Hoyer ff2fda
Subject: [PATCH] Record loaded kernel modules when hostonly mode is enabled
Harald Hoyer ff2fda
Harald Hoyer ff2fda
A hostonly image will not include every possibly required kernel module,
Harald Hoyer ff2fda
so if any hardware or configuration changed, the image may fail to boot.
Harald Hoyer ff2fda
Harald Hoyer ff2fda
One way to know if there are any hardware change or configuration change
Harald Hoyer ff2fda
that will require an image rebuild or not is to check the loaded kernel
Harald Hoyer ff2fda
module list. If the loaded kernel module list differs from last build
Harald Hoyer ff2fda
time, then the image may require to be rebuilt.
Harald Hoyer ff2fda
Harald Hoyer ff2fda
This commit will let dracut record the loaded kernel module list when
Harald Hoyer ff2fda
the image is being built, so other tools or services can compare this
Harald Hoyer ff2fda
list with currently loaded kernel modules to decide if dracut should be
Harald Hoyer ff2fda
called to rebuild the image.
Harald Hoyer ff2fda
Harald Hoyer ff2fda
To retrieve the loaded kernel modules list when an image is built, use
Harald Hoyer ff2fda
lsinitrd command:
Harald Hoyer ff2fda
Harald Hoyer ff2fda
lsinitrd $image -f */lib/dracut/loaded-kernel-modules.txt
Harald Hoyer ff2fda
---
Harald Hoyer ff2fda
 dracut-functions.sh | 11 +++++++++++
Harald Hoyer ff2fda
 dracut.sh           |  3 +++
Harald Hoyer ff2fda
 2 files changed, 14 insertions(+)
Harald Hoyer ff2fda
Harald Hoyer ff2fda
diff --git a/dracut-functions.sh b/dracut-functions.sh
Harald Hoyer ff2fda
index ccc48971..7c408f83 100755
Harald Hoyer ff2fda
--- a/dracut-functions.sh
Harald Hoyer ff2fda
+++ b/dracut-functions.sh
Harald Hoyer ff2fda
@@ -676,6 +676,17 @@ get_ucode_file ()
Harald Hoyer ff2fda
     fi
Harald Hoyer ff2fda
 }
Harald Hoyer ff2fda
 
Harald Hoyer ff2fda
+# Get currently loaded modules
Harald Hoyer ff2fda
+# sorted, and delimited by newline
Harald Hoyer ff2fda
+get_loaded_kernel_modules ()
Harald Hoyer ff2fda
+{
Harald Hoyer ff2fda
+    local modules=( )
Harald Hoyer ff2fda
+    while read _module _size _used _used_by; do
Harald Hoyer ff2fda
+        modules+=( "$_module" )
Harald Hoyer ff2fda
+    done <<< $(lsmod | sed -n '1!p')
Harald Hoyer ff2fda
+    printf '%s\n' "${modules[@]}" | sort
Harald Hoyer ff2fda
+}
Harald Hoyer ff2fda
+
Harald Hoyer ff2fda
 # Not every device in /dev/mapper should be examined.
Harald Hoyer ff2fda
 # If it is an LVM device, touch only devices which have /dev/VG/LV symlink.
Harald Hoyer ff2fda
 lvm_internal_dev() {
Harald Hoyer ff2fda
diff --git a/dracut.sh b/dracut.sh
Harald Hoyer ff2fda
index cfa4abde..6614d27d 100755
Harald Hoyer ff2fda
--- a/dracut.sh
Harald Hoyer ff2fda
+++ b/dracut.sh
Harald Hoyer ff2fda
@@ -1492,6 +1492,9 @@ dinfo "*** Including modules done ***"
Harald Hoyer ff2fda
 
Harald Hoyer ff2fda
 ## final stuff that has to happen
Harald Hoyer ff2fda
 if [[ $no_kernel != yes ]]; then
Harald Hoyer ff2fda
+    if [[ $hostonly ]]; then
Harald Hoyer ff2fda
+        echo "$(get_loaded_kernel_modules)" > $initdir/lib/dracut/loaded-kernel-modules.txt
Harald Hoyer ff2fda
+    fi
Harald Hoyer ff2fda
 
Harald Hoyer ff2fda
     if [[ $drivers ]]; then
Harald Hoyer ff2fda
         hostonly='' instmods $drivers
Harald Hoyer ff2fda