1#!/usr/bin/env bash 2hypervisor="kvm" 3test_filter="" 4build_kernel=false 5 6# Checkout source code of a GIT repo with specified branch and commit 7# Args: 8# $1: Target directory 9# $2: GIT URL of the repo 10# $3: Required branch 11# $4: Required commit (optional) 12checkout_repo() { 13 SRC_DIR="$1" 14 GIT_URL="$2" 15 GIT_BRANCH="$3" 16 GIT_COMMIT="$4" 17 18 # Check whether the local HEAD commit same as the requested commit or not. 19 # If commit is not specified, compare local HEAD and remote HEAD. 20 # Remove the folder if there is difference. 21 if [ -d "$SRC_DIR" ]; then 22 pushd "$SRC_DIR" || exit 23 git fetch 24 SRC_LOCAL_COMMIT=$(git rev-parse HEAD) 25 if [ -z "$GIT_COMMIT" ]; then 26 GIT_COMMIT=$(git rev-parse remotes/origin/"$GIT_BRANCH") 27 fi 28 popd || exit 29 if [ "$SRC_LOCAL_COMMIT" != "$GIT_COMMIT" ]; then 30 rm -rf "$SRC_DIR" 31 fi 32 fi 33 34 # Checkout the specified branch and commit (if required) 35 if [ ! -d "$SRC_DIR" ]; then 36 git clone --depth 1 "$GIT_URL" -b "$GIT_BRANCH" "$SRC_DIR" 37 if [ "$GIT_COMMIT" ]; then 38 pushd "$SRC_DIR" || exit 39 git fetch --depth 1 origin "$GIT_COMMIT" 40 git reset --hard FETCH_HEAD 41 popd || exit 42 fi 43 fi 44} 45 46# Not actively used by CI 47build_custom_linux() { 48 ARCH=$(uname -m) 49 SRCDIR=$PWD 50 LINUX_CUSTOM_DIR="$WORKLOADS_DIR/linux-custom" 51 LINUX_CUSTOM_BRANCH="ch-6.2" 52 LINUX_CUSTOM_URL="https://github.com/cloud-hypervisor/linux.git" 53 54 checkout_repo "$LINUX_CUSTOM_DIR" "$LINUX_CUSTOM_URL" "$LINUX_CUSTOM_BRANCH" 55 56 cp "$SRCDIR"/resources/linux-config-"${ARCH}" "$LINUX_CUSTOM_DIR"/.config 57 58 pushd "$LINUX_CUSTOM_DIR" || exit 59 make -j "$(nproc)" 60 if [ "${ARCH}" == "x86_64" ]; then 61 cp vmlinux "$WORKLOADS_DIR/" || exit 1 62 cp arch/x86/boot/bzImage "$WORKLOADS_DIR/" || exit 1 63 elif [ "${ARCH}" == "aarch64" ]; then 64 cp arch/arm64/boot/Image "$WORKLOADS_DIR/" || exit 1 65 cp arch/arm64/boot/Image.gz "$WORKLOADS_DIR/" || exit 1 66 fi 67 popd || exit 68} 69 70cmd_help() { 71 echo "" 72 echo "Cloud Hypervisor $(basename "$0")" 73 echo "Usage: $(basename "$0") [<args>]" 74 echo "" 75 echo "Available arguments:" 76 echo "" 77 echo " --hypervisor Underlying hypervisor. Options kvm, mshv" 78 echo " --test-filter Tests to run" 79 echo " --build-guest-kernel Build guest kernel from source instead of downloading pre-built" 80 echo "" 81 echo " --help Display this help message." 82 echo "" 83} 84 85# shellcheck disable=SC2034 86process_common_args() { 87 while [ $# -gt 0 ]; do 88 case "$1" in 89 "-h" | "--help") { 90 cmd_help 91 exit 1 92 } ;; 93 "--hypervisor") 94 shift 95 hypervisor="$1" 96 ;; 97 "--test-filter") 98 shift 99 test_filter="$1" 100 ;; 101 "--build-guest-kernel") 102 build_kernel=true 103 ;; 104 "--") { 105 shift 106 break 107 } ;; 108 *) 109 echo "Unknown test scripts argument: $1. Please use '-- --help' for help." 110 exit 111 ;; 112 esac 113 shift 114 done 115 if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then 116 die "Hypervisor value must be kvm or mshv" 117 fi 118 # shellcheck disable=SC2034 119 test_binary_args=("$@") 120} 121 122download_hypervisor_fw() { 123 if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then 124 echo "Using authenticated download from GitHub" 125 FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest \ 126 --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \ 127 --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]') 128 else 129 echo "Using anonymous download from GitHub" 130 FW_URL=$(curl --silent https://api.github.com/repos/cloud-hypervisor/rust-hypervisor-firmware/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]') 131 fi 132 FW="$WORKLOADS_DIR/hypervisor-fw" 133 pushd "$WORKLOADS_DIR" || exit 134 rm -f "$FW" 135 time wget --quiet "$FW_URL" || exit 1 136 popd || exit 137} 138 139download_linux() { 140 if [ -n "$AUTH_DOWNLOAD_TOKEN" ]; then 141 echo "Using authenticated download from GitHub" 142 KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/latest \ 143 --header "Authorization: Token $AUTH_DOWNLOAD_TOKEN" \ 144 --header "X-GitHub-Api-Version: 2022-11-28" | grep "browser_download_url" | grep -o 'https://.*[^ "]') 145 else 146 echo "Using anonymous download from GitHub" 147 KERNEL_URLS=$(curl --silent https://api.github.com/repos/cloud-hypervisor/linux/releases/latest | grep "browser_download_url" | grep -o 'https://.*[^ "]') 148 fi 149 pushd "$WORKLOADS_DIR" || exit 150 for url in $KERNEL_URLS; do 151 wget -N --quiet "$url" || exit 1 152 done 153 154 popd || exit 155} 156 157prepare_linux() { 158 if [ "$build_kernel" = true ]; then 159 echo "Building kernel from source" 160 build_custom_linux 161 echo "Using kernel built from source" 162 else 163 echo "Downloading pre-built kernel from GitHub" 164 download_linux 165 echo "Using kernel downloaded from GitHub" 166 fi 167} 168 169download_ovmf() { 170 OVMF_FW_TAG="ch-6624aa331f" 171 OVMF_FW_URL="https://github.com/cloud-hypervisor/edk2/releases/download/$OVMF_FW_TAG/CLOUDHV.fd" 172 OVMF_FW="$WORKLOADS_DIR/CLOUDHV.fd" 173 pushd "$WORKLOADS_DIR" || exit 174 rm -f "$OVMF_FW" 175 time wget --quiet $OVMF_FW_URL || exit 1 176 popd || exit 177} 178