xref: /cloud-hypervisor/scripts/test-util.sh (revision 2b05753716936506ed440863fe6a29dfc7a427e2)
191fe48d5SPhilipp Schuster#!/usr/bin/env bash
2*2b057537SBo Chenset -x
3*2b057537SBo Chen
427b5f8d7SMuminul Islamhypervisor="kvm"
5c2acea51SRob Bradfordtest_filter=""
62f9436bcSRob Bradfordbuild_kernel=false
7c2acea51SRob Bradford
84335e5f8SHenry Wang# Checkout source code of a GIT repo with specified branch and commit
94335e5f8SHenry Wang# Args:
104335e5f8SHenry Wang#   $1: Target directory
114335e5f8SHenry Wang#   $2: GIT URL of the repo
124335e5f8SHenry Wang#   $3: Required branch
134335e5f8SHenry Wang#   $4: Required commit (optional)
144335e5f8SHenry Wangcheckout_repo() {
154335e5f8SHenry Wang    SRC_DIR="$1"
164335e5f8SHenry Wang    GIT_URL="$2"
174335e5f8SHenry Wang    GIT_BRANCH="$3"
184335e5f8SHenry Wang    GIT_COMMIT="$4"
194335e5f8SHenry Wang
204335e5f8SHenry Wang    # Check whether the local HEAD commit same as the requested commit or not.
214335e5f8SHenry Wang    # If commit is not specified, compare local HEAD and remote HEAD.
224335e5f8SHenry Wang    # Remove the folder if there is difference.
234335e5f8SHenry Wang    if [ -d "$SRC_DIR" ]; then
242b2d0065SRuslan Mstoi        pushd "$SRC_DIR" || exit
254335e5f8SHenry Wang        git fetch
264335e5f8SHenry Wang        SRC_LOCAL_COMMIT=$(git rev-parse HEAD)
274335e5f8SHenry Wang        if [ -z "$GIT_COMMIT" ]; then
284335e5f8SHenry Wang            GIT_COMMIT=$(git rev-parse remotes/origin/"$GIT_BRANCH")
294335e5f8SHenry Wang        fi
302b2d0065SRuslan Mstoi        popd || exit
314335e5f8SHenry Wang        if [ "$SRC_LOCAL_COMMIT" != "$GIT_COMMIT" ]; then
324335e5f8SHenry Wang            rm -rf "$SRC_DIR"
334335e5f8SHenry Wang        fi
344335e5f8SHenry Wang    fi
354335e5f8SHenry Wang
364335e5f8SHenry Wang    # Checkout the specified branch and commit (if required)
374335e5f8SHenry Wang    if [ ! -d "$SRC_DIR" ]; then
384335e5f8SHenry Wang        git clone --depth 1 "$GIT_URL" -b "$GIT_BRANCH" "$SRC_DIR"
394335e5f8SHenry Wang        if [ "$GIT_COMMIT" ]; then
402b2d0065SRuslan Mstoi            pushd "$SRC_DIR" || exit
414335e5f8SHenry Wang            git fetch --depth 1 origin "$GIT_COMMIT"
424335e5f8SHenry Wang            git reset --hard FETCH_HEAD
432b2d0065SRuslan Mstoi            popd || exit
444335e5f8SHenry Wang        fi
454335e5f8SHenry Wang    fi
464335e5f8SHenry Wang}
474335e5f8SHenry Wang
48a10e9e6cSRob Bradford# Not actively used by CI
4926351a81SHenry Wangbuild_custom_linux() {
5026351a81SHenry Wang    ARCH=$(uname -m)
5126351a81SHenry Wang    LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom"
52f8927894SRob Bradford    LINUX_CUSTOM_BRANCH="ch-6.12.8"
5326351a81SHenry Wang    LINUX_CUSTOM_URL="https://github.com/cloud-hypervisor/linux.git"
5426351a81SHenry Wang
5526351a81SHenry Wang    checkout_repo "$LINUX_CUSTOM_DIR" "$LINUX_CUSTOM_URL" "$LINUX_CUSTOM_BRANCH"
5626351a81SHenry Wang
572b2d0065SRuslan Mstoi    pushd "$LINUX_CUSTOM_DIR" || exit
58f8927894SRob Bradford    make ch_defconfig
592b2d0065SRuslan Mstoi    make -j "$(nproc)"
602b2d0065SRuslan Mstoi    if [ "${ARCH}" == "x86_64" ]; then
61*2b057537SBo Chen        cp vmlinux "$WORKLOADS_DIR/vmlinux-x86_64" || exit 1
62*2b057537SBo Chen        cp arch/x86/boot/bzImage "$WORKLOADS_DIR/bzImage-x86_64" || exit 1
632b2d0065SRuslan Mstoi    elif [ "${ARCH}" == "aarch64" ]; then
64*2b057537SBo Chen        cp arch/arm64/boot/Image "$WORKLOADS_DIR/Image-arm64" || exit 1
65*2b057537SBo Chen        cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/Image-arm64.gz" || exit 1
6626351a81SHenry Wang    fi
672b2d0065SRuslan Mstoi    popd || exit
6826351a81SHenry Wang}
6926351a81SHenry Wang
7027b5f8d7SMuminul Islamcmd_help() {
7127b5f8d7SMuminul Islam    echo ""
722b2d0065SRuslan Mstoi    echo "Cloud Hypervisor $(basename "$0")"
732b2d0065SRuslan Mstoi    echo "Usage: $(basename "$0") [<args>]"
7427b5f8d7SMuminul Islam    echo ""
7527b5f8d7SMuminul Islam    echo "Available arguments:"
7627b5f8d7SMuminul Islam    echo ""
7727b5f8d7SMuminul Islam    echo "    --hypervisor  Underlying hypervisor. Options kvm, mshv"
78c2acea51SRob Bradford    echo "    --test-filter Tests to run"
79906580eeSRuoqing He    echo "    --build-guest-kernel Build guest kernel from source instead of downloading pre-built"
8027b5f8d7SMuminul Islam    echo ""
8127b5f8d7SMuminul Islam    echo "    --help        Display this help message."
8227b5f8d7SMuminul Islam    echo ""
8327b5f8d7SMuminul Islam}
8427b5f8d7SMuminul Islam
85906580eeSRuoqing He# shellcheck disable=SC2034
8627b5f8d7SMuminul Islamprocess_common_args() {
8727b5f8d7SMuminul Islam    while [ $# -gt 0 ]; do
8827b5f8d7SMuminul Islam        case "$1" in
89318caeb9SRuslan Mstoi        "-h" | "--help") {
90318caeb9SRuslan Mstoi            cmd_help
91318caeb9SRuslan Mstoi            exit 1
92318caeb9SRuslan Mstoi        } ;;
9327b5f8d7SMuminul Islam        "--hypervisor")
9427b5f8d7SMuminul Islam            shift
9527b5f8d7SMuminul Islam            hypervisor="$1"
9627b5f8d7SMuminul Islam            ;;
97c2acea51SRob Bradford        "--test-filter")
98c2acea51SRob Bradford            shift
99c2acea51SRob Bradford            test_filter="$1"
100c2acea51SRob Bradford            ;;
101906580eeSRuoqing He        "--build-guest-kernel")
102906580eeSRuoqing He            build_kernel=true
103906580eeSRuoqing He            ;;
1044552d07aSBo Chen        "--") {
1054552d07aSBo Chen            shift
1064552d07aSBo Chen            break
1074552d07aSBo Chen        } ;;
10827b5f8d7SMuminul Islam        *)
1094552d07aSBo Chen            echo "Unknown test scripts argument: $1. Please use '-- --help' for help."
1104552d07aSBo Chen            exit
11127b5f8d7SMuminul Islam            ;;
11227b5f8d7SMuminul Islam        esac
11327b5f8d7SMuminul Islam        shift
11427b5f8d7SMuminul Islam    done
115ca4857b5SMuminul Islam    if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
116ca4857b5SMuminul Islam        die "Hypervisor value must be kvm or mshv"
11727b5f8d7SMuminul Islam    fi
1182b2d0065SRuslan Mstoi    # shellcheck disable=SC2034
1192b2d0065SRuslan Mstoi    test_binary_args=("$@")
12027b5f8d7SMuminul Islam}
12172e213ebSRob Bradford
12272e213ebSRob Bradforddownload_hypervisor_fw() {
123261bfac4SRuoqing He    FW_TAG="0.5.0"
12414907d07SWei Liu    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
1255e1806aeSRob Bradford        echo "Using authenticated download from GitHub"
126261bfac4SRuoqing He        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/tags/${FW_TAG} \
1275e1806aeSRob Bradford            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
128261bfac4SRuoqing He            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" |
129261bfac4SRuoqing He            grep -oP '"https://[^"]*hypervisor-fw"' | sed -e 's/^"//' -e 's/"$//')
1305e1806aeSRob Bradford    else
1315e1806aeSRob Bradford        echo "Using anonymous download from GitHub"
132261bfac4SRuoqing He        FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/tags/${FW_TAG} |
133261bfac4SRuoqing He            grep "browser_download_url" | grep -oP '"https://[^"]*hypervisor-fw"' | sed -e 's/^"//' -e 's/"$//')
1345e1806aeSRob Bradford    fi
13572e213ebSRob Bradford    FW="$WORKLOADS_DIR/hypervisor-fw"
1362b2d0065SRuslan Mstoi    pushd "$WORKLOADS_DIR" || exit
1372b2d0065SRuslan Mstoi    rm -f "$FW"
1382b2d0065SRuslan Mstoi    time wget --quiet "$FW_URL" || exit 1
1392b2d0065SRuslan Mstoi    popd || exit
14072e213ebSRob Bradford}
14176dbe660SThomas Barrett
142a10e9e6cSRob Bradforddownload_linux() {
143*2b057537SBo Chen    KERNEL_TAG="ch-release-v6.12.8-20250613"
144a10e9e6cSRob Bradford    if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then
145a10e9e6cSRob Bradford        echo "Using authenticated download from GitHub"
1462f9436bcSRob Bradford        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/tags/${KERNEL_TAG} \
147a10e9e6cSRob Bradford            --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \
148a10e9e6cSRob Bradford            --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]')
149a10e9e6cSRob Bradford    else
150a10e9e6cSRob Bradford        echo "Using anonymous download from GitHub"
1512f9436bcSRob Bradford        KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/tags/${KERNEL_TAG} | grep "browser_download_url" | grep -o 'https://.*[^ "]')
152a10e9e6cSRob Bradford    fi
153a10e9e6cSRob Bradford    pushd "$WORKLOADS_DIR" || exit
154a10e9e6cSRob Bradford    for url in $KERNEL_URLS; do
15519d36c76SRob Bradford        wget -N --quiet "$url" || exit 1
156a10e9e6cSRob Bradford    done
157a10e9e6cSRob Bradford
158a10e9e6cSRob Bradford    popd || exit
159a10e9e6cSRob Bradford}
160a10e9e6cSRob Bradford
161906580eeSRuoqing Heprepare_linux() {
162906580eeSRuoqing He    if [ "$build_kernel" = true ]; then
163906580eeSRuoqing He        echo "Building kernel from source"
164906580eeSRuoqing He        build_custom_linux
165906580eeSRuoqing He        echo "Using kernel built from source"
166906580eeSRuoqing He    else
167906580eeSRuoqing He        echo "Downloading pre-built kernel from GitHub"
168906580eeSRuoqing He        download_linux
169906580eeSRuoqing He        echo "Using kernel downloaded from GitHub"
170906580eeSRuoqing He    fi
171906580eeSRuoqing He}
172906580eeSRuoqing He
17376dbe660SThomas Barrettdownload_ovmf() {
174fa686fdfSRob Bradford    OVMF_FW_TAG="ch-a54f262b09"
17576dbe660SThomas Barrett    OVMF_FW_URL="https://github.com/cloud-hypervisor/edk2/releases/download/$OVMF_FW_TAG/CLOUDHV.fd"
17676dbe660SThomas Barrett    OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd"
1772b2d0065SRuslan Mstoi    pushd "$WORKLOADS_DIR" || exit
1782b2d0065SRuslan Mstoi    rm -f "$OVMF_FW"
17976dbe660SThomas Barrett    time wget --quiet $OVMF_FW_URL || exit 1
1802b2d0065SRuslan Mstoi    popd || exit
18176dbe660SThomas Barrett}
182