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="20220405-0" 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="bridge" 32 33# Cargo paths 34# Full path to the cargo registry dir on the host. This appears on the host 35# because we want to persist the cargo registry across container invocations. 36# Otherwise, any rust crates from crates.io would be downloaded again each time 37# we build or test. 38CARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry" 39 40# Full path to the cargo git registry on the host. This serves the same purpose 41# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of 42# crates.io. 43CARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry" 44 45# Full path to the cargo target dir on the host. 46CARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target" 47 48# Send a decorated message to stdout, followed by a new line 49# 50say() { 51 [ -t 1 ] && [ -n "$TERM" ] && 52 echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" || 53 echo "[$CLI_NAME] $*" 54} 55 56# Send a decorated message to stdout, without a trailing new line 57# 58say_noln() { 59 [ -t 1 ] && [ -n "$TERM" ] && 60 echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" || 61 echo "[$CLI_NAME] $*" 62} 63 64# Send a text message to stderr 65# 66say_err() { 67 [ -t 2 ] && [ -n "$TERM" ] && 68 echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 || 69 echo "[$CLI_NAME] $*" 1>&2 70} 71 72# Send a warning-highlighted text to stdout 73say_warn() { 74 [ -t 1 ] && [ -n "$TERM" ] && 75 echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" || 76 echo "[$CLI_NAME] $*" 77} 78 79# Exit with an error message and (optional) code 80# Usage: die [-c <error code>] <error message> 81# 82die() { 83 code=1 84 [[ "$1" = "-c" ]] && { 85 code="$2" 86 shift 2 87 } 88 say_err "$@" 89 exit "$code" 90} 91 92# Exit with an error message if the last exit code is not 0 93# 94ok_or_die() { 95 code=$? 96 [[ $code -eq 0 ]] || die -c $code "$@" 97} 98 99# Make sure the build/ dirs are available. Exit if we can't create them. 100# Upon returning from this call, the caller can be certain the build/ dirs exist. 101# 102ensure_build_dir() { 103 for dir in "$CLH_BUILD_DIR" \ 104 "$CLH_INTEGRATION_WORKLOADS" \ 105 "$CLH_CTR_BUILD_DIR" \ 106 "$CARGO_TARGET_DIR" \ 107 "$CARGO_REGISTRY_DIR" \ 108 "$CARGO_GIT_REGISTRY_DIR"; do 109 mkdir -p "$dir" || die "Error: cannot create dir $dir" 110 [ -x "$dir" ] && [ -w "$dir" ] || 111 { 112 say "Wrong permissions for $dir. Attempting to fix them ..." 113 chmod +x+w "$dir" 114 } || 115 die "Error: wrong permissions for $dir. Should be +x+w" 116 done 117} 118 119# Make sure we're using the latest dev container, by just pulling it. 120ensure_latest_ctr() { 121 if [ "$CTR_IMAGE_VERSION" = "local" ]; then 122 build_container 123 else 124 $DOCKER_RUNTIME pull "$CTR_IMAGE" 125 126 if [ $? -ne 0 ]; then 127 build_container 128 fi 129 130 ok_or_die "Error pulling/building container image. Aborting." 131 fi 132} 133 134# Fix main directory permissions after a container ran as root. 135# Since the container ran as root, any files it creates will be owned by root. 136# This fixes that by recursively changing the ownership of /cloud-hypervisor to the 137# current user. 138# 139fix_dir_perms() { 140 # Yes, running Docker to get elevated privileges, just to chown some files 141 # is a dirty hack. 142 $DOCKER_RUNTIME run \ 143 --workdir "$CTR_CLH_ROOT_DIR" \ 144 --rm \ 145 --volume /dev:/dev \ 146 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 147 "$CTR_IMAGE" \ 148 chown -R "$(id -u):$(id -g)" "$CTR_CLH_ROOT_DIR" 149 150 return "$1" 151} 152# Process exported volumes argument, separate the volumes and make docker compatible 153# Sample input: --volumes /a:/a#/b:/b 154# Sample output: --volume /a:/a --volume /b:/b 155# 156process_volumes_args() { 157 if [ -z "$arg_vols" ]; then 158 return 159 fi 160 exported_volumes="" 161 arr_vols=(${arg_vols//#/ }) 162 for var in "${arr_vols[@]}"; do 163 parts=(${var//:/ }) 164 if [[ ! -e "${parts[0]}" ]]; then 165 echo "The volume ${parts[0]} does not exist." 166 exit 1 167 fi 168 exported_volumes="$exported_volumes --volume $var" 169 done 170} 171cmd_help() { 172 echo "" 173 echo "Cloud Hypervisor $(basename "$0")" 174 echo "Usage: $(basename "$0") <command> [<command args>]" 175 echo "" 176 echo "Available commands:" 177 echo "" 178 echo " build [--debug|--release] [--libc musl|gnu] [-- [<cargo args>]]" 179 echo " Build the Cloud Hypervisor binaries." 180 echo " --debug Build the debug binaries. This is the default." 181 echo " --release Build the release binaries." 182 echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 183 echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 184 echo " --hypervisor Underlying hypervisor. Options kvm, mshv" 185 echo "" 186 echo " tests [<test type (see below)>] [--libc musl|gnu] [-- [<test scripts args>] [-- [<test binary args>]]] " 187 echo " Run the Cloud Hypervisor tests." 188 echo " --unit Run the unit tests." 189 echo " --integration Run the integration tests." 190 echo " --integration-sgx Run the SGX integration tests." 191 echo " --integration-vfio Run the VFIO integration tests." 192 echo " --integration-windows Run the Windows guest integration tests." 193 echo " --integration-live-migration Run the live-migration integration tests." 194 echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 195 echo " --metrics Generate performance metrics" 196 echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 197 echo " --hypervisor Underlying hypervisor. Options kvm, mshv" 198 echo " --all Run all tests." 199 echo "" 200 echo " build-container [--type]" 201 echo " Build the Cloud Hypervisor container." 202 echo "" 203 echo " clean [<cargo args>]]" 204 echo " Remove the Cloud Hypervisor artifacts." 205 echo "" 206 echo " shell" 207 echo " Run the development container into an interactive, privileged BASH shell." 208 echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 209 echo "" 210 echo " help" 211 echo " Display this help message." 212 echo "" 213} 214 215cmd_build() { 216 build="debug" 217 libc="gnu" 218 hypervisor="kvm" 219 features_build="" 220 exported_device="/dev/kvm" 221 while [ $# -gt 0 ]; do 222 case "$1" in 223 "-h" | "--help") { 224 cmd_help 225 exit 1 226 } ;; 227 "--debug") { build="debug"; } ;; 228 "--release") { build="release"; } ;; 229 "--libc") 230 shift 231 [[ "$1" =~ ^(musl|gnu)$ ]] || 232 die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 233 libc="$1" 234 ;; 235 "--volumes") 236 shift 237 arg_vols="$1" 238 ;; 239 "--hypervisor") 240 shift 241 hypervisor="$1" 242 ;; 243 "--features") 244 shift 245 features_build="--features $1" 246 ;; 247 "--") { 248 shift 249 break 250 } ;; 251 *) 252 die "Unknown build argument: $1. Please use --help for help." 253 ;; 254 esac 255 shift 256 done 257 258 ensure_build_dir 259 ensure_latest_ctr 260 261 process_volumes_args 262 if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then 263 die "Hypervisor value must be kvm or mshv" 264 fi 265 if [[ "$hypervisor" = "mshv" ]]; then 266 exported_device="/dev/mshv" 267 fi 268 target="$(uname -m)-unknown-linux-${libc}" 269 270 cargo_args=("$@") 271 [ $build = "release" ] && cargo_args+=("--release") 272 cargo_args+=(--target "$target") 273 274 rustflags="" 275 target_cc="" 276 if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then 277 rustflags="-C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" 278 target_cc="musl-gcc" 279 fi 280 281 $DOCKER_RUNTIME run \ 282 --user "$(id -u):$(id -g)" \ 283 --workdir "$CTR_CLH_ROOT_DIR" \ 284 --rm \ 285 --volume $exported_device \ 286 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 287 --env RUSTFLAGS="$rustflags" \ 288 --env TARGET_CC="$target_cc" \ 289 "$CTR_IMAGE" \ 290 cargo build --all $features_build \ 291 --target-dir "$CTR_CLH_CARGO_TARGET" \ 292 "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$target/$build" 293} 294 295cmd_clean() { 296 cargo_args=("$@") 297 298 ensure_build_dir 299 ensure_latest_ctr 300 301 $DOCKER_RUNTIME run \ 302 --user "$(id -u):$(id -g)" \ 303 --workdir "$CTR_CLH_ROOT_DIR" \ 304 --rm \ 305 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 306 "$CTR_IMAGE" \ 307 cargo clean \ 308 --target-dir "$CTR_CLH_CARGO_TARGET" \ 309 "${cargo_args[@]}" 310} 311 312cmd_tests() { 313 unit=false 314 integration=false 315 integration_sgx=false 316 integration_vfio=false 317 integration_windows=false 318 integration_live_migration=false 319 metrics=false 320 libc="gnu" 321 arg_vols="" 322 hypervisor="kvm" 323 exported_device="/dev/kvm" 324 while [ $# -gt 0 ]; do 325 case "$1" in 326 "-h" | "--help") { 327 cmd_help 328 exit 1 329 } ;; 330 "--unit") { unit=true; } ;; 331 "--integration") { integration=true; } ;; 332 "--integration-sgx") { integration_sgx=true; } ;; 333 "--integration-vfio") { integration_vfio=true; } ;; 334 "--integration-windows") { integration_windows=true; } ;; 335 "--integration-live-migration") { integration_live_migration=true; } ;; 336 "--metrics") { metrics=true; } ;; 337 "--libc") 338 shift 339 [[ "$1" =~ ^(musl|gnu)$ ]] || 340 die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 341 libc="$1" 342 ;; 343 "--volumes") 344 shift 345 arg_vols="$1" 346 ;; 347 "--hypervisor") 348 shift 349 hypervisor="$1" 350 ;; 351 "--all") { 352 cargo=true 353 unit=true 354 integration=true 355 } ;; 356 "--") { 357 shift 358 break 359 } ;; 360 *) 361 die "Unknown tests argument: $1. Please use --help for help." 362 ;; 363 esac 364 shift 365 done 366 if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then 367 die "Hypervisor value must be kvm or mshv" 368 fi 369 370 if [[ "$hypervisor" = "mshv" ]]; then 371 exported_device="/dev/mshv" 372 fi 373 374 set -- '--hypervisor' "$hypervisor" "$@" 375 376 ensure_build_dir 377 ensure_latest_ctr 378 379 process_volumes_args 380 target="$(uname -m)-unknown-linux-${libc}" 381 382 if [[ "$unit" = true ]]; then 383 say "Running unit tests for $target..." 384 $DOCKER_RUNTIME run \ 385 --workdir "$CTR_CLH_ROOT_DIR" \ 386 --rm \ 387 --device $exported_device \ 388 --device /dev/net/tun \ 389 --cap-add net_admin \ 390 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 391 --env BUILD_TARGET="$target" \ 392 "$CTR_IMAGE" \ 393 ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $? 394 fi 395 396 if [ "$integration" = true ]; then 397 say "Running integration tests for $target..." 398 $DOCKER_RUNTIME run \ 399 --workdir "$CTR_CLH_ROOT_DIR" \ 400 --rm \ 401 --privileged \ 402 --security-opt seccomp=unconfined \ 403 --ipc=host \ 404 --net="$CTR_CLH_NET" \ 405 --mount type=tmpfs,destination=/tmp \ 406 --volume /dev:/dev \ 407 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 408 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 409 --env USER="root" \ 410 --env CH_LIBC="${libc}" \ 411 "$CTR_IMAGE" \ 412 ./scripts/run_integration_tests_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $? 413 fi 414 415 if [ "$integration_sgx" = true ]; then 416 say "Running SGX integration tests for $target..." 417 $DOCKER_RUNTIME run \ 418 --workdir "$CTR_CLH_ROOT_DIR" \ 419 --rm \ 420 --privileged \ 421 --security-opt seccomp=unconfined \ 422 --ipc=host \ 423 --net="$CTR_CLH_NET" \ 424 --mount type=tmpfs,destination=/tmp \ 425 --volume /dev:/dev \ 426 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 427 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 428 --env USER="root" \ 429 --env CH_LIBC="${libc}" \ 430 "$CTR_IMAGE" \ 431 ./scripts/run_integration_tests_sgx.sh "$@" || fix_dir_perms $? || exit $? 432 fi 433 434 if [ "$integration_vfio" = true ]; then 435 say "Running VFIO integration tests for $target..." 436 $DOCKER_RUNTIME run \ 437 --workdir "$CTR_CLH_ROOT_DIR" \ 438 --rm \ 439 --privileged \ 440 --security-opt seccomp=unconfined \ 441 --ipc=host \ 442 --net="$CTR_CLH_NET" \ 443 --mount type=tmpfs,destination=/tmp \ 444 --volume /dev:/dev \ 445 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 446 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 447 --env USER="root" \ 448 --env CH_LIBC="${libc}" \ 449 "$CTR_IMAGE" \ 450 ./scripts/run_integration_tests_vfio.sh "$@" || fix_dir_perms $? || exit $? 451 fi 452 453 if [ "$integration_windows" = true ]; then 454 say "Running Windows integration tests for $target..." 455 $DOCKER_RUNTIME run \ 456 --workdir "$CTR_CLH_ROOT_DIR" \ 457 --rm \ 458 --privileged \ 459 --security-opt seccomp=unconfined \ 460 --ipc=host \ 461 --net="$CTR_CLH_NET" \ 462 --mount type=tmpfs,destination=/tmp \ 463 --volume /dev:/dev \ 464 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 465 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 466 --env USER="root" \ 467 --env CH_LIBC="${libc}" \ 468 "$CTR_IMAGE" \ 469 ./scripts/run_integration_tests_windows.sh "$@" || fix_dir_perms $? || exit $? 470 fi 471 472 if [ "$integration_live_migration" = true ]; then 473 say "Running 'live migration' integration tests for $target..." 474 $DOCKER_RUNTIME run \ 475 --workdir "$CTR_CLH_ROOT_DIR" \ 476 --rm \ 477 --privileged \ 478 --security-opt seccomp=unconfined \ 479 --ipc=host \ 480 --net="$CTR_CLH_NET" \ 481 --mount type=tmpfs,destination=/tmp \ 482 --volume /dev:/dev \ 483 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 484 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 485 --env USER="root" \ 486 --env CH_LIBC="${libc}" \ 487 "$CTR_IMAGE" \ 488 ./scripts/run_integration_tests_live_migration.sh "$@" || fix_dir_perms $? || exit $? 489 fi 490 491 if [ "$metrics" = true ]; then 492 say "Generating performance metrics for $target..." 493 $DOCKER_RUNTIME run \ 494 --workdir "$CTR_CLH_ROOT_DIR" \ 495 --rm \ 496 --privileged \ 497 --security-opt seccomp=unconfined \ 498 --ipc=host \ 499 --net="$CTR_CLH_NET" \ 500 --mount type=tmpfs,destination=/tmp \ 501 --volume /dev:/dev \ 502 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 503 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 504 --env USER="root" \ 505 --env CH_LIBC="${libc}" \ 506 "$CTR_IMAGE" \ 507 ./scripts/run_metrics.sh "$@" || fix_dir_perms $? || exit $? 508 fi 509 510 fix_dir_perms $? 511} 512 513build_container() { 514 ensure_build_dir 515 516 BUILD_DIR=/tmp/cloud-hypervisor/container/ 517 518 mkdir -p $BUILD_DIR 519 cp "$CLH_DOCKERFILE" $BUILD_DIR 520 521 [ "$(uname -m)" = "aarch64" ] && TARGETARCH="arm64" 522 [ "$(uname -m)" = "x86_64" ] && TARGETARCH="amd64" 523 524 $DOCKER_RUNTIME build \ 525 --target dev \ 526 -t $CTR_IMAGE \ 527 -f $BUILD_DIR/Dockerfile \ 528 --build-arg TARGETARCH=$TARGETARCH \ 529 $BUILD_DIR 530} 531 532cmd_build-container() { 533 while [ $# -gt 0 ]; do 534 case "$1" in 535 "-h" | "--help") { 536 cmd_help 537 exit 1 538 } ;; 539 "--") { 540 shift 541 break 542 } ;; 543 *) 544 die "Unknown build-container argument: $1. Please use --help for help." 545 ;; 546 esac 547 shift 548 done 549 550 build_container 551} 552 553cmd_shell() { 554 while [ $# -gt 0 ]; do 555 case "$1" in 556 "-h" | "--help") { 557 cmd_help 558 exit 1 559 } ;; 560 "--volumes") 561 shift 562 arg_vols="$1" 563 ;; 564 "--") { 565 shift 566 break 567 } ;; 568 *) ;; 569 570 esac 571 shift 572 done 573 ensure_build_dir 574 ensure_latest_ctr 575 process_volumes_args 576 say_warn "Starting a privileged shell prompt as root ..." 577 say_warn "WARNING: Your $CLH_ROOT_DIR folder will be bind-mounted in the container under $CTR_CLH_ROOT_DIR" 578 $DOCKER_RUNTIME run \ 579 -ti \ 580 --workdir "$CTR_CLH_ROOT_DIR" \ 581 --rm \ 582 --privileged \ 583 --security-opt seccomp=unconfined \ 584 --ipc=host \ 585 --net="$CTR_CLH_NET" \ 586 --tmpfs /tmp:exec \ 587 --volume /dev:/dev \ 588 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 589 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 590 --env USER="root" \ 591 --entrypoint bash \ 592 "$CTR_IMAGE" 593 594 fix_dir_perms $? 595} 596 597# Parse main command line args. 598# 599while [ $# -gt 0 ]; do 600 case "$1" in 601 -h | --help) { 602 cmd_help 603 exit 1 604 } ;; 605 --local) { 606 CTR_IMAGE_VERSION="local" 607 CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}" 608 } ;; 609 -*) 610 die "Unknown arg: $1. Please use \`$0 help\` for help." 611 ;; 612 *) 613 break 614 ;; 615 esac 616 shift 617done 618 619# $1 is now a command name. Check if it is a valid command and, if so, 620# run it. 621# 622declare -f "cmd_$1" >/dev/null 623ok_or_die "Unknown command: $1. Please use \`$0 help\` for help." 624 625cmd=cmd_$1 626shift 627 628$cmd "$@" 629