塞巴斯蒂安 发表于 2022-2-2 12:19:03

哪吒系列文章之15——Fedora支持

本帖最后由 塞巴斯蒂安 于 2022-2-2 12:18 编辑

Fedora 是知名的Linux操作系统,是由全球社区爱好者构建的面向日常应用的快速、稳定、强大的操作系统。

哪吒是一个基于全志 D1的单板计算机(SBC). 此页用于集中所有关于Fedora on Nezha的信息.

目录
1      在哪吒上启动Fedora的最简说明
1.1      预编译的镜像文件
1.1.1      解压镜像文件
1.1.2      写入一个TF卡 (必须大于或等于16GB)
1.1.3      插入TF卡并上电
1.1.4      可能出现的问题
1.1.4.1      内核无法访挂载rootfs
1.1.4.2      修复SD卡中的启动固件
1.1.4.3      桌面自动息屏
1.1.5      最新的测试镜像
2      硬件信息
2.1      IP 信息
2.2      Soc 和开发板信息
3      软件
3.1      参考源码
3.1.1      最初源码来源
3.1.2      Smaeul的仓库
3.2      Tekkaman用于Fedora Image的仓库
3.2.1      固件构建顺序
3.2.2      SPL(Boot0)
3.2.2.1      编译
3.2.2.2      导入SD卡
3.2.3      OpenSBI
3.2.3.1      编译
3.2.4      U-boot
3.2.4.1      编译
3.2.5      创建 u-boot.toc1
3.2.6      导入 u-boot.toc1 到 SD 卡中
3.2.7      GRUB for RISC-V
3.2.8      针对哪吒 D1 的 Linux 内核 (持续开发中)
3.2.8.1      重点参考
3.2.8.2      Fedora 内核 正在开发中
3.2.8.3      为 D1 编译内核
3.2.8.3.1      导出源码
3.2.8.3.2      配置
3.2.8.3.3      (交叉)编译 并将其安装到合适的位置
3.3      如何为 D1 编译 RPM 包
3.3.1      借助 QEMU 使用 rpmbuild 命令编译
3.3.2      使用 mock 和 Qemu 用户空间模式(usermode)
3.4      如何创建你自己的 Fedora Remix Image
3.5      如何将 D1 作为 Koji Builder

目前在D1-H哪吒开发板上已经支持了Fedora系统。

原文WiKi链接:在哪吒上启动Fedora的最简说明

一、在哪吒上启动Fedora的最简说明

1.1 预编译的镜像文件

我们已经为您准备好一个可以在全志 D1-H上直接运行的预编译Fedora镜像文件: Fedora-riscv64-d1-developer-xfce-with-esp-Rawhide-latest-sda.raw.zst
您只需要做以下操作:

1.1.1解压镜像文件
unzstdFedora-riscv64-d1-developer-xfce-with-esp-Rawhide-latest-sda.raw.zst1.1.2写入一个TF卡 (必须大于或等于16GB)
sudo wipefs -a /dev/sdX
sudo dd if=Fedora-riscv64-d1-developer-xfce-with-esp-Rawhide-latest-sda.raw of=/dev/sdX status=progress bs=4M请在烧录前一定要使用wipefs清理备份的GPT分区表,否则会出现这种问题:Architectures/RISC-V/Allwinner/zh-cn#内核无法访挂载rootfs

对于dd命令,有一个GUI工具:balena-etcher-electron,简单好用,但wipefs依然是必须在之前执行,切记!

1.1.3插入TF卡并上电

[*]默认是在 LCD (竖屏)上显示:
Allwinner D1 LCD

[*](可选) 转换到HDMI显示器上 (仅支持 1080p 模式竖屏)
cd /sys/kernel/debug/dispdbg
echo disp0 > name; echo switch1 > command; echo 4 10 0 0 0x4 0x101 0 0 0 8 > param; echo 1 > start;
1.1.4可能出现的问题

1.1.4.1内核无法访挂载ROOTFS

