§2023-09-06

¶1. Create disk image file to simulate

$ pwd
/home/alexlai/build/gentoo/src
$ ls -l
total 1781388
drwxrwxr-x  2 alexlai alexlai           6  九   1 15:34 boot
drwxr-xr-x 21 root    root           4096  八  28 10:32 root
-rw-rw-r--  1 alexlai alexlai 34359738368  九   1 15:41 rpi3-32G.img
-rw-rw-r--  1 alexlai alexlai   245624008  八  28 11:06 stage3-arm64-systemd-20230827T234643Z.tar.xz
alexlai@h2Jammy:~/build/gentoo/src$ dd if=/dev/zero of=rpi3-32G-systemd-2023-09-06.img bs=1M count=32768

# find out which /dev/loop?? is available
$ sudo losetup -f
[sudo] password for alexlai: 
/dev/loop23

# associate the loop to img file
$ sudo losetup /dev/loop23 rpi3-32G-systemd-2023-09-06.img



alexlai@h2Jammy:~/build/gentoo/src$ sudo fdisk /dev/loop23 

Welcome to fdisk (util-linux 2.37.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xbcc810c9.

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-67108863, default 2048): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-67108863, default 67108863): +512M

Created a new partition 1 of type 'Linux' and of size 512 MiB.

Command (m for help): t
Selected partition 1
Hex code or alias (type L to list all): 0c
Changed type of partition 'Linux' to 'W95 FAT32 (LBA)'.

Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (1050624-67108863, default 1050624): 
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1050624-67108863, default 67108863): 

Created a new partition 2 of type 'Linux' and of size 31.5 GiB.

Command (m for help): p
Disk /dev/loop23: 32 GiB, 34359738368 bytes, 67108864 sectors
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: dos
Disk identifier: 0xbcc810c9

Device        Boot   Start      End  Sectors  Size Id Type
/dev/loop23p1         2048  1050623  1048576  512M  c W95 FAT32 (LBA)
/dev/loop23p2      1050624 67108863 66058240 31.5G 83 Linux

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Invalid argument

The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or partx(8).

/dev/loop1: []: (/var/lib/snapd/snaps/core18_2790.snap)
/dev/loop19: []: (/var/lib/snapd/snaps/ubuntu-budgie-welcome_467.snap)
/dev/loop17: []: (/var/lib/snapd/snaps/snapd_20092.snap)
/dev/loop8: []: (/var/lib/snapd/snaps/core20_1974.snap)
/dev/loop15: []: (/var/lib/snapd/snaps/gnome-42-2204_126.snap)
/dev/loop6: []: (/var/lib/snapd/snaps/firefox_3026.snap)
/dev/loop23: []: (/home/alexlai/build/gentoo/src/rpi3-32G-systemd-2023-09-06.img)
/dev/loop13: []: (/var/lib/snapd/snaps/hunspell-dictionaries-1-7-2004_2.snap)
/dev/loop4: []: (/var/lib/snapd/snaps/core22_858.snap)
/dev/loop21: []: (/var/lib/snapd/snaps/zola_810.snap)
/dev/loop11: []: (/var/lib/snapd/snaps/gnome-42-2204_120.snap)
/dev/loop2: []: (/var/lib/snapd/snaps/core20_2015.snap)
/dev/loop0: []: (/var/lib/snapd/snaps/core18_2785.snap)
/dev/loop18: []: (/var/lib/snapd/snaps/snapd-desktop-integration_57.snap)
/dev/loop9: []: (/var/lib/snapd/snaps/gnome-3-38-2004_143.snap)
/dev/loop16: []: (/var/lib/snapd/snaps/snapd-desktop-integration_83.snap)
/dev/loop7: []: (/var/lib/snapd/snaps/firefox_3068.snap)
/dev/loop14: []: (/var/lib/snapd/snaps/snapd_19993.snap)
/dev/loop5: []: (/var/lib/snapd/snaps/core22_864.snap)
/dev/loop22: []: (/var/lib/snapd/snaps/zola_814.snap)
/dev/loop12: []: (/var/lib/snapd/snaps/gtk-common-themes_1535.snap)
/dev/loop3: []: (/var/lib/snapd/snaps/bare_5.snap)
/dev/loop20: []: (/var/lib/snapd/snaps/ubuntu-budgie-welcome_469.snap)
/dev/loop10: []: (/var/lib/snapd/snaps/gnome-3-38-2004_140.snap)

