From 337e2e1f9dffb2ec344015e3af7482d570f8694f Mon Sep 17 00:00:00 2001 From: Antony Chazapis Date: Wed, 2 Oct 2024 15:02:53 +0300 Subject: [PATCH] Add support for RISC-V Signed-off-by: Antony Chazapis --- .drone.yml | 70 ++++++++++++++++++++++++++++++++++++ Dockerfile.dapper | 2 +- README.md | 2 +- install.sh | 4 +++ scripts/binary_size_check.sh | 2 ++ scripts/build | 7 ++++ scripts/ci | 3 ++ scripts/package-cli | 5 +++ scripts/prepare-cross | 39 ++++++++++++++++++++ 9 files changed, 132 insertions(+), 2 deletions(-) create mode 100755 scripts/prepare-cross diff --git a/.drone.yml b/.drone.yml index 447d09358e81..554214ce50c8 100644 --- a/.drone.yml +++ b/.drone.yml @@ -406,6 +406,76 @@ volumes: host: path: /var/run/docker.sock +--- +kind: pipeline +name: riscv64 + +platform: + os: linux + arch: amd64 + +trigger: + event: + exclude: + - cron + +steps: +- name: skipfiles + image: plugins/git + commands: + - export NAME=$(test $DRONE_BUILD_EVENT = pull_request && echo remotes/origin/${DRONE_COMMIT_BRANCH:-master} || echo ${DRONE_COMMIT_SHA}~) + - export DIFF=$(git --no-pager diff --name-only $NAME | grep -v -f .droneignore); + - if [ -z "$DIFF" ]; then + echo "All files in PR are on ignore list"; + exit 78; + else + echo "Some files in PR are not ignored, $DIFF"; + fi; + when: + event: + - pull_request + +- name: build + image: rancher/dapper:v0.5.0 + secrets: [ AWS_SECRET_ACCESS_KEY-k3s-ci-uploader, AWS_ACCESS_KEY_ID-k3s-ci-uploader ] + environment: + AWS_SECRET_ACCESS_KEY: + from_secret: AWS_SECRET_ACCESS_KEY-k3s-ci-uploader + AWS_ACCESS_KEY_ID: + from_secret: AWS_ACCESS_KEY_ID-k3s-ci-uploader + commands: + - ARCH=riscv64 SKIP_IMAGE=true SKIP_VALIDATE=true SKIP_AIRGAP=true dapper ci + - echo "${DRONE_TAG}-riscv64" | sed -e 's/+/-/g' >.tags + volumes: + - name: docker + path: /var/run/docker.sock + +- name: github_binary_release + image: plugins/github-release + settings: + api_key: + from_secret: github_token + prerelease: true + checksum: + - sha256 + checksum_file: CHECKSUMsum-riscv64.txt + checksum_flatten: true + files: + - "dist/artifacts/*" + when: + instance: + - drone-publish.k3s.io + ref: + - refs/head/master + - refs/tags/* + event: + - tag + +volumes: +- name: docker + host: + path: /var/run/docker.sock + --- kind: pipeline name: validate_go_mods diff --git a/Dockerfile.dapper b/Dockerfile.dapper index ce604819e7c3..76aabbf2e1b1 100644 --- a/Dockerfile.dapper +++ b/Dockerfile.dapper @@ -52,7 +52,7 @@ ENV SELINUX=${SELINUX} # Set Dapper configuration variables ENV DAPPER_RUN_ARGS="--privileged -v k3s-cache:/go/src/github.com/k3s-io/k3s/.cache -v trivy-cache:/root/.cache/trivy" \ - DAPPER_ENV="REPO TAG DRONE_TAG IMAGE_NAME SKIP_VALIDATE SKIP_IMAGE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG GOCOVER DEBUG" \ + DAPPER_ENV="REPO TAG DRONE_TAG IMAGE_NAME ARCH SKIP_VALIDATE SKIP_IMAGE SKIP_AIRGAP AWS_SECRET_ACCESS_KEY AWS_ACCESS_KEY_ID GITHUB_TOKEN GOLANG GOCOVER DEBUG" \ DAPPER_SOURCE="/go/src/github.com/k3s-io/k3s/" \ DAPPER_OUTPUT="./bin ./dist ./build/out ./build/static ./pkg/static ./pkg/deploy" \ DAPPER_DOCKER_SOCKET=true \ diff --git a/README.md b/README.md index 749da5d8f715..f5c142ac6016 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/install.sh b/install.sh index b08e6054a4ea..c09b1b623acc 100755 --- a/install.sh +++ b/install.sh @@ -315,6 +315,10 @@ setup_verify_arch() { ARCH=s390x SUFFIX=-${ARCH} ;; + riscv64) + ARCH=riscv64 + SUFFIX=-${ARCH} + ;; aarch64) ARCH=arm64 SUFFIX=-${ARCH} diff --git a/scripts/binary_size_check.sh b/scripts/binary_size_check.sh index f019ec53e1ed..86e889b6e647 100755 --- a/scripts/binary_size_check.sh +++ b/scripts/binary_size_check.sh @@ -22,6 +22,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}${BINARY_POSTFIX}" diff --git a/scripts/build b/scripts/build index 43e4614fc43e..1231cde7f1ad 100755 --- a/scripts/build +++ b/scripts/build @@ -100,6 +100,13 @@ 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-alpine-linux-musl-gcc" + export PKG_CONFIG_PATH=/home/builder/sysroot-riscv64/usr/lib/pkgconfig/ +fi + k3s_binaries=( "bin/k3s-agent" "bin/k3s-server" diff --git a/scripts/ci b/scripts/ci index 9fb3ec5fb6bd..db3d3f0d5816 100755 --- a/scripts/ci +++ b/scripts/ci @@ -6,6 +6,9 @@ pushd $SCRIPT_DIR ./download ./validate +if [ ${ARCH} = riscv64 ]; then + ./prepare-cross +fi ./build ./package diff --git a/scripts/package-cli b/scripts/package-cli index c790f04aab41..c2c5c2e18f37 100755 --- a/scripts/package-cli +++ b/scripts/package-cli @@ -67,11 +67,16 @@ 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}${BINARY_POSTFIX} GOOS=linux CC=gcc CXX=g++ "${GO}" generate +if [ ${ARCH} = riscv64 ]; then + export GOARCH="riscv64" +fi LDFLAGS=" -X github.com/k3s-io/k3s/pkg/version.Version=$VERSION -X github.com/k3s-io/k3s/pkg/version.GitCommit=${COMMIT:0:8} diff --git a/scripts/prepare-cross b/scripts/prepare-cross new file mode 100755 index 000000000000..e4237b6bc618 --- /dev/null +++ b/scripts/prepare-cross @@ -0,0 +1,39 @@ +#/bin/bash + +# This script prepares a cross-compilation environment for $ARCH + +if [ -z "$ARCH" ]; then + echo "Error: ARCH not defined" + exit 1; +fi + +if [ "$ARCH" != "riscv64" ]; then + echo "Error: Cross-compilation only available for riscv64" + exit 1; +fi + +if [ "$ARCH" = "riscv64" ]; then + ALPINE_APK_REPO=https://dl-cdn.alpinelinux.org/alpine/edge/main +fi + +# Build packages for cross-compiling (bootstrap.sh requires a non-root user) +adduser -D builder +addgroup builder abuild + +su builder -c "abuild-keygen -an" +cp ~builder/.abuild/builder-*.rsa.pub /etc/apk/keys/ + +ALPINE_VERSION=$(. /etc/os-release; echo $VERSION_ID | cut -f1,2 -d.) +su builder -c "cd; git clone --single-branch --branch=$ALPINE_VERSION-stable --depth 1 https://github.com/alpinelinux/aports" +sed -i "/Cross building base system/i exit 0\n" /home/builder/aports/scripts/bootstrap.sh +apk update +su builder -c "CBUILDROOT=~/sysroot-$ARCH ~/aports/scripts/bootstrap.sh $ARCH" + +# Install the cross-compiler in the host +apk -X /home/builder/packages/main add gcc-$ARCH + +# Install headers and libraries in the gcc sysroot (upstream packages) +apk -X $ALPINE_APK_REPO -U --allow-untrusted \ + -p /home/builder/sysroot-$ARCH --arch $ARCH add \ + musl musl-dev linux-headers libseccomp-dev libseccomp-static sqlite-dev sqlite-static zlib-static libgcc +sed -i "s/^prefix=.*/prefix=\/home\/builder\/sysroot-$ARCH\/usr/g" /home/builder/sysroot-riscv64/usr/lib/pkgconfig/libseccomp.pc