如果你出现以下启动失败的问题:
[    9.015501] md: Waiting for all devices to be available before autodetect
[    9.039910] md: If you don't use raid, use raid=noautodetect
[    9.064235] md: Autodetecting RAID arrays.
[    9.085291] md: autorun ...
[    9.104713] md: ... autorun DONE.
[    9.126379] List of all partitions:
[    9.146691] b300      15558144 mmcblk0
[    9.146697]driver: mmcblk
[    9.186490]   b301         1048576 mmcblk0p1 1676bb7b-c349-4f5b-a48a-0f77d0cb680b
[    9.186495]
[    9.228663]   b302            32 mmcblk0p2 de35d1f7-9081-4f33-8952-3bc51a4b10c6
[    9.228668]
[    9.270932]   b303         16384 mmcblk0p3 47aae416-9ac3-47dc-a4f4-8023251eaefc
[    9.270937]
[    9.313210]   b304      14491119 mmcblk0p4 b93e5544-034f-41b7-b64d-1d7c2a5cfe58
[    9.313214]
[    9.355206] No filesystem could mount root, tried:
[    9.355212]ext4
[    9.376478]
[    9.411420] Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(179,4)
[    9.436391] CPU: 0 PID: 1 Comm: swapper Not tainted 5.4.61 #3
[    9.458151] Call Trace:
[    9.475929] [<ffffffe0000d6598>] walk_stackframe+0x0/0xc4
[    9.496999] [<ffffffe0000d6838>] show_stack+0x3c/0x46
[    9.517441] [<ffffffe000bb19f6>] dump_stack+0x24/0x2c
[    9.537749] [<ffffffe0000e1f0a>] panic+0x100/0x32a
[    9.557557] [<ffffffe000001522>] 0xffffffe000001522
[    9.577224] [<ffffffe0000015e2>] 0xffffffe0000015e2
[    9.596711] [<ffffffe00000173c>] 0xffffffe00000173c
[    9.616044] [<ffffffe00000105e>] 0xffffffe00000105e
[    9.635113] [<ffffffe000bc6e5a>] kernel_init+0x1c/0x100
[    9.654494] [<ffffffe0000d4838>] ret_from_exception+0x0/0xc
[    9.674194] ---[ end Kernel panic - not syncing: VFS: Unable to mount root fs on unknown-block(179,4) ]---说明您使用的SD卡存在GPT分区表残留,导致内核不知道如何找到rootfs。(似乎您忘记了在烧写前使用wipefs清理GPT备份分区表) 解决的办法如下:

[*]将出问题的SD卡插入PC,通过gdisk清除残余的分区表,过程如下:
$ sudo gdisk/dev/sdf
GPT fdisk (gdisk) version 1.0.5

Caution: invalid main GPT header, but valid backup; regenerating main header
from backup!

Warning: Invalid CRC on main header data; loaded backup partition table.
Warning! Main and backup partition tables differ! Use the 'c' and 'e' options
on the recovery & transformation menu to examine the two tables.

Warning! Main partition table CRC mismatch! Loaded backup partition table
instead of main partition table!

Warning! One or more CRCs don't match. You should repair the disk!
Main header: ERROR
Backup header: OK
Main partition table: ERROR
Backup partition table: OK

Partition table scan:
MBR: MBR only
BSD: not present
APM: not present
GPT: damaged

Found valid MBR and corrupt GPT. Which do you want to use? (Using the
GPT MAY permit recovery of GPT data.)
1 - MBR
2 - GPT
3 - Create blank GPT

Your answer: 1

Command (? for help): p
Disk /dev/sdf: 62333952 sectors, 29.7 GiB
Model: Multi-Reader-3
Sector size (logical/physical): 512/512 bytes
Disk identifier (GUID): 632BFA1B-F09D-42A8-82F4-5FAB32E41DC2
Partition table holds up to 128 entries
Main partition table begins at sector 2 and ends at sector 33
First usable sector is 34, last usable sector is 62333918
Partitions will be aligned on 2048-sector boundaries
Total free space is 37084093 sectors (17.7 GiB)

