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 --workdir "$CTR_CLH_ROOT_DIR" \ 167 --rm \ 168 --volume /dev:/dev \ 169 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 170 "$CTR_IMAGE" \ 171 cargo build \ 172 --target-dir "$CTR_CLH_CARGO_TARGET" \ 173 "${cargo_args[@]}" 174 175 ret=$? 176 177 # If `cargo build` was successful, let's copy the binaries to a more 178 # accessible location. 179 [ $ret -eq 0 ] && { 180 cargo_bin_dir="$CLH_CARGO_TARGET/$build" 181 say "Binaries placed under $cargo_bin_dir" 182 } 183} 184 185cmd_clean() { 186 cargo_args=("$@") 187 188 $DOCKER_RUNTIME run \ 189 --workdir "$CTR_CLH_ROOT_DIR" \ 190 --rm \ 191 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 192 "$CTR_IMAGE" \ 193 cargo clean \ 194 --target-dir "$CTR_CLH_CARGO_TARGET" \ 195 "${cargo_args[@]}" 196 } 197 198cmd_tests() { 199 unit=false 200 cargo=false 201 integration=false 202 203 while [ $# -gt 0 ]; do 204 case "$1" in 205 "-h"|"--help") { cmd_help; exit 1; } ;; 206 "--unit") { unit=true; } ;; 207 "--cargo") { cargo=true; } ;; 208 "--integration") { integration=true; } ;; 209 "--all") { cargo=true; unit=true; integration=true; } ;; 210 "--") { shift; break; } ;; 211 *) 212 die "Unknown tests argument: $1. Please use --help for help." 213 ;; 214 esac 215 shift 216 done 217 218 if [ "$unit" = true ] ; then 219 say "Running unit tests..." 220 $DOCKER_RUNTIME run \ 221 --workdir "$CTR_CLH_ROOT_DIR" \ 222 --rm \ 223 --privileged \ 224 --volume /dev:/dev \ 225 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 226 "$CTR_IMAGE" \ 227 ./scripts/run_unit_tests.sh "$@" || exit $? 228 fi 229 230 if [ "$cargo" = true ] ; then 231 say "Running cargo tests..." 232 $DOCKER_RUNTIME run \ 233 --workdir "$CTR_CLH_ROOT_DIR" \ 234 --rm \ 235 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 236 "$CTR_IMAGE" \ 237 ./scripts/run_cargo_tests.sh || exit $? 238 fi 239 240 if [ "$integration" = true ] ; then 241 say "Running integration tests..." 242 $DOCKER_RUNTIME run \ 243 --workdir "$CTR_CLH_ROOT_DIR" \ 244 --rm \ 245 --privileged \ 246 --mount type=tmpfs,destination=/tmp \ 247 --volume /dev:/dev \ 248 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 249 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 250 "$CTR_IMAGE" \ 251 ./scripts/run_integration_tests.sh "$@" || exit $? 252 fi 253} 254 255cmd_build-container() { 256 container_type="dev" 257 258 while [ $# -gt 0 ]; do 259 case "$1" in 260 "-h"|"--help") { cmd_help; exit 1; } ;; 261 "--dev") { container_type="dev"; } ;; 262 "--") { shift; break; } ;; 263 *) 264 die "Unknown build-container argument: $1. Please use --help for help." 265 ;; 266 esac 267 shift 268 done 269 270 BUILD_DIR=/tmp/cloud-hypervisor/container/ 271 272 mkdir -p $BUILD_DIR 273 cp $CLH_DOCKERFILE $BUILD_DIR 274 275 $DOCKER_RUNTIME build \ 276 --target $container_type \ 277 -t $CTR_IMAGE \ 278 -f $BUILD_DIR/Dockerfile \ 279 $BUILD_DIR 280} 281 282# Parse main command line args. 283# 284while [ $# -gt 0 ]; do 285 case "$1" in 286 -h|--help) { cmd_help; exit 1; } ;; 287 -y|--unattended) { OPT_UNATTENDED=true; } ;; 288 -*) 289 die "Unknown arg: $1. Please use \`$0 help\` for help." 290 ;; 291 *) 292 break 293 ;; 294 esac 295 shift 296done 297 298# $1 is now a command name. Check if it is a valid command and, if so, 299# run it. 300# 301declare -f "cmd_$1" > /dev/null 302ok_or_die "Unknown command: $1. Please use \`$0 help\` for help." 303 304cmd=cmd_$1 305shift 306 307ensure_build_dir 308 309$cmd "$@" 310