Raspberry Pi/ARM64 in place installation

This page is based on https://cmpct.info/~sam/gentoo/arm64/PiGentoo64.html by sam_ (CC-BY-SA 3.0)

We will bootstrap the Gentoo install from a stage3 provided by gentoo.org, via RaspberryPi OS 64bit.

You do not need a Gentoo system already or to cross-compile.

IRC
If you need help join #gentoo-arm on irc.libera.chat, ask your question and wait for at least 24h (see also: IRC). This guide was written by sam_ on libera.chat.

Notes about the installation
f2fs is used for the root filesystem in this document, you don’t have to. Ext4, btrfs and other filesystems should work, too. Please install the appropriate packages and customize the configuration.

The installation in this document was tested with RaspberryPi OS (former Raspbian) and bootstrapped from there. In theory, you could jump in from any arm64 system (read: kernel).

The terminal multiplexer is handy to make it easy to do other things, like check  during setup & compilation.

Prerequisites

 * Raspberry Pi 4B or 400 (should work with 3B/B+ too)
 * An SD card with at least 32GB minimum is recommended. (Using a 16GB card may be possible when using e.g. rust-bin.)
 * The latest RaspberryPi OS 64bit (former Raspbian) flashed onto the card

Nice-to-haves

 * The biggest the SD card, the better; it’ll wear out slower;
 * A fan and heatsinks (to reduce heat when compiling, prevent throttling);
 * A high-quality power supply (prevent corruption, prevent throttling).
 * A Linux machine sitting around to make tweaks if needed.

Useful links

 * Raspberry_Pi_3_64_bit_Install
 * https://github.com/sakaki-/gentoo-on-rpi-64bit
 * For RPis with 4GB Ram or less: Knowledge_Base:Emerge_out_of_memory

Prepare
Flash with latest Raspbian 64bit (September 2019 here) Easiest way to avoid filesystem mess is to modify cmdline.txt to change init=/usr/lib/raspi-config/init_resize.sh to Edit config.txt and tell the bootloader to use the 64 bit kernel (kernel8.img). Place a file in /boot called ssh. This enables sshd on the next boot: Setup WiFi if appropriate.

Boot
Verify that you are running 64bit by looking for aarch64 in uname -a:

Update and reboot your system
To get the latest bootloader, kernel, etc.

Filesystem setup
Recommended filesystems for the root partition are ext4, btrfs or f2fs. f2fs is used as an example: Install for mkfs.f2fs and resize.f2fs: Create an f2fs partition: The author had to create a partition inbetween the main ext4 one and the f2fs one to get the right size and couldn’t figure out why at the time. Mount the f2fs partition at /mnt/gentoo and cd there: Unmount /boot from Raspbian (so we can mount it in the chroot):

Fetch the stage file
Fetch the stage3 (Please check for a newer one on gentoo.org/downloads -> ARM64 ).

Preparations and chroot
Untar the stage file as usual: Start customising make.conf. Set MAKEOPTS="-jx" suitable for your system: Note that you can set -march etc appropriately to use distcc, but still get the benefits.

Copy over some firmware for WiFi and Bluetooth from RaspberryPi OS:

Enter chroot as usual: Copy DNS settings first, then mount, then chroot:

Prerequisites
Updating the system is likely needed to avoid e.g. bindist conflicts! You may be able to for now --exclude gcc though:

Fetch build requirements for the kernel:

Get the sources so e.g. emerge knows what kernel we're running:

Configure and build the kernel
Generate base configs for a Pi 4, Broadcom bcm2711:

Customise the kernel:

Modify arch/arm64/Makefile (optional, probably not very useful):

Start compiling, the time will differ:

Mount /boot inside the chroot this time:

Take a backup of the old kernel. kernel8 is for ARM64:

Install modules and kernel:

Tasks before reboot
Update world

Create /etc/fstab:

Modify two parameters (root, rootfstype) in /boot/cmdline.txt:

Set up network and SSH
Needed for WiFi / network: Create wpa_supplicant.conf as appropriate https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md Tell wpa_supplicant and sshd to start on boot: Setup the network scripts: Wpa_supplicant

Create a user with sudo rights
Create a user, e.g. gentoo: Set a password (optional): Add in an SSH key (optional): Put your public ssh key in ~/.ssh/authorized_keys Install

Utilize the built-in random number generator and additional tools
Install and configure the system to start the RNG at boot:

Emerge useful tools like   (optional):

Syncing time
Emerge : Start ntpd on boot: Set up swclock to save last clock on shutdown, restore on boot:

Reboot and hope for the best!

Force setting time
If your time is wrong, force a manual sync. Stop running ntpd: Manual sync: Wait a few seconds for it to sync, then CTRL-C (quit). Run to check time is now accurate. Start up ntpd again:

Set CPU_FLAGS_ARM
Install, use and remove a tool for setting the cpuflags:

Installing Raspberry Pi userland
Now it's installed, we need to make it accessible

Regenerate ld cache

Happily use vcgencmd and friends!

Update the firmware (Pi 4/Pi 400 only)
For convenience, add a dir to PATH. Only run this part the first time you update.

Run `git clone ...` the first time, run `git pull` in future to check for updates

Copy the firmware files to where tool wants:

Copy vl805 to your bin: Run the actual update:

Install dosfsutils
fsck.vfat is needed for the boot partition. Install :