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