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="ghcr.io/cloud-hypervisor/cloud-hypervisor" 10CTR_IMAGE_VERSION="20231108-0" 11: "${CTR_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} 171 172cmd_help() { 173 echo "" 174 echo "Cloud Hypervisor $(basename "$0")" 175 echo "Usage: $(basename "$0") [flags] <command> [<command args>]" 176 echo "" 177 echo "Available flags": 178 echo "" 179 echo " --local Set the container image version being used to \"local\"." 180 echo "" 181 echo "Available commands:" 182 echo "" 183 echo " build [--debug|--release] [--libc musl|gnu] [-- [<cargo args>]]" 184 echo " Build the Cloud Hypervisor binaries." 185 echo " --debug Build the debug binaries. This is the default." 186 echo " --release Build the release binaries." 187 echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 188 echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 189 echo " --hypervisor Underlying hypervisor. Options kvm, mshv" 190 echo "" 191 echo " tests [<test type (see below)>] [--libc musl|gnu] [-- [<test scripts args>] [-- [<test binary args>]]] " 192 echo " Run the Cloud Hypervisor tests." 193 echo " --unit Run the unit tests." 194 echo " --integration Run the integration tests." 195 echo " --integration-sgx Run the SGX integration tests." 196 echo " --integration-vfio Run the VFIO integration tests." 197 echo " --integration-windows Run the Windows guest integration tests." 198 echo " --integration-live-migration Run the live-migration integration tests." 199 echo " --integration-rate-limiter Run the rate-limiter integration tests." 200 echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 201 echo " --metrics Generate performance metrics" 202 echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 203 echo " --hypervisor Underlying hypervisor. Options kvm, mshv" 204 echo " --all Run all tests." 205 echo "" 206 echo " build-container [--type]" 207 echo " Build the Cloud Hypervisor container." 208 echo "" 209 echo " clean [<cargo args>]]" 210 echo " Remove the Cloud Hypervisor artifacts." 211 echo "" 212 echo " shell" 213 echo " Run the development container into an interactive, privileged BASH shell." 214 echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 215 echo "" 216 echo " help" 217 echo " Display this help message." 218 echo "" 219} 220 221cmd_build() { 222 build="debug" 223 libc="gnu" 224 hypervisor="kvm" 225 features_build="" 226 exported_device="/dev/kvm" 227 while [ $# -gt 0 ]; do 228 case "$1" in 229 "-h" | "--help") { 230 cmd_help 231 exit 1 232 } ;; 233 "--debug") { build="debug"; } ;; 234 "--release") { build="release"; } ;; 235 "--runtime") 236 shift 237 DOCKER_RUNTIME="$1" 238 export DOCKER_RUNTIME 239 ;; 240 "--libc") 241 shift 242 [[ "$1" =~ ^(musl|gnu)$ ]] || 243 die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 244 libc="$1" 245 ;; 246 "--volumes") 247 shift 248 arg_vols="$1" 249 ;; 250 "--hypervisor") 251 shift 252 hypervisor="$1" 253 ;; 254 "--features") 255 shift 256 features_build="--features $1" 257 ;; 258 "--") { 259 shift 260 break 261 } ;; 262 *) 263 die "Unknown build argument: $1. Please use --help for help." 264 ;; 265 esac 266 shift 267 done 268 269 ensure_build_dir 270 ensure_latest_ctr 271 272 process_volumes_args 273 if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then 274 die "Hypervisor value must be kvm or mshv" 275 fi 276 if [[ "$hypervisor" = "mshv" ]]; then 277 exported_device="/dev/mshv" 278 fi 279 target="$(uname -m)-unknown-linux-${libc}" 280 281 cargo_args=("$@") 282 [ $build = "release" ] && cargo_args+=("--release") 283 cargo_args+=(--target "$target") 284 285 rustflags="$RUSTFLAGS" 286 target_cc="" 287 if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then 288 rustflags="$rustflags -C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" 289 target_cc="musl-gcc" 290 fi 291 292 $DOCKER_RUNTIME run \ 293 --user "$(id -u):$(id -g)" \ 294 --workdir "$CTR_CLH_ROOT_DIR" \ 295 --rm \ 296 --volume $exported_device \ 297 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 298 --env RUSTFLAGS="$rustflags" \ 299 --env TARGET_CC="$target_cc" \ 300 "$CTR_IMAGE" \ 301 cargo build --all $features_build \ 302 --target-dir "$CTR_CLH_CARGO_TARGET" \ 303 "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$target/$build" 304} 305 306cmd_clean() { 307 cargo_args=("$@") 308 309 ensure_build_dir 310 ensure_latest_ctr 311 312 $DOCKER_RUNTIME run \ 313 --user "$(id -u):$(id -g)" \ 314 --workdir "$CTR_CLH_ROOT_DIR" \ 315 --rm \ 316 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 317 "$CTR_IMAGE" \ 318 cargo clean \ 319 --target-dir "$CTR_CLH_CARGO_TARGET" \ 320 "${cargo_args[@]}" 321} 322 323cmd_tests() { 324 unit=false 325 integration=false 326 integration_sgx=false 327 integration_vfio=false 328 integration_windows=false 329 integration_live_migration=false 330 integration_rate_limiter=false 331 metrics=false 332 libc="gnu" 333 arg_vols="" 334 hypervisor="kvm" 335 exported_device="/dev/kvm" 336 while [ $# -gt 0 ]; do 337 case "$1" in 338 "-h" | "--help") { 339 cmd_help 340 exit 1 341 } ;; 342 "--unit") { unit=true; } ;; 343 "--integration") { integration=true; } ;; 344 "--integration-sgx") { integration_sgx=true; } ;; 345 "--integration-vfio") { integration_vfio=true; } ;; 346 "--integration-windows") { integration_windows=true; } ;; 347 "--integration-live-migration") { integration_live_migration=true; } ;; 348 "--integration-rate-limiter") { integration_rate_limiter=true; } ;; 349 "--metrics") { metrics=true; } ;; 350 "--libc") 351 shift 352 [[ "$1" =~ ^(musl|gnu)$ ]] || 353 die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 354 libc="$1" 355 ;; 356 "--volumes") 357 shift 358 arg_vols="$1" 359 ;; 360 "--hypervisor") 361 shift 362 hypervisor="$1" 363 ;; 364 "--all") { 365 cargo=true 366 unit=true 367 integration=true 368 } ;; 369 "--") { 370 shift 371 break 372 } ;; 373 *) 374 die "Unknown tests argument: $1. Please use --help for help." 375 ;; 376 esac 377 shift 378 done 379 if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then 380 die "Hypervisor value must be kvm or mshv" 381 fi 382 383 if [[ "$hypervisor" = "mshv" ]]; then 384 exported_device="/dev/mshv" 385 fi 386 387 if [ ! -e "${exported_device}" ] ; then 388 die "${exported_device} does not exist on the system" 389 fi 390 391 set -- '--hypervisor' "$hypervisor" "$@" 392 393 ensure_build_dir 394 ensure_latest_ctr 395 396 process_volumes_args 397 target="$(uname -m)-unknown-linux-${libc}" 398 399 rustflags="$RUSTFLAGS" 400 target_cc="" 401 if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then 402 rustflags="$rustflags -C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs" 403 target_cc="musl-gcc" 404 fi 405 406 if [[ "$unit" = true ]]; then 407 say "Running unit tests for $target..." 408 $DOCKER_RUNTIME run \ 409 --workdir "$CTR_CLH_ROOT_DIR" \ 410 --rm \ 411 --device $exported_device \ 412 --device /dev/net/tun \ 413 --cap-add net_admin \ 414 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 415 --env BUILD_TARGET="$target" \ 416 --env RUSTFLAGS="$rustflags" \ 417 --env TARGET_CC="$target_cc" \ 418 "$CTR_IMAGE" \ 419 ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $? 420 fi 421 422 if [ "$integration" = true ]; then 423 say "Running integration tests for $target..." 424 $DOCKER_RUNTIME run \ 425 --workdir "$CTR_CLH_ROOT_DIR" \ 426 --rm \ 427 --privileged \ 428 --security-opt seccomp=unconfined \ 429 --ipc=host \ 430 --net="$CTR_CLH_NET" \ 431 --mount type=tmpfs,destination=/tmp \ 432 --volume /dev:/dev \ 433 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 434 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 435 --env USER="root" \ 436 --env BUILD_TARGET="$target" \ 437 --env RUSTFLAGS="$rustflags" \ 438 --env TARGET_CC="$target_cc" \ 439 --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 440 "$CTR_IMAGE" \ 441 dbus-run-session ./scripts/run_integration_tests_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $? 442 fi 443 444 if [ "$integration_sgx" = true ]; then 445 say "Running SGX integration tests for $target..." 446 $DOCKER_RUNTIME run \ 447 --workdir "$CTR_CLH_ROOT_DIR" \ 448 --rm \ 449 --privileged \ 450 --security-opt seccomp=unconfined \ 451 --ipc=host \ 452 --net="$CTR_CLH_NET" \ 453 --mount type=tmpfs,destination=/tmp \ 454 --volume /dev:/dev \ 455 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 456 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 457 --env USER="root" \ 458 --env BUILD_TARGET="$target" \ 459 --env RUSTFLAGS="$rustflags" \ 460 --env TARGET_CC="$target_cc" \ 461 --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 462 "$CTR_IMAGE" \ 463 ./scripts/run_integration_tests_sgx.sh "$@" || fix_dir_perms $? || exit $? 464 fi 465 466 if [ "$integration_vfio" = true ]; then 467 say "Running VFIO integration tests for $target..." 468 $DOCKER_RUNTIME run \ 469 --workdir "$CTR_CLH_ROOT_DIR" \ 470 --rm \ 471 --privileged \ 472 --security-opt seccomp=unconfined \ 473 --ipc=host \ 474 --net="$CTR_CLH_NET" \ 475 --mount type=tmpfs,destination=/tmp \ 476 --volume /dev:/dev \ 477 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 478 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 479 --env USER="root" \ 480 --env BUILD_TARGET="$target" \ 481 --env RUSTFLAGS="$rustflags" \ 482 --env TARGET_CC="$target_cc" \ 483 --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 484 "$CTR_IMAGE" \ 485 ./scripts/run_integration_tests_vfio.sh "$@" || fix_dir_perms $? || exit $? 486 fi 487 488 if [ "$integration_windows" = true ]; then 489 say "Running Windows integration tests for $target..." 490 $DOCKER_RUNTIME run \ 491 --workdir "$CTR_CLH_ROOT_DIR" \ 492 --rm \ 493 --privileged \ 494 --security-opt seccomp=unconfined \ 495 --ipc=host \ 496 --net="$CTR_CLH_NET" \ 497 --mount type=tmpfs,destination=/tmp \ 498 --volume /dev:/dev \ 499 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 500 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 501 --env USER="root" \ 502 --env BUILD_TARGET="$target" \ 503 --env RUSTFLAGS="$rustflags" \ 504 --env TARGET_CC="$target_cc" \ 505 --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 506 "$CTR_IMAGE" \ 507 ./scripts/run_integration_tests_windows_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $? 508 fi 509 510 if [ "$integration_live_migration" = true ]; then 511 say "Running 'live migration' integration tests for $target..." 512 $DOCKER_RUNTIME run \ 513 --workdir "$CTR_CLH_ROOT_DIR" \ 514 --rm \ 515 --privileged \ 516 --security-opt seccomp=unconfined \ 517 --ipc=host \ 518 --net="$CTR_CLH_NET" \ 519 --mount type=tmpfs,destination=/tmp \ 520 --volume /dev:/dev \ 521 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 522 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 523 --env USER="root" \ 524 --env BUILD_TARGET="$target" \ 525 --env RUSTFLAGS="$rustflags" \ 526 --env TARGET_CC="$target_cc" \ 527 --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 528 "$CTR_IMAGE" \ 529 ./scripts/run_integration_tests_live_migration.sh "$@" || fix_dir_perms $? || exit $? 530 fi 531 532 if [ "$integration_rate_limiter" = true ]; then 533 say "Running 'rate limiter' integration tests for $target..." 534 $DOCKER_RUNTIME run \ 535 --workdir "$CTR_CLH_ROOT_DIR" \ 536 --rm \ 537 --privileged \ 538 --security-opt seccomp=unconfined \ 539 --ipc=host \ 540 --net="$CTR_CLH_NET" \ 541 --mount type=tmpfs,destination=/tmp \ 542 --volume /dev:/dev \ 543 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 544 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 545 --env USER="root" \ 546 --env BUILD_TARGET="$target" \ 547 --env RUSTFLAGS="$rustflags" \ 548 --env TARGET_CC="$target_cc" \ 549 --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 550 "$CTR_IMAGE" \ 551 ./scripts/run_integration_tests_rate_limiter.sh "$@" || fix_dir_perms $? || exit $? 552 fi 553 554 if [ "$metrics" = true ]; then 555 say "Generating performance metrics for $target..." 556 $DOCKER_RUNTIME run \ 557 --workdir "$CTR_CLH_ROOT_DIR" \ 558 --rm \ 559 --privileged \ 560 --security-opt seccomp=unconfined \ 561 --ipc=host \ 562 --net="$CTR_CLH_NET" \ 563 --mount type=tmpfs,destination=/tmp \ 564 --volume /dev:/dev \ 565 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 566 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 567 --env USER="root" \ 568 --env BUILD_TARGET="$target" \ 569 --env RUSTFLAGS="$rustflags" \ 570 --env TARGET_CC="$target_cc" \ 571 --env RUST_BACKTRACE="${RUST_BACKTRACE}" \ 572 --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 573 "$CTR_IMAGE" \ 574 ./scripts/run_metrics.sh "$@" || fix_dir_perms $? || exit $? 575 fi 576 577 fix_dir_perms $? 578} 579 580build_container() { 581 ensure_build_dir 582 583 BUILD_DIR=/tmp/cloud-hypervisor/container/ 584 585 mkdir -p $BUILD_DIR 586 cp "$CLH_DOCKERFILE" $BUILD_DIR 587 588 [ "$(uname -m)" = "aarch64" ] && TARGETARCH="arm64" 589 [ "$(uname -m)" = "x86_64" ] && TARGETARCH="amd64" 590 591 $DOCKER_RUNTIME build \ 592 --target dev \ 593 -t $CTR_IMAGE \ 594 -f $BUILD_DIR/Dockerfile \ 595 --build-arg TARGETARCH=$TARGETARCH \ 596 $BUILD_DIR 597} 598 599cmd_build-container() { 600 while [ $# -gt 0 ]; do 601 case "$1" in 602 "-h" | "--help") { 603 cmd_help 604 exit 1 605 } ;; 606 "--") { 607 shift 608 break 609 } ;; 610 *) 611 die "Unknown build-container argument: $1. Please use --help for help." 612 ;; 613 esac 614 shift 615 done 616 617 build_container 618} 619 620cmd_shell() { 621 while [ $# -gt 0 ]; do 622 case "$1" in 623 "-h" | "--help") { 624 cmd_help 625 exit 1 626 } ;; 627 "--volumes") 628 shift 629 arg_vols="$1" 630 ;; 631 "--") { 632 shift 633 break 634 } ;; 635 *) ;; 636 637 esac 638 shift 639 done 640 ensure_build_dir 641 ensure_latest_ctr 642 process_volumes_args 643 say_warn "Starting a privileged shell prompt as root ..." 644 say_warn "WARNING: Your $CLH_ROOT_DIR folder will be bind-mounted in the container under $CTR_CLH_ROOT_DIR" 645 $DOCKER_RUNTIME run \ 646 -ti \ 647 --workdir "$CTR_CLH_ROOT_DIR" \ 648 --rm \ 649 --privileged \ 650 --security-opt seccomp=unconfined \ 651 --ipc=host \ 652 --net="$CTR_CLH_NET" \ 653 --tmpfs /tmp:exec \ 654 --volume /dev:/dev \ 655 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \ 656 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 657 --env USER="root" \ 658 --entrypoint bash \ 659 "$CTR_IMAGE" 660 661 fix_dir_perms $? 662} 663 664if [ $# = 0 ]; then 665 cmd_help 666 say_err "Please specify command to run!" 667 exit 1 668fi 669 670# Parse main command line args. 671# 672while [ $# -gt 0 ]; do 673 case "$1" in 674 -h | --help) { 675 cmd_help 676 exit 1 677 } ;; 678 --local) { 679 CTR_IMAGE_VERSION="local" 680 CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}" 681 } ;; 682 -*) 683 die "Unknown arg: $1. Please use \`$0 help\` for help." 684 ;; 685 *) 686 break 687 ;; 688 esac 689 shift 690done 691 692# $1 is now a command name. Check if it is a valid command and, if so, 693# run it. 694# 695declare -f "cmd_$1" >/dev/null 696ok_or_die "Unknown command: $1. Please use \`$0 help\` for help." 697 698cmd=cmd_$1 699shift 700 701$cmd "$@" 702