flatcar / Flatcar

Flatcar project repository for issue tracking, project documentation, etc.
https://www.flatcar.org/
Apache License 2.0
695 stars 30 forks source link

[RFE] Add RISC-V support for Flatcar Container Linux #1420

Open ader1990 opened 6 months ago

ader1990 commented 6 months ago

Current situation

Flatcar cannot be built for the RISC-V architecture (https://riscv.org).

Impact

Consumer boards are starting to become widely available, like the Vision 5, LicheePi 4A or Milk-V. Enterprise grade manufacturers like Ventana are also providing server grade RISC-V CPUs. Also, cloud providers like Scaleway already offer RISC-V instances.

Ideal future situation

To be able to run Flatcar on RISC-V systems.

Implementation options

Upstream Gentoo already has support for most of the packages. The SDK needs to be worked on and maybe also streamline / organize the build code for ARM64 and RISC-V at the same time, so that ARM-on-ARM and RISC-on-RISC builds can be possible, without forcing cross-compilation.

ader1990 commented 2 months ago

I managed to get a Gentoo RISC-V built, here is the documentation step by step on how to build the image, maybe someone finds it useful.

Need to test it out on my RISC-V board and then see how it can be applied to Flatcar SDK workflow, work in progress. Readme: https://github.com/ader1990/configs/blob/master/gentoo/Readme-RISCV.md

Initial document how-to, the starting point

https://wiki.gentoo.org/wiki/User:Dlan/RISC-V/TH1520#Host_Env_Setup

Install Gentoo upstream on an AMD64 box was done using the script from this folder

prepare the env - install the toolings

emerge dev-vcs/git
emerge app-misc/screen

prepare the crossdev repos (instructions from @chewie)

mkdir -p /var/db/repos/crossdev/{metadata,profiles}
echo crossdev > /var/db/repos/crossdev/profiles/repo_name
cat > /var/db/repos/crossdev/metadata/layout.conf <<EOF
masters = gentoo
thin-manifests = true
cache-formats = md5-dict
EOF
cat > /etc/portage/repos.conf/crossdev.conf <<EOF
[crossdev]
priority = 1001
location = /var/db/repos/crossdev
sync-type =
EOF

prepare the crossdev env

emerge --ask sys-devel/crossdev
crossdev --target riscv64-unknown-linux-gnu

build uboot

git clone --depth=1 -b th1520 https://github.com/revyos/thead-u-boot u-boot
pushd u-boot
alias rvmake='make ARCH=riscv CROSS_COMPILE=riscv64-unknown-linux-gnu- -j30 '
# use the 16g target for the 16GB RAM board
rvmake distclean && rvmake light_lpi4a_16g_defconfig && rvmake
ls -liath u-boot-with-spl.bin
popd

build kernel

git clone --depth=1 -b lpi4a https://github.com/revyos/thead-kernel kernel
pushd kernel
rvmake revyos_defconfig
# to fix the pahole issue
emerge -av dev-util/pahole
rvmake Image
ls -liath ./arch/riscv/boot/Image
rvmake modules
ls -liath modules
rvmake dtbs
ls -liath ./arch/riscv/boot/dts/thead/light-lpi4a-16gb.dtb

git clone https://github.com/revyos/opensbi -b th1520-v1.4
pushd opensbi
rvmake PLATFORM=generic FW_PIC=y
ls build/platform/generic/firmware/fw_dynamic.bin
popd

# get the two closed source light_aon_fpga and light_c906_audio from
# https://git.beagleboard.org/beaglev-ahead/xuantie-ubuntu/-/tree/master/bins
# https://lore.kernel.org/buildroot/20230821234359.06008b73@windsurf/T/
# light_aon_fpga.bin: "aon" is an `Always On` fpga binary used for power
# management... If you don't have this, the board will boot, but things
# like cpufreq don't work..
# light_c906_audio.bin: Probably Audio, never really tested it too much...

# this is how a bootfs should look like
# Image           light-lpi4a-ddr2G.dtb  light_aon_fpga.bin
# fw_dynamic.bin  light-lpi4a.dtb        light_c906_audio.bin
# kernel-release  light-lpi4a_2Ghz.dtb   lost+found
popd

get the stage 3 built image

https://mirror.bytemark.co.uk/gentoo/releases/riscv/autobuilds/current-stage3-rv64_lp64d-systemd/stage3-rv64_lp64d-systemd-20240621T170422Z.tar.xz

ader1990 commented 2 months ago

Got a Gentoo recent version running on the LicheePi4A:

localhost ~ # cat /etc/os-release
NAME=Gentoo
ID=gentoo
PRETTY_NAME="Gentoo Linux"
ANSI_COLOR="1;32"
HOME_URL="https://www.gentoo.org/"
SUPPORT_URL="https://www.gentoo.org/support/"
BUG_REPORT_URL="https://bugs.gentoo.org/"
VERSION_ID="2.15"
localhost ~ # uname -a
Linux localhost 5.10.113-th1520 #2024.05.31.16.27+2dec14431 SMP PREEMPT Fri May 31 16:28:40 UTC  riscv64 GNU/Linux
localhost ~ # systemctl --version
systemd 255 (255)
+PAM -AUDIT -SELINUX -APPARMOR +IMA +SMACK +SECCOMP +GCRYPT -GNUTLS +OPENSSL +ACL +BLKID -CURL -ELFUTILS -FIDO2 +IDN2 -IDN -IPTC +KMOD -LIBCRYPTSETUP +LIBFDISK +PCRE2 -PWQUALITY -P11KIT -QRENCODE -TPM2 -BZIP2 +LZ4 -XZ -ZLIB +ZSTD -BPF_FRAMEWORK -XKBCOMMON +UTMP -SYSVINIT default-hierarchy=unified

Next step is to follow up on @chewi great work and how to's to add a new architecture for Flatcar and get a Flatcar on RISC-V PoC: https://github.com/flatcar/flatcar-website/blob/chewi/new-cpu-architecture/content/docs/latest/reference/developer-guides/new-cpu-architecture.md#porting-flatcar-itself

ader1990 commented 2 months ago

WOIP to get a working SDK for cross-building RISCV64 packages: https://github.com/ader1990/scripts/pull/1/files. I have purposely chosen flatcar-4012 as a proof of concept, as there are alot of current and upcoming patches related to Catalyst, and did not want to have to rebase in case of errors / change of workflow. When this PR gets streamlined and after the Catalyst / repo file renames are done, the change should be somewhat easy to do once than continously having to rebase.

After the SDK has been created, there will be probably some upstream packages that need to be changed to have the ~riscv keyword.

@chewi created this bug report for the selinux* ones: https://bugs.gentoo.org/936297, more to come.

Gentoo sys-libs/libselinux-3.6-r1 needs also a fix to handle the .a removal, as it is not working on RISCV64 lp64d. use static-libs || rm "${D}"/usr/lib*/*.a || die -> https://github.com/flatcar/scripts/blob/main/sdk_container/src/third_party/portage-stable/sys-libs/libselinux/libselinux-3.6-r1.ebuild#L144

chewi commented 2 months ago

Here's the libselinux fix upstream: gentoo/gentoo@7b643dfa2a2d94c26ce7791885ea9651ec95b6a6

ader1990 commented 2 months ago

Plan to add experimental support for RISC-V in Flatcar, in this chronological order to be done:

chewi commented 2 months ago

Something to be aware of. Gentoo is planning to split the riscv keyword into riscv and riscv32. That shouldn't affect this, although there was also a suggestion to use riscv64, and the plan hasn't been finalised yet.

ader1990 commented 2 months ago

Something to be aware of. Gentoo is planning to split the riscv keyword into riscv and riscv32. That shouldn't affect this, although there was also a suggestion to use riscv64, and the plan hasn't been finalised yet.

All over the code bases, there are risc-v, riscv, riscv64, riscv64gc - so many different wordings for the same thing. I hope the industry comes together and decides on just one.

ader1990 commented 1 month ago

Linking https://github.com/flatcar/scripts/pull/2247, as mayday was not compiling on risc-v because of the old golang bindings.