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="v4" 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_BUILT_DIR="${CTR_CLH_ROOT_DIR}/build" 27CTR_CLH_CARGO_TARGET="${CTR_CLH_CARGO_BUILT_DIR}/cargo_target" 28CTR_CLH_INTEGRATION_WORKLOADS="/root/workloads" 29 30# Container networking option 31CTR_CLH_NET="host" 32[ $(uname -m) = "aarch64" ] && CTR_CLH_NET="bridge" 33 34# Cargo paths 35# Full path to the cargo registry dir on the host. This appears on the host 36# because we want to persist the cargo registry across container invocations. 37# Otherwise, any rust crates from crates.io would be downloaded again each time 38# we build or test. 39CARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry" 40 41# Full path to the cargo git registry on the host. This serves the same purpose 42# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of 43# crates.io. 44CARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry" 45 46# Full path to the cargo target dir on the host. 47CARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target" 48 49# Send a decorated message to stdout, followed by a new line 50# 51say() { 52 [ -t 1 ] && [ -n "$TERM" ] \ 53 && echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \ 54 || echo "[$CLI_NAME] $*" 55} 56 57# Send a decorated message to stdout, without a trailing new line 58# 59say_noln() { 60 [ -t 1 ] && [ -n "$TERM" ] \ 61 && echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \ 62 || echo "[$CLI_NAME] $*" 63} 64 65# Send a text message to stderr 66# 67say_err() { 68 [ -t 2 ] && [ -n "$TERM" ] \ 69 && echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 \ 70 || echo "[$CLI_NAME] $*" 1>&2 71} 72 73# Send a warning-highlighted text to stdout 74say_warn() { 75 [ -t 1 ] && [ -n "$TERM" ] \ 76 && echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" \ 77 || echo "[$CLI_NAME] $*" 78} 79 80# Exit with an error message and (optional) code 81# Usage: die [-c <error code>] <error message> 82# 83die() { 84 code=1 85 [[ "$1" = "-c" ]] && { 86 code="$2" 87 shift 2 88 } 89 say_err "$@" 90 exit $code 91} 92 93# Exit with an error message if the last exit code is not 0 94# 95ok_or_die() { 96 code=$? 97 [[ $code -eq 0 ]] || die -c $code "$@" 98} 99 100# Make sure the build/ dirs are available. Exit if we can't create them. 101# Upon returning from this call, the caller can be certain the build/ dirs exist. 102# 103ensure_build_dir() { 104 for dir in "$CLH_BUILD_DIR" \ 105 "$CLH_INTEGRATION_WORKLOADS" \ 106 "$CLH_CTR_BUILD_DIR" \ 107 "$CARGO_TARGET_DIR" \ 108 "$CARGO_REGISTRY_DIR" \ 109 "$CARGO_GIT_REGISTRY_DIR"; do 110 mkdir -p "$dir" || die "Error: cannot create dir $dir" 111 [ -x "$dir" ] && [ -w "$dir" ] || \ 112 { 113 say "Wrong permissions for $dir. Attempting to fix them ..." 114 chmod +x+w "$dir" 115 } || \ 116 die "Error: wrong permissions for $dir. Should be +x+w" 117 done 118} 119 120# Make sure we're using the latest dev container, by just pulling it. 121ensure_latest_ctr() { 122 $DOCKER_RUNTIME pull "$CTR_IMAGE" 123 124 ok_or_die "Error pulling container image. Aborting." 125} 126 127# Fix main directory permissions after a container ran as root. 128# Since the container ran as root, any files it creates will be owned by root. 129# This fixes that by recursively changing the ownership of /cloud-hypervisor to the 130# current user. 131# 132fix_dir_perms() { 133 # Yes, running Docker to get elevated privileges, just to chown some files 134 # is a dirty hack. 135 $DOCKER_RUNTIME run \ 136 --workdir "$CTR_CLH_ROOT_DIR" \ 137 --rm \ 138 --volume /dev:/dev \ 139 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 140 "$CTR_IMAGE" \ 141 chown -R "$(id -u):$(id -g)" "$CTR_CLH_ROOT_DIR" 142 143 return $1 144} 145 146cmd_help() { 147 echo "" 148 echo "Cloud Hypervisor $(basename $0)" 149 echo "Usage: $(basename $0) <command> [<command args>]" 150 echo "" 151 echo "Available commands:" 152 echo "" 153 echo " build [--debug|--release] [--libc musl|gnu] [-- [<cargo args>]]" 154 echo " Build the Cloud Hypervisor binaries." 155 echo " --debug Build the debug binaries. This is the default." 156 echo " --release Build the release binaries." 157 echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 158 echo "" 159 echo " tests [--unit|--cargo|--all] [--libc musl|gnu] [-- [<cargo test args>]]" 160 echo " Run the Cloud Hypervisor tests." 161 echo " --unit Run the unit tests." 162 echo " --cargo Run the cargo tests." 163 echo " --integration Run the integration tests." 164 echo " --integration-sgx Run the SGX integration tests." 165 echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 166 echo " --all Run all tests." 167 echo "" 168 echo " build-container [--type]" 169 echo " Build the Cloud Hypervisor container." 170 echo " --dev Build dev container. This is the default." 171 echo "" 172 echo " clean [<cargo args>]]" 173 echo " Remove the Cloud Hypervisor artifacts." 174 echo "" 175 echo " shell" 176 echo " Run the development container into an interactive, privileged BASH shell." 177 echo "" 178 echo " help" 179 echo " Display this help message." 180 echo "" 181} 182 183cmd_build() { 184 build="debug" 185 libc="gnu" 186 187 while [ $# -gt 0 ]; do 188 case "$1" in 189 "-h"|"--help") { cmd_help; exit 1; } ;; 190 "--debug") { build="debug"; } ;; 191 "--release") { build="release"; } ;; 192 "--libc") 193 shift 194 [[ "$1" =~ ^(musl|gnu)$ ]] || \ 195 die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 196 libc="$1" 197 ;; 198 "--") { shift; break; } ;; 199 *) 200 die "Unknown build argument: $1. Please use --help for help." 201 ;; 202 esac 203 shift 204 done 205 206 target="$(uname -m)-unknown-linux-${libc}" 207 208 cargo_args=("$@") 209 [ $build = "release" ] && cargo_args+=("--release") 210 cargo_args+=(--target "$target") 211 [ $(uname -m) = "aarch64" ] && cargo_args+=("--no-default-features") 212 [ $(uname -m) = "aarch64" ] && cargo_args+=(--features "mmio,kvm") 213 214 rustflags="" 215 if [ $(uname -m) = "aarch64" ] && [ $libc = "musl" ] ; then 216 rustflags="-C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" 217 fi 218 219 $DOCKER_RUNTIME run \ 220 --user "$(id -u):$(id -g)" \ 221 --workdir "$CTR_CLH_ROOT_DIR" \ 222 --rm \ 223 --volume /dev:/dev \ 224 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 225 --env RUSTFLAGS="$rustflags" \ 226 "$CTR_IMAGE" \ 227 cargo build --all \ 228 --target-dir "$CTR_CLH_CARGO_TARGET" \ 229 "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$target/$build" 230} 231 232cmd_clean() { 233 cargo_args=("$@") 234 235 $DOCKER_RUNTIME run \ 236 --user "$(id -u):$(id -g)" \ 237 --workdir "$CTR_CLH_ROOT_DIR" \ 238 --rm \ 239 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 240 "$CTR_IMAGE" \ 241 cargo clean \ 242 --target-dir "$CTR_CLH_CARGO_TARGET" \ 243 "${cargo_args[@]}" 244 } 245 246cmd_tests() { 247 unit=false 248 cargo=false 249 integration=false 250 integration_sgx=false 251 libc="gnu" 252 253 while [ $# -gt 0 ]; do 254 case "$1" in 255 "-h"|"--help") { cmd_help; exit 1; } ;; 256 "--unit") { unit=true; } ;; 257 "--cargo") { cargo=true; } ;; 258 "--integration") { integration=true; } ;; 259 "--integration-sgx") { integration_sgx=true; } ;; 260 "--libc") 261 shift 262 [[ "$1" =~ ^(musl|gnu)$ ]] || \ 263 die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 264 libc="$1" 265 ;; 266 "--all") { cargo=true; unit=true; integration=true; } ;; 267 "--") { shift; break; } ;; 268 *) 269 die "Unknown tests argument: $1. Please use --help for help." 270 ;; 271 esac 272 shift 273 done 274 275 target="$(uname -m)-unknown-linux-${libc}" 276 cflags="" 277 target_cc="" 278 if [[ "$target" == "x86_64-unknown-linux-musl" ]]; then 279 target_cc="musl-gcc" 280 cflags="-I /usr/include/x86_64-linux-musl/ -idirafter /usr/include/" 281 fi 282 283 if [ "$unit" = true ] ; then 284 say "Running unit tests for $target..." 285 $DOCKER_RUNTIME run \ 286 --workdir "$CTR_CLH_ROOT_DIR" \ 287 --rm \ 288 --device /dev/kvm \ 289 --device /dev/net/tun \ 290 --cap-add net_admin \ 291 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 292 --env BUILD_TARGET="$target" \ 293 --env CFLAGS="$cflags" \ 294 --env TARGET_CC="$target_cc" \ 295 "$CTR_IMAGE" \ 296 ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $? 297 fi 298 299 if [ "$cargo" = true ] ; then 300 say "Running cargo tests..." 301 $DOCKER_RUNTIME run \ 302 --workdir "$CTR_CLH_ROOT_DIR" \ 303 --rm \ 304 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 305 "$CTR_IMAGE" \ 306 ./scripts/run_cargo_tests.sh || fix_dir_perms $? || exit $? 307 fi 308 309 if [ "$integration" = true ] ; then 310 say "Running integration tests for $target..." 311 $DOCKER_RUNTIME run \ 312 --workdir "$CTR_CLH_ROOT_DIR" \ 313 --rm \ 314 --privileged \ 315 --security-opt seccomp=unconfined \ 316 --ipc=host \ 317 --net="$CTR_CLH_NET" \ 318 --mount type=tmpfs,destination=/tmp \ 319 --volume /dev:/dev \ 320 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 321 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 322 --env USER="root" \ 323 --env CH_LIBC="${libc}" \ 324 "$CTR_IMAGE" \ 325 ./scripts/run_integration_tests_$(uname -m).sh "$@" || fix_dir_perms $? || exit $? 326 fi 327 328 if [ "$integration_sgx" = true ] ; then 329 say "Running integration tests for $target..." 330 $DOCKER_RUNTIME run \ 331 --workdir "$CTR_CLH_ROOT_DIR" \ 332 --rm \ 333 --privileged \ 334 --security-opt seccomp=unconfined \ 335 --ipc=host \ 336 --net="$CTR_CLH_NET" \ 337 --mount type=tmpfs,destination=/tmp \ 338 --volume /dev:/dev \ 339 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 340 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 341 --env USER="root" \ 342 --env CH_LIBC="${libc}" \ 343 "$CTR_IMAGE" \ 344 ./scripts/run_integration_tests_sgx.sh "$@" || fix_dir_perms $? || exit $? 345 fi 346 347 fix_dir_perms $? 348} 349 350cmd_build-container() { 351 container_type="dev" 352 353 while [ $# -gt 0 ]; do 354 case "$1" in 355 "-h"|"--help") { cmd_help; exit 1; } ;; 356 "--dev") { container_type="dev"; } ;; 357 "--") { shift; break; } ;; 358 *) 359 die "Unknown build-container argument: $1. Please use --help for help." 360 ;; 361 esac 362 shift 363 done 364 365 BUILD_DIR=/tmp/cloud-hypervisor/container/ 366 367 mkdir -p $BUILD_DIR 368 cp $CLH_DOCKERFILE $BUILD_DIR 369 370 $DOCKER_RUNTIME build \ 371 --target $container_type \ 372 -t $CTR_IMAGE \ 373 -f $BUILD_DIR/Dockerfile \ 374 --build-arg TARGETARCH="$(uname -m)" \ 375 $BUILD_DIR 376} 377 378cmd_shell() { 379 say_warn "Starting a privileged shell prompt as root ..." 380 say_warn "WARNING: Your $CLH_ROOT_DIR folder will be bind-mounted in the container under $CTR_CLH_ROOT_DIR" 381 $DOCKER_RUNTIME run \ 382 -ti \ 383 --workdir "$CTR_CLH_ROOT_DIR" \ 384 --rm \ 385 --privileged \ 386 --security-opt seccomp=unconfined \ 387 --ipc=host \ 388 --net="$CTR_CLH_NET" \ 389 --tmpfs /tmp:exec \ 390 --volume /dev:/dev \ 391 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 392 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 393 --env USER="root" \ 394 --entrypoint bash \ 395 "$CTR_IMAGE" 396 397 fix_dir_perms $? 398} 399 400# Parse main command line args. 401# 402while [ $# -gt 0 ]; do 403 case "$1" in 404 -h|--help) { cmd_help; exit 1; } ;; 405 -y|--unattended) { OPT_UNATTENDED=true; } ;; 406 -*) 407 die "Unknown arg: $1. Please use \`$0 help\` for help." 408 ;; 409 *) 410 break 411 ;; 412 esac 413 shift 414done 415 416# $1 is now a command name. Check if it is a valid command and, if so, 417# run it. 418# 419declare -f "cmd_$1" > /dev/null 420ok_or_die "Unknown command: $1. Please use \`$0 help\` for help." 421 422cmd=cmd_$1 423shift 424 425ensure_build_dir 426if [ $(uname -m) = "x86_64" ]; then 427 ensure_latest_ctr 428fi 429 430# Before a public image for AArch64 ready, we build the container if needed. 431if [ $(uname -m) = "aarch64" ]; then 432 cmd_build-container 433fi 434 435$cmd "$@" 436