NumberStart (sector)    End (sector)Size       CodeName
   2         69632          319487   122.0 MiB   0700Microsoft basic data
   3          319488         1320959   489.0 MiB   8300Linux filesystem
   4         1320960      25319423   11.4 GiB    8300Linux filesystem

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT) to /dev/sdf.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.如果此过程中导致您的 boot0_sdcard_sun20iw1p1.bin 或者 boot_package.fex 受损,请通过以下预编译固件修复

1.1.4.2修复SD卡中的启动固件

[*]针对5.4内核
* boot0_sdcard_sun20iw1p1.bin
* boot_package.fex
sudo dd if=boot_package.fex of=/dev/sdf seek=32800 bs=512
sudo dd if=boot0_sdcard_sun20iw1p1.bin of=/dev/sdf seek=16 bs=512
[*]建议:先刷boot_package.fex,再刷boot0_sdcard_sun20iw1p1.bin

1.1.4.3桌面自动息屏

XFCE桌面有自动节能息屏功能,如果需要禁用息屏,操作如下:

[*]登录桌面后,菜单,applications-> settings-> XScreensaver里选disable

[*]登录桌面后,在桌面中运行一个terminal,在其中使用 root 权限运行命令:sudo xset s 0
Set s 1.1.5最新的测试镜像最新的测试镜像包含了主线内核(包含额外的补丁): fedora-riscv64-d1-developer-xfce-rawhide-Rawhide-20211124-132603.n.0-sda.raw.zst

[*]如何通过GRUB启动最新的内核: 使用 "v" 停止 u-boot 的自动启动,然后输入
run boot_grub
二、硬件信息

2.1IP 信息

平头哥: 玄铁 C906 RISC-V

2.2Soc 和开发板信息
全志: D1-H SoC 介绍(英文版)
Sunxi: D1-H芯片
Sunxi: 全志哪吒开发板

三、软件
3.1参考源码

3.1.1最初源码来源

[*]全志: 全志客户服务平台
为了获取源码,您需要注册并登录.

[*]备选下载地址: 位于 SUNXI 网站上的 D1-H 资源

3.1.2Smaeul的仓库

[*]SPL/BOOT0
[*]OpenSBI
[*]U-Boot
[*]Linux内核

3.2Tekkaman用于Fedora Image的仓库

3.2.1固件构建顺序
3.2.2SPL(Boot0)

