OpenCloudOS-Kernel/dist/sources/filter-modules.sh

187 lines
6.9 KiB
Bash
Executable File

#!/bin/bash
#
# Used Fedora kernel-ark repo as reference.
#
# Called as filter-modules.sh <depmod base path> <kernel version> <arch> <System.map>
#
# This script collects modules, and filters modules into the kernel-core and kernel-modules
# subpackages. We list out subsystems/subdirs to prune from the installed
# module directory. What is left is put into the kernel-core package. What is
# pruned is contained in the kernel-modules package.
#
# This file contains the default subsys/subdirs to prune from all architectures.
# If an architecture needs to differ, we source a per-arch filter-<arch>.sh file
# that contains the set of override lists to be used instead. If a module or
# subsys should be in kernel-modules on all arches, please change the defaults
# listed here.
# Set the default dirs and modules to filter out as external modules
driverdirs="atm auxdisplay bcma bluetooth firewire fmc fpga iio infiniband isdn leds media memstick message mfd mmc mtd nfc ntb pcmcia platform power powercap ssb soundwire staging thermal tty uio w1 ofed_addon"
chardrvs="mwave pcmcia"
netdrvs="appletalk can dsa hamradio ieee802154 irda ppp slip usb wireless"
ethdrvs="3com adaptec alteon amd aquantia arc atheros broadcom cadence calxeda chelsio cisco dec dlink emulex icplus marvell mellanox micrel myricom neterion nvidia oki-semi packetengines qlogic rdc renesas sfc silan sis smsc stmicro sun tehuti ti wiznet xircom"
inputdrvs="gameport tablet touchscreen joystick"
hiddrvs="surface-hid"
scsidrvs="aacraid aic7xxx aic94xx be2iscsi bfa bnx2i bnx2fc csiostor cxgbi esas2r fcoe fnic hisi_sas isci libsas lpfc megaraid mpt2sas mpt3sas mvsas pm8001 qla2xxx qla4xxx sym53c8xx_2 ufs qedf"
usbdrvs="atm image misc serial wusbcore"
drmdrvs="amd ast bridge gma500 i2c i915 mgag200 panel radeon via"
netprots="6lowpan appletalk atm ax25 batman-adv bluetooth can dccp dsa ieee802154 irda l2tp mac80211 mac802154 mpls netrom nfc rds rfkill rose sctp smc wireless"
fsdrvs="affs befs cifs coda cramfs dlm ecryptfs hfs hfsplus jfs jffs2 minix ncpfs nilfs2 ocfs2 reiserfs romfs squashfs sysv ubifs ufs gfs2"
# .ko files to be filtered
singlemods="ntb_netdev iscsi_ibft iscsi_boot_sysfs megaraid pmcraid qedi qla1280 9pnet_rdma rpcrdma nvmet-rdma nvme-rdma hid-picolcd hid-prodikeys hwa-hc hwpoison-inject hid-sensor-hub target_core_user sbp_target cxgbit iw_cxgb3 iw_cxgb4 cxgb3i cxgb3i cxgb3i_ddp cxgb4i chcr chtls parport_serial ism regmap-sdw regmap-sdw-mbq arizona-micsupp hid-asus iTCO_wdt rnbd-client rnbd-server mlx5_ib mlx5_vdpa spi-altera-dfl nct6775 hid-playstation hid-nintendo ntc_thermistor configs"
# Overrides is individual modules which need to remain in kernel-core due to deps.
overrides="cec wmi mxm-wmi mfd-core"
BASE_DIR=$1
KERNEL_UNAMER=$2
ARCH=$3
SYSTEM_MAP=$(realpath "$4")
MODULEPKG=$5
MODULE_DIR=lib/modules/$KERNEL_UNAMER
# Some distro will hide /sbin for non-root user but we need depmod to work.
# depmod can work well without root permission in dry-run mode.
PATH=$PATH:/usr/sbin:/sbin
error() {
echo "filter-modules.sh: $*" >&2
}
if ! cd "$BASE_DIR"; then
error "Invalid base path: '$BASE_DIR'"
exit 1
fi
if ! cd "$MODULE_DIR"; then
error "Invalid kernel module path: '$MODULE_DIR'"
exit 1
fi
# To be read from build path
core_modules_list=
modules_list=
# Read all kernel modules in the core modules list at the beginning
# filter them into external modules list step by step
#
# Not filtering internal or vdso so start with kernel/
core_modules_list=$(find kernel -name '*.ko')
filter_mods() {
local prefix=$1 mods=$2 suffix=$3
local mod filter_list
for mod in $mods; do
if ! filter_list=$(grep "$prefix$mod$suffix" <<< "$core_modules_list"); then
error "$prefix$mod$suffix is marked as non-core module but not built, skipping."
else
core_modules_list=$(grep -v "$prefix$mod$suffix" <<< "$core_modules_list")
modules_list+=$filter_list
modules_list+=$'\n'
fi
done
}
filter_override() {
local filter_list
for mod in $1; do
if filter_list=$(grep "/$mod.ko" <<< "$modules_list"); then
modules_list=$(grep -v "/$mod.ko" <<< "$modules_list")
core_modules_list+=$filter_list
core_modules_list+=$'\n'
fi
done
}
# Check if modules dependency are still sane after splitting these mods out
# args: moduled to be spliited out
check_modules_dependency_after_split() {
# Mask external mods to do a depmod check
for mod in $modules_list; do
mv "$mod" "$mod.bak"
done
# Run depmod on the resulting module tree and make sure it isn't broken
depmod_err=$(depmod "$KERNEL_UNAMER" -b "$BASE_DIR" -naeF "$SYSTEM_MAP" 2>&1 1>/dev/null)
if [ "$depmod_err" ]; then
error "Failed to filter out external modules, broken depmod:"
error "$depmod_err"
exit 1
fi
# Move the mods back
for mod in $modules_list; do
mv "$mod.bak" "$mod"
done
}
# Modules override for differnet arch
case $ARCH in
aarch64 )
driverdirs="atm auxdisplay bcma bluetooth firewire fpga infiniband leds media memstick message mmc mtd nfc ntb pcmcia power ssb soundwire staging tty uio w1 ofed_addon"
ethdrvs="3com adaptec arc alteon atheros broadcom cadence calxeda chelsio cisco dec dlink emulex marvell micrel myricom neterion nvidia packetengines qlogic rdc sfc silan sis smsc stmicro sun tehuti ti via wiznet xircom"
drmdrvs="amd arm bridge ast exynos hisilicon i2c imx mgag200 meson msm panel pl111 radeon rockchip tegra sun4i tiny vc4"
singlemods="ntb_netdev iscsi_ibft iscsi_boot_sysfs megaraid pmcraid qedi qla1280 9pnet_rdma rpcrdma nvmet-rdma nvme-rdma hid-picolcd hid-prodikeys hwpoison-inject target_core_user sbp_target cxgbit chcr rnbd-client rnbd-server mlx5_ib mlx5_core mlx5_vdpa dfl-emif octeontx2-cpt octeontx2-cptvf spi-altera-dfl rvu_cptpf rvu_cptvf regmap-sdw regmap-sdw-mbq hid-playstation hid-nintendo configs"
;;
riscv64 )
;;
loongarch64 )
;;
x86_64 )
;;
* )
error "Unknown arch '$ARCH'"
;;
esac
# Filter for split and verify
case $MODULEPKG in
non-core-modules )
filter_mods "drivers/" "$driverdirs" /
filter_mods "drivers/char/" "$chardrvs" /
filter_mods "drivers/net/" "$netdrvs" /
filter_mods "drivers/net/ethernet/" "$ethdrvs" /
filter_mods "drivers/input/" "$inputdrvs" /
filter_mods "drivers/hid/" "$hiddrvs" /
filter_mods "drivers/scsi/" "$scsidrvs" /
filter_mods "drivers/usb/" "$usbdrvs" /
filter_mods "drivers/gpu/drm/" "$drmdrvs" /
filter_mods "net/" "$netprots" /
filter_mods "fs/" "$fsdrvs" /
# Just kill sound.
filter_mods "" "sound" /
filter_mods "drivers" "soundwire" /
# Filter single modules
filter_mods "" "$singlemods"
# Now process the override list to bring those modules back into core
filter_override "$overrides"
;;
* )
error "Invalid module packaging param '$1'"
exit 1
;;
esac
# Ensure this packaging splitting won't break core modules dependency
check_modules_dependency_after_split
# Print the modules_list after sort, and prepend /lib/modules/<KERNEL_UNAMER>/ to each line
echo "%dir /lib/modules/$KERNEL_UNAMER/"
echo "$modules_list" | sort -n | sed "/^$/d;s,^,/${MODULE_DIR}/,"
exit 0