Skip to content

Commit

Permalink
Dockerfile.riscv64: Refactoring with shell scripts
Browse files Browse the repository at this point in the history
To avoid potential space limitation on the git runners
due to running too many separate RUN commands.

Signed-off-by: Tan En De <[email protected]>
  • Loading branch information
endeneer committed Dec 19, 2023
1 parent b6b2b91 commit 39bbed4
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 79 deletions.
94 changes: 15 additions & 79 deletions Dockerfile.riscv64
Original file line number Diff line number Diff line change
@@ -1,73 +1,20 @@
#==============================
# Binaries Builder
# Builder Stage
# Cross-compile QEMU, OpenSBI, Linux
#==============================
FROM ubuntu:22.04 AS builder
ARG ARCH=riscv
ARG CROSS_COMPILE=riscv64-linux-gnu-

#--------------------
# Version tag
#--------------------
ARG QEMU_TAG=v8.1.2
ARG OPENSBI_TAG=v1.3.1
ARG LINUX_TAG=v6.5

#--------------------
# Directory paths
#--------------------
## Directory path where git clones repos to
ARG DIR_PREFIX=/opt/build
## Build output path (to store output of make)
ARG OUTPUT_DIR=/opt/bin
RUN mkdir -p $OUTPUT_DIR
## Build install path (to store output of make install)
ARG INSTALL_DIR=/opt/install

## Repo paths
ARG QEMU_DIR=$DIR_PREFIX/qemu
ARG OPENSBI_DIR=$DIR_PREFIX/opensbi
ARG LINUX_DIR=$DIR_PREFIX/linux

#--------------------
# Prerequisites
#--------------------
RUN apt update
RUN apt -y install git python3 python3-pip build-essential pkg-config libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build gcc-riscv64-linux-gnu libssl-dev wget curl bc flex bison libslirp-dev

WORKDIR $DIR_PREFIX
RUN git clone --depth 1 --branch $QEMU_TAG https://gitlab.com/qemu-project/qemu.git
RUN git clone --depth 1 --branch $OPENSBI_TAG https://github.com/riscv-software-src/opensbi.git
RUN git clone --depth 1 --branch $LINUX_TAG https://github.com/torvalds/linux.git

#--------------------
# QEMU
#--------------------
WORKDIR $QEMU_DIR
RUN ./configure --target-list="riscv64-softmmu" --enable-slirp --prefix=$INSTALL_DIR/usr/local && \
make -j$(nproc) && \
make install

#--------------------
# OpenSBI
#--------------------
WORKDIR $OPENSBI_DIR
RUN make -j$(nproc) PLATFORM=generic
RUN mv $OPENSBI_DIR/build/platform/generic/firmware/fw_jump.elf $OUTPUT_DIR

#--------------------
# Linux
#--------------------
WORKDIR $LINUX_DIR
RUN sed -i "s|^CONFIG_KVM=.*|CONFIG_KVM=y|g" $LINUX_DIR/arch/riscv/configs/defconfig
RUN make defconfig && make -j$(nproc)
RUN mv $LINUX_DIR/arch/riscv/boot/Image $OUTPUT_DIR
COPY build_container_riscv64.builder.sh /opt/src/scripts/build_container_riscv64.builder.sh
RUN /opt/src/scripts/build_container_riscv64.builder.sh

#==============================
# Root Filesystem
# Rootfs Stage
# Generate guest rootfs
#==============================
FROM riscv64/ubuntu:22.04 AS rootfs_builder
RUN apt update && apt upgrade -y
RUN apt install -y systemd init ifupdown busybox udev
FROM --platform=linux/riscv64 riscv64/ubuntu:22.04 AS rootfs_builder

COPY build_container_riscv64.rootfs.sh /opt/src/scripts/build_container_riscv64.rootfs.sh
RUN /opt/src/scripts/build_container_riscv64.rootfs.sh

## Install test service that run CI test on systemd boot
COPY riscv64/test.service /etc/systemd/system/
Expand All @@ -83,29 +30,15 @@ COPY riscv64/resolv.conf /etc/
#==============================
FROM ubuntu:22.04 AS final

#--------------------
# Version tag
#--------------------
ARG RUST_TOOLCHAIN="1.72.0"
COPY build_container_riscv64.final.sh /opt/src/scripts/build_container_riscv64.final.sh
RUN /opt/src/scripts/build_container_riscv64.final.sh

#--------------------
# Directories to be installed from builder and rootfs_builder
#--------------------
ARG OUTPUT_DIR=/opt/bin
ARG INSTALL_DIR=/opt/install
ARG ROOTFS_DIR=/rootfs
ENV PATH="$PATH:/root/.cargo/bin:$INSTALL_DIR/usr/local/bin"

