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