为树莓派 3 构建 64 位内核
编辑:在写完这个这篇文章之后,我在树莓派 3 上基于 Debian 开始打造 64 位的系统。你可以在这里找到。
树莓派 3 配有 Broadcom BCM2837 64 位 ARMv8 四核 Cortex A53 处理器,它是一个 64 位 CPU。如果你有一块,运行以下命令可能会让你感到惊讶:
<span class="kwd">uname</span><span class="pun">-</span><span class="pln">a</span>
<span class="typ">Linux</span><span class="pln"> raspberrypi </span><span class="lit">4.4</span><span class="pun">.</span><span class="lit">34</span><span class="pun">-</span><span class="pln">v7</span><span class="pun">+</span><span class="com">#</span><span class="lit">930</span><span class="pln"> SMP </span><span class="typ">Wed</span><span class="typ">Nov</span><span class="lit">23</span><span class="lit">15</span><span class="pun">:</span><span class="lit">20</span><span class="pun">:</span><span class="lit">41</span><span class="pln"> GMT </span><span class="lit">2016</span><span class="pln"> armv7l GNU</span><span class="pun">/</span><span class="typ">Linux</span>
是的,这是一个 32 位内核。这是因为树莓派基金会还没有为官方的树莓派系统 Raspbian 提供 64 位版本。然而你可以构建一个,多亏了 Electron752 提供的许多补丁。
构建内核
树莓派基金会维护着它们自己的 Linux 内核分支,它为它们的设备特别裁剪过,同时定期地从上游合并。
我们将会遵照这个页面的指导来构建一个 64 位内核。
我们不能使用“本地构建”的方法,因为它需要一块 64 位的树莓派,这个我们明显还没有。因此我们需要交叉编译它,Ubuntu 是推荐的系统。我个人没有 Ubuntu,因此我在一个有 2 个 CPU 的 Ubuntu 16.04 Digital Ocean 实例上构建,这应该花费我 $0.03。如果你也想这么做,你可以通过这个链接得到 $10 的免费额度。或者你可以通过使用 Virtualbox 中的 Ubuntu VM 作为实例。
首先,我们需要一些构建工具以及** aarch64 交叉编译器**:
<span class="kwd">apt-get</span><span class="pln"> update</span>
<span class="kwd">apt-get</span><span class="pln"> install </span><span class="pun">-</span><span class="pln">y bc build</span><span class="pun">-</span><span class="pln">essential </span><span class="kwd">gcc</span><span class="pun">-</span><span class="pln">aarch64</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu </span><span class="kwd">git</span><span class="pln"> unzip</span>
接着我们可以下载 Linux 内核源码:
<span class="kwd">git</span><span class="kwd">clone</span><span class="pun">–</span><span class="pln">depth</span><span class="pun">=</span><span class="lit">1</span><span class="pun">-</span><span class="pln">b rpi</span><span class="pun">-</span><span class="lit">4.8</span><span class="pun">.</span><span class="pln">y https</span><span class="pun">:</span><span class="com">//github.com/raspberrypi/linux.git</span>
进入到创建的 git 目录。另外你可以为你的内核添加额外的版本标签,可以通过编辑 Makefile
的开始几行完成:
<span class="pln">VERSION </span><span class="pun">=</span><span class="lit">4</span>
<span class="pln">PATCHLEVEL </span><span class="pun">=</span><span class="lit">8</span>
<span class="pln">SUBLEVEL </span><span class="pun">=</span><span class="lit">13</span>
<span class="pln">EXTRAVERSION </span><span class="pun">=</span><span class="pun">+</span><span class="pln">bilal</span>
为了构建它,运行下面的命令:
<span class="kwd">make</span><span class="pln"> ARCH</span><span class="pun">=</span><span class="pln">arm64 CROSS_COMPILE</span><span class="pun">=</span><span class="pln">aarch64</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu</span><span class="pun">-</span><span class="pln"> bcmrpi3_defconfig</span>
<span class="kwd">make</span><span class="pun">-</span><span class="pln">j </span><span class="lit">3</span><span class="pln"> ARCH</span><span class="pun">=</span><span class="pln">arm64 CROSS_COMPILE</span><span class="pun">=</span><span class="pln">aarch64</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu</span><span class="pun">-</span>
第一个应该很快。第二个则会完全不同,我没有精确计时,但是对我来说大概要半个小时。根据你的 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 会话并运行下面的命令:
<span class="kwd">wget</span><span class="pun">-</span><span class="pln">O raspbian</span><span class="pun">.</span><span class="pln">zip https</span><span class="pun">:</span><span class="com">//downloads.raspberrypi.org/raspbian_lite_latest</span>
<span class="pln">unzip raspbian</span><span class="pun">.</span><span class="pln">zip</span>
我们用下面的命令审查:
<span class="kwd">fdisk</span><span class="pun">-</span><span class="pln">l </span><span class="lit">2016</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">25</span><span class="pun">-</span><span class="pln">raspbian</span><span class="pun">-</span><span class="pln">jessie</span><span class="pun">-</span><span class="pln">lite</span><span class="pun">.</span><span class="pln">img</span>
<span class="typ">Disk</span><span class="lit">2016</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">25</span><span class="pun">-</span><span class="pln">raspbian</span><span class="pun">-</span><span class="pln">jessie</span><span class="pun">-</span><span class="pln">lite</span><span class="pun">.</span><span class="pln">img</span><span class="pun">:</span><span class="lit">1.3</span><span class="typ">GiB</span><span class="pun">,</span><span class="lit">1390411776</span><span class="pln"> bytes</span><span class="pun">,</span><span class="lit">2715648</span><span class="pln"> sectors</span>
<span class="typ">Units</span><span class="pun">:</span><span class="pln"> sectors of </span><span class="lit">1</span><span class="pun">*</span><span class="lit">512</span><span class="pun">=</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Sector</span><span class="kwd">size</span><span class="pun">(</span><span class="pln">logical</span><span class="pun">/</span><span class="pln">physical</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="pln">I</span><span class="pun">/</span><span class="pln">O </span><span class="kwd">size</span><span class="pun">(</span><span class="pln">minimum</span><span class="pun">/</span><span class="pln">optimal</span><span class="pun">):</span><span class="lit">512</span><span class="pln"> bytes </span><span class="pun">/</span><span class="lit">512</span><span class="pln"> bytes</span>
<span class="typ">Disklabel</span><span class="pln"> type</span><span class="pun">:</span><span class="pln"> dos</span>
<span class="typ">Disk</span><span class="pln"> identifier</span><span class="pun">:</span><span class="lit">0x244b8248</span>
<span class="typ">Device</span><span class="typ">Boot</span><span class="typ">Start</span><span class="typ">End</span><span class="typ">Sectors</span><span class="typ">Size</span><span class="typ">Id</span><span class="typ">Type</span>
<span class="lit">2016</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">25</span><span class="pun">-</span><span class="pln">raspbian</span><span class="pun">-</span><span class="pln">jessie</span><span class="pun">-</span><span class="pln">lite</span><span class="pun">.</span><span class="pln">img1 </span><span class="lit">8192</span><span class="lit">137215</span><span class="lit">129024</span><span class="lit">63M</span><span class="pln"> c W95 FAT32 </span><span class="pun">(</span><span class="pln">LBA</span><span class="pun">)</span>
<span class="lit">2016</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">25</span><span class="pun">-</span><span class="pln">raspbian</span><span class="pun">-</span><span class="pln">jessie</span><span class="pun">-</span><span class="pln">lite</span><span class="pun">.</span><span class="pln">img2 </span><span class="lit">137216</span><span class="lit">2715647</span><span class="lit">2578432</span><span class="lit">1.2G</span><span class="lit">83</span><span class="typ">Linux</span>
我们可以看到它有两个分区。第一个是启动分区,它主要包含了 bootloader、Linux 内核以及少量配置文件。第二个是根分区。
我们可以在我们的文件系统上挂载这些分区,从根分区开始:
<span class="kwd">mount</span><span class="pun">-</span><span class="pln">o loop</span><span class="pun">,</span><span class="pln">offset</span><span class="pun">=</span><span class="lit">70254592</span><span class="lit">2016</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">25</span><span class="pun">-</span><span class="pln">raspbian</span><span class="pun">-</span><span class="pln">jessie</span><span class="pun">-</span><span class="pln">lite</span><span class="pun">.</span><span class="pln">img </span><span class="pun">/</span><span class="pln">mnt</span>
offset
取决于扇区大小(512):70254592 = 512 * 137216
接着是启动分区:
<span class="kwd">mount</span><span class="pun">-</span><span class="pln">o loop</span><span class="pun">,</span><span class="pln">offset</span><span class="pun">=</span><span class="lit">4194304</span><span class="pun">,</span><span class="pln">sizelimit</span><span class="pun">=</span><span class="lit">66060288</span><span class="lit">2016</span><span class="pun">-</span><span class="lit">11</span><span class="pun">-</span><span class="lit">25</span><span class="pun">-</span><span class="pln">raspbian</span><span class="pun">-</span><span class="pln">jessie</span><span class="pun">-</span><span class="pln">lite</span><span class="pun">.</span><span class="pln">img </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">boot</span>
offset
:4194304 = 512 * 8192,sizelimit
:66060288 = 512 * 129024 。
树莓派系统现在应该可以在 /mnt
中看到了。我们基本要完成了。
打包内核
内核编译完成后,最后一步包括复制 Linux 内核以及设备树到启动分区中:
<span class="kwd">cp</span><span class="pln"> arch</span><span class="pun">/</span><span class="pln">arm64</span><span class="pun">/</span><span class="pln">boot</span><span class="pun">/</span><span class="typ">Image</span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">boot</span><span class="pun">/</span><span class="pln">kernel8</span><span class="pun">.</span><span class="pln">img</span>
<span class="kwd">cp</span><span class="pln"> arch</span><span class="pun">/</span><span class="pln">arm64</span><span class="pun">/</span><span class="pln">boot</span><span class="pun">/</span><span class="pln">dts</span><span class="pun">/</span><span class="pln">broadcom</span><span class="pun">/</span><span class="pln">bcm2710</span><span class="pun">-</span><span class="pln">rpi</span><span class="pun">-</span><span class="lit">3</span><span class="pun">-</span><span class="pln">b</span><span class="pun">.</span><span class="pln">dtb </span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">boot</span><span class="pun">/</span>
调整 config.txt
:
<span class="kwd">echo</span><span class="pun">“</span><span class="pln">kernel</span><span class="pun">=</span><span class="pln">kernel8</span><span class="pun">.</span><span class="pln">img</span><span class="pun">”</span><span class="pun">>></span><span class="str">/mnt/</span><span class="pln">boot</span><span class="pun">/</span><span class="pln">config</span><span class="pun">.</span><span class="pln">txt</span>
安装内核模块 :
<span class="kwd">make</span><span class="pln"> ARCH</span><span class="pun">=</span><span class="pln">arm64 CROSS_COMPILE</span><span class="pun">=</span><span class="pln">aarch64</span><span class="pun">-</span><span class="pln">linux</span><span class="pun">-</span><span class="pln">gnu INSTALL_MOD_PATH</span><span class="pun">=/</span><span class="pln">mnt modules_install</span>
<span class="kwd">umount</span><span class="pun">/</span><span class="pln">mnt</span><span class="pun">/</span><span class="pln">boot</span>
<span class="kwd">umount</span><span class="pun">/</span><span class="pln">mnt</span>
就是这样了,用于树莓派 3 的** ARM64 Linux 内核**诞生了!
现在你可以压缩镜像,通过 scp 下载下来,并按照标准的步骤放到你的 SD 卡中。
最后你会得到:
<span class="kwd">uname</span><span class="pun">-</span><span class="pln">a</span>
<span class="typ">Linux</span><span class="pln"> raspberrypi </span><span class="lit">4.8</span><span class="pun">.</span><span class="lit">13</span><span class="pun">+</span><span class="pln">bilal</span><span class="pun">-</span><span class="pln">v8</span><span class="pun">+</span><span class="com">#</span><span class="lit">1</span><span class="pln"> SMP </span><span class="typ">Wed</span><span class="typ">Dec</span><span class="lit">14</span><span class="lit">14</span><span class="pun">:</span><span class="lit">09</span><span class="pun">:</span><span class="lit">38</span><span class="pln"> UTC </span><span class="lit">2016</span><span class="pln"> aarch64 GNU</span><span class="pun">/</span><span class="typ">Linux</span>
via: https://devsidestory.com/build-a-64-bit-kernel-for-your-raspberry-pi-3/
作者:Bilal Amarni 译者:geekpi 校对:wxy