编译raspberry pi2内核

本文介绍如何为树莓派2编译定制内核。内容包括配置交叉编译环境、安装必要工具、修改Makefile文件以及将编译后的内核映像和模块写入SD卡等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

准备工作:

下载源码及工具:

内核源码:https://github.com/raspberrypi/linux.git

交叉编译工具:https://github.com/raspberrypi/tools.git

环境配置:

在~/.bashrc加入交叉编译工具的路径到环境变量,然后重启(本人电脑是64位):

export PATH=$PATH:$HOME/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin

测试交叉编译环境是否配置成功:

$arm-linux-gnueabihf-gcc -v

Using built-in specs.
COLLECT_GCC=arm-linux-gnueabihf-gcc
COLLECT_LTO_WRAPPER=/home/linjc/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/../libexec/gcc/arm-linux-gnueabihf/4.8.3/lto-wrapper
Target: arm-linux-gnueabihf
Configured with: /home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/src/gcc-linaro-4.8-2014.03/configure --build=x86_64-build_unknown-linux-gnu --host=x86_64-build_unknown-linux-gnu --target=arm-linux-gnueabihf --prefix=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/install --with-sysroot=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-languages=c,c++,fortran --disable-multilib --enable-multiarch --with-arch=armv6 --with-tune=arm1176jz-s --with-fpu=vfp --with-float=hard --with-pkgversion='crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03' --with-bugurl=https://bugs.launchpad.net/gcc-linaro --enable-__cxa_atexit --enable-libmudflap --enable-libgomp --enable-libssp --with-gmp=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpfr=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-mpc=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-isl=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-cloog=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --with-libelf=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/.build/arm-linux-gnueabihf/build/static --enable-threads=posix --disable-libstdcxx-pch --enable-linker-build-id --enable-plugin --enable-gold --with-local-prefix=/home/zhehe01/work/bzr/pi-build/builds/arm-linux-gnueabihf-raspbian-linux/install/arm-linux-gnueabihf/libc --enable-c99 --enable-long-long --with-float=hard
Thread model: posix
gcc version 4.8.3 20140303 (prerelease) (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03)
如打印出以上信息则配置成功。

编译内核(参考:https://www.raspberrypi.org/documentation/linux/kernel/building.md):

编译前应安装bc:sudo apt-get install bc 或 pacman -S bc

编译前修改Makefile:

-ARCH           ?= $(SUBARCH)
-CROSS_COMPILE  ?= $(CONFIG_CROSS_COMPILE:"%"=%)
+ARCH           ?= arm
+CROSS_COMPILE  ?= arm-linux-gnueabihf-
编译:

$ cd linux
$ KERNEL=kernel7
$ make bcm2709_defconfig
$ make zImage modules dtbs
把编译结果写入SD卡:

之前使用NOOBS烧写完固件之后把SD卡插入电脑会多出两个盘,如:

/dev/sdc1

/dev/sdc2

我的电脑自动挂载为:

/run/media/linjc/boot/

/run/media/linjc/f24a4949-f4b2-4cad-a780-a138695079ec/

如果想要备份原来的内核,执行:

$ sudo cp /run/media/linjc/boot/$KERNEL.img /run/media/linjc/boot/$KERNEL-backup.img

然后执行:

$ sudo make INSTALL_MOD_PATH=/run/media/linjc/f24a4949-f4b2-4cad-a780-a138695079ec/ modules_install
$ sudo scripts/mkknlimg arch/arm/boot/zImage /run/media/linjc/boot/$KERNEL.img
$ sudo cp arch/arm/boot/dts/*.dtb /run/media/linjc/boot/
$ sudo cp arch/arm/boot/dts/overlays/*.dtb* /run/media/linjc/boot/overlays/
$ sudo cp arch/arm/boot/dts/overlays/README /run/media/linjc/boot/overlays/
$ sudo umount /run/media/linjc/boot/
$ sudo umount run/media/linjc/f24a4949-f4b2-4cad-a780-a138695079ec/

最后把SD卡插入paspberry pi2,重新上电即可。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值