树莓派 3 配有 Broadcom BCM2837 64 位 ARMv8 四核 Cortex A53 处理器,它是一个 64 位 CPU。如果你有一块,运行以下命令可能会让你感到惊讶:
uname-a
Linuxraspberrypi4.4.34-v7+#930SMPWedNov2315:20:41GMT2016armv7l GNU/Linux
是的,这是一个 32 位内核。这是因为树莓派基金会还没有为官方的树莓派系统 Raspbian 提供 64 位版本。然而你可以构建一个,多亏了 Electron752 提供的许多补丁。
构建内核
树莓派基金会维护着它们自己的 Linux 内核分支,它为它们的设备特别裁剪过,同时定期地从上游合并。
我们将会遵照这个页面的指导来构建一个 64 位内核。
我们不能使用“本地构建”的方法,因为它需要一块 64 位的树莓派,这个我们明显还没有。因此我们需要交叉编译它,Ubuntu 是推荐的系统。我个人没有 Ubuntu,因此我在一个有 2 个 CPU 的 Ubuntu 16.04 Digital Ocean 实例上构建,这应该花费我 $0.03。如果你也想这么做,你可以通过这个链接得到 $10 的免费额度。或者你可以通过使用 Virtualbox 中的 Ubuntu VM 作为实例。
首先,我们需要一些构建工具以及** aarch64 交叉编译器**:
apt-getupdate
apt-getinstall-y bc build-essentialgcc-aarch64-linux-gnugitunzip
接着我们可以下载 Linux 内核源码:
gitclone–depth=1-b rpi-4.8.y https://github.com/raspberrypi/linux.git
进入到创建的 git 目录。另外你可以为你的内核添加额外的版本标签,可以通过编辑 Makefile 的开始几行完成:
VERSION=4
PATCHLEVEL=8
SUBLEVEL=13
EXTRAVERSION=+bilal
为了构建它,运行下面的命令:
makeARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-bcmrpi3_defconfig
make-j3ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
第一个应该很快。第二个则会完全不同,我没有精确计时,但是对我来说大概要半个小时。根据你的 CPU 数(nproc * 1.5)调整 -j 标志。
选择一个 Linux 发行版
在内核编译的时候,我们可以开始准备它的 Linux 发行版了。在本教程中为了简单我使用 Raspbian,即使这是一个只有 32 位的发行版。
如果你想要一直用 64 位系统,你应该选一个有 aarch64 支持的发行版,Debian 有一个健壮的 ARM64 移植版。得到它基本有三种方式:
下载一个预构建的根文件系统,这很可能会如页面中提到的那样给你一个过期的版本。
如果你熟悉 debootstrap,用它构建你自己的(这回比较棘手,因为它需要一些手工调整,它最初的目的是在已经运行的主机上进行 chroot,而不是为其他机器构建根文件系统)
我建议使用 multistrap,这里有一个很好的教程:http://free-electrons.com/blog/embdebian-with-multistrap/
回到 Raspbian,我们现在可以下载官方系统,并开始准备了。
打开一个新的 shell 会话并运行下面的命令:
wget-O raspbian.zip https://downloads.raspberrypi.org/raspbian_lite_latest
unzip raspbian.zip
我们用下面的命令审查:
fdisk-l2016-11-25-raspbian-jessie-lite.img
Disk2016-11-25-raspbian-jessie-lite.img:1.3GiB,1390411776bytes,2715648sectors
Units:sectors of1*512=512bytes
Sectorsize(logical/physical):512bytes/512bytes
I/Osize(minimum/optimal):512bytes/512bytes
Disklabeltype:dos
Diskidentifier:0x244b8248
DeviceBootStartEndSectorsSizeIdType
2016-11-25-raspbian-jessie-lite.img1819213721512902463Mc W95 FAT32(LBA)
2016-11-25-raspbian-jessie-lite.img2137216271564725784321.2G83Linux
我们可以看到它有两个分区。第一个是启动分区,它主要包含了 bootloader、Linux 内核以及少量配置文件。第二个是根分区。
我们可以在我们的文件系统上挂载这些分区,从根分区开始:
mount-o loop,offset=702545922016-11-25-raspbian-jessie-lite.img/mnt
offset 取决于扇区大小(512):70254592 = 512 * 137216
接着是启动分区:
mount-o loop,offset=4194304,sizelimit=660602882016-11-25-raspbian-jessie-lite.img/mnt/boot
offset :4194304 = 512 * 8192,sizelimit:66060288 = 512 * 129024 。
树莓派系统现在应该可以在 /mnt 中看到了。我们基本要完成了。
打包内核
内核编译完成后,最后一步包括复制 Linux 内核以及设备树到启动分区中:
cparch/arm64/boot/Image/mnt/boot/kernel8.img
cparch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb/mnt/boot/
调整 config.txt :
echo“kernel=kernel8.img”>>/mnt/boot/config.txt
安装内核模块 :
makeARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu INSTALL_MOD_PATH=/mnt modules_install
umount/mnt/boot
umount/mnt
就是这样了,用于树莓派 3 的** ARM64 Linux 内核**诞生了!
现在你可以压缩镜像,通过 scp 下载下来,并按照标准的步骤放到你的 SD 卡中。
最后你会得到:
uname-a
Linuxraspberrypi4.8.13+bilal-v8+#1SMPWedDec1414:09:38UTC2016aarch64 GNU/Linux