Allow riscv64 image to run without binfmt_misc

This commit is contained in:
Vidar Holen
2024-06-11 06:00:22 +00:00
parent 15de97e33f
commit 23e76de4f2
3 changed files with 126 additions and 21 deletions

View File

@@ -1,4 +1,4 @@
FROM ubuntu:22.04
FROM ubuntu:24.04
ENV TARGETNAME linux.riscv64
ENV TARGET riscv64-linux-gnu
@@ -7,39 +7,47 @@ USER root
ENV DEBIAN_FRONTEND noninteractive
# Init base
RUN apt update -y
RUN apt-get update -y
# Install qemu
RUN apt install -y --no-install-recommends build-essential ninja-build python3 pkg-config libglib2.0-dev libpixman-1-dev curl ca-certificates python3-virtualenv
RUN apt-get install -y --no-install-recommends build-essential ninja-build python3 pkg-config libglib2.0-dev libpixman-1-dev curl ca-certificates python3-virtualenv git python3-setuptools
WORKDIR /qemu
RUN curl -Lv "https://download.qemu.org/qemu-9.0.0.tar.xz" | tar xJ --strip-components=1
RUN ./configure --target-list=riscv64-linux-user --static --disable-system --disable-pie
RUN git clone --depth 1 https://github.com/koalaman/qemu .
#RUN git clone https://github.com/balena-io/qemu .
# Release 7.0.0
#RUN git checkout 639d1d8903f65d74eb04c49e0df7a4b2f014cd86
RUN ./configure --target-list=riscv64-linux-user --static --disable-system --disable-pie --disable-werror
RUN cd build && ninja qemu-riscv64
ENV QEMU_EXECVE 1
# Set up a riscv64 userspace
RUN apt install -y --no-install-recommends debootstrap
RUN debootstrap --arch=riscv64 --foreign jammy /rvfs http://ports.ubuntu.com/ubuntu-ports
RUN apt-get install -y --no-install-recommends debootstrap
RUN debootstrap --arch=riscv64 --foreign noble /rvfs http://ports.ubuntu.com/ubuntu-ports
RUN cp /qemu/build/qemu-riscv64 /rvfs/usr/bin/qemu-riscv64-static
RUN printf > /bin/rv '%s\n' '#!/bin/sh' 'chroot /rvfs /usr/bin/qemu-riscv64-static /usr/bin/env "$@"'
# Command to run riscv binaries in the chroot. The Haskell runtime allocates 1TB
# vspace up front and QEmu has a RAM cost per vspace, so use ulimit to allocate
# less and reduce RAM usage.
RUN printf > /bin/rv '%s\n' '#!/bin/sh' 'ulimit -v $((10*1024*1024)); chroot /rvfs /usr/bin/qemu-riscv64-static /usr/bin/env "$@"'
RUN chmod +x /bin/rv
RUN [ ! -e /rvfs/debootstrap ] || rv '/debootstrap/debootstrap' --second-stage
# Install deps in the chroot
RUN printf > /rvfs/etc/apt/sources.list '%s\n' 'deb http://ports.ubuntu.com/ubuntu-ports jammy main universe'
RUN rv apt update -y
RUN rv apt install -y --no-install-recommends ghc cabal-install
RUN printf > /rvfs/etc/apt/sources.list '%s\n' 'deb http://ports.ubuntu.com/ubuntu-ports noble main universe'
RUN rv apt-get update -y
RUN rv apt-get install -y --no-install-recommends ghc cabal-install
# Finally we can build the current dependencies. This takes hours.
# jobs must be 1, GHS riscv will use about 40G memory
RUN rv cabal update
RUN IFS=";" && rv cabal install --dependencies-only --jobs=1 ShellCheck
RUN IFS=';' && rv cabal install --lib --jobs=1 fgl
# Clean up
RUN rm -rf /qemu
# Generated with: cabal freeze -c 'hashable -arch-native'. We put it in /etc so cabal won't find it.
COPY cabal.project.freeze /rvfs/etc
# Awful hack to install everything from the freeze file
# This basically turns 'any.tagged ==0.8.8' into tagged-0.8.8 to install by version,
# and adds a -c before 'hashable -arch-native +integer-gmp' to make it a flag constraint.
RUN < /rvfs/etc/cabal.project.freeze sed 's/constraints:/&\n /' | grep -vw rts | sed -n -e 's/^ *\([^,]*\).*/\1/p' | sed -e 's/any\.\([^ ]*\) ==\(.*\)/\1-\2/; te; s/.*/-c\n&/; :e' > /tmp/preinstall-flags
# Finally we can build the current dependencies. This takes hours.
# There's apparently a random segfault during assembly, so retry a few times.
RUN set -x; IFS=${IFS# }; f() { rv cabal install --keep-going $(cat /tmp/preinstall-flags); }; for i in $(seq 5); do f; ret=$?; [ $ret = 0 ] && break; done; exit $ret
# Copy the build script
WORKDIR /rvfs/scratch