`boot0 或者 SPL (二级程序加载器) 是 BootROM 之后的第一个被执行的程序, 负责初始化内存控制器和加载下一级固件(来自于固态存储,闪存或者SD卡)。

使用可直接使用 Smaeul的SPL,也可以使用 Tekkaman 的fork

3.2.2.1编译
git clone https://github.com/tekkamanninja/sun20i_d1_spl
pushd sun20i_d1_spl
git checkout origin/mainline
make CROSS_COMPILE=riscv64-linux-gnu- p=sun20iw1p1 mmc
popd3.2.2.2导入SD卡
pushd sun20i_d1_spl
sudo dd if=nboot/boot0_sdcard_sun20iw1p1.bin of=/dev/sdX bs=512 seek=163.2.3OpenSBI
RISC-V 开源管理者模式(Supervisor)二进制接口, 类似 arm-TF本文主要使用 Smaeul 的补丁, 但是会 rebase 到最新的主线上游 OpenSBI, 并修复可能出现的小问题。

Tekkaman 的 OpenSBI 仓库
3.2.3.1编译
git clone https://github.com/tekkamanninja/opensbi -b allwinner_d1
pushd opensbi
CROSS_COMPILE=riscv64-linux-gnu- PLATFORM=generic FW_PIC=y make
popd最终结果中的 build/platform/generic/firmware/fw_dynamic.bin 将在下面被放入 u-boot.toc1 。

3.2.4U-boot

本文主要使用 Smaeul 的补丁, 但是会 rebase 到最新的主线上游 U-boot, 并使能更多的特性并添加一些运行时的默认环境变量来自动载入 GRUB。

Tekkaman 的 U-boot 仓库
3.2.4.1编译
git clone https://github.com/tekkamanninja/u-boot -b allwinner_d1
pushd u-boot
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv nezha_defconfig
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv menuconfig
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv u-boot.bin u-boot.dtb
popd最终生成的u-boot.bin 的 u-boot.dtb 将在下面被放入 u-boot.toc1 中.

3.2.5创建 u-boot.toc1


[*]下面需要使用的配置文件 toc1.cfg 如下:

file = fw_dynamic.bin
addr = 0x40000000

file = u-boot.dtb
addr = 0x44000000

file = u-boot.bin
addr = 0x4a000000
[*]构建命令:
首先拷贝配置文件和opensbi,连同本目录中的u-boot.dtb以及u-boot.bin一起生成 u-boot.toc1。
pushd u-boot
cp ${PATH_TO_TOC1_CFG}/toc1.cfg ${PATH_TO_OPENSBI}/fw_dynamic.bin .
tools/mkimage -T sunxi_toc1 -d toc1.cfgu-boot.toc1
popd3.2.6导入 u-boot.toc1 到 SD 卡中
pushd u-boot
sudo dd if=u-boot.toc1 of=/dev/sdX bs=512 seek=328003.2.7GRUB for RISC-V
请仓库这个页面来编译GRUB2 for RISCV: Architectures/RISC-V/GRUB2

你可以通过上面的页面编译出一个 grubriscv64.efi 。然后将其放入SD卡EFI分区中的EFI/目录。.
一些位于boot分区中范例文件:

/boot/uEnv.txt 目的在于让u-boot自动执行加载GRUB2并跳入执行的步骤。
bootcmd=load mmc 0:2 0x58000000 /EFI/fedora/grubriscv64.efi; bootefi 0x58000000
bootcmd_mmc0=devnum=0; run mmc_boot/grub.cfg GRUB的启动配置文件,和普通PC机类似。
set default=0
set timeout_style=menu
set timeout=3

set debug="linux,loader,mm"
set term="vt100"

menuentry 'Fedora vmlinux-5.15.0-rc1' {
      linux /vmlinuz-5.15.0-rc1 earlyprintk=sunxi-uart,0x02500000 console=ttyS0,115200 console=tty0 loglevel=8root=/dev/mmcblk0p4 selinux=0 rhgb LANG=en_US.UTF-8
      devicetree /sun20i-d1-nezha.dtb
      initrd /initramfs-5.15.0-rc1.img
}
3.2.8针对哪吒 D1-H 的 Linux 内核 (持续开发中)
本项目使用了来自Smaeul/郭任(平头哥)/全志软件工程师的补丁,使能Fedora需要的了尽可能多的特性,并可能让主线可能 to the latest upstream kernel master branch.

3.2.8.1重点参考

[*]来自平头哥的 Linux 内核源码
[*]全志软件工程师的仓库

3.2.8.2FEDORA 内核 正在开发中

Tekkaman's Linux 内核仓库
注意: 此内核还在开发中,目标是尽量和主线 Fedora 的配置匹配,以方便构建RPM包以及和Fedora文件系统一起工作

3.2.8.3为 D1-H 编译内核

3.2.8.3.1导出源码
git clone https://github.com/tekkamanninja/linux.git
pushd linux
git checkout origin/allwinner_nezha_d1_devel
popd
[*]allwinner_nezha_d1_devel:主要开发分支,经过验证可用,默认 Fedora defconfig 为 nezha_fedora_defconfig
[*]allwinner_nezha_d1_devel_5.*.0-rc?: 特定内核版本测试分支,开发中...

3.2.8.3.2配置
pushd linux
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv nezha_fedora_defconfig
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv menuconfig
popd3.2.8.3.3(交叉)编译 并将其安装到合适的位置
# RISCV_ROOTFS_BOOT 是 SD 卡中 boot 分区的挂载点
# RISCV_ROOTFS_BOOT=/run/media/../boot
# RISCV_ROOTFS是 SD 卡中 Fedora rootfs 分区的挂载点
# RISCV_ROOTFS=/run/media/../rootfs
pushd linux
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv INSTALL_PATH=${RISCV_ROOTFS_BOOT} zinstall
make CROSS_COMPILE=riscv64-linux-gnu- ARCH=riscv INSTALL_MOD_PATH=${RISCV_ROOTFS} modules_install
popd
3.3如何为 D1-H 编译 RPM 包

3.3.1借助 QEMU 使用 rpmbuild 命令编译
如何使用RISC-V QEMU 启动 Fedora 请参考Architectures/RISC-V/Build Bootable Image#Run_Fedora_on_QEMU


[*]在RISC-V QEMU 上启动 Fedora:

[*]准备目录树: 将 spec file 放入 /rpmbuild/SPECS 并将所有的源码文件放入 /rpmbuild/SOURCES
[*]然后通过 rpmbuild 编译RPM包:
#From spec file
$ rpmbuild -ba ~/rpmbuild/SPECS/pakcage.spec
[*]当然,您也可以通过现有的SRPM编译RPM包:
#'''OR''' to build from an existing SRPM file:
$ rpmbuild -ra package.src.rpm使用SPEC文件的方法比较适合开发调试阶段的代码和RPM包,使用现有SRPM包的方法比较适合单纯的RPM包重新编译。
更多RPM相关的知识 请参考如何创建 GNU 的 Hello World RPM 包

3.3.2使用 mock 和 Qemu 用户空间模式(usermode)

我们可以在 x86_64 系统上借助 mock 和 Qemu 用户空间模式(usermode) 编译 RISC-V 的 RPMS 包 。[正在总结整理中]

3.4如何创建你自己的 Fedora Remix Image

想要创建自己的 Fedora 镜像? 请参考 RISC-V: Build Bootable Image

3.5如何将 D1-H 作为 Koji Builder

如果我们已经部署了一个 koji hub(例如 openkoji 和 oepkg),然后我们添加 Nezha D1 作为新的 koji builder.

效果如以下页面所示,我们已经尝试添加了Nezha D1-H 作为 koji builder,并测试了编包:

[*]oepkgs hosts
[*]example build on D1-H builder in oepkgs

您可按照以下简单的流程:

[*]前提条件: koji CA 证书和 builder 认证证书,必须由 koji hub 的管理员提供。
[*]安装必要的软件包
# dnf install koji-builder
[*]调整 /etc/kojid.conf 中关键的配置
server=<koji hub XMLRPC server URL>
topurl=<koji HTT file share URL>
allowed_scms=<SCM repo rules>
cert=<builder certificate>
serverca=<koji CA certificate>
[*]信任 koji hub 的 CA 证书
cp koji_ca_cert.crt /etc/pki/ca-trust/source/anchors/
update-ca-trust
[*]在 koji hub 中添加 Builder,必须由 koji hub 的管理员操作。
koji add-host <hostname> riscv64
# *hostname* 必须和证书中的一致
[*]使能并启动 kojid
systemctl enable --now kojid
[*]验证 koji builder 是否在线,必须由 koji hub 的管理员操作:
koji list-hosts
# 初始化后: 新添加的 builder状态在列表中应该是 ready(就绪/待命) .注意:* builder 需要最少 8GiB 的空闲磁盘空间在 /var/lib/mock 中,否则不会进入 ready(就绪/待命)状态。且不支持将 NFS 网络存储挂载到 /var/lib/mock 中。 所以在 builder 中使用高容量 SD 卡,并 resize 跟文件系统到整个SD卡的空闲空间是必要条件。
(本文更新于20210910,原文wiki不断更新中,最新动态以原文wiki更新内容为准)


页: [1]
查看完整版本: 哪吒系列文章之15——Fedora支持