Kernel-Modules

(Buildroot) 如何自動載入模組

  • May 10, 2021

我想在啟動時載入一些額外的模組。

這可以從命令行正常工作:

modprobe -a i2c-dev
modprobe -a snd-soc-pcm512x
modprobe -a snd-soc-wm8804

但我希望在啟動時完成此操作。我嘗試使用其中的模組名稱創建 /etc/modules、/etc/modprobe.conf 和 /etc/modprobe.d/i2c-dev.conf 等,但沒有運氣。

我正在使用 buildroot-2017-08,我認為它使用 kmod,並帶有 BusyBox init。

我可以創建一個 init.d 腳本,但我認為有一個特定的位置應該包含要載入的模組列表。

找不到許多經過完善和準備好的腳本。事實證明,Linux From Scratch (LFS) 有一些看起來不錯且易於使用的腳本。

我為普通的 BusyBox 初始化載入模組的解決方案:

/etc/init.d/S02modules

#!/bin/sh
########################################################################
#
# Description : Module auto-loading script
#
# Authors     : Zack Winkles
#
# Version     : 00.00
#
# Notes       :
#
########################################################################

. /etc/sysconfig/functions

# Assure that the kernel has module support.
[ -e /proc/ksyms -o -e /proc/modules ] || exit 0

case "${1}" in
   start)

       # Exit if there's no modules file or there are no
       # valid entries
       [ -r /etc/sysconfig/modules ] &&
           egrep -qv '^($|#)' /etc/sysconfig/modules ||
           exit 0

       boot_mesg -n "Loading modules:" ${INFO}

       # Only try to load modules if the user has actually given us
       # some modules to load.
       while read module args; do

           # Ignore comments and blank lines.
           case "$module" in
               ""|"#"*) continue ;;
           esac

           # Attempt to load the module, making
           # sure to pass any arguments provided.
           modprobe ${module} ${args} >/dev/null

           # Print the module name if successful,
           # otherwise take note.
           if [ $? -eq 0 ]; then
               boot_mesg -n " ${module}" ${NORMAL}
           else
               failedmod="${failedmod} ${module}"
           fi
       done < /etc/sysconfig/modules

       boot_mesg "" ${NORMAL}
       # Print a message about successfully loaded
       # modules on the correct line.
       echo_ok

       # Print a failure message with a list of any
       # modules that may have failed to load.
       if [ -n "${failedmod}" ]; then
           boot_mesg "Failed to load modules:${failedmod}" ${FAILURE}
           echo_failure
       fi
       ;;
   *)
       echo "Usage: ${0} {start}"
       exit 1
       ;;
esac

基於這個 LFS 腳本: http ://www.linuxfromscratch.org/lfs/view/6.5/scripts/apds05.html

/etc/sysconfig/functions

#!/bin/sh
#######################################################################
#
# Description : Run Level Control Functions
#
# Authors     : Gerard Beekmans - gerard@linuxfromscratch.org
#
# Version     : 00.00
#
# Notes       : With code based on Matthias Benkmann's simpleinit-msb
#        http://winterdrache.de/linux/newboot/index.html
#
########################################################################

## Environmental setup
# Setup default values for environment
umask 022
export PATH="/bin:/usr/bin:/sbin:/usr/sbin"

# Signal sent to running processes to refresh their configuration
RELOADSIG="HUP"

# Number of seconds between STOPSIG and FALLBACK when stopping processes
KILLDELAY="3"

## Screen Dimensions
# Find current screen size
if [ -z "${COLUMNS}" ]; then
   COLUMNS=$(stty size)
   COLUMNS=${COLUMNS##* }
fi

# When using remote connections, such as a serial port, stty size returns 0
if [ "${COLUMNS}" = "0" ]; then
   COLUMNS=80
fi

## Measurements for positioning result messages
COL=$((${COLUMNS} - 8))
WCOL=$((${COL} - 2))

## Provide an echo that supports -e and -n
# If formatting is needed, $ECHO should be used
case "`echo -e -n test`" in
   -[en]*)
       ECHO=/bin/echo
       ;;
   *)
       ECHO=echo
       ;;
esac

## Set Cursor Position Commands, used via $ECHO
SET_COL="\\033[${COL}G"      # at the $COL char
SET_WCOL="\\033[${WCOL}G"    # at the $WCOL char
CURS_UP="\\033[1A\\033[0G"   # Up one line, at the 0'th char

## Set color commands, used via $ECHO
# Please consult `man console_codes for more information
# under the "ECMA-48 Set Graphics Rendition" section
#
# Warning: when switching from a 8bit to a 9bit font,
# the linux console will reinterpret the bold (1;) to
# the top 256 glyphs of the 9bit font.  This does
# not affect framebuffer consoles
NORMAL="\\033[0;39m"         # Standard console grey
SUCCESS="\\033[1;32m"        # Success is green
WARNING="\\033[1;33m"        # Warnings are yellow
FAILURE="\\033[1;31m"        # Failures are red
INFO="\\033[1;36m"           # Information is light cyan
BRACKET="\\033[1;34m"        # Brackets are blue

STRING_LENGTH="0"   # the length of the current message

#*******************************************************************************
# Function - boot_mesg()
#
# Purpose:      Sending information from bootup scripts to the console
#
# Inputs:       $1 is the message
#               $2 is the colorcode for the console
#
# Outputs:      Standard Output
#
# Dependencies: - sed for parsing strings.
#            - grep for counting string length.
#
# Todo:
#*******************************************************************************
boot_mesg()
{
   local ECHOPARM=""

   while true
   do
       case "${1}" in
           -n)
               ECHOPARM=" -n "
               shift 1
               ;;
           -*)
               echo "Unknown Option: ${1}"
               return 1
               ;;
           *)
               break
               ;;
       esac
   done

   ## Figure out the length of what is to be printed to be used
   ## for warning messages.
   STRING_LENGTH=$((${#1} + 1))

   # Print the message to the screen
   ${ECHO} ${ECHOPARM} -e "${2}${1}"

}

boot_mesg_flush()
{
   # Reset STRING_LENGTH for next message
   STRING_LENGTH="0"
}

echo_ok()
{
   ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${SUCCESS}  OK  ${BRACKET}]"
   ${ECHO} -e "${NORMAL}"
       boot_mesg_flush
}

echo_failure()
{
   ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${FAILURE} FAIL ${BRACKET}]"
   ${ECHO} -e "${NORMAL}"
       boot_mesg_flush
}

echo_warning()
{
   ${ECHO} -n -e "${CURS_UP}${SET_COL}${BRACKET}[${WARNING} WARN ${BRACKET}]"
   ${ECHO} -e "${NORMAL}"
       boot_mesg_flush
}

基於這個 LFS 腳本: http ://www.linuxfromscratch.org/lfs/view/6.5/scripts/apds02.html

/etc/sysconfig/模組

i2c-dev
snd-soc-pcm512x
snd-soc-wm8804
snd-soc-hifiberry_dac

(或任何你想載入的模組,顯然)

如果在 BusyBox 初始化中以這種方式載入模組有任何問題或問題,我相信它們最終會出現在下面的評論中;-)。

這取決於您使用的是哪個初始化系統。如果您已將 Buildroot 配置為使用 Busybox init 或 SysV init,則處理此問題的正確方法可能是通過 init 腳本。但是,如果您將其配置為使用 Systemd,則可以將帶有.conf副檔名的文件拖放到要載入的每個模組中,/etc/modules-load.d/或者/usr/lib/modules-load.d/將要載入的每個模組單獨列在一行上,systemd 將在啟動時載入它們。

引用自:https://unix.stackexchange.com/questions/396542