§2023-09-08

¶1. Create disk image file to simulate

$ pwd
/home/alexlai/build/gentoo/src
$ dd if=/dev/zero of=gentoo-32G-systemd-2023-09-08.img bs=1M count=32768
$ sudo losetup -f
/dev/loop3
$ sudo losetup /dev/loop3 rpi3-32G-systemd-2023-09-06.img
$ sudo fdisk /dev/loop3 

デバイス     起動 開始位置 最後から   セクタ サイズ Id タイプ
/dev/loop3p1          2048  1050623  1048576   512M  c W95 FAT32 (LBA)
/dev/loop3p2       1050624 67108863 66058240  31.5G 83 Linux

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

コマンド (m でヘルプ): w
パーティション情報が変更されました。
ioctl() を呼び出してパーティション情報を再読み込みします。
パーティション情報の再読み込みに失敗しました。: 無効な引数です

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).
$ sudo partprobe /dev/loop3
$ lsblk /dev/loop3
NAME      MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
loop3       7:3    0   32G  0 loop 
├─loop3p1 259:6    0  512M  0 part 
└─loop3p2 259:7    0 31.5G  0 part
$ sudo mkfs.vfat -F 32 /dev/loop3p1
$ sudo mkfs.ext4 /dev/loop3p2
$ sudo mount /dev/loop3p2 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 / #
 

¶ 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

# ls -l /etc/portage/make.profile      <-- check 
lrwxrwxrwx 1 root root 67 Aug 28 02:41 /etc/portage/make.profile -> ../../var/db/repos/gentoo/profiles/default/linux/amd64/17.1/systemd

# ls -l /var/db/repos/
合計 4
drwxr-xr-x 174 root root 4096 Sep  8 13:02 gentoo

¶ 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 crossdev -t aarch64-unknown-linux-gnu command, I did setup 02-cross-dev first!!! or as follows:

(chroot) h2Jammy / # ls /var/db/repos/
gentoo
(chroot) h2Jammy / # mkdir -p /var/db/repos/crossdev/{profiles,metadata}
(chroot) h2Jammy / # echo 'crossdev' > /var/db/repos/crossdev/profiles/repo_name
(chroot) h2Jammy / # echo 'masters = gentoo' > /var/db/repos/crossdev/metadata/layout.conf
(chroot) h2Jammy / # chown -R portage:portage /var/db/repos/crossdev

If the Gentoo ebuild repository is synchronized using Git, or any other method with Manifest files that do not include checksums for ebuilds, prevent "masked by: corruption" errors with:

masters = gentoo
thin-manifests = true
(chroot) h2Jammy / # ls /etc/portage/repos.conf/crossdev.conf
ls: cannot access '/etc/portage/repos.conf/crossdev.conf': No such file or directory
(chroot) h2Jammy / # nano /etc/portage/repos.conf/crossdev.conf # as follows
[crossdev]
location = /var/db/repos/crossdev
priority = 10
masters = gentoo
auto-sync = no

(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 / # crossdev -t aarch64-linux-gnu
---------------------------------------------------------------------------------------------------------------------------------------------------------
 * crossdev version:      20230616
 * 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-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-linux-gnu-binutils.log
 * Emerging cross-binutils ...
(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. install git

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

&para; 11.kernel source

(chroot) SurfacePro / # emerge --ask sys-kernel/gentoo-sources

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

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

[ebuild N ] dev-libs/elfutils-0.189-r1 USE="bzip2 nls utils -lzma -static-libs -test -verify-sig -zstd" ABI_X86="(64) -32 (-x32)" [ebuild N ] virtual/libelf-3-r1 ABI_X86="(64) -32 (-x32)" [ebuild N ] app-arch/cpio-2.14 USE="nls" [ebuild N ] app-alternatives/cpio-0 USE="gnu (split-usr) -libarchive" [ebuild N ] sys-kernel/gentoo-sources-6.1.46 USE="-build -experimental -symlink"

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

... (chroot) SurfacePro / # ls /usr/src/ linux-6.1.46-gentoo


&para; 12. Install the kernel tools and firmware.

- emerge --ask sys-kernel/genkernel        --->  to make initramfs.img, !!! All ebuilds that could satisfy "sys-kernel/linux-firmware" have been masked.

(chroot) SurfacePro / # emerge --ask sys-kernel/genkernel <-- failed !!! All ebuilds that could satisfy "sys-kernel/linux-firmware" have been masked. !!! One of the following masked packages is required to complete your request:

(dependency required by "sys-kernel/genkernel-4.3.6::gentoo[firmware]" [ebuild]) (dependency required by "sys-kernel/genkernel" [argument]) For more information, see the MASKED PACKAGES section in the emerge man page or refer to the Gentoo Handbook.


- to solve the above add `ACCEPT_LICENSE="linux-fw-redistributable" in /etc/portage/make.confn and run again

(chroot) SurfacePro / # emerge --ask sys-kernel/genkernel

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

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

[ebuild N ] sys-kernel/linux-firmware-20230804 USE="redistributable -compress-xz -compress-zstd -initramfs -savedconfig (-unknown-license)" [ebuild N ] sys-kernel/genkernel-4.3.6 USE="firmware (-ibm)" PYTHON_SINGLE_TARGET="python3_11 -python3_10"

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

(chroot) SurfacePro / # which genkernel /usr/bin/genkernel


&para; 13.  `emerge -av dev-embedded/u-boot-tools` ---> mkimg

(chroot) SurfacePro / # emerge -av dev-embedded/u-boot-tools

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

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

[ebuild N ] dev-lang/swig-4.1.1-r1::gentoo USE="pcre -ccache -doc -test" 8400 KiB [ebuild N ] dev-embedded/u-boot-tools-2023.01::gentoo USE="-envtools" 18126 KiB

Total: 2 packages (2 new), Size of downloads: 26525 KiB

Would you like to merge these packages? [Yes/No] Yes ... (chroot) SurfacePro / # which mkimage /usr/bin/mkimage


- emerge --ask sys-fs/cryptsetup
- emerge --ask sys-kernel/linux-
- emerge --ask sys-kernel/dracut