From a8d44c22ad142386c9bf3f1da805469bda4b415d Mon Sep 17 00:00:00 2001 From: Antony Chazapis Date: Wed, 14 Jun 2023 14:43:04 +0300 Subject: [PATCH] Add support for RISC-V Signed-off-by: Antony Chazapis --- README.md | 2 +- cross-compile-riscv.sh | 33 +++++++++++++++++++++++++++++++++ install.sh | 4 ++++ scripts/binary_size_check.sh | 2 ++ scripts/build | 8 ++++++++ scripts/download | 6 +++++- scripts/image_scan.sh | 4 ++-- scripts/package-cli | 3 +++ scripts/test-helpers | 2 +- 9 files changed, 59 insertions(+), 5 deletions(-) create mode 100755 cross-compile-riscv.sh diff --git a/README.md b/README.md index 7fa2e63582e1..c88d841b85fe 100644 --- a/README.md +++ b/README.md @@ -139,7 +139,7 @@ curl -sfL https://get.k3s.io | K3S_URL=https://myserver:6443 K3S_TOKEN=XXX sh - Manual Download --------------- -1. Download `k3s` from latest [release](https://github.com/k3s-io/k3s/releases/latest), x86_64, armhf, arm64 and s390x are supported. +1. Download `k3s` from latest [release](https://github.com/k3s-io/k3s/releases/latest), x86_64, armhf, arm64, s390x and riscv64 are supported. 1. Run the server. ```bash diff --git a/cross-compile-riscv.sh b/cross-compile-riscv.sh new file mode 100755 index 000000000000..d4bd5eaa529e --- /dev/null +++ b/cross-compile-riscv.sh @@ -0,0 +1,33 @@ +#/bin/bash + +# Cross-compile for RISC-V in an Ubuntu 22.04 container + +# To run mount a clean checkout of the code inside the container and run the script: +# +# docker run --rm --mount type=bind,source=${PWD},target=/k3s ubuntu:22.04 /bin/bash -c /k3s/cross-compile-riscv.sh + +dpkg --add-architecture riscv64 +apt-get update +apt-get install -y wget curl git gcc-riscv64-linux-gnu g++-riscv64-linux-gnu pkg-config libseccomp-dev:riscv64 make zstd + +HOST_ARCH=$(uname -m) +if [ ${HOST_ARCH} = aarch64 ]; then + HOST_ARCH=arm64 +fi + +wget -P /tmp "https://dl.google.com/go/go1.20.5.linux-${HOST_ARCH}.tar.gz" +(cd /; tar -C /usr/local -xzf "/tmp/go1.20.5.linux-${HOST_ARCH}.tar.gz") +rm -rf /tmp/go1.20.5.linux-${HOST_ARCH}.tar.gz +mkdir -p /go/src /go/bin + +export GOPATH=/go +export PATH=$GOPATH/bin:/usr/local/go/bin:$PATH + +wget -qO /usr/local/bin/yq https://github.com/mikefarah/yq/releases/latest/download/${HOST_ARCH} +chmod +x /usr/local/bin/yq + +cd /k3s +export ARCH=riscv64 +bash -x ./scripts/download +bash -x ./scripts/build +bash -x ./scripts/package-cli diff --git a/install.sh b/install.sh index ae917becaba0..b5b1789f8f2c 100755 --- a/install.sh +++ b/install.sh @@ -318,6 +318,10 @@ setup_verify_arch() { ARCH=arm SUFFIX=-${ARCH}hf ;; + riscv64) + ARCH=riscv64 + SUFFIX=-${ARCH} + ;; *) fatal "Unsupported architecture $ARCH" esac diff --git a/scripts/binary_size_check.sh b/scripts/binary_size_check.sh index 741a9f954847..576e22d387d8 100755 --- a/scripts/binary_size_check.sh +++ b/scripts/binary_size_check.sh @@ -19,6 +19,8 @@ elif [ ${ARCH} = arm ]; then BIN_SUFFIX="-armhf" elif [ ${ARCH} = s390x ]; then BIN_SUFFIX="-s390x" +elif [ ${ARCH} = riscv64 ]; then + BIN_SUFFIX="-riscv64" fi CMD_NAME="dist/artifacts/k3s${BIN_SUFFIX}" diff --git a/scripts/build b/scripts/build index 9acc17a2a200..31b8041e7d12 100755 --- a/scripts/build +++ b/scripts/build @@ -98,6 +98,14 @@ if [ ${ARCH} = s390x ]; then export GOARCH="s390x" fi +if [ ${ARCH} = riscv64 ]; then + export GOARCH="riscv64" + # These are needed as we are cross-compiling + export CC=riscv64-linux-gnu-gcc + export PKG_CONFIG_PATH=/usr/lib/riscv64-linux-gnu/pkgconfig/ + export CGO_ENABLED=1 +fi + rm -f \ bin/k3s-agent \ bin/k3s-server \ diff --git a/scripts/download b/scripts/download index 58c37b3f1c80..a972c0f71d1a 100755 --- a/scripts/download +++ b/scripts/download @@ -20,7 +20,11 @@ rm -rf ${CONTAINERD_DIR} mkdir -p ${CHARTS_DIR} mkdir -p ${DATA_DIR} -curl --compressed -sfL https://github.com/k3s-io/k3s-root/releases/download/${VERSION_ROOT}/k3s-root-${ARCH}.tar | tar xf - +if [ ${ARCH} = riscv64 ]; then + curl --compressed -sfL https://github.com/CARV-ICS-FORTH/k3s-root/releases/download/20230614/k3s-root-riscv64.tar | tar xf - +else + curl --compressed -sfL https://github.com/k3s-io/k3s-root/releases/download/${VERSION_ROOT}/k3s-root-${ARCH}.tar | tar xf - +fi git clone --single-branch --branch=${VERSION_RUNC} --depth=1 https://github.com/opencontainers/runc ${RUNC_DIR} diff --git a/scripts/image_scan.sh b/scripts/image_scan.sh index 8e7ced20e1dc..04dda818fe67 100755 --- a/scripts/image_scan.sh +++ b/scripts/image_scan.sh @@ -9,8 +9,8 @@ fi ARCH=$2 -# skipping image scan for s390x since trivy doesn't support s390x arch yet -if [ "${ARCH}" == "s390x" ]; then +# skipping image scan for s390x and riscv64 since trivy doesn't support these archs yet +if [ "${ARCH}" == "s390x" ] || [ "${ARCH}" == "riscv64" ]; then exit 0 fi diff --git a/scripts/package-cli b/scripts/package-cli index 92fee50e1e7a..b084abdb14f1 100755 --- a/scripts/package-cli +++ b/scripts/package-cli @@ -48,6 +48,9 @@ elif [ ${ARCH} = arm ]; then BIN_SUFFIX="-armhf" elif [ ${ARCH} = s390x ]; then BIN_SUFFIX="-s390x" +elif [ ${ARCH} = riscv64 ]; then + export GOARCH="riscv64" + BIN_SUFFIX="-riscv64" fi CMD_NAME=dist/artifacts/k3s${BIN_SUFFIX} diff --git a/scripts/test-helpers b/scripts/test-helpers index 7f3f517e1084..d663a80ba46a 100755 --- a/scripts/test-helpers +++ b/scripts/test-helpers @@ -231,7 +231,7 @@ export -f test-wait # --- sonobuoy-test() { - if [ "$ARCH" = 'arm' ]; then + if [ "$ARCH" = 'arm' ] || [ "$ARCH" = 'riscv64' ]; then echo "Aborting sonobuoy tests, images not available for $ARCH" return 0 fi