§2023-09-12

以下: 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-6.1.y
$ mv linux linux-rpi-6.1.y/
$ $ wget https://distfiles.gentoo.org/releases/arm64/autobuilds/20230903T203159Z/stage3-arm64-desktop-systemd-20230903T203159Z.tar.xz
$ wget https://mirror.leaseweb.com/gentoo/snapshots/portage-latest.tar.bz2
$ ls
cmdline.txt  fstab                                        linux-rpi-5.15.y  portage-latest.tar.bz2
config.txt   gentoo-32G-openrc-desktop-2023-09-12.img.xz  linux-rpi-6.1.y   stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz
firmware     inittab                                      n                 stage3-arm64-desktop-systemd-20230903T203159Z.tar.xz

¶02. using a 256G microSD

/dev/sdc1            2048   1050623   1048576  512M  c W95 FAT32 (LBA)
/dev/sdc2         1050624  17827839  16777216    8G 82 Linux swap / Solaris
/dev/sdc3        17827840 143656959 125829120   60G 83 Linux
/dev/sdc4       143656960 493895679 350238720  167G 83 Linux
$ sudo mkfs.vfat -F 32 /dev/sdc1
$ sudo mkswap /dev/sdc2
$ sudo mkfs.ext4 /dev/sdc3
$ sudo mkfs.xfs /dev/sdc4
$ lsblk -f /dev/sdc*
NAME   FSTYPE FSVER LABEL UUID                                 FSAVAIL FSUSE% MOUNTPOINTS
sdc                                                                           
├─sdc1 vfat   FAT32       A4F4-F265                                           
├─sdc2 swap   1           c4619ccb-2bc5-452c-a180-5370f36edbd6                
├─sdc3 ext4   1.0         6bd5b0ac-f879-4313-8280-4483b554bbe1                
└─sdc4 xfs                f42372b4-dc1d-437a-803c-c6716a14c3cb   

¶03. mount the partition

$ sudo mkdir /mnt/gentoo
$ sudo mount /dev/sdc3 /mnt/gentoo
$ sudo mkdir /mnt/gentoo/boot/
$ sudo mount /dev/sdc1 /mnt/gentoo/boot
$ lsblk /dev/sdc*
NAME   MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
sdc      8:16   1 235.5G  0 disk 
├─sdc1   8:17   1   512M  0 part /mnt/gentoo/boot
├─sdc2   8:18   1     8G  0 part 
├─sdc3   8:19   1    60G  0 part /mnt/gentoo
└─sdc4   8:20   1   167G  0 part 

¶04. Extracting files

$ sudo tar xvpf stage3-arm64-desktop-systemd-20230910T213208Z.tar.xz -C /mnt/gentoo/
$ sudo mkdir /mnt/gentoo/var/db/repos/gentoo      <-- I did create it
$ sudo tar xvpf portage-latest.tar.bz2 --strip-components=1 -C /mnt/gentoo/var/db/repos/gentoo

$ sudo find /mnt/gentoo/ -name dhcpcd --> none found

¶05. Cross compiling and installing kernel, modules and device tree

$ pwd /home/alexlai/build/rpi-4B $ ls cmdline.txt fstab linux-rpi-5.15.y portage-latest.tar.bz2 config.txt gentoo-32G-openrc-desktop-2023-09-12.img.xz linux-rpi-6.1.y stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz firmware inittab n stage3-arm64-desktop-systemd-20230903T203159Z.tar.xz alexlai@h2Jammy:~/build/rpi-4B$ $ cd linux-rpi-6.1.y

$ 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 54m45.200s user 194m49.830s sys 22m35.340s $ ls -l arch/arm64/boot/ total 22440 drwxrwxr-x 35 alexlai alexlai 4096 九 12 17:48 dts -rw-rw-r-- 1 alexlai alexlai 22962688 九 12 19:17 Image -rwxrwxr-x 1 alexlai alexlai 962 九 12 17:48 install.sh -rw-rw-r-- 1 alexlai alexlai 1198 九 12 17:48 Makefile $ pwd /home/alexlai/build/rpi-4B/linux-rpi-6.1.y $ sudo ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- make modules_install INSTALL_MOD_PATH=/mnt/gentoo <-- don't forget

$ cd .. $ ls cmdline.txt fstab linux-rpi-5.15.y portage-latest.tar.bz2 config.txt gentoo-32G-openrc-desktop-2023-09-12.img.xz linux-rpi-6.1.y stage3-arm64-desktop-openrc-20230910T213208Z.tar.xz firmware inittab n stage3-arm64-desktop-systemd-20230903T203159Z.tar.xz

$ pwd /home/alexlai/build/rpi-4B

$ sudo cp -vr firmware/boot/* /mnt/gentoo/boot/ $ sudo cp -vr firmware/modules /mnt/gentoo/lib $ sudo cp -vr linux-rpi-6.1.y/arch/arm64/boot/dts/overlays/.dtbo /mnt/gentoo/boot/overlays $ sudo cp -vr fstab /mnr/gentoo/etc $ sudo cp -vr cmdline.txt /mnt/gentoo/boot/ $ sudo cp -v config.txt /mnt/gentoo/boot/ $ sudo cp -v inittab /mnt/gentoo/etc/ $ sudo cp -v linux-rpi-6.1.y/arch/arm64/boot/Image /mnt/gentoo/boot/kernel8.img $ sudo echo "Asia/Taipei" > /mnt/gentoo/etc/timezone $ sudo cp -v /mnt/gentoo/usr/share/zoneinfo/Asia/Taipei /mnt/gentoo/etc/localtime $ sudo sed -i 's/^root:./root::::::::/' /mnt/gentoo/etc/shadow

&para;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 <-- not for systemd

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


&para;07, change my /etc/fstab, cmline.txt, if partion the disk differenctly

/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


&para;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:

echo "Asia/Taipei" > /mnt/gentoo/etc/timezone

cp -v /mnt/gentoo/usr/share/zoneinfo/Asia/Taipei /mnt/gentoo/etc/localtime


&para; Miscellaneous
# sed -i 's/^root:.*/root::::::::/' /mnt/gentoo/etc/shadow
sudo sed -i 's/^keymap="us"/keymap="de"/' /mnt/gentoo/etc/conf.d/keymaps

¶Umount

# umount /mnt/gentoo/boot/
# umount /mnt/gentoo
# eject /dev/sdc
# lsblk /dev/sdc
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
sdc    8:16   1   0B  0 disk 
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

# 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/
# ls /mnt/gentoo/usr/share/zoneinfo
# cp /mnt/gentoo/usr/share/zoneinfo/Europe/Berlin /mnt/gentoo/etc/localtime
# echo "Europe/Berlin" > /mnt/gentoo/etc/timezone
sudo sed -i 's/^root:.*/root::::::::/' /mnt/gentoo/etc/shadow
sudo sed -i 's/^keymap="us"/keymap="de"/' /mnt/gentoo/etc/conf.d/keymaps
sudo umount /mnt/gentoo/boot
sudo umount /mnt/gentoo

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