自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
核心模組支援
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
通用核心映像檔 (GKI) 可能不包含必要的驅動程式支援,無法讓裝置掛載分區。為了讓裝置掛載分割區並繼續啟動,第一階段的 init
會經過強化,以便載入位於 RAM 磁碟的核心模組。此 RAM 磁碟會分為一般和供應商 RAM 磁碟。供應商核心模組會儲存在供應商 RAM 磁碟中。您可以設定核心模組的載入順序。
模組位置
RAM 磁碟區是第一階段 init,
的檔案系統,也是 A/B 裝置和虛擬 A/B 裝置上的復原/快速啟動映像檔。這是由兩個 cpio 封存檔組成的 initramfs
,會由引導程式連結。第一個 cpio 封存檔會儲存在供應商-啟動分區中的供應商 ramdisk,並包含下列元件:
- 第一階段
init
供應商核心模組,位於 /lib/modules/
。
modprobe
設定檔,位於 /lib/modules/
:modules.dep
、modules.softdep
、modules.alias
、modules.options
。
modules.load
檔案,用於在 /lib/modules/
中指出在第一階段初始化時要載入哪些模組,以及載入順序。
/lib/modules/
中的供應商復原核心模組 (適用於 A/B 和虛擬 A/B 裝置)
modules.load.recovery
,在 /lib/modules
中,此值會指出要載入的模組,以及 A/B 和虛擬 A/B 裝置的載入順序。
第二個 cpio 封存檔會以 GKI 的形式提供,做為 boot.img 的 RAM 磁碟,並套用在第一個 cpio 封存檔上,其中包含 first_stage_init
和其依附的程式庫。
在第一階段初始化時載入模組
第一階段的 init
會從 RAM 磁碟上的 /lib/modules/
讀取 modprobe 設定檔。接著,它會讀取 /lib/modules/modules.load
中指定的模組清單 (或在復原的情況下,/lib/modules/modules.load.recovery
),並嘗試依序載入每個模組,遵循先前載入檔案中指定的設定。為了滿足硬性或軟性依附元件,系統可能會偏離所要求的順序。
建構支援、第一階段初始化
如要指定要複製至供應商 RAM 磁碟 cpio 的核心模組,請在 BOARD_VENDOR_RAMDISK_KERNEL_MODULES
中列出這些模組。建構作業會在這些模組上執行 depmod
,並將產生的 modprobe 設定檔放入供應商的 RAMDISK cpio。
建構作業也會建立 modules.load
檔案,並將檔案儲存在供應商的 RAM 磁碟 cpio 中。根據預設,它包含 BOARD_VENDOR_RAMDISK_KERNEL_MODULES
中列出的所有模組。如要覆寫該檔案的內容,請使用 BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD
,如以下範例所示:
BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD := \
device/vendor/mydevice-kernel/first.ko \
device/vendor/mydevice-kernel/second.ko \
device/vendor/mydevice-kernel/third.ko
建構支援、完整 Android
與 Android 10 以下版本相同,Android 平台會將 BOARD_VENDOR_KERNEL_MODULES
中列出的核心模組複製至 /vendor/lib/modules
中的供應商分區。平台建構作業會在這些模組上執行 depmod
,並將 depmod
輸出檔案複製到相同位置的供應商分區。從 /vendor
載入核心模組的機制,與先前 Android 版本相同。您可以決定何時及如何載入這些模組,但通常會使用 init.rc
指令碼來執行此操作。
萬用字元和整合的核心版本
如果供應商將裝置核心版本與 Android 平台版本結合,可能會遇到問題,因為他們會使用上述 BOARD
巨集指定要複製至裝置的核心模組。如果供應商不想在裝置的平台建構檔案中列出核心模組,可以使用萬用字元 ($(wildcard device/vendor/mydevice/*.ko
)。請注意,在整合式核心建構的情況下,萬用字元無法運作,因為在叫用 make 並在 makefile 中展開巨集時,核心模組尚未建構,因此巨集為空白。
為解決這個問題,供應商可以讓其核心版本建立 ZIP 封存檔,其中包含要複製到各個分區的核心模組。在 BOARD_*_KERNEL_MODULES_ARCHIVE
中設定該 ZIP 封存檔的路徑,其中 *
是分區的名稱 (例如 BOARD_VENDOR_KERNEL_MODULES_ARCHIVE
)。Android 平台版本會將這個 ZIP 封存檔解壓縮至適當位置,並在模組上執行 depmod
。
核心模組 ZIP 封存檔應設有編譯規則,確保平台建構作業可在需要時產生封存檔。
復原
在先前的 Android 版本中,系統會在 BOARD_RECOVERY_KERNEL_MODULES
中指定復原作業所需的核心模組。在 Android 12 中,仍會使用這個巨集指定復原作業所需的核心模組。不過,復原核心模組會複製至供應商的 RAMDISK cpio,而非一般 RAMDISK cpio。根據預設,BOARD_RECOVERY_KERNEL_MODULES
中列出的所有核心模組會在第一階段 init
期間載入。如果您只想載入這些模組的子集,請在 BOARD_RECOVERY_KERNEL_MODULES_LOAD
中指定該子集的內容。
如要瞭解如何建立供應商啟動分割區 (其中包含本頁提及的供應商 RAMDISK),請參閱「啟動分割區」。
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Kernel module support\n\nA generic kernel image (GKI) may not contain the required driver support to\nenable a device to mount partitions. To enable a device to mount partitions and\nto continue booting, first-stage `init` is enhanced to load the\nkernel modules present on a ramdisk. The ramdisk is split into generic and\nvendor ramdisks. Vendor kernel modules are stored in the vendor ramdisk. The\norder in which kernel modules are loaded is configurable.\n\nModule location\n---------------\n\nThe ramdisk is the filesystem for first-stage `init,` and for the\nrecovery/fastbootd image on A/B and virtual A/B devices. It's an\n`initramfs` composed of two cpio archives that get concatenated by\nthe bootloader. The first cpio archive, which is stored as the vendor ramdisk\nin the vendor-boot partition, contains these components:\n\n- First-stage `init` vendor kernel modules, located in `/lib/modules/`.\n- `modprobe` config files, located in `/lib/modules/`: `modules.dep`, `modules.softdep`, `modules.alias`, `modules.options`.\n- A `modules.load` file that indicates which modules to load during first stage init, and in which order, in `/lib/modules/`.\n- Vendor recovery-kernel modules, for A/B and Virtual A/B devices, in `/lib/modules/`\n- `modules.load.recovery` which indicates the modules to load, and in which order, for A/B and Virtual A/B devices, in `/lib/modules`.\n\n\nThe second cpio archive, which is supplied with the GKI\nas the ramdisk of the boot.img and applied on top of the\nfirst, contains `first_stage_init` and the libraries on which it depends.\n\nModule loading in first-stage init\n----------------------------------\n\nFirst-stage `init` begins by reading the modprobe configuration\nfiles from `/lib/modules/` on the ramdisk. Next, it reads the list\nof modules specified in `/lib/modules/modules.load` (or in the case\nof recovery, `/lib/modules/modules.load.recovery`) and attempts to\nload each of those modules in order, following the configuration specified in\nthe previously loaded files. The requested order may be deviated from to\nsatisfy hard or soft dependencies.\n\nBuild support, first-stage init\n-------------------------------\n\nTo specify kernel modules to be copied into the vendor ramdisk cpio, list\nthem in `BOARD_VENDOR_RAMDISK_KERNEL_MODULES`. The build runs\n`depmod` on these modules and puts the resulting modprobe configuration\nfiles in the vendor ramdisk cpio.\n\nThe build also creates a `modules.load` file and stores it in the\nvendor ramdisk cpio. By default it contains all of the modules listed in\n`BOARD_VENDOR_RAMDISK_KERNEL_MODULES`. To override the contents of\nthat file, use `BOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD`, as shown\nin this example: \n\n```maple\nBOARD_VENDOR_RAMDISK_KERNEL_MODULES_LOAD := \\\n device/vendor/mydevice-kernel/first.ko \\\n device/vendor/mydevice-kernel/second.ko \\\n device/vendor/mydevice-kernel/third.ko\n```\n\nBuild support, full Android\n---------------------------\n\nAs is the case in Android 10 and lower releases, kernel modules listed in\n`BOARD_VENDOR_KERNEL_MODULES` are copied by the Android platform\nbuild into the vendor partition at `/vendor/lib/modules`. The\nplatform build runs `depmod` on these modules, and copies the\n`depmod` output files into the vendor partition at the same\nlocation. The mechanism for loading kernel modules from `/vendor`\nremains the same as it was for prior releases of Android. It's your decision\nhow and when to load these modules, although typically this is done using\n`init.rc` scripts.\n\nWildcards and integrated kernel builds\n--------------------------------------\n\nVendors who combine their device kernel build with the Android platform build\nmay run into a problem using the above mentioned `BOARD` macros to\nspecify kernel modules to be copied on to the device. If the vendor wishes to avoid\nlisting kernel modules in the device's platform build files, they can use a wildcard\n(`$(wildcard device/vendor/mydevice/*.ko`). Note that the wildcard doesn't\nwork in the case of an integrated kernel build, because when make is invoked and the\nmacros are expanded in makefiles, the kernel modules haven't been built, so the macros\nare empty.\n\nTo get around this problem, the vendor may have their kernel build create a zip\narchive containing the kernel modules to be be copied onto each partition.\nSet the path of that zip archive in `BOARD_*_KERNEL_MODULES_ARCHIVE`\nwhere `*` is the name of the partition (such as\n`BOARD_VENDOR_KERNEL_MODULES_ARCHIVE`). The Android platform build\nextracts this zip archive into the appropriate location and runs `depmod`\non the modules.\n\nThe kernel module zip archive should have a make rule that ensures the platform\nbuild can generate the archive when required.\n\nRecovery\n--------\n\nIn prior Android releases, kernel modules required for recovery were\nspecified in `BOARD_RECOVERY_KERNEL_MODULES`. In Android 12,\nkernel modules required for recovery are still\nspecified using this macro. However, the recovery kernel modules are copied to\nthe vendor ramdisk cpio, rather than the generic ramdisk cpio. By default all\nkernel modules listed in `BOARD_RECOVERY_KERNEL_MODULES` are loaded\nduring first-stage `init`. If you only want a subset of these\nmodules to be loaded, specify the contents of that subset in\n`BOARD_RECOVERY_KERNEL_MODULES_LOAD`.\n\nRelated documentation\n---------------------\n\nTo learn about creating a vendor boot partition (which contains the vendor\nramdisk mentioned on this page), see\n[Boot\npartitions](/docs/core/architecture/bootloader/partitions/vendor-boot-partitions)."]]