191fe48d5SPhilipp Schuster#!/usr/bin/env bash 2db6f894eSSamuel Ortiz 3db6f894eSSamuel Ortiz# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4db6f894eSSamuel Ortiz# Copyright © 2020 Intel Corporation 5db6f894eSSamuel Ortiz# SPDX-License-Identifier: Apache-2.0 6db6f894eSSamuel Ortiz 7db6f894eSSamuel OrtizCLI_NAME="Cloud Hypervisor" 8db6f894eSSamuel Ortiz 9ca96ff60SRob BradfordCTR_IMAGE_TAG="ghcr.io/cloud-hypervisor/cloud-hypervisor" 1024f384d2SRavi kumar VeeramallyCTR_IMAGE_VERSION="20231220-0" 1170cfd1beSRuslan Mstoi: "${CTR_IMAGE:=${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}}" 12db6f894eSSamuel Ortiz 13db6f894eSSamuel OrtizDOCKER_RUNTIME="docker" 14db6f894eSSamuel Ortiz 15db6f894eSSamuel Ortiz# Host paths 16db6f894eSSamuel OrtizCLH_SCRIPTS_DIR=$(cd "$(dirname "$0")" && pwd) 17db6f894eSSamuel OrtizCLH_ROOT_DIR=$(cd "${CLH_SCRIPTS_DIR}/.." && pwd) 18db6f894eSSamuel OrtizCLH_BUILD_DIR="${CLH_ROOT_DIR}/build" 19db6f894eSSamuel OrtizCLH_CARGO_TARGET="${CLH_BUILD_DIR}/cargo_target" 20db6f894eSSamuel OrtizCLH_DOCKERFILE="${CLH_SCRIPTS_DIR}/../resources/Dockerfile" 21db6f894eSSamuel OrtizCLH_CTR_BUILD_DIR="/tmp/cloud-hypervisor/ctr-build" 22db6f894eSSamuel OrtizCLH_INTEGRATION_WORKLOADS="${HOME}/workloads" 23db6f894eSSamuel Ortiz 24db6f894eSSamuel Ortiz# Container paths 25db6f894eSSamuel OrtizCTR_CLH_ROOT_DIR="/cloud-hypervisor" 260a1d6e1cSSamuel OrtizCTR_CLH_CARGO_BUILT_DIR="${CTR_CLH_ROOT_DIR}/build" 270a1d6e1cSSamuel OrtizCTR_CLH_CARGO_TARGET="${CTR_CLH_CARGO_BUILT_DIR}/cargo_target" 28db6f894eSSamuel OrtizCTR_CLH_INTEGRATION_WORKLOADS="/root/workloads" 29db6f894eSSamuel Ortiz 30cf1b5156SMichael Zhao# Container networking option 317889fc92SSebastien BoeufCTR_CLH_NET="bridge" 32cf1b5156SMichael Zhao 33db6f894eSSamuel Ortiz# Cargo paths 34db6f894eSSamuel Ortiz# Full path to the cargo registry dir on the host. This appears on the host 35db6f894eSSamuel Ortiz# because we want to persist the cargo registry across container invocations. 36db6f894eSSamuel Ortiz# Otherwise, any rust crates from crates.io would be downloaded again each time 37db6f894eSSamuel Ortiz# we build or test. 38db6f894eSSamuel OrtizCARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry" 39db6f894eSSamuel Ortiz 40db6f894eSSamuel Ortiz# Full path to the cargo git registry on the host. This serves the same purpose 41db6f894eSSamuel Ortiz# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of 42db6f894eSSamuel Ortiz# crates.io. 43db6f894eSSamuel OrtizCARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry" 44db6f894eSSamuel Ortiz 45db6f894eSSamuel Ortiz# Full path to the cargo target dir on the host. 46db6f894eSSamuel OrtizCARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target" 47db6f894eSSamuel Ortiz 48db6f894eSSamuel Ortiz# Send a decorated message to stdout, followed by a new line 49db6f894eSSamuel Ortiz# 50db6f894eSSamuel Ortizsay() { 511a5b94eeSRob Bradford [ -t 1 ] && [ -n "$TERM" ] && 521a5b94eeSRob Bradford echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" || 531a5b94eeSRob Bradford echo "[$CLI_NAME] $*" 54db6f894eSSamuel Ortiz} 55db6f894eSSamuel Ortiz 56db6f894eSSamuel Ortiz# Send a decorated message to stdout, without a trailing new line 57db6f894eSSamuel Ortiz# 58db6f894eSSamuel Ortizsay_noln() { 591a5b94eeSRob Bradford [ -t 1 ] && [ -n "$TERM" ] && 601a5b94eeSRob Bradford echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" || 611a5b94eeSRob Bradford echo "[$CLI_NAME] $*" 62db6f894eSSamuel Ortiz} 63db6f894eSSamuel Ortiz 64db6f894eSSamuel Ortiz# Send a text message to stderr 65db6f894eSSamuel Ortiz# 66db6f894eSSamuel Ortizsay_err() { 671a5b94eeSRob Bradford [ -t 2 ] && [ -n "$TERM" ] && 681a5b94eeSRob Bradford echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 || 691a5b94eeSRob Bradford echo "[$CLI_NAME] $*" 1>&2 70db6f894eSSamuel Ortiz} 71db6f894eSSamuel Ortiz 72db6f894eSSamuel Ortiz# Send a warning-highlighted text to stdout 73db6f894eSSamuel Ortizsay_warn() { 741a5b94eeSRob Bradford [ -t 1 ] && [ -n "$TERM" ] && 751a5b94eeSRob Bradford echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" || 761a5b94eeSRob Bradford echo "[$CLI_NAME] $*" 77db6f894eSSamuel Ortiz} 78db6f894eSSamuel Ortiz 79db6f894eSSamuel Ortiz# Exit with an error message and (optional) code 80db6f894eSSamuel Ortiz# Usage: die [-c <error code>] <error message> 81db6f894eSSamuel Ortiz# 82db6f894eSSamuel Ortizdie() { 83db6f894eSSamuel Ortiz code=1 84db6f894eSSamuel Ortiz [[ "$1" = "-c" ]] && { 85db6f894eSSamuel Ortiz code="$2" 86db6f894eSSamuel Ortiz shift 2 87db6f894eSSamuel Ortiz } 88db6f894eSSamuel Ortiz say_err "$@" 892805e7b1SRob Bradford exit "$code" 90db6f894eSSamuel Ortiz} 91db6f894eSSamuel Ortiz 92db6f894eSSamuel Ortiz# Exit with an error message if the last exit code is not 0 93db6f894eSSamuel Ortiz# 94db6f894eSSamuel Ortizok_or_die() { 95db6f894eSSamuel Ortiz code=$? 96db6f894eSSamuel Ortiz [[ $code -eq 0 ]] || die -c $code "$@" 97db6f894eSSamuel Ortiz} 98db6f894eSSamuel Ortiz 99db6f894eSSamuel Ortiz# Make sure the build/ dirs are available. Exit if we can't create them. 100db6f894eSSamuel Ortiz# Upon returning from this call, the caller can be certain the build/ dirs exist. 101db6f894eSSamuel Ortiz# 102db6f894eSSamuel Ortizensure_build_dir() { 103db6f894eSSamuel Ortiz for dir in "$CLH_BUILD_DIR" \ 104db6f894eSSamuel Ortiz "$CLH_INTEGRATION_WORKLOADS" \ 105db6f894eSSamuel Ortiz "$CLH_CTR_BUILD_DIR" \ 106db6f894eSSamuel Ortiz "$CARGO_TARGET_DIR" \ 107db6f894eSSamuel Ortiz "$CARGO_REGISTRY_DIR" \ 108db6f894eSSamuel Ortiz "$CARGO_GIT_REGISTRY_DIR"; do 109db6f894eSSamuel Ortiz mkdir -p "$dir" || die "Error: cannot create dir $dir" 1101a5b94eeSRob Bradford [ -x "$dir" ] && [ -w "$dir" ] || 111db6f894eSSamuel Ortiz { 112db6f894eSSamuel Ortiz say "Wrong permissions for $dir. Attempting to fix them ..." 113db6f894eSSamuel Ortiz chmod +x+w "$dir" 1141a5b94eeSRob Bradford } || 115db6f894eSSamuel Ortiz die "Error: wrong permissions for $dir. Should be +x+w" 116db6f894eSSamuel Ortiz done 117db6f894eSSamuel Ortiz} 118db6f894eSSamuel Ortiz 1192fc86ffeSSamuel Ortiz# Make sure we're using the latest dev container, by just pulling it. 1202fc86ffeSSamuel Ortizensure_latest_ctr() { 12155b8a218SRob Bradford if [ "$CTR_IMAGE_VERSION" = "local" ]; then 12255b8a218SRob Bradford build_container 12355b8a218SRob Bradford else 1242b2d0065SRuslan Mstoi if ! $DOCKER_RUNTIME pull "$CTR_IMAGE"; then 12555b8a218SRob Bradford build_container 12655b8a218SRob Bradford fi 12755b8a218SRob Bradford 12855b8a218SRob Bradford ok_or_die "Error pulling/building container image. Aborting." 12955b8a218SRob Bradford fi 1302fc86ffeSSamuel Ortiz} 1312fc86ffeSSamuel Ortiz 132296ada94SSamuel Ortiz# Fix main directory permissions after a container ran as root. 1330a1d6e1cSSamuel Ortiz# Since the container ran as root, any files it creates will be owned by root. 134296ada94SSamuel Ortiz# This fixes that by recursively changing the ownership of /cloud-hypervisor to the 1350a1d6e1cSSamuel Ortiz# current user. 1360a1d6e1cSSamuel Ortiz# 137296ada94SSamuel Ortizfix_dir_perms() { 1380a1d6e1cSSamuel Ortiz # Yes, running Docker to get elevated privileges, just to chown some files 1390a1d6e1cSSamuel Ortiz # is a dirty hack. 1400a1d6e1cSSamuel Ortiz $DOCKER_RUNTIME run \ 1410a1d6e1cSSamuel Ortiz --workdir "$CTR_CLH_ROOT_DIR" \ 1420a1d6e1cSSamuel Ortiz --rm \ 1430a1d6e1cSSamuel Ortiz --volume /dev:/dev \ 1442b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 1452b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 1460a1d6e1cSSamuel Ortiz "$CTR_IMAGE" \ 147296ada94SSamuel Ortiz chown -R "$(id -u):$(id -g)" "$CTR_CLH_ROOT_DIR" 1480a1d6e1cSSamuel Ortiz 1492805e7b1SRob Bradford return "$1" 1500a1d6e1cSSamuel Ortiz} 1512e1866a5SMuminul Islam# Process exported volumes argument, separate the volumes and make docker compatible 1522e1866a5SMuminul Islam# Sample input: --volumes /a:/a#/b:/b 1532e1866a5SMuminul Islam# Sample output: --volume /a:/a --volume /b:/b 1542e1866a5SMuminul Islam# 1552e1866a5SMuminul Islamprocess_volumes_args() { 1562e1866a5SMuminul Islam if [ -z "$arg_vols" ]; then 1572e1866a5SMuminul Islam return 1582e1866a5SMuminul Islam fi 1592e1866a5SMuminul Islam exported_volumes="" 1602b2d0065SRuslan Mstoi arr_vols=("${arg_vols//#/ }") 1611a5b94eeSRob Bradford for var in "${arr_vols[@]}"; do 1622b2d0065SRuslan Mstoi parts=("${var//:/ }") 1632e1866a5SMuminul Islam if [[ ! -e "${parts[0]}" ]]; then 1642e1866a5SMuminul Islam echo "The volume ${parts[0]} does not exist." 1652e1866a5SMuminul Islam exit 1 1662e1866a5SMuminul Islam fi 1672e1866a5SMuminul Islam exported_volumes="$exported_volumes --volume $var" 1682e1866a5SMuminul Islam done 1692e1866a5SMuminul Islam} 1702e94a86bSRuslan Mstoi 171db6f894eSSamuel Ortizcmd_help() { 172db6f894eSSamuel Ortiz echo "" 1732805e7b1SRob Bradford echo "Cloud Hypervisor $(basename "$0")" 17406a9c3b8SOmer Faruk Bayram echo "Usage: $(basename "$0") [flags] <command> [<command args>]" 17506a9c3b8SOmer Faruk Bayram echo "" 17606a9c3b8SOmer Faruk Bayram echo "Available flags": 17706a9c3b8SOmer Faruk Bayram echo "" 17806a9c3b8SOmer Faruk Bayram echo " --local Set the container image version being used to \"local\"." 179db6f894eSSamuel Ortiz echo "" 180db6f894eSSamuel Ortiz echo "Available commands:" 181db6f894eSSamuel Ortiz echo "" 182ad9374bdSSamuel Ortiz echo " build [--debug|--release] [--libc musl|gnu] [-- [<cargo args>]]" 183db6f894eSSamuel Ortiz echo " Build the Cloud Hypervisor binaries." 184db6f894eSSamuel Ortiz echo " --debug Build the debug binaries. This is the default." 185db6f894eSSamuel Ortiz echo " --release Build the release binaries." 186ad9374bdSSamuel Ortiz echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 1872e1866a5SMuminul Islam echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 18827b5f8d7SMuminul Islam echo " --hypervisor Underlying hypervisor. Options kvm, mshv" 189db6f894eSSamuel Ortiz echo "" 1906eb47bdbSRob Bradford echo " tests [<test type (see below)>] [--libc musl|gnu] [-- [<test scripts args>] [-- [<test binary args>]]] " 191db6f894eSSamuel Ortiz echo " Run the Cloud Hypervisor tests." 192db6f894eSSamuel Ortiz echo " --unit Run the unit tests." 193db6f894eSSamuel Ortiz echo " --integration Run the integration tests." 19456b0c855SSebastien Boeuf echo " --integration-sgx Run the SGX integration tests." 19504dc4968SSebastien Boeuf echo " --integration-vfio Run the VFIO integration tests." 196934f9925SRob Bradford echo " --integration-windows Run the Windows guest integration tests." 197a1a0bc85SBo Chen echo " --integration-live-migration Run the live-migration integration tests." 198ef1983eeSBo Chen echo " --integration-rate-limiter Run the rate-limiter integration tests." 199ad9374bdSSamuel Ortiz echo " --libc Select the C library Cloud Hypervisor will be built against. Default is gnu" 2001cf73c83SBo Chen echo " --metrics Generate performance metrics" 2012e1866a5SMuminul Islam echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 20227b5f8d7SMuminul Islam echo " --hypervisor Underlying hypervisor. Options kvm, mshv" 203db6f894eSSamuel Ortiz echo " --all Run all tests." 204db6f894eSSamuel Ortiz echo "" 205db6f894eSSamuel Ortiz echo " build-container [--type]" 206db6f894eSSamuel Ortiz echo " Build the Cloud Hypervisor container." 207db6f894eSSamuel Ortiz echo "" 208275cb5c9SSamuel Ortiz echo " clean [<cargo args>]]" 209275cb5c9SSamuel Ortiz echo " Remove the Cloud Hypervisor artifacts." 210275cb5c9SSamuel Ortiz echo "" 2115a6b8d63SSamuel Ortiz echo " shell" 2125a6b8d63SSamuel Ortiz echo " Run the development container into an interactive, privileged BASH shell." 213972e96eaSWei Liu echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol" 2145a6b8d63SSamuel Ortiz echo "" 215db6f894eSSamuel Ortiz echo " help" 216db6f894eSSamuel Ortiz echo " Display this help message." 217db6f894eSSamuel Ortiz echo "" 218db6f894eSSamuel Ortiz} 219db6f894eSSamuel Ortiz 220db6f894eSSamuel Ortizcmd_build() { 221db6f894eSSamuel Ortiz build="debug" 222ad9374bdSSamuel Ortiz libc="gnu" 22327b5f8d7SMuminul Islam hypervisor="kvm" 224*3f2ca537SAlexandru Matei features_build=() 225b339aa6bSMuminul Islam exported_device="/dev/kvm" 226db6f894eSSamuel Ortiz while [ $# -gt 0 ]; do 227db6f894eSSamuel Ortiz case "$1" in 2281a5b94eeSRob Bradford "-h" | "--help") { 2291a5b94eeSRob Bradford cmd_help 2301a5b94eeSRob Bradford exit 1 2311a5b94eeSRob Bradford } ;; 232db6f894eSSamuel Ortiz "--debug") { build="debug"; } ;; 233db6f894eSSamuel Ortiz "--release") { build="release"; } ;; 23488ed8524SVincent Batts "--runtime") 23588ed8524SVincent Batts shift 23688ed8524SVincent Batts DOCKER_RUNTIME="$1" 23788ed8524SVincent Batts export DOCKER_RUNTIME 23888ed8524SVincent Batts ;; 239ad9374bdSSamuel Ortiz "--libc") 240ad9374bdSSamuel Ortiz shift 2411a5b94eeSRob Bradford [[ "$1" =~ ^(musl|gnu)$ ]] || 242ad9374bdSSamuel Ortiz die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 243ad9374bdSSamuel Ortiz libc="$1" 244ad9374bdSSamuel Ortiz ;; 2452e1866a5SMuminul Islam "--volumes") 2462e1866a5SMuminul Islam shift 2472e1866a5SMuminul Islam arg_vols="$1" 2482e1866a5SMuminul Islam ;; 24927b5f8d7SMuminul Islam "--hypervisor") 25027b5f8d7SMuminul Islam shift 25127b5f8d7SMuminul Islam hypervisor="$1" 25227b5f8d7SMuminul Islam ;; 2535343e09eSFabiano Fidêncio "--features") 2545343e09eSFabiano Fidêncio shift 255*3f2ca537SAlexandru Matei features_build=(--features "$1") 2565343e09eSFabiano Fidêncio ;; 2571a5b94eeSRob Bradford "--") { 2581a5b94eeSRob Bradford shift 2591a5b94eeSRob Bradford break 2601a5b94eeSRob Bradford } ;; 261db6f894eSSamuel Ortiz *) 262db6f894eSSamuel Ortiz die "Unknown build argument: $1. Please use --help for help." 263db6f894eSSamuel Ortiz ;; 264db6f894eSSamuel Ortiz esac 265db6f894eSSamuel Ortiz shift 266db6f894eSSamuel Ortiz done 267b8342ebeSRob Bradford 268b8342ebeSRob Bradford ensure_build_dir 269b8342ebeSRob Bradford ensure_latest_ctr 270b8342ebeSRob Bradford 2712e1866a5SMuminul Islam process_volumes_args 272ca4857b5SMuminul Islam if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then 273ca4857b5SMuminul Islam die "Hypervisor value must be kvm or mshv" 27427b5f8d7SMuminul Islam fi 275b339aa6bSMuminul Islam if [[ "$hypervisor" = "mshv" ]]; then 276b339aa6bSMuminul Islam exported_device="/dev/mshv" 277b339aa6bSMuminul Islam fi 278ad9374bdSSamuel Ortiz target="$(uname -m)-unknown-linux-${libc}" 279ad9374bdSSamuel Ortiz 280db6f894eSSamuel Ortiz cargo_args=("$@") 281db6f894eSSamuel Ortiz [ $build = "release" ] && cargo_args+=("--release") 282ad9374bdSSamuel Ortiz cargo_args+=(--target "$target") 2830090ec2dSMichael Zhao 2842b2d0065SRuslan Mstoi # shellcheck disable=SC2153 2859d42f48fSWei Liu rustflags="$RUSTFLAGS" 286b0324f85SSebastien Boeuf target_cc="" 2872805e7b1SRob Bradford if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then 28824f384d2SRavi kumar Veeramally rustflags="$rustflags -C link-args=-Wl,-Bstatic -C link-args=-lc" 2890090ec2dSMichael Zhao fi 290db6f894eSSamuel Ortiz 291db6f894eSSamuel Ortiz $DOCKER_RUNTIME run \ 292c8fa8092SSamuel Ortiz --user "$(id -u):$(id -g)" \ 293db6f894eSSamuel Ortiz --workdir "$CTR_CLH_ROOT_DIR" \ 294db6f894eSSamuel Ortiz --rm \ 295b339aa6bSMuminul Islam --volume $exported_device \ 2962b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 2972b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 2980090ec2dSMichael Zhao --env RUSTFLAGS="$rustflags" \ 299b0324f85SSebastien Boeuf --env TARGET_CC="$target_cc" \ 300db6f894eSSamuel Ortiz "$CTR_IMAGE" \ 301*3f2ca537SAlexandru Matei cargo build --all "${features_build[@]}" \ 302db6f894eSSamuel Ortiz --target-dir "$CTR_CLH_CARGO_TARGET" \ 303ad9374bdSSamuel Ortiz "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$target/$build" 304db6f894eSSamuel Ortiz} 305db6f894eSSamuel Ortiz 306275cb5c9SSamuel Ortizcmd_clean() { 307275cb5c9SSamuel Ortiz cargo_args=("$@") 308275cb5c9SSamuel Ortiz 309b8342ebeSRob Bradford ensure_build_dir 310b8342ebeSRob Bradford ensure_latest_ctr 311b8342ebeSRob Bradford 312275cb5c9SSamuel Ortiz $DOCKER_RUNTIME run \ 313c8fa8092SSamuel Ortiz --user "$(id -u):$(id -g)" \ 314275cb5c9SSamuel Ortiz --workdir "$CTR_CLH_ROOT_DIR" \ 315275cb5c9SSamuel Ortiz --rm \ 3162b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 3172b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 318275cb5c9SSamuel Ortiz "$CTR_IMAGE" \ 319275cb5c9SSamuel Ortiz cargo clean \ 320275cb5c9SSamuel Ortiz --target-dir "$CTR_CLH_CARGO_TARGET" \ 321275cb5c9SSamuel Ortiz "${cargo_args[@]}" 322275cb5c9SSamuel Ortiz} 323275cb5c9SSamuel Ortiz 324db6f894eSSamuel Ortizcmd_tests() { 325db6f894eSSamuel Ortiz unit=false 326db6f894eSSamuel Ortiz integration=false 32756b0c855SSebastien Boeuf integration_sgx=false 32804dc4968SSebastien Boeuf integration_vfio=false 329934f9925SRob Bradford integration_windows=false 330a1a0bc85SBo Chen integration_live_migration=false 331ef1983eeSBo Chen integration_rate_limiter=false 3321cf73c83SBo Chen metrics=false 333ad9374bdSSamuel Ortiz libc="gnu" 3342e1866a5SMuminul Islam arg_vols="" 33527b5f8d7SMuminul Islam hypervisor="kvm" 336b339aa6bSMuminul Islam exported_device="/dev/kvm" 337db6f894eSSamuel Ortiz while [ $# -gt 0 ]; do 338db6f894eSSamuel Ortiz case "$1" in 3391a5b94eeSRob Bradford "-h" | "--help") { 3401a5b94eeSRob Bradford cmd_help 3411a5b94eeSRob Bradford exit 1 3421a5b94eeSRob Bradford } ;; 343db6f894eSSamuel Ortiz "--unit") { unit=true; } ;; 344db6f894eSSamuel Ortiz "--integration") { integration=true; } ;; 34556b0c855SSebastien Boeuf "--integration-sgx") { integration_sgx=true; } ;; 34604dc4968SSebastien Boeuf "--integration-vfio") { integration_vfio=true; } ;; 347934f9925SRob Bradford "--integration-windows") { integration_windows=true; } ;; 348a1a0bc85SBo Chen "--integration-live-migration") { integration_live_migration=true; } ;; 349ef1983eeSBo Chen "--integration-rate-limiter") { integration_rate_limiter=true; } ;; 3501cf73c83SBo Chen "--metrics") { metrics=true; } ;; 351ad9374bdSSamuel Ortiz "--libc") 352ad9374bdSSamuel Ortiz shift 3531a5b94eeSRob Bradford [[ "$1" =~ ^(musl|gnu)$ ]] || 354ad9374bdSSamuel Ortiz die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"." 355ad9374bdSSamuel Ortiz libc="$1" 356ad9374bdSSamuel Ortiz ;; 3572e1866a5SMuminul Islam "--volumes") 3582e1866a5SMuminul Islam shift 3592e1866a5SMuminul Islam arg_vols="$1" 3602e1866a5SMuminul Islam ;; 36127b5f8d7SMuminul Islam "--hypervisor") 36227b5f8d7SMuminul Islam shift 36327b5f8d7SMuminul Islam hypervisor="$1" 36427b5f8d7SMuminul Islam ;; 3651a5b94eeSRob Bradford "--all") { 3661a5b94eeSRob Bradford unit=true 3671a5b94eeSRob Bradford integration=true 3681a5b94eeSRob Bradford } ;; 3691a5b94eeSRob Bradford "--") { 3701a5b94eeSRob Bradford shift 3711a5b94eeSRob Bradford break 3721a5b94eeSRob Bradford } ;; 373db6f894eSSamuel Ortiz *) 374db6f894eSSamuel Ortiz die "Unknown tests argument: $1. Please use --help for help." 375db6f894eSSamuel Ortiz ;; 376db6f894eSSamuel Ortiz esac 377db6f894eSSamuel Ortiz shift 378db6f894eSSamuel Ortiz done 379ca4857b5SMuminul Islam if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then 380ca4857b5SMuminul Islam die "Hypervisor value must be kvm or mshv" 38127b5f8d7SMuminul Islam fi 382ca4857b5SMuminul Islam 383b339aa6bSMuminul Islam if [[ "$hypervisor" = "mshv" ]]; then 384b339aa6bSMuminul Islam exported_device="/dev/mshv" 385b339aa6bSMuminul Islam fi 386ca4857b5SMuminul Islam 387e436b382SMuminul Islam if [ ! -e "${exported_device}" ]; then 388e436b382SMuminul Islam die "${exported_device} does not exist on the system" 389e436b382SMuminul Islam fi 390e436b382SMuminul Islam 3914552d07aSBo Chen set -- '--hypervisor' "$hypervisor" "$@" 392d79594aaSMuminul Islam 393b8342ebeSRob Bradford ensure_build_dir 394b8342ebeSRob Bradford ensure_latest_ctr 395b8342ebeSRob Bradford 3962e1866a5SMuminul Islam process_volumes_args 397ad9374bdSSamuel Ortiz target="$(uname -m)-unknown-linux-${libc}" 398ad9374bdSSamuel Ortiz 3998ba5682eSWei Liu rustflags="$RUSTFLAGS" 4008ba5682eSWei Liu target_cc="" 4018ba5682eSWei Liu if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then 40224f384d2SRavi kumar Veeramally rustflags="$rustflags -C link-args=-Wl,-Bstatic -C link-args=-lc" 4038ba5682eSWei Liu fi 4048ba5682eSWei Liu 4059cdcbb51SMuminul Islam if [[ "$unit" = true ]]; then 406ad9374bdSSamuel Ortiz say "Running unit tests for $target..." 407db6f894eSSamuel Ortiz $DOCKER_RUNTIME run \ 408db6f894eSSamuel Ortiz --workdir "$CTR_CLH_ROOT_DIR" \ 409db6f894eSSamuel Ortiz --rm \ 410b339aa6bSMuminul Islam --device $exported_device \ 4117fabca35SSebastien Boeuf --device /dev/net/tun \ 4127fabca35SSebastien Boeuf --cap-add net_admin \ 4132b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 4142b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 415ad9374bdSSamuel Ortiz --env BUILD_TARGET="$target" \ 4168ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 4178ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 418db6f894eSSamuel Ortiz "$CTR_IMAGE" \ 419d79594aaSMuminul Islam ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $? 420db6f894eSSamuel Ortiz fi 421db6f894eSSamuel Ortiz 422db6f894eSSamuel Ortiz if [ "$integration" = true ]; then 423ad9374bdSSamuel Ortiz say "Running integration tests for $target..." 424db6f894eSSamuel Ortiz $DOCKER_RUNTIME run \ 425db6f894eSSamuel Ortiz --workdir "$CTR_CLH_ROOT_DIR" \ 426db6f894eSSamuel Ortiz --rm \ 427db6f894eSSamuel Ortiz --privileged \ 428f21cd31bSSamuel Ortiz --security-opt seccomp=unconfined \ 429f21cd31bSSamuel Ortiz --ipc=host \ 430cf1b5156SMichael Zhao --net="$CTR_CLH_NET" \ 431a5b053f8SSamuel Ortiz --mount type=tmpfs,destination=/tmp \ 432db6f894eSSamuel Ortiz --volume /dev:/dev \ 4332b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 4342b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 435db6f894eSSamuel Ortiz --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 436f21cd31bSSamuel Ortiz --env USER="root" \ 437c7e51e51SWei Liu --env BUILD_TARGET="$target" \ 4388ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 4398ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 44099a25510SRob Bradford --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 441db6f894eSSamuel Ortiz "$CTR_IMAGE" \ 442a7aecb5eSOmer Faruk Bayram dbus-run-session ./scripts/run_integration_tests_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $? 443db6f894eSSamuel Ortiz fi 4440a1d6e1cSSamuel Ortiz 44556b0c855SSebastien Boeuf if [ "$integration_sgx" = true ]; then 44657f81d03SWei Liu say "Running SGX integration tests for $target..." 44756b0c855SSebastien Boeuf $DOCKER_RUNTIME run \ 44856b0c855SSebastien Boeuf --workdir "$CTR_CLH_ROOT_DIR" \ 44956b0c855SSebastien Boeuf --rm \ 45056b0c855SSebastien Boeuf --privileged \ 45156b0c855SSebastien Boeuf --security-opt seccomp=unconfined \ 45256b0c855SSebastien Boeuf --ipc=host \ 45356b0c855SSebastien Boeuf --net="$CTR_CLH_NET" \ 45456b0c855SSebastien Boeuf --mount type=tmpfs,destination=/tmp \ 45556b0c855SSebastien Boeuf --volume /dev:/dev \ 4562b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 4572b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 45856b0c855SSebastien Boeuf --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 45956b0c855SSebastien Boeuf --env USER="root" \ 460c7e51e51SWei Liu --env BUILD_TARGET="$target" \ 4618ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 4628ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 46399a25510SRob Bradford --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 46456b0c855SSebastien Boeuf "$CTR_IMAGE" \ 465d79594aaSMuminul Islam ./scripts/run_integration_tests_sgx.sh "$@" || fix_dir_perms $? || exit $? 46656b0c855SSebastien Boeuf fi 46756b0c855SSebastien Boeuf 46804dc4968SSebastien Boeuf if [ "$integration_vfio" = true ]; then 46904dc4968SSebastien Boeuf say "Running VFIO integration tests for $target..." 47004dc4968SSebastien Boeuf $DOCKER_RUNTIME run \ 47104dc4968SSebastien Boeuf --workdir "$CTR_CLH_ROOT_DIR" \ 47204dc4968SSebastien Boeuf --rm \ 47304dc4968SSebastien Boeuf --privileged \ 47404dc4968SSebastien Boeuf --security-opt seccomp=unconfined \ 47504dc4968SSebastien Boeuf --ipc=host \ 47604dc4968SSebastien Boeuf --net="$CTR_CLH_NET" \ 47704dc4968SSebastien Boeuf --mount type=tmpfs,destination=/tmp \ 47804dc4968SSebastien Boeuf --volume /dev:/dev \ 4792b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 4802b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 48104dc4968SSebastien Boeuf --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 48204dc4968SSebastien Boeuf --env USER="root" \ 483c7e51e51SWei Liu --env BUILD_TARGET="$target" \ 4848ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 4858ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 48699a25510SRob Bradford --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 48704dc4968SSebastien Boeuf "$CTR_IMAGE" \ 48804dc4968SSebastien Boeuf ./scripts/run_integration_tests_vfio.sh "$@" || fix_dir_perms $? || exit $? 48904dc4968SSebastien Boeuf fi 49004dc4968SSebastien Boeuf 491934f9925SRob Bradford if [ "$integration_windows" = true ]; then 49257f81d03SWei Liu say "Running Windows integration tests for $target..." 493934f9925SRob Bradford $DOCKER_RUNTIME run \ 494934f9925SRob Bradford --workdir "$CTR_CLH_ROOT_DIR" \ 495934f9925SRob Bradford --rm \ 496934f9925SRob Bradford --privileged \ 497934f9925SRob Bradford --security-opt seccomp=unconfined \ 498934f9925SRob Bradford --ipc=host \ 499934f9925SRob Bradford --net="$CTR_CLH_NET" \ 500934f9925SRob Bradford --mount type=tmpfs,destination=/tmp \ 501934f9925SRob Bradford --volume /dev:/dev \ 5022b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 5032b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 504934f9925SRob Bradford --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 505934f9925SRob Bradford --env USER="root" \ 506c7e51e51SWei Liu --env BUILD_TARGET="$target" \ 5078ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 5088ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 50999a25510SRob Bradford --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 510934f9925SRob Bradford "$CTR_IMAGE" \ 511944d0920SAnatol Belski ./scripts/run_integration_tests_windows_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $? 512934f9925SRob Bradford fi 513a1a0bc85SBo Chen 514a1a0bc85SBo Chen if [ "$integration_live_migration" = true ]; then 515a1a0bc85SBo Chen say "Running 'live migration' integration tests for $target..." 516a1a0bc85SBo Chen $DOCKER_RUNTIME run \ 517a1a0bc85SBo Chen --workdir "$CTR_CLH_ROOT_DIR" \ 518a1a0bc85SBo Chen --rm \ 519a1a0bc85SBo Chen --privileged \ 520a1a0bc85SBo Chen --security-opt seccomp=unconfined \ 521a1a0bc85SBo Chen --ipc=host \ 522a1a0bc85SBo Chen --net="$CTR_CLH_NET" \ 523a1a0bc85SBo Chen --mount type=tmpfs,destination=/tmp \ 524a1a0bc85SBo Chen --volume /dev:/dev \ 5252b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 5262b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 527a1a0bc85SBo Chen --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 528a1a0bc85SBo Chen --env USER="root" \ 529c7e51e51SWei Liu --env BUILD_TARGET="$target" \ 5308ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 5318ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 53299a25510SRob Bradford --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 533a1a0bc85SBo Chen "$CTR_IMAGE" \ 534a1a0bc85SBo Chen ./scripts/run_integration_tests_live_migration.sh "$@" || fix_dir_perms $? || exit $? 535a1a0bc85SBo Chen fi 5361cf73c83SBo Chen 537ef1983eeSBo Chen if [ "$integration_rate_limiter" = true ]; then 538ef1983eeSBo Chen say "Running 'rate limiter' integration tests for $target..." 539ef1983eeSBo Chen $DOCKER_RUNTIME run \ 540ef1983eeSBo Chen --workdir "$CTR_CLH_ROOT_DIR" \ 541ef1983eeSBo Chen --rm \ 542ef1983eeSBo Chen --privileged \ 543ef1983eeSBo Chen --security-opt seccomp=unconfined \ 544ef1983eeSBo Chen --ipc=host \ 545ef1983eeSBo Chen --net="$CTR_CLH_NET" \ 546ef1983eeSBo Chen --mount type=tmpfs,destination=/tmp \ 547ef1983eeSBo Chen --volume /dev:/dev \ 5482b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 5492b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 550ef1983eeSBo Chen --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 551ef1983eeSBo Chen --env USER="root" \ 552c7e51e51SWei Liu --env BUILD_TARGET="$target" \ 5538ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 5548ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 55599a25510SRob Bradford --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 556ef1983eeSBo Chen "$CTR_IMAGE" \ 557ef1983eeSBo Chen ./scripts/run_integration_tests_rate_limiter.sh "$@" || fix_dir_perms $? || exit $? 558ef1983eeSBo Chen fi 559ef1983eeSBo Chen 5601cf73c83SBo Chen if [ "$metrics" = true ]; then 5611cf73c83SBo Chen say "Generating performance metrics for $target..." 5621cf73c83SBo Chen $DOCKER_RUNTIME run \ 5631cf73c83SBo Chen --workdir "$CTR_CLH_ROOT_DIR" \ 5641cf73c83SBo Chen --rm \ 5651cf73c83SBo Chen --privileged \ 5661cf73c83SBo Chen --security-opt seccomp=unconfined \ 5671cf73c83SBo Chen --ipc=host \ 5681cf73c83SBo Chen --net="$CTR_CLH_NET" \ 5691cf73c83SBo Chen --mount type=tmpfs,destination=/tmp \ 5701cf73c83SBo Chen --volume /dev:/dev \ 5712b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 5722b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 5731cf73c83SBo Chen --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 5741cf73c83SBo Chen --env USER="root" \ 575c7e51e51SWei Liu --env BUILD_TARGET="$target" \ 5768ba5682eSWei Liu --env RUSTFLAGS="$rustflags" \ 5778ba5682eSWei Liu --env TARGET_CC="$target_cc" \ 578b56da6eeSsmit-gardhariya --env RUST_BACKTRACE="${RUST_BACKTRACE}" \ 57999a25510SRob Bradford --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \ 5801cf73c83SBo Chen "$CTR_IMAGE" \ 5811cf73c83SBo Chen ./scripts/run_metrics.sh "$@" || fix_dir_perms $? || exit $? 5821cf73c83SBo Chen fi 5831cf73c83SBo Chen 584296ada94SSamuel Ortiz fix_dir_perms $? 585db6f894eSSamuel Ortiz} 586db6f894eSSamuel Ortiz 58755b8a218SRob Bradfordbuild_container() { 588b8342ebeSRob Bradford ensure_build_dir 589b8342ebeSRob Bradford 590db6f894eSSamuel Ortiz BUILD_DIR=/tmp/cloud-hypervisor/container/ 591db6f894eSSamuel Ortiz 592db6f894eSSamuel Ortiz mkdir -p $BUILD_DIR 5932805e7b1SRob Bradford cp "$CLH_DOCKERFILE" $BUILD_DIR 594db6f894eSSamuel Ortiz 5952805e7b1SRob Bradford [ "$(uname -m)" = "aarch64" ] && TARGETARCH="arm64" 5962805e7b1SRob Bradford [ "$(uname -m)" = "x86_64" ] && TARGETARCH="amd64" 5978bf0bac5SHenry Wang 598db6f894eSSamuel Ortiz $DOCKER_RUNTIME build \ 59955b8a218SRob Bradford --target dev \ 6002b2d0065SRuslan Mstoi -t "$CTR_IMAGE" \ 601db6f894eSSamuel Ortiz -f $BUILD_DIR/Dockerfile \ 6022b2d0065SRuslan Mstoi --build-arg TARGETARCH="$TARGETARCH" \ 603db6f894eSSamuel Ortiz $BUILD_DIR 604db6f894eSSamuel Ortiz} 605db6f894eSSamuel Ortiz 60655b8a218SRob Bradfordcmd_build-container() { 60755b8a218SRob Bradford while [ $# -gt 0 ]; do 60855b8a218SRob Bradford case "$1" in 60955b8a218SRob Bradford "-h" | "--help") { 61055b8a218SRob Bradford cmd_help 61155b8a218SRob Bradford exit 1 61255b8a218SRob Bradford } ;; 61355b8a218SRob Bradford "--") { 61455b8a218SRob Bradford shift 61555b8a218SRob Bradford break 61655b8a218SRob Bradford } ;; 61755b8a218SRob Bradford *) 61855b8a218SRob Bradford die "Unknown build-container argument: $1. Please use --help for help." 61955b8a218SRob Bradford ;; 62055b8a218SRob Bradford esac 62155b8a218SRob Bradford shift 62255b8a218SRob Bradford done 62355b8a218SRob Bradford 62455b8a218SRob Bradford build_container 62555b8a218SRob Bradford} 62655b8a218SRob Bradford 6275a6b8d63SSamuel Ortizcmd_shell() { 628972e96eaSWei Liu while [ $# -gt 0 ]; do 629972e96eaSWei Liu case "$1" in 6301a5b94eeSRob Bradford "-h" | "--help") { 6311a5b94eeSRob Bradford cmd_help 6321a5b94eeSRob Bradford exit 1 6331a5b94eeSRob Bradford } ;; 634972e96eaSWei Liu "--volumes") 635972e96eaSWei Liu shift 636972e96eaSWei Liu arg_vols="$1" 637972e96eaSWei Liu ;; 6381a5b94eeSRob Bradford "--") { 6391a5b94eeSRob Bradford shift 6401a5b94eeSRob Bradford break 6411a5b94eeSRob Bradford } ;; 6421a5b94eeSRob Bradford *) ;; 6431a5b94eeSRob Bradford 644972e96eaSWei Liu esac 645972e96eaSWei Liu shift 646972e96eaSWei Liu done 647b8342ebeSRob Bradford ensure_build_dir 648b8342ebeSRob Bradford ensure_latest_ctr 649972e96eaSWei Liu process_volumes_args 6505a6b8d63SSamuel Ortiz say_warn "Starting a privileged shell prompt as root ..." 6515a6b8d63SSamuel Ortiz say_warn "WARNING: Your $CLH_ROOT_DIR folder will be bind-mounted in the container under $CTR_CLH_ROOT_DIR" 6525a6b8d63SSamuel Ortiz $DOCKER_RUNTIME run \ 6535a6b8d63SSamuel Ortiz -ti \ 6545a6b8d63SSamuel Ortiz --workdir "$CTR_CLH_ROOT_DIR" \ 6555a6b8d63SSamuel Ortiz --rm \ 6565a6b8d63SSamuel Ortiz --privileged \ 6575a6b8d63SSamuel Ortiz --security-opt seccomp=unconfined \ 6585a6b8d63SSamuel Ortiz --ipc=host \ 659cf1b5156SMichael Zhao --net="$CTR_CLH_NET" \ 6605a6b8d63SSamuel Ortiz --tmpfs /tmp:exec \ 6615a6b8d63SSamuel Ortiz --volume /dev:/dev \ 6622b2d0065SRuslan Mstoi --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 6632b2d0065SRuslan Mstoi ${exported_volumes:+"$exported_volumes"} \ 6645a6b8d63SSamuel Ortiz --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 6655a6b8d63SSamuel Ortiz --env USER="root" \ 6665a6b8d63SSamuel Ortiz --entrypoint bash \ 6675a6b8d63SSamuel Ortiz "$CTR_IMAGE" 6685a6b8d63SSamuel Ortiz 6695a6b8d63SSamuel Ortiz fix_dir_perms $? 6705a6b8d63SSamuel Ortiz} 6715a6b8d63SSamuel Ortiz 6722e94a86bSRuslan Mstoiif [ $# = 0 ]; then 6732e94a86bSRuslan Mstoi cmd_help 6742e94a86bSRuslan Mstoi say_err "Please specify command to run!" 6752e94a86bSRuslan Mstoi exit 1 6762e94a86bSRuslan Mstoifi 6772e94a86bSRuslan Mstoi 678db6f894eSSamuel Ortiz# Parse main command line args. 679db6f894eSSamuel Ortiz# 680db6f894eSSamuel Ortizwhile [ $# -gt 0 ]; do 681db6f894eSSamuel Ortiz case "$1" in 6821a5b94eeSRob Bradford -h | --help) { 6831a5b94eeSRob Bradford cmd_help 6841a5b94eeSRob Bradford exit 1 6851a5b94eeSRob Bradford } ;; 68655b8a218SRob Bradford --local) { 68755b8a218SRob Bradford CTR_IMAGE_VERSION="local" 68855b8a218SRob Bradford CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}" 68955b8a218SRob Bradford } ;; 690db6f894eSSamuel Ortiz -*) 691db6f894eSSamuel Ortiz die "Unknown arg: $1. Please use \`$0 help\` for help." 692db6f894eSSamuel Ortiz ;; 693db6f894eSSamuel Ortiz *) 694db6f894eSSamuel Ortiz break 695db6f894eSSamuel Ortiz ;; 696db6f894eSSamuel Ortiz esac 697db6f894eSSamuel Ortiz shift 698db6f894eSSamuel Ortizdone 699db6f894eSSamuel Ortiz 700db6f894eSSamuel Ortiz# $1 is now a command name. Check if it is a valid command and, if so, 701db6f894eSSamuel Ortiz# run it. 702db6f894eSSamuel Ortiz# 703db6f894eSSamuel Ortizdeclare -f "cmd_$1" >/dev/null 704db6f894eSSamuel Ortizok_or_die "Unknown command: $1. Please use \`$0 help\` for help." 705db6f894eSSamuel Ortiz 706db6f894eSSamuel Ortizcmd=cmd_$1 707db6f894eSSamuel Ortizshift 708db6f894eSSamuel Ortiz 709db6f894eSSamuel Ortiz$cmd "$@" 710