#--------------------
# Prerequisites
#--------------------
RUN apt update
RUN apt install -y curl libglib2.0-dev libfdt-dev libpixman-1-dev libslirp-dev gcc gcc-riscv64-linux-gnu

RUN curl https://sh.rustup.rs -sSf | sh -s -- \
-y --default-toolchain "$RUST_TOOLCHAIN" \
--profile minimal --component clippy,rustfmt
RUN rustup target add riscv64gc-unknown-linux-gnu

#--------------------
# Combining all stages
Expand All @@ -116,3 +49,6 @@ COPY --from=rootfs_builder / $ROOTFS_DIR

## Install script for starting QEMU RISC-V guest
COPY riscv64/qemu.sh /bin/

## rust path and qemu path
ENV PATH="$PATH:/root/.cargo/bin:$INSTALL_DIR/usr/local/bin"
72 changes: 72 additions & 0 deletions build_container_riscv64.builder.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env bash
set -ex

export ARCH=riscv
export CROSS_COMPILE=riscv64-linux-gnu-

#--------------------
# Version tags
#--------------------
QEMU_TAG=v8.1.2
OPENSBI_TAG=v1.3.1
LINUX_TAG=v6.5

#--------------------
# Directory paths
#--------------------
## Directory path where git clones repos to
DIR_PREFIX=/opt/build
mkdir -p $DIR_PREFIX
## Build output path (to store output of make)
OUTPUT_DIR=/opt/bin
mkdir -p $OUTPUT_DIR
## Build install path (to store output of make install)
INSTALL_DIR=/opt/install

## Repo paths
QEMU_DIR=$DIR_PREFIX/qemu
OPENSBI_DIR=$DIR_PREFIX/opensbi
LINUX_DIR=$DIR_PREFIX/linux

#--------------------
# Prerequisites
#--------------------
PACKAGE_LIST="git python3 python3-pip build-essential pkg-config libglib2.0-dev libfdt-dev libpixman-1-dev zlib1g-dev ninja-build gcc-riscv64-linux-gnu libssl-dev wget curl bc flex bison libslirp-dev"
apt-get update
apt-get -y install $PACKAGE_LIST

cd $DIR_PREFIX
git clone --depth 1 --branch $QEMU_TAG https://gitlab.com/qemu-project/qemu.git
git clone --depth 1 --branch $OPENSBI_TAG https://github.com/riscv-software-src/opensbi.git
git clone --depth 1 --branch $LINUX_TAG https://github.com/torvalds/linux.git

#--------------------
# QEMU
#--------------------
cd $QEMU_DIR
./configure --target-list="riscv64-softmmu" --enable-slirp --prefix=$INSTALL_DIR/usr/local && \
make -j$(nproc) && \
make install

#--------------------
# OpenSBI
#--------------------
cd $OPENSBI_DIR
make -j$(nproc) PLATFORM=generic
mv $OPENSBI_DIR/build/platform/generic/firmware/fw_jump.elf $OUTPUT_DIR

#--------------------
# Linux
#--------------------
cd $LINUX_DIR
sed -i "s|^CONFIG_KVM=.*|CONFIG_KVM=y|g" $LINUX_DIR/arch/riscv/configs/defconfig
make defconfig && make -j$(nproc)
mv $LINUX_DIR/arch/riscv/boot/Image $OUTPUT_DIR

#--------------------
# Cleanup
#--------------------
rm -rf $QEMU_DIR
rm -rf $OPENSBI_DIR
rm -rf $LINUX_DIR
apt-get -y remove $PACKAGE_LIST
19 changes: 19 additions & 0 deletions build_container_riscv64.final.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
set -ex

#--------------------
# Version tag
#--------------------
RUST_TOOLCHAIN="1.72.0"
export PATH="$PATH:/root/.cargo/bin"

#--------------------
# Prerequisites
#--------------------
apt-get update
apt-get -y install curl libglib2.0-dev libfdt-dev libpixman-1-dev libslirp-dev gcc gcc-riscv64-linux-gnu

curl https://sh.rustup.rs -sSf | sh -s -- \
-y --default-toolchain "$RUST_TOOLCHAIN" \
--profile minimal --component clippy,rustfmt
rustup target add riscv64gc-unknown-linux-gnu
6 changes: 6 additions & 0 deletions build_container_riscv64.rootfs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env bash
set -ex

apt-get update
apt-get -y upgrade
apt-get -y install systemd init ifupdown busybox udev

0 comments on commit 39bbed4

Please sign in to comment.