$ sudo partprobe /dev/loop23 <-- has to ??? like daemong-reload

$ sudo mkfs.vfat -F 32 /dev/loop23p1
mkfs.fat 4.2 (2021-01-31)
alexlai@h2Jammy:~/build/gentoo/src$ sudo mkfs.ext4 /dev/loop23p2
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks: done                            
Creating filesystem with 8257280 4k blocks and 2064384 inodes
Filesystem UUID: 0b28f169-afbb-4f0c-93f4-3754e627af42
Superblock backups stored on blocks: 
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
        4096000, 7962624

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   

$ sudo mount /dev/loop23p2 root
$ sudo tar xvfp stage3-amd64-systemd-20230903T170202Z.tar.xz -C root   <-- amd64 x86_64 version

¶ Step 2. arch-chroot

$ sudo arch-chroot root /bin/bash
h2Jammy / # source /etc/profile
h2Jammy / # export PS1="(chroot) $PS1"
(chroot) h2Jammy / #
(chroot) h2Jammy / # lsblk
NAME       MAJ:MIN RM   SIZE RO TYPE MOUNTPOINTS
loop0        7:0    0     4K  1 loop 
loop1        7:1    0  55.7M  1 loop 
loop2        7:2    0  63.4M  1 loop 
loop3        7:3    0  55.7M  1 loop 
loop4        7:4    0  63.5M  1 loop 
loop5        7:5    0  73.9M  1 loop 
loop6        7:6    0   7.7M  1 loop 
loop7        7:7    0 237.2M  1 loop 
loop8        7:8    0 236.8M  1 loop 
loop9        7:9    0 485.5M  1 loop 
loop10       7:10   0 485.5M  1 loop 
loop11       7:11   0 349.7M  1 loop 
loop12       7:12   0 349.7M  1 loop 
loop13       7:13   0  91.7M  1 loop 
loop14       7:14   0  37.1M  1 loop 
loop15       7:15   0  53.3M  1 loop 
loop16       7:16   0   428K  1 loop 
loop17       7:17   0  73.9M  1 loop 
loop18       7:18   0  40.8M  1 loop 
loop19       7:19   0    32G  0 loop 
loop20       7:20   0  26.8M  1 loop 
loop21       7:21   0   452K  1 loop 
loop22       7:22   0   7.6M  1 loop 
loop23       7:23   0  26.8M  1 loop 
loop23       7:24   0    32G  0 loop 
|-loop23p1 259:0    0   512M  0 part 
`-loop23p2 259:1    0  31.5G  0 part /
sda          8:0    0 931.5G  0 disk 
|-sda1       8:1    0   512M  0 part 
|-sda2       8:2    0  15.2G  0 part [SWAP]
`-sda3       8:3    0 915.8G  0 part 

¶ 2-1 nano /etc/resolv.conf as

nameserver 8.8.8.8
nameserver 8.8.4.4
(chroot) h2Jammy / # ping google.com
PING google.com (142.251.42.238) 56(84) bytes of data.
64 bytes from tsa01s11-in-f14.1e100.net (142.251.42.238): icmp_seq=1 ttl=116 time=5.21 ms
64 bytes from tsa01s11-in-f14.1e100.net (142.251.42.238): icmp_seq=2 ttl=116 time=3.69 ms

¶2-2. set locale

(chroot) h2Jammy / # locale-gen 
 * Generating 2 locales (this might take a while) with 4 jobs
 *  (2/2) Generating C.UTF-8 ...                                                                                                                                                                                                                                    [ ok ]
 *  (1/2) Generating en_US.UTF-8 ...                                                                                                                                                                                                                                [ ok ]
 * Generation complete
 * Adding locales to archive ..

¶2.3 /etc/profile

# /etc/profile: login shell setup
#
# That this file is used by any Bourne-shell derivative to setup the
# environment for login shells.
#

