§2023-09-11
以下: using h2Jammy.yushei.net
¶00. Build
¶01. build essentula
$ sudo apt-get install build-essential gawk gcc g++ gfortran git texinfo bison libncurses-dev flex libssl-dev gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
¶01. Get the sources
$ mkdir -p build/rpi-4B && cd $_
$ git clone --depth=1 https://github.com/raspberrypi/firmware -b stable
$ git clone --depth=1 https://github.com/raspberrypi/linux -b rpi-5.15.y
$ wget https://distfiles.gentoo.org/releases/arm64/autobuilds/20230910T213208Z/stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz
$ wget https://mirror.leaseweb.com/gentoo/snapshots/portage-latest.tar.bz2
ls
firmware linux portage-latest.tar.bz2 stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz
¶02. make disk image to simulate
$ dd if=/dev/zero of=gentoo-16G-openrc-desktop-2023-09-12.img bs=1M count=16384 <-- 32G 32768 is too big ???
$ sudo losetup -f
/dev/loop24
$ sudo fdisk /dev/loop24
Device Boot Start End Sectors Size Id Type
/dev/loop24p1 2048 1050623 1048576 512M c W95 FAT32 (LBA)
/dev/loop24p2 1050624 67108863 66058240 31.5G 83 Linux
$ sudo partprobe /dev/loop24
$ lsblk /dev/loop24
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop24 7:24 0 32G 0 loop
├─loop24p1 259:2 0 512M 0 part
└─loop24p2 259:3 0 31.5G 0 part
$ sudo mkfs.vfat -F 32 /dev/loop24p1
mkfs.fat 4.2 (2021-01-31)
$ sudo mkfs.ext4 /dev/loop24p2
mke2fs 1.46.5 (30-Dec-2021)
¶03. mount the partition
sudo mkdir /mnt/gentoo
sudo mount /dev/loop24p2 /mnt/gentoo
sudo mkdir /mnt/gentoo/boot/
sudo mount /dev/loop24p1 /mnt/gentoo/boot
$ lsblk /dev/loop24*
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop24 7:24 0 32G 0 loop
├─loop24p1 259:2 0 512M 0 part /mnt/gentoo/boot
└─loop24p2 259:3 0 31.5G 0 part /mnt/gentoo
¶04. Extracting files
$ sudo tar xpfv stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz --xattrs-include='*.*' --numeric-owner -C /mnt/gentoo/
$ sudo mkdir /mnt/gentoo/var/db/repos/gentoo
$ sudo tar xvpf portage-latest.tar.bz2 --strip-components=1 -C /mnt/gentoo/var/db/repos/gentoo
¶05. Cross compiling and installing kernel, modules and device tree
$ pwd /home/alexlai/build/rpi-4B $ ls firmware gentoo-32G-openrc-desktop-2023-09-12.img linux portage-latest.tar.bz2 stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz $ cd linux
$ cd linux $ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make distclean $ ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make bcm2711_defconfig $ time ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make -j4 Image modules dtbs real 51m18.735s user 181m56.288s sys 21m8.824s $ ls -l arch/arm64/boot/ total 21556 drwxrwxr-x 33 alexlai alexlai 4096 九 12 07:42 dts -rw-rw-r-- 1 alexlai alexlai 22059520 九 12 09:03 Image -rw-rw-r-- 1 alexlai alexlai 1562 九 12 07:42 install.sh -rw-rw-r-- 1 alexlai alexlai 960 九 12 07:42 Makefile
$ sudo ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make modules_install INSTALL_MOD_PATH=/mnt/gentoo
$ cd .. $ ls firmware gentoo-32G-openrc-desktop-2023-09-12.img linux portage-latest.tar.bz2 stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz
$ sudo cp -vr firmware/boot/* /mnt/gentoo/boot/ $ sudo cp -vr firmware/modules /mnt/gentoo/lib $ sudo cp -vr linux/arch/arm64/boot/dts/overlays/*.dtbo /mnt/gentoo/boot/overlays $ sudo cp -v linux/arch/arm64/boot/dts/broadcom/bcm2711-rpi-4-b.dtb /mnt/gentoo/boot/ $ sudo cp -v linux/arch/arm64/boot/Image /mnt/gentoo/boot/kernel8.img
¶06. Configuration
get some config files:
$ pwd /home/alexlai/build/rpi-4B
$ wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/fstab $ wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/inittab $ wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/config.txt $ wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/cmdline.txt $ ls cmdline.txt config.txt firmware fstab gentoo-32G-openrc-desktop-2023-09-12.img inittab linux portage-latest.tar.bz2 stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz
$ su
mv /mnt/gentoo/etc/fstab /mnt/gentoo/etc/fstab_bak
cp -v fstab /mnt/gentoo/etc/
mv /mnt/gentoo/etc/inittab /mnt/gentoo/etc/inittab_bak
cp -v inittab /mnt/gentoo/etc/
mv /mnt/gentoo/boot/config.txt /mnt/gentoo/boot/config_txt_bak
mv: cannot stat '/mnt/gentoo/boot/config.txt': No such file or directory
cp -v config.txt /mnt/gentoo/boot/
cp -v cmdline.txt /mnt/gentoo/boot
¶07, change my /ets/fstab, cmline.txt
/dev/mmcblk0p1 /boot vfat noauto,noatime 1 2
/dev/mmcblk0p2 none swap sw 0 0 <-- I do not have swap
/dev/mmcblk0p2 / ext4 noatime 0 1
console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 rootwait <-- original is root=/dev/mmcblk0p3
¶08. set localtime
$ ls /mnt/gentoo/usr/share/zoneinfo Africa Asia Canada Cuba EST Factory GMT+0 Hongkong Iran Japan Libya MST7MDT Pacific PST8PDT Turkey US zone1970.tab America Atlantic CET EET EST5EDT GB GMT-0 HST iso3166.tab Kwajalein MET Navajo Poland ROC tzdata.zi UTC zone.tab Antarctica Australia Chile Egypt Etc GB-Eire GMT0 Iceland Israel leapseconds Mexico NZ Portugal ROK UCT WET Zulu Arctic Brazil CST6CDT Eire Europe GMT Greenwich Indian Jamaica leap-seconds.list MST NZ-CHAT PRC Singapore Universal W-SU $ ls /mnt/gentoo/usr/share/zoneinfo/Asia/ Aden Ashgabat Bangkok Chita Damascus Gaza Irkutsk Kamchatka Kolkata Macau Novokuznetsk Pyongyang Saigon Srednekolymsk Thimbu Ulan_Bator Yangon Almaty Ashkhabad Barnaul Choibalsan Dhaka Harbin Istanbul Karachi Krasnoyarsk Magadan Novosibirsk Qatar Sakhalin Taipei Thimphu Urumqi Yekaterinburg Amman Atyrau Beirut Chongqing Dili Hebron Jakarta Kashgar Kuala_Lumpur Makassar Omsk Qostanay Samarkand Tashkent Tokyo Ust-Nera Yerevan Anadyr Baghdad Bishkek Chungking Dubai Ho_Chi_Minh Jayapura Kathmandu Kuching Manila Oral Qyzylorda Seoul Tbilisi Tomsk Vientiane Aqtau Bahrain Brunei Colombo Dushanbe Hong_Kong Jerusalem Katmandu Kuwait Muscat Phnom_Penh Rangoon Shanghai Tehran Ujung_Pandang Vladivostok Aqtobe Baku Calcutta Dacca Famagusta Hovd Kabul Khandyga Macao Nicosia Pontianak Riyadh Singapore Tel_Aviv Ulaanbaatar Yakutsk $ su Password:
sudo echo "Asia/Taipei" > /mnt/gentoo/etc/timezone
cp -v /mnt/gentoo/usr/share/zoneinfo/Asia/Taipei /mnt/gentoo/etc/localtime
date
公曆 20廿三年 九月 十二日 週二 九時36分廿三秒
¶ Miscellaneous
- clrear root password: login without password
sudo sed -i 's/^root:.*/root::::::::/' /mnt/gentoo/etc/shadow
- set keymap:
sudo sed -i 's/^keymap="us"/keymap="de"/' /mnt/gentoo/etc/conf.d/keymaps
¶Umount
# umount /mnt/gentoo/boot/
root@h2Jammy:/home/alexlai/build/rpi-4B# umount /mnt/gentoo
root@h2Jammy:/home/alexlai/build/rpi-4B# lsblk /dev/loop24
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
loop24 7:24 0 32G 0 loop
├─loop24p1 259:2 0 512M 0 part
└─loop24p2 259:3 0 31.5G 0 part
- compress
xz -9e gentoo-32G-openrc-desktop-2023-09-12.img
xz -d gentoo-32G-openrc-desktop-2023-09-12.img.xz
以下: using gentoo-32G-systemd-2023-09-08.img
on surfacePro.yushei.net, all in arch-chroot
¶00. Build
# mkdir rpi-5.15
# cd rpi-5.15/
# git clone --depth=1 https://github.com/raspberrypi/firmware -b stable
# git clone --depth=1 https://github.com/raspberrypi/linux -b rpi-5.15.y
# wget https://distfiles.gentoo.org/releases/arm64/autobuilds/20230903T203159Z/stage3-arm64-desktop-openrc-20230903T203159Z.tar.xz
# wget https://mirror.leaseweb.com/gentoo/snapshots/portage-latest.tar.bz2
¶01. Preparing the SD card
¶02. Installation
- Mounting the partitions
# mkdir /mnt/gentoo
# mount /dev/sdc2 /mnt/gentoo/
# mkdir /mnt/gentoo/boot
# mount /dev/sdc1 /mnt/gentoo/boot
# lsblk /dev/sdc*
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdc 8:32 1 29.7G 0 disk
├─sdc1 8:33 1 256M 0 part /mnt/gentoo/boot
└─sdc2 8:34 1 29.5G 0 part /mnt/gentoo
sdc1 8:33 1 256M 0 part /mnt/gentoo/boot
sdc2 8:34 1 29.5G 0 part /mnt/gentoo
¶03. Extracting files
# tar xpvf stage3-arm64-desktop-openrc-20230903T203159Z.tar.xz --xattrs-include='*.*' --numeric-owner -C /mnt/gentoo/
# mkdir /mnt/gentoo/var/db/repos/gentoo
# tar xvpf portage-latest.tar.bz2 --strip-components=1 -C /mnt/gentoo/var/db/repos/gentoo
¶04. Cross compiling and installing kernel, modules and device tree
# cd linux
# ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- make distclean
# ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- make bcm2711_defconfig
# time ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- make -j4 Image modules dtbs
real 51m28.513s
user 187m7.960s
sys 17m22.418s
(chroot) SurfacePro
# ARCH=arm64 CROSS_COMPILE=aarch64-unknown-linux-gnu- make modules_install INSTALL_MOD_PATH=/mnt/gentoo
..
INSTALL /mnt/gentoo/lib/modules/5.15.92-v8+/kernel/sound/usb/snd-usbmidi-lib.ko
XZ /mnt/gentoo/lib/modules/5.15.92-v8+/kernel/sound/usb/snd-usbmidi-lib.ko.xz
DEPMOD /mnt/gentoo/lib/modules/5.15.92-v8+
# cd ..
# cp -vr firmware/boot/* /mnt/gentoo/boot/
# cp -vr firmware/modules /mnt/gentoo/lib
# cp -vr linux/arch/arm64/boot/dts/overlays/*.dtbo /mnt/gentoo/boot/overlays
# cp -v linux/arch/arm64/boot/dts/broadcom/bcm2711-rpi-4-b.dtb /mnt/gentoo/boot/
# cp -v linux/arch/arm64/boot/Image /mnt/gentoo/boot/kernel8.img
¶05. Configuration
get some config files:
wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/fstab
wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/inittab
wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/config.txt
wget https://github.com/mck-sbs/gentoo-rpi-64-bit/raw/main/cmdline.txt
mv /mnt/gentoo/etc/fstab /mnt/gentoo/etc/fstab_bak
mv fstab /mnt/gentoo/etc/
mv /mnt/gentoo/etc/inittab /mnt/gentoo/etc/inittab_bak
mv inittab /mnt/gentoo/etc/
mv /mnt/gentoo/boot/config.txt /mnt/gentoo/boot/config_txt_bak
sudo mv config.txt /mnt/gentoo/boot/
sudo mv cmdline.txt /mnt/gentoo/boot/
- List the timezones:
# ls /mnt/gentoo/usr/share/zoneinfo
- and choose one:
# cp /mnt/gentoo/usr/share/zoneinfo/Europe/Berlin /mnt/gentoo/etc/localtime
# echo "Europe/Berlin" > /mnt/gentoo/etc/timezone
- clrear root password:
sudo sed -i 's/^root:.*/root::::::::/' /mnt/gentoo/etc/shadow
- set keymap:
sudo sed -i 's/^keymap="us"/keymap="de"/' /mnt/gentoo/etc/conf.d/keymaps
- Unmount SD
sudo umount /mnt/gentoo/boot
sudo umount /mnt/gentoo
- Back up
Before you put in your SD card and boot the rpi, you should back up your sd card:
sudo dd if=/dev/mmcblk0 of=image.img
ToDo shrink
https://medium.com/platformer-blog/creating-a-custom-raspbian-os-image-for-production-3fcb43ff3630
- References
- https://wiki.gentoo.org/wiki/Raspberry_Pi_3_64_bit_Install
- https://wiki.gentoo.org/wiki/Raspberry_Pi4_64_Bit_Install
- https://github.com/sakaki-/gentoo-on-rpi-64bit
- https://wiki.gentoo.org/wiki/Raspberry_Pi/Quick_Install_Guide
- https://deardevices.com/2019/04/18/how-to-crosscompile-raspi/
- https://raspberrypi.stackexchange.com/questions/311/how-do-i-backup-my-raspberry-pi
- https://wiki.gentoo.org/wiki/Raspberry_Pi/Kernel_Compilation