How to cross-compile and use Mainline Kernel
https://gist.github.com/lategoodbye/c7317a42bf7f9c07f5a91baed8c68f75
Notes on how to set up a new Ubuntu 18.04 LTS x64 environment, how to build the Mainline Kernel and place it on a Raspbian SD card.
Procedure
- Install tools needed:
# apt install git make gcc device-tree-compiler bison flex libssl-dev libncurses-dev
- Install an up-to-date cross compiler and associated toolset. This may be obtained from https://releases.linaro.org/components/toolchain/binaries/latest-7/. The directory
arm-linux-gnueabihf
contains the necessary compiler for ARM 32-bit and the directoryaarch64-linux-gnu
for ARM 64-bit.
Choose the version suited to your development machine's architecture. For example, at the present time, for use on 64-bit Ubuntu 18.04 on an Intel-based development machine, the appropriate version is gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz
.
$ wget https://releases.linaro.org/components/toolchain/binaries/latest-7/arm-linux-gnueabihf/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz
$ sudo tar xf gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf.tar.xz -C /opt
- Get Raspberry Pi Linux Sources
$ git clone https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
- Move into the repository folder and setup cross-compiler (for ARM 64-bit replace
ARCH=arm
withARCH=arm64
):
$ cd linux
$ export ARCH=arm
$ export CROSS_COMPILE=/opt/gcc-linaro-7.3.1-2018.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-
- Choose the kernel configuration. To do this, you need to know what model of System on a Chip (SoC) is used on your Pi. Please refer to the Raspberry Pi Wikipedia Section. Since we are building from an upstream source, then, by reference to Eric Anholt's pages we can deduce that we should select
bcm2835_defconfig
for BCM2835-based models,multi_v7_defconfig
for BCM2836-, BCM2837- and BCM2711-based models (ARM 32-bit) ordefconfig
for BCM2837- and BCM2711-based models (ARM64). For the Raspberry Pi 3B Plus (ARM 32-bit), the following is appropriate:
$ make multi_v7_defconfig
- At this point you can modify the configuration using, for instance,
menuconfig
:
$ make menuconfig
- Build the kernel, the modules and the device tree blobs:
$ make
To speed up compilation on a multicore machine, add the argument -j <number_of_cores+1>
to the above command.
- Insert an existing Raspbian SD Card to your development machine. In Ubuntu 18.04, it will appear at
/media/<username>/boot
and/media/<username>/rootfs
, where<username>
is the username you are logged in under. - Identify the appropriate kernel file to replace. For some models, the operative kernel is in
/media/<username>/boot/kernel.img
, whereas in others including the Raspberry Pi 3B Plus used here, it is in the file/media/<username>/boot/kernel7.img
. Refer to the official Kernel Building documentation to identify the correct kernel file. Copy the kernel file to the card's boot directory, renaming the existing kernel file beforehand to preserve it (for ARM 64-bit the kernel file is justImage
):
$ cp arch/arm/boot/zImage /media/<username>/boot/
- Copy the device tree blob to the card's boot directory. The exact device tree blob file to use depends on the model. The following is for a Raspberry Pi 3B Plus, so the device tree blob's filename is
bcm2837-rpi-3-b-plus.dtb
:
$ cp arch/arm/boot/dts/bcm2837-rpi-3-b-plus.dtb /media/<username>/boot/
- Adjust the device tree information in
/media/<username>/boot/config.txt
:
- Add the following lines to
/media/<username>/boot/config.txt
. The filename is that of the device tree blob copied over in Step 10. If/media/<username>/boot/config.txt
already contains adevice_tree
entry, replace it with this.
device_tree=bcm2837-rpi-3-b-plus.dtb
kernel=zImage
- Copy the kernel modules to the SD card. Note that you need to be in superuser mode for this (for ARM 64-bit replace
ARCH=arm
withARCH=arm64
):
# sudo ARCH=arm make modules_install INSTALL_MOD_PATH=/media/<username>/rootfs
- Safely dismount the SD card from the development machine, put it into the target machine and reboot.
Notes
- Avoid using
rpi-update
, as it will overwrite your mainline binary. The downstream kernel typically has problems with the upstream device tree blob. - In some cases (e.g. the
arm64
), the kernel image tends to be very big (~ 16 MB) and may hit the limit of the boot partition.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通