# Load environment settings from profile.env, which is created by
# env-update from the files in /etc/env.d
if [ -e /etc/profile.env ] ; then
	. /etc/profile.env
fi

# You should override these in your ~/.bashrc (or equivalent) for per-user
# settings.  For system defaults, you can add a new file in /etc/profile.d/.
export EDITOR=${EDITOR:-/bin/nano}
export PAGER=${PAGER:-/usr/bin/less}

unset ROOTPATH

# process *.sh files in /etc/profile.d
for sh in /etc/profile.d/*.sh ; do
	[ -r "$sh" ] && . "$sh"
done
unset sh

if [ -n "${BASH_VERSION-}" ] ; then
	# Newer bash ebuilds include /etc/bash/bashrc which will setup PS1
	# including color.  We leave out color here because not all
	# terminals support it.
	if [ -f /etc/bash/bashrc ] ; then
		# Bash login shells run only /etc/profile
		# Bash non-login shells run only /etc/bash/bashrc
		# Since we want to run /etc/bash/bashrc regardless, we source it
		# from here.  It is unfortunate that there is no way to do
		# this *after* the user's .bash_profile runs (without putting
		# it in the user's dot-files), but it shouldn't make any
		# difference.
		. /etc/bash/bashrc
	else
		PS1='\u@\h \w \$ '
	fi
else
	# Setup a bland default prompt.  Since this prompt should be useable
	# on color and non-color terminals, as well as shells that don't
	# understand sequences such as \h, don't put anything special in it.
	PS1="${USER:-$(whoami 2>/dev/null)}@$(uname -n 2>/dev/null) \$ "
fi

# export LANG, LC_ALL 
export LANG=en_US.UTF-8
# LANG is the primary environmental variable that sets the default locale for your system.
export LC_ALL=C.UTF-8
# LC_ALL is another environmental variable that can override the settings of LANG and other LC_* variables for all locale categories.
# 

# PS1
export PS1="(chroot) $PS1" # or PS1="(rpi3Chr) $PS1" to idicating building for rpi3

¶2.4 set time zone

(chroot) h2Jammy / # date
Sat Sep  2 01:21:48 -00 2023
(chroot) h2Jammy / # ln -sf /usr/share/zoneinfo/Asia/Taipei /etc/localtime
(chroot) h2Jammy / # date
Sat Sep  2 09:22:00 CST 2023

¶4. /etc/portage/repos.conf/gentoo.conf

[gentoo]
location = /var/db/repos/gentoo
sync-type = rsync
sync-uri = rsync://rsync.namerica.gentoo.org/gentoo-portage
auto-sync = yes
priority = 10

¶5. /etc/portage/make.conf

# These settings were set by the catalyst build script that automatically
# built this stage.
# Please consult /usr/share/portage/config/make.conf.example for a more
# detailed example.
COMMON_FLAGS="-O2 -pipe"
CFLAGS="${COMMON_FLAGS}"
CXXFLAGS="${COMMON_FLAGS}"
FCFLAGS="${COMMON_FLAGS}"
FFLAGS="${COMMON_FLAGS}"

# WARNING: Changing your CHOST is not something that should be done lightly.
# Please consult https://wiki.gentoo.org/wiki/Changing_the_CHOST_variable before changing.
# CHOST="aarch64-unknown-linux-gnu"
# to be able to `crossdev -t aarch64-unknown-linux-gnu --ov-output /opt/toolchains/`
# error messages:
# * Refusing to create a cross-compiler using the same
# * target name as your host utils.
# you have to comment `CHOST="aarch64-unknown-linux-gnu"`

# NOTE: This stage was built with the bindist Use flag enabled


# This sets the language of build output to English.
# Please keep this setting intact when reporting bugs.
LC_MESSAGES=C.utf8

# Pi3 users (or Pi4 users with a 1GiB variant) should be
# less aggressive here
MAKEOPTS="-j4"

¶6. emerge-webrsync

chroot) h2Jammy / # emerge-webrsync
!!! Section 'gentoo' in repos.conf has location attribute set to nonexistent directory: '/var/db/repos/gentoo'
!!! Invalid Repository Location (not a dir): '/var/db/repos/gentoo'
 * PGP verification method: gemato
 * Fetching most recent snapshot ...
 * Trying to retrieve 20230901 snapshot from http://distfiles.gentoo.org ...
 * Fetching file gentoo-20230901.tar.xz.md5sum ...
...
 Number of deleted files: 0
Number of regular files transferred: 119,498
Total file size: 194.95M bytes
Total transferred file size: 194.95M bytes
Literal data: 194.95M bytes
Matched data: 0 bytes
File list size: 3.57M
File list generation time: 0.002 seconds
File list transfer time: 0.000 seconds
Total bytes sent: 104.66M
Total bytes received: 2.39M

sent 104.66M bytes  received 2.39M bytes  3.51M bytes/sec
total size is 194.95M  speedup is 1.82
 * Cleaning up ...

Performing Global Updates
(Could take a couple of minutes if you have a lot of binary packages.)



 * IMPORTANT: 10 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.

skip

(chroot) h2nas01 / # ls -l /etc/portage/make.profile
lrwxrwxrwx 1 root root 67 Aug 28 02:41 /etc/portage/make.profile -> ../../var/db/repos/gentoo/profiles/default/linux/amd64/17.1/systemd
(chroot) h2nas01 / # ls -l /var/db/repos/
total 4

¶ 7. Performing Global Updates

(chroot) h2nas01 / # emerge --ask --verbose --update --deep --newuse @world

 * IMPORTANT: 10 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.


These are the packages that would be merged, in order:
...

emerge --depclean   <-- if updated sucessfully, then

¶ 8. Install crossdev on the PC

(rpi3Chr) h2nas01 / # emerge --ask sys-devel/crossdev

(rpi3Chr) h2nas01 / # nano /etc/portage/make.conf 
(rpi3Chr) h2nas01 / # crossdev -t aarch64-unknown-linux-gnu --ov-output /opt/toolchains/  <-- I have to comment `CHOST="aarch64-unknown-linux-gnu"
                                                                                        <-- /etc/portage/make.conf

¶ 9. Using crossdev to build a cross compiler

before issuing the following command, I did setup 02-cross-dev first

(chroot) h2Jammy / # crossdev -t aarch64-unknown-linux-gnu
---------------------------------------------------------------------------
 * crossdev version:      20230321
 * Host Portage ARCH:     amd64
 * Host Portage System:   x86_64-pc-linux-gnu (i686-pc-linux-gnu x86_64-pc-linux-gnu)
 * Target Portage ARCH:   arm64
 * Target System:         aarch64-unknown-linux-gnu
 * Stage:                 4 (C/C++ compiler)
 * USE=multilib:          no
 * Target ABIs:           arm64

 * binutils:              binutils-[latest]
 * gcc:                   gcc-[latest]
 * headers:               linux-headers-[latest]
 * libc:                  glibc-[latest]

 * CROSSDEV_OVERLAY:      /var/db/repos/crossdev
 * PORT_LOGDIR:           /var/log/portage
 * PORTAGE_CONFIGROOT:    /
 * Portage flags:         
  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  
 * leaving metadata/layout.conf alone in /var/db/repos/crossdev
  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  _  -  ~  -  
 * Log: /var/log/portage/cross-aarch64-unknown-linux-gnu-binutils.log
 * Emerging cross-binutils ...                                                                                                                                                                                                                                      [ ok ]
 * Log: /var/log/portage/cross-aarch64-unknown-linux-gnu-gcc-stage1.log
 * Emerging cross-gcc-stage1 ...                                                                                                                                                                                                                                    [ ok ]
 * Log: /var/log/portage/cross-aarch64-unknown-linux-gnu-linux-headers.log
 * Emerging cross-linux-headers ...                                                                                                                                                                                                                                 [ ok ]
 * Log: /var/log/portage/cross-aarch64-unknown-linux-gnu-glibc.log
 * Emerging cross-glibc ...                                                                                                                                                                                                                                         [ ok ]
 * Log: /var/log/portage/cross-aarch64-unknown-linux-gnu-gcc-stage2.log
 * Emerging cross-gcc-stage2 ...              
(chroot) h2Jammy / # which aarch64-unknown-linux-gnu-gcc
/usr/bin/aarch64-unknown-linux-gnu-gcc

(chroot) h2Jammy / # aarch64-unknown-linux-gnu-gcc --version
aarch64-unknown-linux-gnu-gcc (Gentoo 13.2.1_p20230826 p7) 13.2.1 20230826
Copyright (C) 2023 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

(chroot) h2Jammy / # gcc-config -l
 [1] aarch64-unknown-linux-gnu-13 *

 [2] x86_64-pc-linux-gnu-12 *

¶10. Fetch the Raspberry Pi firmware

emerge --ask dev-vcs/git

(rpi3Chr) h2nas01 / # emerge --ask dev-vcs/git

 * IMPORTANT: 10 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.


These are the packages that would be merged, in order:

Calculating dependencies... done!
Dependency resolution took 1.93 s.

[ebuild  N     ] virtual/perl-Digest-MD5-2.580.100_rc 
[ebuild  N     ] dev-perl/Error-0.170.290  USE="-test" 
[ebuild  N     ] dev-perl/TimeDate-2.330.0-r1  USE="-test" 
[ebuild  N     ] virtual/perl-IO-1.520.0 
[ebuild  N     ] dev-perl/Mozilla-CA-20999999-r1  USE="-test" 
[ebuild  N     ] virtual/perl-MIME-Base64-3.160.100_rc 
[ebuild  N     ] virtual/perl-Digest-SHA-6.40.0 
[ebuild  N     ] dev-perl/Digest-HMAC-1.40.0 
[ebuild  N     ] dev-perl/Net-SSLeay-1.920.0-r1  USE="-examples -minimal -test" 
[ebuild  N     ] dev-perl/Authen-SASL-2.160.0-r2  USE="-kerberos -test" 
[ebuild  N     ] dev-perl/IO-Socket-SSL-2.83.0  USE="-examples -idn -test" 
[ebuild  N     ] virtual/perl-libnet-3.150.0  USE="ssl" 
[ebuild  N     ] dev-perl/MailTools-2.210.0  USE="-examples -test" 
[ebuild  N     ] dev-vcs/git-2.41.0  USE="blksha1 curl gpg iconv nls pcre perl safe-directory webdav -cgi -cvs -doc -highlight -keyring -mediawiki -perforce (-selinux) -subversion -test -tk -xinetd" PYTHON_SINGLE_TARGET="python3_11 -python3_10 (-python3_12)" 

Would you like to merge these packages? [Yes/No] Yes

>>> Verifying ebuild manifests

>>> Emerging (1 of 14) virtual/perl-Digest-MD5-2.580.100_rc::gentoo
 * Fetching files in the background.
 * To view fetch progress, run in another terminal:
 * tail -f /var/log/emerge-fetch.log
>>> Unpacking source...
>>> Source unpacked in /var/tmp/portage/virtual/perl-Digest-MD5-2.580.100_rc/work
>>> Preparing source in /var/tmp/portage/virtual/perl-Digest-MD5-2.580.100_rc/work ...
>>> Source prepared.
>>> Configuring source in /var/tmp/portage/virtual/perl-Digest-MD5-2.580.100_rc/work ...
>>> Source configured.
>>> Compiling source in /var/tmp/portage/virtual/perl-Digest-MD5-2.580.100_rc/work ...
>>> Source compiled.
>>> Test phase [not enabled]: virtual/perl-Digest-MD5-2.580.100_rc

>>> Install virtual/perl-Digest-MD5-2.580.100_rc into /var/tmp/portage/virtual/perl-Digest-MD5-2.580.100_rc/image
>>> Completed installing virtual/perl-Digest-MD5-2.580.100_rc into /var/tmp/portage/virtual/perl-Digest-MD5-2.580.100_rc/image

 * Final size of build directory: 4 KiB
 * Final
...
* IMPORTANT: 10 news items need reading for repository 'gentoo'.
 * Use eselect news read to view new items.

(rpi3Chr) h2nas01 / # which git
/usr/bin/git

---