为树莓派 3 构建 64 位内核
树莓派 64位

> 
> 编辑：在写完这个这篇文章之后，我在树莓派 3 上基于 Debian 开始打造 64 位的系统。你可以[在这里找到](https://github.com/bamarni/pi64)。
> 
> 
> 


**![](/data/attachment/album/201707/02/225856ciizhnzt6l42hhok.jpg)**


**树莓派 3** 配有 Broadcom BCM2837 64 位 ARMv8 四核 Cortex A53 处理器，它是一个 **64 位 CPU**。如果你有一块，运行以下命令可能会让你感到惊讶：



```
uname -a
Linux raspberrypi 4.4.34-v7+ #930 SMP Wed Nov 23 15:20:41 GMT 2016 armv7l GNU/Linux

```

是的，这是一个 **32 位内核**。这是因为树莓派基金会还没有为官方的树莓派系统 Raspbian 提供 64 位版本。然而你可以构建一个，多亏了 [Electron752](https://github.com/Electron752) 提供的许多补丁。


### 构建内核


树莓派基金会维护着[它们自己的 Linux 内核分支](https://github.com/raspberrypi/linux)，它为它们的设备特别裁剪过，同时定期地从上游合并。


我们将会遵照[这个页面](https://www.raspberrypi.org/documentation/linux/kernel/building.md)的指导来**构建一个 64 位内核**。


我们不能使用“本地构建”的方法，因为它需要一块 64 位的树莓派，这个我们明显还没有。因此我们需要**交叉编译**它，**Ubuntu** 是推荐的系统。我个人没有 Ubuntu，因此我在一个有 2 个 CPU 的 Ubuntu 16.04 Digital Ocean 实例上构建，这应该花费我 $0.03。如果你也想这么做，你可以通过[这个链接](https://m.do.co/c/8ef9c5832a9c)得到 $10 的免费额度。或者你可以通过使用 Virtualbox 中的 Ubuntu VM 作为实例。


首先，我们需要一些**构建工具**以及\*\* aarch64 交叉编译器\*\*：



```
apt-get update
apt-get install -y bc build-essential gcc-aarch64-linux-gnu git unzip

```

接着我们可以下载 **Linux 内核源码**：



```
git clone –depth=1 -b rpi-4.8.y https://github.com/raspberrypi/linux.git

```

进入到创建的 git 目录。另外你可以为你的内核添加额外的版本标签，可以通过编辑 `Makefile` 的开始几行完成：



```
VERSION = 4
PATCHLEVEL = 8
SUBLEVEL = 13
EXTRAVERSION = +bilal

```

为了**构建它**，运行下面的命令：



```
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig
make -j 3 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-

```

第一个应该很快。第二个则会完全不同，我没有精确计时，但是对我来说大概要半个小时。根据你的 CPU 数（nproc \* 1.5）调整 `-j` 标志。


### 选择一个 Linux 发行版


在内核编译的时候，我们可以开始准备它的 Linux 发行版了。在本教程中为了简单我使用 **Raspbian**，即使这是一个只有 32 位的发行版。



> 
> 如果你想要一直用 64 位系统，你应该选一个有 aarch64 支持的发行版，Debian 有一个健壮的 [ARM64 移植版](https://wiki.debian.org/Arm64Port)。得到它基本有三种方式：
> 
> 
> * 下载一个预构建的根文件系统，这很可能会如页面中提到的那样给你一个过期的版本。
> * 如果你熟悉 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 -l 2016-11-25-raspbian-jessie-lite.img
Disk 2016-11-25-raspbian-jessie-lite.img: 1.3 GiB, 1390411776 bytes, 2715648 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x244b8248

Device Boot Start End Sectors Size Id Type
2016-11-25-raspbian-jessie-lite.img1 8192 137215 129024 63M c W95 FAT32 (LBA)
2016-11-25-raspbian-jessie-lite.img2 137216 2715647 2578432 1.2G 83 Linux

```

我们可以看到它有**两个分区**。第一个是**启动分区**，它主要包含了 bootloader、Linux 内核以及少量配置文件。第二个是**根分区**。


我们可以在我们的文件系统上**挂载这些分区**，从**根分区**开始：



```
mount -o loop,offset=70254592 2016-11-25-raspbian-jessie-lite.img /mnt

```

`offset` 取决于扇区大小（512）：70254592 = 512 \* 137216


接着是**启动分区**：



```
mount -o loop,offset=4194304,sizelimit=66060288 2016-11-25-raspbian-jessie-lite.img /mnt/boot

```

`offset` ：4194304 = 512 \* 8192，`sizelimit`：66060288 = 512 \* 129024 。


树莓派系统现在应该可以在 `/mnt` 中看到了。我们基本要完成了。


### 打包内核


内核编译完成后，最后一步包括**复制 Linux 内核**以及**设备树**到启动分区中：



```
cp arch/arm64/boot/Image /mnt/boot/kernel8.img
cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb /mnt/boot/

```

调整 `config.txt` :



```
echo “kernel=kernel8.img” >> /mnt/boot/config.txt

```

安装**内核模块** :



```
make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu INSTALL_MOD_PATH=/mnt modules_install
umount /mnt/boot
umount /mnt

```

就是这样了，用于树莓派 3 的\*\* ARM64 Linux 内核\*\*诞生了！


现在你可以压缩镜像，通过 scp 下载下来，并按照标准的步骤放到你的 SD 卡中。


最后你会得到：



```
uname -a
Linux raspberrypi 4.8.13+bilal-v8+ #1 SMP Wed Dec 14 14:09:38 UTC 2016 aarch64 GNU/Linux

```



---


via: <https://devsidestory.com/build-a-64-bit-kernel-for-your-raspberry-pi-3/>


作者：[Bilal Amarni](http://devsidestory.com/about-me) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
# Build a 64-bit kernel for your Raspberry Pi 3


EDIT : After writing this blog post I’ve started a 64-bit OS for the Raspberry Pi 3, based on Debian. You can find it[here].

The **Raspberry Pi 3** ships with a Broadcom BCM2837 64bit ARMv8 quad core Cortex A53 processor,
which is a **64-bit CPU**. If you own one of these, running the following command might surprise you :

```
$ uname -a
Linux raspberrypi 4.4.34-v7+ #930 SMP Wed Nov 23 15:20:41 GMT 2016 armv7l GNU/Linux
```


Yes, this is a **32-bit kernel**. The reason for this is that the Raspberry Pi foundation doesn’t yet provides a 64-bit
version of Raspbian, the official OS for Raspberry Pi. It is however possible to build one, thanks to the various
patches sent by [Electron752](https://github.com/Electron752).

# Build the Kernel

The Raspberry Pi foundation maintains [their own fork](https://github.com/raspberrypi/linux) of the Linux Kernel which
is especially tailored for their devices, while upstream gets merged regularly.

We’re going to adapt instructions from [that page](https://www.raspberrypi.org/documentation/linux/kernel/building.md)
to **build a 64-bit Kernel**.

We cannot use the “Local building” method as it’d require a 64-bit Raspberry Pi, which we obviously don’t have yet.
So we have to **cross-compile** it, **Ubuntu** is the recommended OS for this. I personally don’t have Ubuntu so
I’ll make my build on a 2 CPUs Ubuntu 16.04 Digital Ocean droplet, which should cost me $0.03. If you also want
to proceed like this, you can get $100 free credits through [this link](https://m.do.co/c/8ef9c5832a9c).
Alternatively, you could use a Ubuntu VM through Virtualbox for instance.

First, we’d need a few **build tools** and the **aarch64 cross-compiler** :

```
$ apt-get update
$ apt-get install -y bc build-essential gcc-aarch64-linux-gnu git unzip
```


Then we can download the **Linux Kernel sources** :

`$ git clone –depth=1 -b rpi-4.8.y https://github.com/raspberrypi/linux.git`


Enter now inside the created git directory. Optionally, you can add an extra version tag for your kernel. This is done by editing the beginning of the Makefile :

```
VERSION = 4
PATCHLEVEL = 8
SUBLEVEL = 13
EXTRAVERSION = +bilal
```


In order to **build it**, run the following commands :

```
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- bcmrpi3_defconfig
$ make -j 3 ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-
```


The first one should be pretty fast. For the second one it’s a whole different story, I haven’t timed it exactly but it was around 30 minutes for me. Make sure to adapt the -j flag depending on your number of CPUs (nproc * 1.5).

# Choose a Linux distribution

While the Kernel is being built, we can start preparing a Linux distribution for it. I’ll be using **Raspbian**
for simplicity in this tutorial, even though this is a 32-bit only distribution.


If you want to go 64-bit all the way you should pick up a distribution available in aarch64, Debian has a robust-[ARM64Port]. To grab it there are basically 3 options :download a pre-built root filesystem, this would most likely give you an outdated one as mentioned in that page-build your own with debootstrap if you’re familiar with it (otherwise it can be tricky as it requires some manual tweaks, the original purpose of it is to chroot from an already running host, not build a root filesystem for another machine).-the one I’d recommend, using multistrap, there seems to be a nice tutorial on this page :[http://free-electrons.com/blog/embdebian-with-multistrap/]

Back to Raspbian, we can now download the official OS and start preparing it.

Open a new shell session and run the following commands :

```
$ wget -O raspbian.zip https://downloads.raspberrypi.org/raspbian_lite_latest
$ unzip raspbian.zip
```


We can inspect it with the following command :

```
$ fdisk -l 2016-11-25-raspbian-jessie-lite.img
Disk 2016-11-25-raspbian-jessie-lite.img: 1.3 GiB, 1390411776 bytes, 2715648 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x244b8248
Device Boot Start End Sectors Size Id Type
2016-11-25-raspbian-jessie-lite.img1 8192 137215 129024 63M c W95 FAT32 (LBA)
2016-11-25-raspbian-jessie-lite.img2 137216 2715647 2578432 1.2G 83 Linux
```


We can see it has **two partitions**. The first one is the **boot partition**, it mainly contains the bootloader,
the Linux Kernel and a few config files. The second one is the **root partition**.

We can **mount those partitions** on our filesystem, starting with the **root partition** :

`$ mount -o loop,offset=70254592 2016-11-25-raspbian-jessie-lite.img /mnt`


The offset depends on the sector size, which is 512 : 70254592 = 512 * 137216

Then the **boot partition** :

`$ mount -o loop,offset=4194304,sizelimit=66060288 2016-11-25-raspbian-jessie-lite.img /mnt/boot`



offset= 4194304 = 512 * 8192,sizelimit= 66060288 = 512 * 129024

The Raspbian OS can now be seen under **/mnt**. We’re almost there.

EDIT : I’ve later on found out about

[kpartx], which lets you mount image partitions without the hassle of dealing with offsets ([see this link]).

# Wrapping it up

Once the Kernel build is finished, the last steps involve **copying the Linux Kernel**
and the **device tree** to the boot partition :

```
$ cp arch/arm64/boot/Image /mnt/boot/kernel8.img
$ cp arch/arm64/boot/dts/broadcom/bcm2710-rpi-3-b.dtb /mnt/boot/
```


Tweaking **config.txt** :

`$ echo “kernel=kernel8.img” >> /mnt/boot/config.txt`


Installing **Kernel modules** :

```
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu INSTALL_MOD_PATH=/mnt modules_install
$ umount /mnt/boot
$ umount /mnt
```


And… that’s it, a freshly baked **ARM64 Linux Kernel** for our Raspberry Pi 3!

You can now **compress the image**, **download it** (through [scp](https://linux.die.net/man/1/scp) for instance)
and follow the standard instructions to **put it on your SD card**.

Eventually you’ll get :

```
> uname -a
Linux raspberrypi 4.8.13+bilal-v8+ #1 SMP Wed Dec 14 14:09:38 UTC 2016 aarch64 GNU/Linux
```