1#!/bin/bash 2 3# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4# Copyright © 2020 Intel Corporation 5# SPDX-License-Identifier: Apache-2.0 6 7CLI_NAME="Cloud Hypervisor" 8 9CTR_IMAGE_TAG="cloudhypervisor/dev" 10CTR_IMAGE_VERSION="v1" 11CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}" 12 13DOCKER_RUNTIME="docker" 14 15# Host paths 16CLH_SCRIPTS_DIR=$(cd "$(dirname "$0")" && pwd) 17CLH_ROOT_DIR=$(cd "${CLH_SCRIPTS_DIR}/.." && pwd) 18CLH_BUILD_DIR="${CLH_ROOT_DIR}/build" 19CLH_CARGO_TARGET="${CLH_BUILD_DIR}/cargo_target" 20CLH_DOCKERFILE="${CLH_SCRIPTS_DIR}/../resources/Dockerfile" 21CLH_CTR_BUILD_DIR="/tmp/cloud-hypervisor/ctr-build" 22CLH_INTEGRATION_WORKLOADS="${HOME}/workloads" 23 24# Container paths 25CTR_CLH_ROOT_DIR="/cloud-hypervisor" 26CTR_CLH_CARGO_TARGET="${CTR_CLH_ROOT_DIR}/build/cargo_target" 27CTR_CLH_INTEGRATION_WORKLOADS="/root/workloads" 28 29# Cargo paths 30# Full path to the cargo registry dir on the host. This appears on the host 31# because we want to persist the cargo registry across container invocations. 32# Otherwise, any rust crates from crates.io would be downloaded again each time 33# we build or test. 34CARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry" 35 36# Full path to the cargo git registry on the host. This serves the same purpose 37# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of 38# crates.io. 39CARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry" 40 41# Full path to the cargo target dir on the host. 42CARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target" 43 44# Send a decorated message to stdout, followed by a new line 45# 46say() { 47 [ -t 1 ] && [ -n "$TERM" ] \ 48 && echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \ 49 || echo "[$CLI_NAME] $*" 50} 51 52# Send a decorated message to stdout, without a trailing new line 53# 54say_noln() { 55 [ -t 1 ] && [ -n "$TERM" ] \ 56 && echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \ 57 || echo "[$CLI_NAME] $*" 58} 59 60# Send a text message to stderr 61# 62say_err() { 63 [ -t 2 ] && [ -n "$TERM" ] \ 64 && echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 \ 65 || echo "[$CLI_NAME] $*" 1>&2 66} 67 68# Send a warning-highlighted text to stdout 69say_warn() { 70 [ -t 1 ] && [ -n "$TERM" ] \ 71 && echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" \ 72 || echo "[$CLI_NAME] $*" 73} 74 75# Exit with an error message and (optional) code 76# Usage: die [-c <error code>] <error message> 77# 78die() { 79 code=1 80 [[ "$1" = "-c" ]] && { 81 code="$2" 82 shift 2 83 } 84 say_err "$@" 85 exit $code 86} 87 88# Exit with an error message if the last exit code is not 0 89# 90ok_or_die() { 91 code=$? 92 [[ $code -eq 0 ]] || die -c $code "$@" 93} 94 95# Make sure the build/ dirs are available. Exit if we can't create them. 96# Upon returning from this call, the caller can be certain the build/ dirs exist. 97# 98ensure_build_dir() { 99 for dir in "$CLH_BUILD_DIR" \ 100 "$CLH_INTEGRATION_WORKLOADS" \ 101 "$CLH_CTR_BUILD_DIR" \ 102 "$CARGO_TARGET_DIR" \ 103 "$CARGO_REGISTRY_DIR" \ 104 "$CARGO_GIT_REGISTRY_DIR"; do 105 mkdir -p "$dir" || die "Error: cannot create dir $dir" 106 [ -x "$dir" ] && [ -w "$dir" ] || \ 107 { 108 say "Wrong permissions for $dir. Attempting to fix them ..." 109 chmod +x+w "$dir" 110 } || \ 111 die "Error: wrong permissions for $dir. Should be +x+w" 112 done 113} 114 115cmd_help() { 116 echo "" 117 echo "Cloud Hypervisor $(basename $0)" 118 echo "Usage: $(basename $0) <command> [<command args>]" 119 echo "" 120 echo "Available commands:" 121 echo "" 122 echo " build [--debug|--release] [-- [<cargo args>]]" 123 echo " Build the Cloud Hypervisor binaries." 124 echo " --debug Build the debug binaries. This is the default." 125 echo " --release Build the release binaries." 126 echo "" 127 echo " tests [--unit|--cargo|--all] [-- [<cargo test args>]]" 128 echo " Run the Cloud Hypervisor tests." 129 echo " --unit Run the unit tests." 130 echo " --cargo Run the cargo tests." 131 echo " --integration Run the integration tests." 132 echo " --all Run all tests." 133 echo "" 134 echo " build-container [--type]" 135 echo " Build the Cloud Hypervisor container." 136 echo " --dev Build dev container. This is the default." 137 echo "" 138 echo " clean [<cargo args>]]" 139 echo " Remove the Cloud Hypervisor artifacts." 140 echo "" 141 echo " help" 142 echo " Display this help message." 143 echo "" 144} 145 146cmd_build() { 147 build="debug" 148 149 while [ $# -gt 0 ]; do 150 case "$1" in 151 "-h"|"--help") { cmd_help; exit 1; } ;; 152 "--debug") { build="debug"; } ;; 153 "--release") { build="release"; } ;; 154 "--") { shift; break; } ;; 155 *) 156 die "Unknown build argument: $1. Please use --help for help." 157 ;; 158 esac 159 shift 160 done 161 162 cargo_args=("$@") 163 [ $build = "release" ] && cargo_args+=("--release") 164 165 $DOCKER_RUNTIME run \ 166 --user "$(id -u):$(id -g)" \ 167 --workdir "$CTR_CLH_ROOT_DIR" \ 168 --rm \ 169 --volume /dev:/dev \ 170 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 171 "$CTR_IMAGE" \ 172 cargo build \ 173 --target-dir "$CTR_CLH_CARGO_TARGET" \ 174 "${cargo_args[@]}" 175 176 ret=$? 177 178 # If `cargo build` was successful, let's copy the binaries to a more 179 # accessible location. 180 [ $ret -eq 0 ] && { 181 cargo_bin_dir="$CLH_CARGO_TARGET/$build" 182 say "Binaries placed under $cargo_bin_dir" 183 } 184} 185 186cmd_clean() { 187 cargo_args=("$@") 188 189 $DOCKER_RUNTIME run \ 190 --user "$(id -u):$(id -g)" \ 191 --workdir "$CTR_CLH_ROOT_DIR" \ 192 --rm \ 193 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 194 "$CTR_IMAGE" \ 195 cargo clean \ 196 --target-dir "$CTR_CLH_CARGO_TARGET" \ 197 "${cargo_args[@]}" 198 } 199 200cmd_tests() { 201 unit=false 202 cargo=false 203 integration=false 204 205 while [ $# -gt 0 ]; do 206 case "$1" in 207 "-h"|"--help") { cmd_help; exit 1; } ;; 208 "--unit") { unit=true; } ;; 209 "--cargo") { cargo=true; } ;; 210 "--integration") { integration=true; } ;; 211 "--all") { cargo=true; unit=true; integration=true; } ;; 212 "--") { shift; break; } ;; 213 *) 214 die "Unknown tests argument: $1. Please use --help for help." 215 ;; 216 esac 217 shift 218 done 219 220 if [ "$unit" = true ] ; then 221 say "Running unit tests..." 222 $DOCKER_RUNTIME run \ 223 --workdir "$CTR_CLH_ROOT_DIR" \ 224 --rm \ 225 --privileged \ 226 --volume /dev:/dev \ 227 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 228 "$CTR_IMAGE" \ 229 ./scripts/run_unit_tests.sh "$@" || exit $? 230 fi 231 232 if [ "$cargo" = true ] ; then 233 say "Running cargo tests..." 234 $DOCKER_RUNTIME run \ 235 --user "$(id -u):$(id -g)" \ 236 --workdir "$CTR_CLH_ROOT_DIR" \ 237 --rm \ 238 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 239 "$CTR_IMAGE" \ 240 ./scripts/run_cargo_tests.sh || exit $? 241 fi 242 243 if [ "$integration" = true ] ; then 244 say "Running integration tests..." 245 $DOCKER_RUNTIME run \ 246 --workdir "$CTR_CLH_ROOT_DIR" \ 247 --rm \ 248 --privileged \ 249 --mount type=tmpfs,destination=/tmp \ 250 --volume /dev:/dev \ 251 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 252 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 253 "$CTR_IMAGE" \ 254 ./scripts/run_integration_tests.sh "$@" || exit $? 255 fi 256} 257 258cmd_build-container() { 259 container_type="dev" 260 261 while [ $# -gt 0 ]; do 262 case "$1" in 263 "-h"|"--help") { cmd_help; exit 1; } ;; 264 "--dev") { container_type="dev"; } ;; 265 "--") { shift; break; } ;; 266 *) 267 die "Unknown build-container argument: $1. Please use --help for help." 268 ;; 269 esac 270 shift 271 done 272 273 BUILD_DIR=/tmp/cloud-hypervisor/container/ 274 275 mkdir -p $BUILD_DIR 276 cp $CLH_DOCKERFILE $BUILD_DIR 277 278 $DOCKER_RUNTIME build \ 279 --target $container_type \ 280 -t $CTR_IMAGE \ 281 -f $BUILD_DIR/Dockerfile \ 282 $BUILD_DIR 283} 284 285# Parse main command line args. 286# 287while [ $# -gt 0 ]; do 288 case "$1" in 289 -h|--help) { cmd_help; exit 1; } ;; 290 -y|--unattended) { OPT_UNATTENDED=true; } ;; 291 -*) 292 die "Unknown arg: $1. Please use \`$0 help\` for help." 293 ;; 294 *) 295 break 296 ;; 297 esac 298 shift 299done 300 301# $1 is now a command name. Check if it is a valid command and, if so, 302# run it. 303# 304declare -f "cmd_$1" > /dev/null 305ok_or_die "Unknown command: $1. Please use \`$0 help\` for help." 306 307cmd=cmd_$1 308shift 309 310ensure_build_dir 311 312$cmd "$@" 313