From bec25456887fb201e4aa186f8ea044aa77b9dc16 Mon Sep 17 00:00:00 2001 From: Thierry Laurion Date: Thu, 22 Feb 2024 13:36:25 -0500 Subject: [PATCH] insmod: check if module already loaded and if so exit early Signed-off-by: Thierry Laurion --- initrd/bin/network-init-recovery | 6 ++---- initrd/sbin/insmod | 10 +++++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/initrd/bin/network-init-recovery b/initrd/bin/network-init-recovery index ed430f01..814eb32c 100755 --- a/initrd/bin/network-init-recovery +++ b/initrd/bin/network-init-recovery @@ -11,8 +11,7 @@ echo "Loading Ethernet network modules..." network_modules="e1000 e1000e igb sfc mdio mlx4_core mlx4_en" for module in $(echo $network_modules); do if [ -f /lib/modules/$module.ko ]; then - #check if module is already loaded and load it if not - lsmod | grep -q $module || insmod /lib/modules/$module.ko + insmod /lib/modules/$module.ko fi done @@ -30,8 +29,7 @@ if [ -e /lib/modules/cdc_ether.ko ]; then echo "Loading USB tethering network related modules: $network_modules..." for module in $(echo $network_modules); do if [ -f /lib/modules/$module.ko ]; then - #check if module is already loaded and load it if not - lsmod | grep -q $module || insmod /lib/modules/$module.ko + insmod /lib/modules/$module.ko fi done if ! [ -e /sys/class/net/usb0 ]; then diff --git a/initrd/sbin/insmod b/initrd/sbin/insmod index 8545482b..c00aab35 100755 --- a/initrd/sbin/insmod +++ b/initrd/sbin/insmod @@ -23,6 +23,14 @@ if [ ! -r "$MODULE" ]; then die "$MODULE: not found?" fi +# Check if module is already loaded while remove trailing .ko if present +module=$(basename "$MODULE") +module=${module%.ko} +if lsmod | grep -q "^$module\\b"; then + DEBUG "$module: already loaded" + exit 0 +fi + if [ ! -r /sys/class/tpm/tpm0/pcrs -o ! -x /bin/tpm ]; then if [ ! -c /dev/tpmrm0 -o ! -x /bin/tpm2 ]; then tpm_missing=1 @@ -48,4 +56,4 @@ fi # the busybox insmod via the original executable DEBUG "Loading $MODULE with busybox insmod" busybox insmod "$MODULE" "$@" \ -|| die "$MODULE: insmod failed" + || die "$MODULE: insmod failed"