在树莓派5上安装Gentoo
译自gentoo wiki,部分有修改
SD卡分区和格式化分区
SD卡分区
通常需要3个分区:boot
, swap
, root
。如果你决定使用swap
作为一个文件,那么就需要两个分区。在这个例子中,我们使用swap
作为一个分区。
- 开始对SD卡进行分区
fdisk /dev/sda
- 创建
GPT
label
Command (m for help): g
Created a new GPT disklabel (GUID: 7D005A92-7338-432E-9068-0E23B9A132F0).
- 创建
boot
分区 +512M
Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-249737182, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-249737182, default 249735167): +512M
Created a new partition 1 of type 'Linux filesystem' and of size 512 MiB.
- 创建
swap
分区 +16G
Command (m for help): n
Partition number (2-128, default 2):
First sector (1050624-249737182, default 1050624):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-249737182, default 249735167): +16G
Created a new partition 2 of type 'Linux filesystem' and of size 16 GiB.
- 创建
root
分区 +last
Command (m for help): n
Partition number (3-128, default 3):
First sector (34605056-249737182, default 34605056):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (34605056-249737182, default 249735167):
Created a new partition 3 of type 'Linux filesystem' and of size 102.6 GiB.
- 设置
boot
分区的文件系统。为了能启动树莓派,boot
分区必须为FAT
。#fat32、Microsoft basic data、 EFI system partition
Command (m for help): t
Partition number (1-3, default 3): 1
Partition type or alias (type L to list all): 1
Changed type of partition 'Linux filesystem' to 'EFI System'.
- 设置
swap
分区的文件系统
Command (m for help): t
Partition number (1-3, default 3): 2
Partition type or alias (type L to list all): 19
Changed type of partition 'Linux filesystem' to 'Linux swap'.
- 设置
root
分区的文件系统
Command (m for help): t
Partition number (1-3, default 3):
Partition type or alias (type L to list all): 27
Changed type of partition 'Linux filesystem' to 'Linux root (ARM-64)'.
- 最后检查下各分区
Command (m for help): p
Disk /dev/sdb: 119.08 GiB, 127865454592 bytes, 249737216 sectors
Disk model: Storage Device
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: gpt
Disk identifier: 7D005A92-7338-432E-9068-0E23B9A132F0
Device Start End Sectors Size Type
/dev/sdb1 2048 1050623 1048576 512M EFI System
/dev/sdb2 1050624 34605055 33554432 16G Linux swap
/dev/sdb3 34605056 249735167 215130112 102.6G Linux root (ARM-64)
Filesystem/RAID signature on partition 1 will be wiped.
- 写入更改到SD卡
Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.
格式化分区
boot
分区
mkfs.fat -F 32 /dev/sdb1
swap
分区
mkswap /dev/sdb2
root
分区
mkfs.ext4 /dev/sdb3
安装基本系统和portage快照
挂载到root
分区
mount /dev/sdb3 /mnt/gentoo
cd /mnt/gentoo
安装stage3
- Download latest stage 3 tar file
wget -c https://mirrors.tuna.tsinghua.edu.cn/gentoo/releases/arm64/autobuilds/current-stage3-arm64-openrc/stage3-arm64-openrc-20240728T234856Z.tar.xz
- Untar stage file
tar xpvf stage3-arm64-openrc-20240728T234856Z.tar.xz --xattrs-include='*.*' --numeric-owner
安装portage快照
- 下载最新的portage快照
wget -c https://mirrors.tuna.tsinghua.edu.cn/gentoo/snapshots/portage-latest.tar.bz2
- 解压文件
mkdir /mnt/gentoo/var/db/repos/gentoo
tar xpvf portage-latest.tar.bz2 --strip-components=1 -C /mnt/gentoo/var/db/repos/gentoo
安装kernel, modules, firmware
安装内核
有两种方法安装树莓派5内核,使用预构建的内核和从源码编译。在这个例子中,我们将要使用预构建的内核。
- 复制
raspberry-firmware
仓库
git clone --depth=1 https://github.com/raspberrypi/firmware.git
- 为了启动树莓派,需要一些boot文件夹下放一些文件。确保你已经从
fireware/boot
下复制了下面的文件到/mnt/gentoo/boot
mount /dev/sdb1 /mnt/gentoo/boot/
cp firmware/boot/bcm2712-rpi-5-b.dtb /mnt/gentoo/boot/
cp firmware/boot/fixup_cd.dat /mnt/gentoo/boot/
cp firmware/boot/fixup.dat /mnt/gentoo/boot/
cp firmware/boot/start_cd.elf /mnt/gentoo/boot/
cp firmware/boot/start.elf /mnt/gentoo/boot/
cp firmware/boot/bootcode.bin /mnt/gentoo/boot/
cp firmware/boot/kernel8.img /mnt/gentoo/boot/
cp -r firmware/boot/overlays /mnt/gentoo/boot/
- 配置引导加载程序
与grub或其他的引导加载程序不同,树莓派从/boot
中寻找cmdline.txt
文件来引导操作系统启动。将下面内容放入到/mnt/gentoo/boot/cmdline.txt
中
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p3 rootfstype=ext4 rootwait
- Config.txt
config.txt
需要配置树莓派来使用正确的驱动。将下面的内容放入到/mnt/gentoo/boot/config.txt
# have a properly sized image
disable_overscan=1
# Enable audio (loads snd_bcm2835)
dtparam=audio=on
# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d-pi5
安装modules
https://github.com/raspberrypi/firmware有着我们所有需要的模块,只需要复制模块到/mnt/gentoo/lib/
。
cp -r firmware/modules /mnt/gentoo/lib/
安装firmware
为了使用wifi和蓝牙,我们需要复制固件到/mnt/gentoo/lib/firmware
文件夹。
WIFI
- 复制wifi固件仓库
git clone --depth=1 https://github.com/RPi-Distro/firmware-nonfree.git
- 如果不存在
/mnt/gentoo/lib/firmware/brcm
,则创建该目录。
mkdir -p /mnt/gentoo/lib/firmware/brcm
- 树莓派5的wifi模块是
brcmfmc43455
,所以我们只需要复制这些文件。
cp firmware-nonfree/debian/config/brcm80211/cypress/cyfmac43455-sdio-standard.bin /mnt/gentoo/lib/firmware/brcm/brcmfmac43455-sdio.bin
cp firmware-nonfree/debian/config/brcm80211/cypress/cyfmac43455-sdio.clm_blob /mnt/gentoo/lib/firmware/brcm/brcmfmac43455-sdio.clm_blob
cp firmware-nonfree/debian/config/brcm80211/brcm/brcmfmac43455-sdio.txt /mnt/gentoo/lib/firmware/brcm/
- 当树莓派5启动时,他会带有型号名称的固件名称,例如
raspberry,5-model-b
,所以我们需要为固件文件创建链接,确保我们有以下链接。
`-rw-r–r– 1 root root 643651 Jan 21 12:20 brcmfmac43455-sdio.bin -rw-r–r– 1 root root 2676 Jan 21 12:18 brcmfmac43455-sdio.clm_blob lrwxrwxrwx 1 root root 22 Jan 21 12:23 brcmfmac43455-sdio.raspberrypi,5-model-b.bin -> brcmfmac43455-sdio.bin lrwxrwxrwx 1 root root 27 Jan 21 12:23 brcmfmac43455-sdio.raspberrypi,5-model-b.clm_blob -> brcmfmac43455-sdio.clm_blob lrwxrwxrwx 1 root root 22 Jan 21 12:24 brcmfmac43455-sdio.raspberrypi,5-model-b.txt -> brcmfmac43455-sdio.txt -rw-r–r– 1 root root 2074 Jan 21 12:19 brcmfmac43455-sdio.txt ``
#### Bluetooth
1. 复制蓝牙固件仓库
```sh
git clone --depth=1 https://github.com/RPi-Distro/bluez-firmware.git
- 如不存在目录
/mnt/gentoo/lib/firmware/brcm
,则创建该目录。
mkdir -p /mnt/gentoo/lib/firmware/brcm
- 对于蓝牙,只需要
BCM4345C0.hcd
。
cp bluez-firmware/debian/firmware/broadcom/BCM4345C0.hcd /mnt/gentoo/lib/firmware/brcm/
- 相似的,我们需要为树莓派5创建一个链接。
ln -s /mnt/gentoo/lib/firmware/brcm/BCM4345C0.hcd /mnt/gentoo/lib/firmware/brcm/BCM4345C0.raspberrypi,5-model-b.hcd
设置wifi
为使用wifi,需要一个网络工具。经过尝试wpa_supplicant
,发现并不能在固件上运行。所以转换到NetworkManager
安装NetworkManager
有两种方式:
- 如果有以太网,你可以完成这个教程并在树莓派上安装。
- 如果你没有以太网,你可以下载所有必要文件到
/var/cache/distfiles
并且安装NetworkManager
到树莓派。
安装NetworkManager
USE="-modemmanager -ppp -gtk-doc -introspection -concheck" emerge networkmanager
NetworkManager下载缓存文件
USE="-modemmanager -ppp -gtk-doc -introspection -concheck" emerge -pf networkmanager
准备使用
在我们启动树莓派的Gentoo之前,有一些东西需要我们来设置。
xorg.conf
如果想看视频使用X11
,编辑(或创建)文件/usr/share/X11/xorg.conf.d/99-rpi5.conf
,包含以下内容
Section "OutputClass"
Identifier "vc4"
MatchDriver "vc4"
Driver "modesetting"
Option "PrimaryGPU" "true"
EndSection
Section "Device"
Identifier "kms"
Driver "modesetting"
Option "AccelMethod" "msdri3"
Option "UseGammaLUT" "off"
EndSection
fstab
确保文件有以下内容:/mnt/gentoo/etc/fstab
# /mnt/gentoo/etc/fstab
/dev/mmcblk0p1 /boot vfat defaults 0 2
/dev/mmcblk0p2 none swap sw 0 0
/dev/mmcblk0p3 / ext4 defaults,noatime 0 1
shadow file
在我们登录进树莓派之前,我们需要更改root
用户的密码。下面密码是raspberry,将其替换为/mnt/gentoo/etc/shadow
文件的第一行,保证root
用户的你只有这一行。
root:$6$xxPVR/Td5iP$/7Asdgq0ux2sgNkklnndcG4g3493kUYfrrdenBXjxBxEsoLneJpDAwOyX/kkpFB4pU5dlhHEyN0SK4eh/WpmO0::0:99999:7:::
inittab
/mnt/gentoo/etc/inittab
文件需要更新,下面内容需要被注释
f0:12345:respawn:/sbin/agetty 9600 ttyAMA0 vt100
卸载所有分区
umount /mnt/gentoo/boot
umount /mnt/gentoo
使用愉快
插入SD卡到树莓派中尽情享用。
不幸的是,还没有结束,还需要进行一些基本的配置,才能够进行基本的使用。
基本配置
日期
树莓派的初始日期总是1970年,这样无法安装软件,通过date
命令来进行手动设置时间
# 2024年08月05日09时04分
date 080509042024
make.conf
COMMON_FLAGS="-mcpu=cortex-a76+crc+crypto -mtune=cortex-a76 -O2 -pipe"
timezone
echo "Asia/Shanghai" > /etc/timezone
emerge --config sys-libs/timezone-data
VIDEO_CARDS
VIDEO_CARDS="v3d vc4"
locale
# /etc/locale.gen
en_US.utf8
locale-gen
eselect locale list
eselect locale set <x>
env-update && source /etc/profile
hostname
echo <your hostname> > /etc/hostname
dhcpcd
rc-update add dhcpcd default
cd /etc/init.d
ln -s net.lo net.eth0
rc-update add net.eth0 default