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