xref: /cloud-hypervisor/scripts/dev_cli.sh (revision 226ecf47bb608d52367de61236fb8ad37b871ca2)
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"
10c4ad9b45SRob Bradford
11c4ad9b45SRob Bradford# Needs to match explicit version in docker-image.yaml workflow
12*226ecf47SRuoqing HeCTR_IMAGE_VERSION="20250412-0"
1370cfd1beSRuslan Mstoi: "${CTR_IMAGE:=${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}}"
14db6f894eSSamuel Ortiz
15db6f894eSSamuel OrtizDOCKER_RUNTIME="docker"
16db6f894eSSamuel Ortiz
17db6f894eSSamuel Ortiz# Host paths
18db6f894eSSamuel OrtizCLH_SCRIPTS_DIR=$(cd "$(dirname "$0")" && pwd)
19db6f894eSSamuel OrtizCLH_ROOT_DIR=$(cd "${CLH_SCRIPTS_DIR}/.." && pwd)
20db6f894eSSamuel OrtizCLH_BUILD_DIR="${CLH_ROOT_DIR}/build"
21db6f894eSSamuel OrtizCLH_CARGO_TARGET="${CLH_BUILD_DIR}/cargo_target"
22db6f894eSSamuel OrtizCLH_DOCKERFILE="${CLH_SCRIPTS_DIR}/../resources/Dockerfile"
23db6f894eSSamuel OrtizCLH_CTR_BUILD_DIR="/tmp/cloud-hypervisor/ctr-build"
24db6f894eSSamuel OrtizCLH_INTEGRATION_WORKLOADS="${HOME}/workloads"
25db6f894eSSamuel Ortiz
26db6f894eSSamuel Ortiz# Container paths
27db6f894eSSamuel OrtizCTR_CLH_ROOT_DIR="/cloud-hypervisor"
280a1d6e1cSSamuel OrtizCTR_CLH_CARGO_BUILT_DIR="${CTR_CLH_ROOT_DIR}/build"
290a1d6e1cSSamuel OrtizCTR_CLH_CARGO_TARGET="${CTR_CLH_CARGO_BUILT_DIR}/cargo_target"
30db6f894eSSamuel OrtizCTR_CLH_INTEGRATION_WORKLOADS="/root/workloads"
31db6f894eSSamuel Ortiz
32cf1b5156SMichael Zhao# Container networking option
337889fc92SSebastien BoeufCTR_CLH_NET="bridge"
34cf1b5156SMichael Zhao
35db6f894eSSamuel Ortiz# Cargo paths
36db6f894eSSamuel Ortiz# Full path to the cargo registry dir on the host. This appears on the host
37db6f894eSSamuel Ortiz# because we want to persist the cargo registry across container invocations.
38db6f894eSSamuel Ortiz# Otherwise, any rust crates from crates.io would be downloaded again each time
39db6f894eSSamuel Ortiz# we build or test.
40db6f894eSSamuel OrtizCARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry"
41db6f894eSSamuel Ortiz
42db6f894eSSamuel Ortiz# Full path to the cargo git registry on the host. This serves the same purpose
43db6f894eSSamuel Ortiz# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of
44db6f894eSSamuel Ortiz# crates.io.
45db6f894eSSamuel OrtizCARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry"
46db6f894eSSamuel Ortiz
47db6f894eSSamuel Ortiz# Full path to the cargo target dir on the host.
48db6f894eSSamuel OrtizCARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target"
49db6f894eSSamuel Ortiz
50db6f894eSSamuel Ortiz# Send a decorated message to stdout, followed by a new line
51db6f894eSSamuel Ortiz#
52db6f894eSSamuel Ortizsay() {
531a5b94eeSRob Bradford    [ -t 1 ] && [ -n "$TERM" ] &&
541a5b94eeSRob Bradford        echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" ||
551a5b94eeSRob Bradford        echo "[$CLI_NAME] $*"
56db6f894eSSamuel Ortiz}
57db6f894eSSamuel Ortiz
58db6f894eSSamuel Ortiz# Send a decorated message to stdout, without a trailing new line
59db6f894eSSamuel Ortiz#
60db6f894eSSamuel Ortizsay_noln() {
611a5b94eeSRob Bradford    [ -t 1 ] && [ -n "$TERM" ] &&
621a5b94eeSRob Bradford        echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" ||
631a5b94eeSRob Bradford        echo "[$CLI_NAME] $*"
64db6f894eSSamuel Ortiz}
65db6f894eSSamuel Ortiz
66db6f894eSSamuel Ortiz# Send a text message to stderr
67db6f894eSSamuel Ortiz#
68db6f894eSSamuel Ortizsay_err() {
691a5b94eeSRob Bradford    [ -t 2 ] && [ -n "$TERM" ] &&
701a5b94eeSRob Bradford        echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 ||
711a5b94eeSRob Bradford        echo "[$CLI_NAME] $*" 1>&2
72db6f894eSSamuel Ortiz}
73db6f894eSSamuel Ortiz
74db6f894eSSamuel Ortiz# Send a warning-highlighted text to stdout
75db6f894eSSamuel Ortizsay_warn() {
761a5b94eeSRob Bradford    [ -t 1 ] && [ -n "$TERM" ] &&
771a5b94eeSRob Bradford        echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" ||
781a5b94eeSRob Bradford        echo "[$CLI_NAME] $*"
79db6f894eSSamuel Ortiz}
80db6f894eSSamuel Ortiz
81db6f894eSSamuel Ortiz# Exit with an error message and (optional) code
82db6f894eSSamuel Ortiz# Usage: die [-c <error code>] <error message>
83db6f894eSSamuel Ortiz#
84db6f894eSSamuel Ortizdie() {
85db6f894eSSamuel Ortiz    code=1
86db6f894eSSamuel Ortiz    [[ "$1" = "-c" ]] && {
87db6f894eSSamuel Ortiz        code="$2"
88db6f894eSSamuel Ortiz        shift 2
89db6f894eSSamuel Ortiz    }
90db6f894eSSamuel Ortiz    say_err "$@"
912805e7b1SRob Bradford    exit "$code"
92db6f894eSSamuel Ortiz}
93db6f894eSSamuel Ortiz
94db6f894eSSamuel Ortiz# Exit with an error message if the last exit code is not 0
95db6f894eSSamuel Ortiz#
96db6f894eSSamuel Ortizok_or_die() {
97db6f894eSSamuel Ortiz    code=$?
98db6f894eSSamuel Ortiz    [[ $code -eq 0 ]] || die -c $code "$@"
99db6f894eSSamuel Ortiz}
100db6f894eSSamuel Ortiz
101db6f894eSSamuel Ortiz# Make sure the build/ dirs are available. Exit if we can't create them.
102db6f894eSSamuel Ortiz# Upon returning from this call, the caller can be certain the build/ dirs exist.
103db6f894eSSamuel Ortiz#
104db6f894eSSamuel Ortizensure_build_dir() {
105db6f894eSSamuel Ortiz    for dir in "$CLH_BUILD_DIR" \
106db6f894eSSamuel Ortiz        "$CLH_INTEGRATION_WORKLOADS" \
107db6f894eSSamuel Ortiz        "$CLH_CTR_BUILD_DIR" \
108db6f894eSSamuel Ortiz        "$CARGO_TARGET_DIR" \
109db6f894eSSamuel Ortiz        "$CARGO_REGISTRY_DIR" \
110db6f894eSSamuel Ortiz        "$CARGO_GIT_REGISTRY_DIR"; do
111db6f894eSSamuel Ortiz        mkdir -p "$dir" || die "Error: cannot create dir $dir"
1121a5b94eeSRob Bradford        [ -x "$dir" ] && [ -w "$dir" ] ||
113db6f894eSSamuel Ortiz            {
114db6f894eSSamuel Ortiz                say "Wrong permissions for $dir. Attempting to fix them ..."
115db6f894eSSamuel Ortiz                chmod +x+w "$dir"
1161a5b94eeSRob Bradford            } ||
117db6f894eSSamuel Ortiz            die "Error: wrong permissions for $dir. Should be +x+w"
118db6f894eSSamuel Ortiz    done
119db6f894eSSamuel Ortiz}
120db6f894eSSamuel Ortiz
1212fc86ffeSSamuel Ortiz# Make sure we're using the latest dev container, by just pulling it.
1222fc86ffeSSamuel Ortizensure_latest_ctr() {
12355b8a218SRob Bradford    if [ "$CTR_IMAGE_VERSION" = "local" ]; then
12455b8a218SRob Bradford        build_container
12555b8a218SRob Bradford    else
1262b2d0065SRuslan Mstoi        if ! $DOCKER_RUNTIME pull "$CTR_IMAGE"; then
12755b8a218SRob Bradford            build_container
12855b8a218SRob Bradford        fi
12955b8a218SRob Bradford
13055b8a218SRob Bradford        ok_or_die "Error pulling/building container image. Aborting."
13155b8a218SRob Bradford    fi
1322fc86ffeSSamuel Ortiz}
1332fc86ffeSSamuel Ortiz
134296ada94SSamuel Ortiz# Fix main directory permissions after a container ran as root.
1350a1d6e1cSSamuel Ortiz# Since the container ran as root, any files it creates will be owned by root.
136296ada94SSamuel Ortiz# This fixes that by recursively changing the ownership of /cloud-hypervisor to the
1370a1d6e1cSSamuel Ortiz# current user.
1380a1d6e1cSSamuel Ortiz#
139296ada94SSamuel Ortizfix_dir_perms() {
1400a1d6e1cSSamuel Ortiz    # Yes, running Docker to get elevated privileges, just to chown some files
1410a1d6e1cSSamuel Ortiz    # is a dirty hack.
1420a1d6e1cSSamuel Ortiz    $DOCKER_RUNTIME run \
1430a1d6e1cSSamuel Ortiz        --workdir "$CTR_CLH_ROOT_DIR" \
1440a1d6e1cSSamuel Ortiz        --rm \
1450a1d6e1cSSamuel Ortiz        --volume /dev:/dev \
1462b2d0065SRuslan Mstoi        --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
1472b2d0065SRuslan Mstoi        ${exported_volumes:+"$exported_volumes"} \
1480a1d6e1cSSamuel Ortiz        "$CTR_IMAGE" \
149296ada94SSamuel Ortiz        chown -R "$(id -u):$(id -g)" "$CTR_CLH_ROOT_DIR"
1500a1d6e1cSSamuel Ortiz
1512805e7b1SRob Bradford    return "$1"
1520a1d6e1cSSamuel Ortiz}
1532e1866a5SMuminul Islam# Process exported volumes argument, separate the volumes and make docker compatible
1542e1866a5SMuminul Islam# Sample input: --volumes /a:/a#/b:/b
1552e1866a5SMuminul Islam# Sample output: --volume /a:/a --volume /b:/b
1562e1866a5SMuminul Islam#
1572e1866a5SMuminul Islamprocess_volumes_args() {
1582e1866a5SMuminul Islam    if [ -z "$arg_vols" ]; then
1592e1866a5SMuminul Islam        return
1602e1866a5SMuminul Islam    fi
1612e1866a5SMuminul Islam    exported_volumes=""
1622b2d0065SRuslan Mstoi    arr_vols=("${arg_vols//#/ }")
1631a5b94eeSRob Bradford    for var in "${arr_vols[@]}"; do
1642b2d0065SRuslan Mstoi        parts=("${var//:/ }")
1652e1866a5SMuminul Islam        if [[ ! -e "${parts[0]}" ]]; then
1662e1866a5SMuminul Islam            echo "The volume ${parts[0]} does not exist."
1672e1866a5SMuminul Islam            exit 1
1682e1866a5SMuminul Islam        fi
1692e1866a5SMuminul Islam        exported_volumes="$exported_volumes --volume $var"
1702e1866a5SMuminul Islam    done
1712e1866a5SMuminul Islam}
1722e94a86bSRuslan Mstoi
173db6f894eSSamuel Ortizcmd_help() {
174db6f894eSSamuel Ortiz    echo ""
1752805e7b1SRob Bradford    echo "Cloud Hypervisor $(basename "$0")"
17606a9c3b8SOmer Faruk Bayram    echo "Usage: $(basename "$0") [flags] <command> [<command args>]"
17706a9c3b8SOmer Faruk Bayram    echo ""
17806a9c3b8SOmer Faruk Bayram    echo "Available flags":
17906a9c3b8SOmer Faruk Bayram    echo ""
18006a9c3b8SOmer Faruk Bayram    echo "    --local        Set the container image version being used to \"local\"."
181db6f894eSSamuel Ortiz    echo ""
182db6f894eSSamuel Ortiz    echo "Available commands:"
183db6f894eSSamuel Ortiz    echo ""
184ad9374bdSSamuel Ortiz    echo "    build [--debug|--release] [--libc musl|gnu] [-- [<cargo args>]]"
185db6f894eSSamuel Ortiz    echo "        Build the Cloud Hypervisor binaries."
186db6f894eSSamuel Ortiz    echo "        --debug               Build the debug binaries. This is the default."
187db6f894eSSamuel Ortiz    echo "        --release             Build the release binaries."
188ad9374bdSSamuel Ortiz    echo "        --libc                Select the C library Cloud Hypervisor will be built against. Default is gnu"
1892e1866a5SMuminul Islam    echo "        --volumes             Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol"
19027b5f8d7SMuminul Islam    echo "        --hypervisor          Underlying hypervisor. Options kvm, mshv"
191db6f894eSSamuel Ortiz    echo ""
1926eb47bdbSRob Bradford    echo "    tests [<test type (see below)>] [--libc musl|gnu] [-- [<test scripts args>] [-- [<test binary args>]]] "
193db6f894eSSamuel Ortiz    echo "        Run the Cloud Hypervisor tests."
194db6f894eSSamuel Ortiz    echo "        --unit                       Run the unit tests."
195db6f894eSSamuel Ortiz    echo "        --integration                Run the integration tests."
19656b0c855SSebastien Boeuf    echo "        --integration-sgx            Run the SGX integration tests."
19704dc4968SSebastien Boeuf    echo "        --integration-vfio           Run the VFIO integration tests."
198934f9925SRob Bradford    echo "        --integration-windows        Run the Windows guest integration tests."
199a1a0bc85SBo Chen    echo "        --integration-live-migration Run the live-migration integration tests."
200ef1983eeSBo Chen    echo "        --integration-rate-limiter   Run the rate-limiter integration tests."
201ad9374bdSSamuel Ortiz    echo "        --libc                       Select the C library Cloud Hypervisor will be built against. Default is gnu"
2021cf73c83SBo Chen    echo "        --metrics                    Generate performance metrics"
2039f028394SSongqian Li    echo "        --coverage                   Generate code coverage information"
2042e1866a5SMuminul Islam    echo "        --volumes                    Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol"
20527b5f8d7SMuminul Islam    echo "        --hypervisor                 Underlying hypervisor. Options kvm, mshv"
206db6f894eSSamuel Ortiz    echo "        --all                        Run all tests."
207db6f894eSSamuel Ortiz    echo ""
208db6f894eSSamuel Ortiz    echo "    build-container [--type]"
209db6f894eSSamuel Ortiz    echo "        Build the Cloud Hypervisor container."
210db6f894eSSamuel Ortiz    echo ""
211275cb5c9SSamuel Ortiz    echo "    clean [<cargo args>]]"
212275cb5c9SSamuel Ortiz    echo "        Remove the Cloud Hypervisor artifacts."
213275cb5c9SSamuel Ortiz    echo ""
2145a6b8d63SSamuel Ortiz    echo "    shell"
2155a6b8d63SSamuel Ortiz    echo "        Run the development container into an interactive, privileged BASH shell."
216972e96eaSWei Liu    echo "        --volumes             Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol"
2175a6b8d63SSamuel Ortiz    echo ""
218db6f894eSSamuel Ortiz    echo "    help"
219db6f894eSSamuel Ortiz    echo "        Display this help message."
220db6f894eSSamuel Ortiz    echo ""
221db6f894eSSamuel Ortiz}
222db6f894eSSamuel Ortiz
223db6f894eSSamuel Ortizcmd_build() {
224db6f894eSSamuel Ortiz    build="debug"
225ad9374bdSSamuel Ortiz    libc="gnu"
22627b5f8d7SMuminul Islam    hypervisor="kvm"
2273f2ca537SAlexandru Matei    features_build=()
228b339aa6bSMuminul Islam    exported_device="/dev/kvm"
229db6f894eSSamuel Ortiz    while [ $# -gt 0 ]; do
230db6f894eSSamuel Ortiz        case "$1" in
2311a5b94eeSRob Bradford        "-h" | "--help") {
2321a5b94eeSRob Bradford            cmd_help
2331a5b94eeSRob Bradford            exit 1
2341a5b94eeSRob Bradford        } ;;
235db6f894eSSamuel Ortiz        "--debug") { build="debug"; } ;;
236db6f894eSSamuel Ortiz        "--release") { build="release"; } ;;
23788ed8524SVincent Batts        "--runtime")
23888ed8524SVincent Batts            shift
23988ed8524SVincent Batts            DOCKER_RUNTIME="$1"
24088ed8524SVincent Batts            export DOCKER_RUNTIME
24188ed8524SVincent Batts            ;;
242ad9374bdSSamuel Ortiz        "--libc")
243ad9374bdSSamuel Ortiz            shift
2441a5b94eeSRob Bradford            [[ "$1" =~ ^(musl|gnu)$ ]] ||
245ad9374bdSSamuel Ortiz                die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"."
246ad9374bdSSamuel Ortiz            libc="$1"
247ad9374bdSSamuel Ortiz            ;;
2482e1866a5SMuminul Islam        "--volumes")
2492e1866a5SMuminul Islam            shift
2502e1866a5SMuminul Islam            arg_vols="$1"
2512e1866a5SMuminul Islam            ;;
25227b5f8d7SMuminul Islam        "--hypervisor")
25327b5f8d7SMuminul Islam            shift
25427b5f8d7SMuminul Islam            hypervisor="$1"
25527b5f8d7SMuminul Islam            ;;
2565343e09eSFabiano Fidêncio        "--features")
2575343e09eSFabiano Fidêncio            shift
2583f2ca537SAlexandru Matei            features_build=(--features "$1")
2595343e09eSFabiano Fidêncio            ;;
2601a5b94eeSRob Bradford        "--") {
2611a5b94eeSRob Bradford            shift
2621a5b94eeSRob Bradford            break
2631a5b94eeSRob Bradford        } ;;
264db6f894eSSamuel Ortiz        *)
265db6f894eSSamuel Ortiz            die "Unknown build argument: $1. Please use --help for help."
266db6f894eSSamuel Ortiz            ;;
267db6f894eSSamuel Ortiz        esac
268db6f894eSSamuel Ortiz        shift
269db6f894eSSamuel Ortiz    done
270b8342ebeSRob Bradford
271b8342ebeSRob Bradford    ensure_build_dir
272b8342ebeSRob Bradford    ensure_latest_ctr
273b8342ebeSRob Bradford
2742e1866a5SMuminul Islam    process_volumes_args
275ca4857b5SMuminul Islam    if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
276ca4857b5SMuminul Islam        die "Hypervisor value must be kvm or mshv"
27727b5f8d7SMuminul Islam    fi
278b339aa6bSMuminul Islam    if [[ "$hypervisor" = "mshv" ]]; then
279b339aa6bSMuminul Islam        exported_device="/dev/mshv"
280b339aa6bSMuminul Islam    fi
281ad9374bdSSamuel Ortiz    target="$(uname -m)-unknown-linux-${libc}"
282ad9374bdSSamuel Ortiz
283db6f894eSSamuel Ortiz    cargo_args=("$@")
284db6f894eSSamuel Ortiz    [ $build = "release" ] && cargo_args+=("--release")
285ad9374bdSSamuel Ortiz    cargo_args+=(--target "$target")
2860090ec2dSMichael Zhao
2872b2d0065SRuslan Mstoi    # shellcheck disable=SC2153
2889d42f48fSWei Liu    rustflags="$RUSTFLAGS"
289b0324f85SSebastien Boeuf    target_cc=""
2902805e7b1SRob Bradford    if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then
29124f384d2SRavi kumar Veeramally        rustflags="$rustflags -C link-args=-Wl,-Bstatic -C link-args=-lc"
2920090ec2dSMichael Zhao    fi
293db6f894eSSamuel Ortiz
294db6f894eSSamuel Ortiz    $DOCKER_RUNTIME run \
295c8fa8092SSamuel Ortiz        --user "$(id -u):$(id -g)" \
296db6f894eSSamuel Ortiz        --workdir "$CTR_CLH_ROOT_DIR" \
297db6f894eSSamuel Ortiz        --rm \
298b339aa6bSMuminul Islam        --volume $exported_device \
2992b2d0065SRuslan Mstoi        --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
3002b2d0065SRuslan Mstoi        ${exported_volumes:+"$exported_volumes"} \
3010090ec2dSMichael Zhao        --env RUSTFLAGS="$rustflags" \
302b0324f85SSebastien Boeuf        --env TARGET_CC="$target_cc" \
303db6f894eSSamuel Ortiz        "$CTR_IMAGE" \
3043f2ca537SAlexandru Matei        cargo build --all "${features_build[@]}" \
305db6f894eSSamuel Ortiz        --target-dir "$CTR_CLH_CARGO_TARGET" \
306ad9374bdSSamuel Ortiz        "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$target/$build"
307db6f894eSSamuel Ortiz}
308db6f894eSSamuel Ortiz
309275cb5c9SSamuel Ortizcmd_clean() {
310275cb5c9SSamuel Ortiz    cargo_args=("$@")
311275cb5c9SSamuel Ortiz
312b8342ebeSRob Bradford    ensure_build_dir
313b8342ebeSRob Bradford    ensure_latest_ctr
314b8342ebeSRob Bradford
315275cb5c9SSamuel Ortiz    $DOCKER_RUNTIME run \
316c8fa8092SSamuel Ortiz        --user "$(id -u):$(id -g)" \
317275cb5c9SSamuel Ortiz        --workdir "$CTR_CLH_ROOT_DIR" \
318275cb5c9SSamuel Ortiz        --rm \
3192b2d0065SRuslan Mstoi        --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
3202b2d0065SRuslan Mstoi        ${exported_volumes:+"$exported_volumes"} \
321275cb5c9SSamuel Ortiz        "$CTR_IMAGE" \
322275cb5c9SSamuel Ortiz        cargo clean \
323275cb5c9SSamuel Ortiz        --target-dir "$CTR_CLH_CARGO_TARGET" \
324275cb5c9SSamuel Ortiz        "${cargo_args[@]}"
325275cb5c9SSamuel Ortiz}
326275cb5c9SSamuel Ortiz
327db6f894eSSamuel Ortizcmd_tests() {
328db6f894eSSamuel Ortiz    unit=false
329db6f894eSSamuel Ortiz    integration=false
33056b0c855SSebastien Boeuf    integration_sgx=false
33104dc4968SSebastien Boeuf    integration_vfio=false
332934f9925SRob Bradford    integration_windows=false
333a1a0bc85SBo Chen    integration_live_migration=false
334ef1983eeSBo Chen    integration_rate_limiter=false
3351cf73c83SBo Chen    metrics=false
3369f028394SSongqian Li    coverage=false
337ad9374bdSSamuel Ortiz    libc="gnu"
3382e1866a5SMuminul Islam    arg_vols=""
33927b5f8d7SMuminul Islam    hypervisor="kvm"
340b339aa6bSMuminul Islam    exported_device="/dev/kvm"
341db6f894eSSamuel Ortiz    while [ $# -gt 0 ]; do
342db6f894eSSamuel Ortiz        case "$1" in
3431a5b94eeSRob Bradford        "-h" | "--help") {
3441a5b94eeSRob Bradford            cmd_help
3451a5b94eeSRob Bradford            exit 1
3461a5b94eeSRob Bradford        } ;;
347db6f894eSSamuel Ortiz        "--unit") { unit=true; } ;;
348db6f894eSSamuel Ortiz        "--integration") { integration=true; } ;;
34956b0c855SSebastien Boeuf        "--integration-sgx") { integration_sgx=true; } ;;
35004dc4968SSebastien Boeuf        "--integration-vfio") { integration_vfio=true; } ;;
351934f9925SRob Bradford        "--integration-windows") { integration_windows=true; } ;;
352a1a0bc85SBo Chen        "--integration-live-migration") { integration_live_migration=true; } ;;
353ef1983eeSBo Chen        "--integration-rate-limiter") { integration_rate_limiter=true; } ;;
3541cf73c83SBo Chen        "--metrics") { metrics=true; } ;;
3559f028394SSongqian Li        "--coverage") { coverage=true; } ;;
356ad9374bdSSamuel Ortiz        "--libc")
357ad9374bdSSamuel Ortiz            shift
3581a5b94eeSRob Bradford            [[ "$1" =~ ^(musl|gnu)$ ]] ||
359ad9374bdSSamuel Ortiz                die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"."
360ad9374bdSSamuel Ortiz            libc="$1"
361ad9374bdSSamuel Ortiz            ;;
3622e1866a5SMuminul Islam        "--volumes")
3632e1866a5SMuminul Islam            shift
3642e1866a5SMuminul Islam            arg_vols="$1"
3652e1866a5SMuminul Islam            ;;
36627b5f8d7SMuminul Islam        "--hypervisor")
36727b5f8d7SMuminul Islam            shift
36827b5f8d7SMuminul Islam            hypervisor="$1"
36927b5f8d7SMuminul Islam            ;;
3701a5b94eeSRob Bradford        "--all") {
3711a5b94eeSRob Bradford            unit=true
3721a5b94eeSRob Bradford            integration=true
3731a5b94eeSRob Bradford        } ;;
3741a5b94eeSRob Bradford        "--") {
3751a5b94eeSRob Bradford            shift
3761a5b94eeSRob Bradford            break
3771a5b94eeSRob Bradford        } ;;
378db6f894eSSamuel Ortiz        *)
379db6f894eSSamuel Ortiz            die "Unknown tests argument: $1. Please use --help for help."
380db6f894eSSamuel Ortiz            ;;
381db6f894eSSamuel Ortiz        esac
382db6f894eSSamuel Ortiz        shift
383db6f894eSSamuel Ortiz    done
384ca4857b5SMuminul Islam    if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
385ca4857b5SMuminul Islam        die "Hypervisor value must be kvm or mshv"
38627b5f8d7SMuminul Islam    fi
387ca4857b5SMuminul Islam
388b339aa6bSMuminul Islam    if [[ "$hypervisor" = "mshv" ]]; then
389b339aa6bSMuminul Islam        exported_device="/dev/mshv"
390b339aa6bSMuminul Islam    fi
391ca4857b5SMuminul Islam
392e436b382SMuminul Islam    if [ ! -e "${exported_device}" ]; then
393e436b382SMuminul Islam        die "${exported_device} does not exist on the system"
394e436b382SMuminul Islam    fi
395e436b382SMuminul Islam
3964552d07aSBo Chen    set -- '--hypervisor' "$hypervisor" "$@"
397d79594aaSMuminul Islam
398b8342ebeSRob Bradford    ensure_build_dir
399b8342ebeSRob Bradford    ensure_latest_ctr
400b8342ebeSRob Bradford
4012e1866a5SMuminul Islam    process_volumes_args
402ad9374bdSSamuel Ortiz    target="$(uname -m)-unknown-linux-${libc}"
403ad9374bdSSamuel Ortiz
4048ba5682eSWei Liu    rustflags="$RUSTFLAGS"
4058ba5682eSWei Liu    target_cc=""
4068ba5682eSWei Liu    if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then
40724f384d2SRavi kumar Veeramally        rustflags="$rustflags -C link-args=-Wl,-Bstatic -C link-args=-lc"
4088ba5682eSWei Liu    fi
4098ba5682eSWei Liu
4109cdcbb51SMuminul Islam    if [[ "$unit" = true ]]; then
411ad9374bdSSamuel Ortiz        say "Running unit tests for $target..."
412db6f894eSSamuel Ortiz        $DOCKER_RUNTIME run \
413db6f894eSSamuel Ortiz            --workdir "$CTR_CLH_ROOT_DIR" \
414db6f894eSSamuel Ortiz            --rm \
415b339aa6bSMuminul Islam            --device $exported_device \
4167fabca35SSebastien Boeuf            --device /dev/net/tun \
4177fabca35SSebastien Boeuf            --cap-add net_admin \
4182b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
4192b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
420ad9374bdSSamuel Ortiz            --env BUILD_TARGET="$target" \
4218ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
4228ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
4239f028394SSongqian Li            --env LLVM_PROFILE_FILE="$LLVM_PROFILE_FILE" \
424db6f894eSSamuel Ortiz            "$CTR_IMAGE" \
425d79594aaSMuminul Islam            ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $?
426db6f894eSSamuel Ortiz    fi
427db6f894eSSamuel Ortiz
428db6f894eSSamuel Ortiz    if [ "$integration" = true ]; then
429ad9374bdSSamuel Ortiz        say "Running integration tests for $target..."
430db6f894eSSamuel Ortiz        $DOCKER_RUNTIME run \
431db6f894eSSamuel Ortiz            --workdir "$CTR_CLH_ROOT_DIR" \
432db6f894eSSamuel Ortiz            --rm \
433db6f894eSSamuel Ortiz            --privileged \
434f21cd31bSSamuel Ortiz            --security-opt seccomp=unconfined \
435f21cd31bSSamuel Ortiz            --ipc=host \
436cf1b5156SMichael Zhao            --net="$CTR_CLH_NET" \
437a5b053f8SSamuel Ortiz            --mount type=tmpfs,destination=/tmp \
438db6f894eSSamuel Ortiz            --volume /dev:/dev \
4392b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
4402b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
441db6f894eSSamuel Ortiz            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
442f21cd31bSSamuel Ortiz            --env USER="root" \
443c7e51e51SWei Liu            --env BUILD_TARGET="$target" \
4448ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
4458ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
44699a25510SRob Bradford            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
4479f028394SSongqian Li            --env LLVM_PROFILE_FILE="$LLVM_PROFILE_FILE" \
448db6f894eSSamuel Ortiz            "$CTR_IMAGE" \
449a7aecb5eSOmer Faruk Bayram            dbus-run-session ./scripts/run_integration_tests_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $?
450db6f894eSSamuel Ortiz    fi
4510a1d6e1cSSamuel Ortiz
45256b0c855SSebastien Boeuf    if [ "$integration_sgx" = true ]; then
45357f81d03SWei Liu        say "Running SGX integration tests for $target..."
45456b0c855SSebastien Boeuf        $DOCKER_RUNTIME run \
45556b0c855SSebastien Boeuf            --workdir "$CTR_CLH_ROOT_DIR" \
45656b0c855SSebastien Boeuf            --rm \
45756b0c855SSebastien Boeuf            --privileged \
45856b0c855SSebastien Boeuf            --security-opt seccomp=unconfined \
45956b0c855SSebastien Boeuf            --ipc=host \
46056b0c855SSebastien Boeuf            --net="$CTR_CLH_NET" \
46156b0c855SSebastien Boeuf            --mount type=tmpfs,destination=/tmp \
46256b0c855SSebastien Boeuf            --volume /dev:/dev \
4632b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
4642b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
46556b0c855SSebastien Boeuf            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
46656b0c855SSebastien Boeuf            --env USER="root" \
467c7e51e51SWei Liu            --env BUILD_TARGET="$target" \
4688ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
4698ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
47099a25510SRob Bradford            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
47156b0c855SSebastien Boeuf            "$CTR_IMAGE" \
472d79594aaSMuminul Islam            ./scripts/run_integration_tests_sgx.sh "$@" || fix_dir_perms $? || exit $?
47356b0c855SSebastien Boeuf    fi
47456b0c855SSebastien Boeuf
47504dc4968SSebastien Boeuf    if [ "$integration_vfio" = true ]; then
47604dc4968SSebastien Boeuf        say "Running VFIO integration tests for $target..."
47704dc4968SSebastien Boeuf        $DOCKER_RUNTIME run \
47804dc4968SSebastien Boeuf            --workdir "$CTR_CLH_ROOT_DIR" \
47904dc4968SSebastien Boeuf            --rm \
48004dc4968SSebastien Boeuf            --privileged \
48104dc4968SSebastien Boeuf            --security-opt seccomp=unconfined \
48204dc4968SSebastien Boeuf            --ipc=host \
48304dc4968SSebastien Boeuf            --net="$CTR_CLH_NET" \
48404dc4968SSebastien Boeuf            --mount type=tmpfs,destination=/tmp \
48504dc4968SSebastien Boeuf            --volume /dev:/dev \
4862b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
4872b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
48804dc4968SSebastien Boeuf            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
48904dc4968SSebastien Boeuf            --env USER="root" \
490c7e51e51SWei Liu            --env BUILD_TARGET="$target" \
4918ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
4928ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
49399a25510SRob Bradford            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
49404dc4968SSebastien Boeuf            "$CTR_IMAGE" \
49504dc4968SSebastien Boeuf            ./scripts/run_integration_tests_vfio.sh "$@" || fix_dir_perms $? || exit $?
49604dc4968SSebastien Boeuf    fi
49704dc4968SSebastien Boeuf
498934f9925SRob Bradford    if [ "$integration_windows" = true ]; then
49957f81d03SWei Liu        say "Running Windows integration tests for $target..."
500934f9925SRob Bradford        $DOCKER_RUNTIME run \
501934f9925SRob Bradford            --workdir "$CTR_CLH_ROOT_DIR" \
502934f9925SRob Bradford            --rm \
503934f9925SRob Bradford            --privileged \
504934f9925SRob Bradford            --security-opt seccomp=unconfined \
505934f9925SRob Bradford            --ipc=host \
506934f9925SRob Bradford            --net="$CTR_CLH_NET" \
507934f9925SRob Bradford            --mount type=tmpfs,destination=/tmp \
508934f9925SRob Bradford            --volume /dev:/dev \
5092b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
5102b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
511934f9925SRob Bradford            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
512934f9925SRob Bradford            --env USER="root" \
513c7e51e51SWei Liu            --env BUILD_TARGET="$target" \
5148ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
5158ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
51699a25510SRob Bradford            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
517934f9925SRob Bradford            "$CTR_IMAGE" \
518944d0920SAnatol Belski            ./scripts/run_integration_tests_windows_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $?
519934f9925SRob Bradford    fi
520a1a0bc85SBo Chen
521a1a0bc85SBo Chen    if [ "$integration_live_migration" = true ]; then
522a1a0bc85SBo Chen        say "Running 'live migration' integration tests for $target..."
523a1a0bc85SBo Chen        $DOCKER_RUNTIME run \
524a1a0bc85SBo Chen            --workdir "$CTR_CLH_ROOT_DIR" \
525a1a0bc85SBo Chen            --rm \
526a1a0bc85SBo Chen            --privileged \
527a1a0bc85SBo Chen            --security-opt seccomp=unconfined \
528a1a0bc85SBo Chen            --ipc=host \
529a1a0bc85SBo Chen            --net="$CTR_CLH_NET" \
530a1a0bc85SBo Chen            --mount type=tmpfs,destination=/tmp \
531a1a0bc85SBo Chen            --volume /dev:/dev \
5322b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
5332b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
534a1a0bc85SBo Chen            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
535a1a0bc85SBo Chen            --env USER="root" \
536c7e51e51SWei Liu            --env BUILD_TARGET="$target" \
5378ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
5388ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
53999a25510SRob Bradford            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
5409f028394SSongqian Li            --env LLVM_PROFILE_FILE="$LLVM_PROFILE_FILE" \
541a1a0bc85SBo Chen            "$CTR_IMAGE" \
542a1a0bc85SBo Chen            ./scripts/run_integration_tests_live_migration.sh "$@" || fix_dir_perms $? || exit $?
543a1a0bc85SBo Chen    fi
5441cf73c83SBo Chen
545ef1983eeSBo Chen    if [ "$integration_rate_limiter" = true ]; then
546ef1983eeSBo Chen        say "Running 'rate limiter' integration tests for $target..."
547ef1983eeSBo Chen        $DOCKER_RUNTIME run \
548ef1983eeSBo Chen            --workdir "$CTR_CLH_ROOT_DIR" \
549ef1983eeSBo Chen            --rm \
550ef1983eeSBo Chen            --privileged \
551ef1983eeSBo Chen            --security-opt seccomp=unconfined \
552ef1983eeSBo Chen            --ipc=host \
553ef1983eeSBo Chen            --net="$CTR_CLH_NET" \
554ef1983eeSBo Chen            --mount type=tmpfs,destination=/tmp \
555ef1983eeSBo Chen            --volume /dev:/dev \
5562b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
5572b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
558ef1983eeSBo Chen            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
559ef1983eeSBo Chen            --env USER="root" \
560c7e51e51SWei Liu            --env BUILD_TARGET="$target" \
5618ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
5628ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
56399a25510SRob Bradford            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
564ef1983eeSBo Chen            "$CTR_IMAGE" \
565ef1983eeSBo Chen            ./scripts/run_integration_tests_rate_limiter.sh "$@" || fix_dir_perms $? || exit $?
566ef1983eeSBo Chen    fi
567ef1983eeSBo Chen
5681cf73c83SBo Chen    if [ "$metrics" = true ]; then
5691cf73c83SBo Chen        say "Generating performance metrics for $target..."
5701cf73c83SBo Chen        $DOCKER_RUNTIME run \
5711cf73c83SBo Chen            --workdir "$CTR_CLH_ROOT_DIR" \
5721cf73c83SBo Chen            --rm \
5731cf73c83SBo Chen            --privileged \
5741cf73c83SBo Chen            --security-opt seccomp=unconfined \
5751cf73c83SBo Chen            --ipc=host \
5761cf73c83SBo Chen            --net="$CTR_CLH_NET" \
5771cf73c83SBo Chen            --mount type=tmpfs,destination=/tmp \
5781cf73c83SBo Chen            --volume /dev:/dev \
5792b2d0065SRuslan Mstoi            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
5802b2d0065SRuslan Mstoi            ${exported_volumes:+"$exported_volumes"} \
5811cf73c83SBo Chen            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
5821cf73c83SBo Chen            --env USER="root" \
583c7e51e51SWei Liu            --env BUILD_TARGET="$target" \
5848ba5682eSWei Liu            --env RUSTFLAGS="$rustflags" \
5858ba5682eSWei Liu            --env TARGET_CC="$target_cc" \
586b56da6eeSsmit-gardhariya            --env RUST_BACKTRACE="${RUST_BACKTRACE}" \
58799a25510SRob Bradford            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
5881cf73c83SBo Chen            "$CTR_IMAGE" \
5891cf73c83SBo Chen            ./scripts/run_metrics.sh "$@" || fix_dir_perms $? || exit $?
5901cf73c83SBo Chen    fi
5911cf73c83SBo Chen
5929f028394SSongqian Li    if [ "$coverage" = true ]; then
5939f028394SSongqian Li        say "Generating code coverage information for $target..."
5949f028394SSongqian Li        $DOCKER_RUNTIME run \
5959f028394SSongqian Li            --workdir "$CTR_CLH_ROOT_DIR" \
5969f028394SSongqian Li            --rm \
5979f028394SSongqian Li            --privileged \
5989f028394SSongqian Li            --security-opt seccomp=unconfined \
5999f028394SSongqian Li            --ipc=host \
6009f028394SSongqian Li            --net="$CTR_CLH_NET" \
6019f028394SSongqian Li            --mount type=tmpfs,destination=/tmp \
6029f028394SSongqian Li            --volume /dev:/dev \
6039f028394SSongqian Li            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
6049f028394SSongqian Li            ${exported_volumes:+"$exported_volumes"} \
6059f028394SSongqian Li            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
6069f028394SSongqian Li            --env USER="root" \
6079f028394SSongqian Li            --env BUILD_TARGET="$target" \
6089f028394SSongqian Li            --env RUSTFLAGS="$rustflags" \
6099f028394SSongqian Li            --env TARGET_CC="$target_cc" \
6109f028394SSongqian Li            --env AUTH_DOWNLOAD_TOKEN="$AUTH_DOWNLOAD_TOKEN" \
6119f028394SSongqian Li            "$CTR_IMAGE" \
6129f028394SSongqian Li            dbus-run-session ./scripts/run_coverage.sh "$@" || fix_dir_perms $? || exit $?
6139f028394SSongqian Li    fi
6149f028394SSongqian Li
615296ada94SSamuel Ortiz    fix_dir_perms $?
616db6f894eSSamuel Ortiz}
617db6f894eSSamuel Ortiz
61855b8a218SRob Bradfordbuild_container() {
619b8342ebeSRob Bradford    ensure_build_dir
620b8342ebeSRob Bradford
621db6f894eSSamuel Ortiz    BUILD_DIR=/tmp/cloud-hypervisor/container/
622db6f894eSSamuel Ortiz
623db6f894eSSamuel Ortiz    mkdir -p $BUILD_DIR
6242805e7b1SRob Bradford    cp "$CLH_DOCKERFILE" $BUILD_DIR
625db6f894eSSamuel Ortiz
6262805e7b1SRob Bradford    [ "$(uname -m)" = "aarch64" ] && TARGETARCH="arm64"
6272805e7b1SRob Bradford    [ "$(uname -m)" = "x86_64" ] && TARGETARCH="amd64"
6288bf0bac5SHenry Wang
629db6f894eSSamuel Ortiz    $DOCKER_RUNTIME build \
63055b8a218SRob Bradford        --target dev \
6312b2d0065SRuslan Mstoi        -t "$CTR_IMAGE" \
632db6f894eSSamuel Ortiz        -f $BUILD_DIR/Dockerfile \
6332b2d0065SRuslan Mstoi        --build-arg TARGETARCH="$TARGETARCH" \
634db6f894eSSamuel Ortiz        $BUILD_DIR
635db6f894eSSamuel Ortiz}
636db6f894eSSamuel Ortiz
63755b8a218SRob Bradfordcmd_build-container() {
63855b8a218SRob Bradford    while [ $# -gt 0 ]; do
63955b8a218SRob Bradford        case "$1" in
64055b8a218SRob Bradford        "-h" | "--help") {
64155b8a218SRob Bradford            cmd_help
64255b8a218SRob Bradford            exit 1
64355b8a218SRob Bradford        } ;;
64455b8a218SRob Bradford        "--") {
64555b8a218SRob Bradford            shift
64655b8a218SRob Bradford            break
64755b8a218SRob Bradford        } ;;
64855b8a218SRob Bradford        *)
64955b8a218SRob Bradford            die "Unknown build-container argument: $1. Please use --help for help."
65055b8a218SRob Bradford            ;;
65155b8a218SRob Bradford        esac
65255b8a218SRob Bradford        shift
65355b8a218SRob Bradford    done
65455b8a218SRob Bradford
65555b8a218SRob Bradford    build_container
65655b8a218SRob Bradford}
65755b8a218SRob Bradford
6585a6b8d63SSamuel Ortizcmd_shell() {
659972e96eaSWei Liu    while [ $# -gt 0 ]; do
660972e96eaSWei Liu        case "$1" in
6611a5b94eeSRob Bradford        "-h" | "--help") {
6621a5b94eeSRob Bradford            cmd_help
6631a5b94eeSRob Bradford            exit 1
6641a5b94eeSRob Bradford        } ;;
665972e96eaSWei Liu        "--volumes")
666972e96eaSWei Liu            shift
667972e96eaSWei Liu            arg_vols="$1"
668972e96eaSWei Liu            ;;
6691a5b94eeSRob Bradford        "--") {
6701a5b94eeSRob Bradford            shift
6711a5b94eeSRob Bradford            break
6721a5b94eeSRob Bradford        } ;;
6731a5b94eeSRob Bradford        *) ;;
6741a5b94eeSRob Bradford
675972e96eaSWei Liu        esac
676972e96eaSWei Liu        shift
677972e96eaSWei Liu    done
678b8342ebeSRob Bradford    ensure_build_dir
679b8342ebeSRob Bradford    ensure_latest_ctr
680972e96eaSWei Liu    process_volumes_args
6815a6b8d63SSamuel Ortiz    say_warn "Starting a privileged shell prompt as root ..."
6825a6b8d63SSamuel Ortiz    say_warn "WARNING: Your $CLH_ROOT_DIR folder will be bind-mounted in the container under $CTR_CLH_ROOT_DIR"
6835a6b8d63SSamuel Ortiz    $DOCKER_RUNTIME run \
6845a6b8d63SSamuel Ortiz        -ti \
6855a6b8d63SSamuel Ortiz        --workdir "$CTR_CLH_ROOT_DIR" \
6865a6b8d63SSamuel Ortiz        --rm \
6875a6b8d63SSamuel Ortiz        --privileged \
6885a6b8d63SSamuel Ortiz        --security-opt seccomp=unconfined \
6895a6b8d63SSamuel Ortiz        --ipc=host \
690cf1b5156SMichael Zhao        --net="$CTR_CLH_NET" \
6915a6b8d63SSamuel Ortiz        --tmpfs /tmp:exec \
6925a6b8d63SSamuel Ortiz        --volume /dev:/dev \
6932b2d0065SRuslan Mstoi        --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \
6942b2d0065SRuslan Mstoi        ${exported_volumes:+"$exported_volumes"} \
6955a6b8d63SSamuel Ortiz        --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
6965a6b8d63SSamuel Ortiz        --env USER="root" \
6975a6b8d63SSamuel Ortiz        --entrypoint bash \
6985a6b8d63SSamuel Ortiz        "$CTR_IMAGE"
6995a6b8d63SSamuel Ortiz
7005a6b8d63SSamuel Ortiz    fix_dir_perms $?
7015a6b8d63SSamuel Ortiz}
7025a6b8d63SSamuel Ortiz
7032e94a86bSRuslan Mstoiif [ $# = 0 ]; then
7042e94a86bSRuslan Mstoi    cmd_help
7052e94a86bSRuslan Mstoi    say_err "Please specify command to run!"
7062e94a86bSRuslan Mstoi    exit 1
7072e94a86bSRuslan Mstoifi
7082e94a86bSRuslan Mstoi
709db6f894eSSamuel Ortiz# Parse main command line args.
710db6f894eSSamuel Ortiz#
711db6f894eSSamuel Ortizwhile [ $# -gt 0 ]; do
712db6f894eSSamuel Ortiz    case "$1" in
7131a5b94eeSRob Bradford    -h | --help) {
7141a5b94eeSRob Bradford        cmd_help
7151a5b94eeSRob Bradford        exit 1
7161a5b94eeSRob Bradford    } ;;
71755b8a218SRob Bradford    --local) {
71855b8a218SRob Bradford        CTR_IMAGE_VERSION="local"
71955b8a218SRob Bradford        CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}"
72055b8a218SRob Bradford    } ;;
721db6f894eSSamuel Ortiz    -*)
722db6f894eSSamuel Ortiz        die "Unknown arg: $1. Please use \`$0 help\` for help."
723db6f894eSSamuel Ortiz        ;;
724db6f894eSSamuel Ortiz    *)
725db6f894eSSamuel Ortiz        break
726db6f894eSSamuel Ortiz        ;;
727db6f894eSSamuel Ortiz    esac
728db6f894eSSamuel Ortiz    shift
729db6f894eSSamuel Ortizdone
730db6f894eSSamuel Ortiz
731db6f894eSSamuel Ortiz# $1 is now a command name. Check if it is a valid command and, if so,
732db6f894eSSamuel Ortiz# run it.
733db6f894eSSamuel Ortiz#
734db6f894eSSamuel Ortizdeclare -f "cmd_$1" >/dev/null
735db6f894eSSamuel Ortizok_or_die "Unknown command: $1. Please use \`$0 help\` for help."
736db6f894eSSamuel Ortiz
737db6f894eSSamuel Ortizcmd=cmd_$1
738db6f894eSSamuel Ortizshift
739db6f894eSSamuel Ortiz
740db6f894eSSamuel Ortiz$cmd "$@"
741