#!/usr/bin/env bash set -euo pipefail # Stage a subset of kernel modules into a target rootfs/initramfs tree. # # Example: # ./stage-initramfs-modules.sh \ # /out/rootfs/lib/modules/6.18.2 \ # /out/initramfs # # Result: # /out/initramfs/lib/modules/6.18.2/... # # Notes: # - This copies selected directories and modules.* metadata. # - It does NOT run depmod for you unless you uncomment that section. # - Best run after kernel modules have already been installed into the source tree. if [[ $# -ne 2 ]]; then echo "Usage: $0 " echo "Example: $0 /out/rootfs/lib/modules/6.18.2 /out/initramfs" exit 1 fi SRC_MODDIR="$1" # e.g. /out/rootfs/lib/modules/6.18.2 DEST_ROOT="$2" # e.g. /out/initramfs if [[ ! -d "$SRC_MODDIR" ]]; then echo "Source module dir does not exist: $SRC_MODDIR" >&2 exit 1 fi KVER="$(basename "$SRC_MODDIR")" DEST_MODDIR="$DEST_ROOT/lib/modules/$KVER" mkdir -p "$DEST_MODDIR" echo "==> Source: $SRC_MODDIR" echo "==> Dest: $DEST_MODDIR" echo # Relative paths under /lib/modules/ PATHS=( # Metadata files used by modprobe/depmod. "modules.builtin" "modules.builtin.modinfo" "modules.order" "modules.dep" "modules.dep.bin" "modules.alias" "modules.alias.bin" "modules.symbols" "modules.symbols.bin" "modules.softdep" # Core module dirs we may need. "kernel/drivers/pwm" "kernel/drivers/hwmon" "kernel/drivers/thermal" # Optional # "kernel/drivers/i2c" # "kernel/drivers/gpio" # "kernel/drivers/base" # "kernel/drivers/power" # "kernel/drivers/platform" ) copy_one() { local rel="$1" local src="$SRC_MODDIR/$rel" local dst="$DEST_MODDIR/$rel" if [[ -d "$src" ]]; then echo "[DIR ] $rel" mkdir -p "$(dirname "$dst")" cp -a "$src" "$dst" elif [[ -f "$src" ]]; then echo "[FILE] $rel" mkdir -p "$(dirname "$dst")" cp -a "$src" "$dst" else echo "[MISS] $rel" fi } for rel in "${PATHS[@]}"; do copy_one "$rel" done echo echo "==> Done staging selected modules." # Optional: show what got copied echo echo "==> Staged files summary:" find "$DEST_MODDIR" -type f | sed "s#^$DEST_ROOT/##" | sort # Optional: if depmod exists INSIDE the build environment and you want to # regenerate metadata for the destination tree, uncomment this block. # # if command -v depmod >/dev/null 2>&1; then # echo # echo "==> Running depmod for destination tree..." # depmod -b "$DEST_ROOT" "$KVER" # fi