Raspberry Pi/ARM64 in place installation

FEEL FREE TO EDIT! original: https://cmpct.info/~sam/gentoo/arm64/PiGentoo64.html At the end is a TODO list for wikifying

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.

Misc

 * #gentoo-arm on irc.libera.chat may be useful
 * I’m sam_ on libera.chat

Prerequisites

 * Raspberry Pi 4 (should work with 3B/B+ too)
 * An SD card (>= 16GB, I used 32GB) (32GB minimum is recommended. Using e.g. rust-bin will be needed to make 16GB work.)
 * The latest 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

Prepare

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

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

Update and reboot your system

 * To get the latest bootloader, kernel, etc.
 * Reboot

Filesystem setup
Recommendd filesystems for the root partition are ext4, btrfs or f2fs. f2fs is used as an example:
 * 1) Install f2fs-tools for mkfs.f2fs and resize.f2fs;
 * 2) Create an f2fs partition. I had to create a partition inbetween the main ext4 one and my f2fs one to get the right size. Couldn’t figure out why at the time.
 * 3) Mount it at /mnt/gentoo and cd there..
 * 4) 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 the site linked above).

Untar the stage file
Untar as usual:
 * 1) Start customising make.conf, set these:  Note that you can set -march etc appropriately to use distcc, but still get the benefits.


 * 1) copy DNS settings first, then mount, then chroot:
 * 2) Copy over some firmware for WiFi and Bluetooth from Raspbian

Enter chroot as usual:

Sync emerge so we can install packages:

Build the (RPi, not upstream) kernel:
Prerequisites: Updating the system is likely needed to avoid e.g. bindist conflicts! You may be able to for now --exclude gcc though: Fetch the sources for the kernel (build requirement):

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):

KBUILD_CFLAGS  += -march=native

Start compiling:

time make -j4 Image modules dtbs

For me, this took:

real   66m55.242s user   234m46.421s sys    27m24.339s

… but I was quite generous with the modules I chose to compile in for fun, e.g. KVM.

Mount /boot inside the chroot this time:

mount /dev/mmcblk0p1 /boot

Take a backup of the old kernel, just in case:

# kernel8 is for arm64 mv /boot/kernel8.img /boot/kernel8.img.bak

Install modules, etc:

make modules_install dtbs_install scripts/mkknlimg arch/arm64/boot/Image /boot/kernel8.img

Tasks before reboot:

emerge -a -uvDN @world

Create /etc/fstab:

proc                 /proc           proc    defaults          0       0 PARTUUID=6c586e13-01 /boot           vfat    defaults          0       2 /dev/mmcblk0p4       /               f2fs    defaults,noatime  0       1 # a swapfile is not a swap partition, no line here #  use  dphys-swapfile swap[on|off]  for that

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

root=/dev/mmcblk0p4 rootfstype=f2fs

Optional extras before reboot:

Set up network and SSH

# Needed for WiFi / network emerge -av dhcpcd wpa_supplicant

# Create wpa_supplicant.conf as appropriate # https://www.raspberrypi.org/documentation/configuration/wireless/wireless-cli.md

# Tell wpa_supplicant, sshd to start on boot rc-update add wpa_supplicant default rc-update add sshd default

# Setup the network scripts # Wpa_supplicant

Create a user with sudo rights

# Create a user useradd -m gentoo -G wheel,video,users,audio,usb

# Set a password (optional) passwd gentoo

# Add in an SSH key (optional) su gentoo mkdir ~/.ssh # Put your public ssh key in ~/.ssh/authorized_keys

# Install sudo emerge -av sudo

rng-tools to use the built-in RNG:

emerge -av rng-tools mkdir /etc/modules-load.d/       echo 'bcm2835-rng' > /etc/modules-load.d/random.conf

rc-update add rngd

# Useful tools like equery, eix (optional) emerge -av gentoolkit eix htop

# Time sync emerge -av openntpd

# Start on boot rc-update add ntpd

# Set up swclock to save last clock on shutdown, restore on boot rc-update add swclock boot

Reboot and hope for the best!

Post-boot:

If your time is wrong, force a manual sync:

# Stop running ntpd /etc/init.d/ntpd stop

# Manual sync ntpd -s -d

# Wait a few seconds for it to sync # Then CTRL-C (quit)

# Run date to check time is now accurate date

# Start up ntpd again /etc/init.d/ntpd start

Set CPU_FLAGS_ARM:

emerge -av cpuid2cpuflags

mkdir /etc/portage/package.use echo "*/* $(cpuid2cpuflags)" > /etc/portage/package.use/00cpuflags

emerge -c cpuid2cpuflags

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 mkdir ~/bin echo 'export PATH="$HOME/bin:${PATH}"' >> ~/.bashrc . ~/.bashrc

cd ~/git/build # Run `git clone ...` the first time # Run `git pull` in future to check for updates git clone https://github.com/raspberrypi/rpi-eeprom cd rpi-eeprom

# Copy the firmware files to where tool wants sudo mkdir /lib/firmware/raspberrypi/bootloader/ sudo cp -rv firmware/* /lib/firmware/raspberrypi/bootloader

# Copy vl805 to your bin cp firmware/vl805 ~/bin

sudo --preserve-env=PATH env ./rpi-eeprom-update
 * 1) Run the actual update

Install dosfsutils for vfat fsck:

TODO wikify

 * section should be extended for other wiki sites
 * The Guide is the main part of this site and starts with 6. (at the moment). Could that be nicer?