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