xref: /cloud-hypervisor/scripts/dev_cli.sh (revision 2571e59438597f53aa4993cd70d6462fe1364ba7)
1#!/bin/bash
2
3# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4# Copyright © 2020 Intel Corporation
5# SPDX-License-Identifier: Apache-2.0
6
7CLI_NAME="Cloud Hypervisor"
8
9CTR_IMAGE_TAG="ghcr.io/cloud-hypervisor/cloud-hypervisor"
10CTR_IMAGE_VERSION="20230804-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=""
286    target_cc=""
287    if [ "$(uname -m)" = "aarch64" ] && [ "$libc" = "musl" ]; then
288        rustflags="-C link-arg=-lgcc -C link_arg=-specs -C link_arg=/usr/lib/aarch64-linux-musl/musl-gcc.specs"
289        target_cc="musl-gcc"
290    fi
291
292    $DOCKER_RUNTIME run \
293        --user "$(id -u):$(id -g)" \
294        --workdir "$CTR_CLH_ROOT_DIR" \
295        --rm \
296        --volume $exported_device \
297        --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
298        --env RUSTFLAGS="$rustflags" \
299        --env TARGET_CC="$target_cc" \
300        "$CTR_IMAGE" \
301        cargo build --all $features_build \
302        --target-dir "$CTR_CLH_CARGO_TARGET" \
303        "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$target/$build"
304}
305
306cmd_clean() {
307    cargo_args=("$@")
308
309    ensure_build_dir
310    ensure_latest_ctr
311
312    $DOCKER_RUNTIME run \
313        --user "$(id -u):$(id -g)" \
314        --workdir "$CTR_CLH_ROOT_DIR" \
315        --rm \
316        --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
317        "$CTR_IMAGE" \
318        cargo clean \
319        --target-dir "$CTR_CLH_CARGO_TARGET" \
320        "${cargo_args[@]}"
321}
322
323cmd_tests() {
324    unit=false
325    integration=false
326    integration_sgx=false
327    integration_vfio=false
328    integration_windows=false
329    integration_live_migration=false
330    integration_rate_limiter=false
331    metrics=false
332    libc="gnu"
333    arg_vols=""
334    hypervisor="kvm"
335    exported_device="/dev/kvm"
336    while [ $# -gt 0 ]; do
337        case "$1" in
338        "-h" | "--help") {
339            cmd_help
340            exit 1
341        } ;;
342        "--unit") { unit=true; } ;;
343        "--integration") { integration=true; } ;;
344        "--integration-sgx") { integration_sgx=true; } ;;
345        "--integration-vfio") { integration_vfio=true; } ;;
346        "--integration-windows") { integration_windows=true; } ;;
347        "--integration-live-migration") { integration_live_migration=true; } ;;
348        "--integration-rate-limiter") { integration_rate_limiter=true; } ;;
349        "--metrics") { metrics=true; } ;;
350        "--libc")
351            shift
352            [[ "$1" =~ ^(musl|gnu)$ ]] ||
353                die "Invalid libc: $1. Valid options are \"musl\" and \"gnu\"."
354            libc="$1"
355            ;;
356        "--volumes")
357            shift
358            arg_vols="$1"
359            ;;
360        "--hypervisor")
361            shift
362            hypervisor="$1"
363            ;;
364        "--all") {
365            cargo=true
366            unit=true
367            integration=true
368        } ;;
369        "--") {
370            shift
371            break
372        } ;;
373        *)
374            die "Unknown tests argument: $1. Please use --help for help."
375            ;;
376        esac
377        shift
378    done
379    if [[ ! ("$hypervisor" = "kvm" || "$hypervisor" = "mshv") ]]; then
380        die "Hypervisor value must be kvm or mshv"
381    fi
382
383    if [[ "$hypervisor" = "mshv" ]]; then
384        exported_device="/dev/mshv"
385    fi
386
387    if [ ! -e "${exported_device}" ] ; then
388        die "${exported_device} does not exist on the system"
389    fi
390
391    set -- '--hypervisor' "$hypervisor" "$@"
392
393    ensure_build_dir
394    ensure_latest_ctr
395
396    process_volumes_args
397    target="$(uname -m)-unknown-linux-${libc}"
398
399    if [[ "$unit" = true ]]; then
400        say "Running unit tests for $target..."
401        $DOCKER_RUNTIME run \
402            --workdir "$CTR_CLH_ROOT_DIR" \
403            --rm \
404            --device $exported_device \
405            --device /dev/net/tun \
406            --cap-add net_admin \
407            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
408            --env BUILD_TARGET="$target" \
409            "$CTR_IMAGE" \
410            ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $?
411    fi
412
413    if [ "$integration" = true ]; then
414        say "Running integration tests for $target..."
415        $DOCKER_RUNTIME run \
416            --workdir "$CTR_CLH_ROOT_DIR" \
417            --rm \
418            --privileged \
419            --security-opt seccomp=unconfined \
420            --ipc=host \
421            --net="$CTR_CLH_NET" \
422            --mount type=tmpfs,destination=/tmp \
423            --volume /dev:/dev \
424            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
425            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
426            --env USER="root" \
427            --env CH_LIBC="${libc}" \
428            "$CTR_IMAGE" \
429            dbus-run-session ./scripts/run_integration_tests_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $?
430    fi
431
432    if [ "$integration_sgx" = true ]; then
433        say "Running SGX integration tests for $target..."
434        $DOCKER_RUNTIME run \
435            --workdir "$CTR_CLH_ROOT_DIR" \
436            --rm \
437            --privileged \
438            --security-opt seccomp=unconfined \
439            --ipc=host \
440            --net="$CTR_CLH_NET" \
441            --mount type=tmpfs,destination=/tmp \
442            --volume /dev:/dev \
443            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
444            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
445            --env USER="root" \
446            --env CH_LIBC="${libc}" \
447            "$CTR_IMAGE" \
448            ./scripts/run_integration_tests_sgx.sh "$@" || fix_dir_perms $? || exit $?
449    fi
450
451    if [ "$integration_vfio" = true ]; then
452        say "Running VFIO integration tests for $target..."
453        $DOCKER_RUNTIME run \
454            --workdir "$CTR_CLH_ROOT_DIR" \
455            --rm \
456            --privileged \
457            --security-opt seccomp=unconfined \
458            --ipc=host \
459            --net="$CTR_CLH_NET" \
460            --mount type=tmpfs,destination=/tmp \
461            --volume /dev:/dev \
462            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
463            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
464            --env USER="root" \
465            --env CH_LIBC="${libc}" \
466            "$CTR_IMAGE" \
467            ./scripts/run_integration_tests_vfio.sh "$@" || fix_dir_perms $? || exit $?
468    fi
469
470    if [ "$integration_windows" = true ]; then
471        say "Running Windows integration tests for $target..."
472        $DOCKER_RUNTIME run \
473            --workdir "$CTR_CLH_ROOT_DIR" \
474            --rm \
475            --privileged \
476            --security-opt seccomp=unconfined \
477            --ipc=host \
478            --net="$CTR_CLH_NET" \
479            --mount type=tmpfs,destination=/tmp \
480            --volume /dev:/dev \
481            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
482            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
483            --env USER="root" \
484            --env CH_LIBC="${libc}" \
485            "$CTR_IMAGE" \
486            ./scripts/run_integration_tests_windows_"$(uname -m)".sh "$@" || fix_dir_perms $? || exit $?
487    fi
488
489    if [ "$integration_live_migration" = true ]; then
490        say "Running 'live migration' integration tests for $target..."
491        $DOCKER_RUNTIME run \
492            --workdir "$CTR_CLH_ROOT_DIR" \
493            --rm \
494            --privileged \
495            --security-opt seccomp=unconfined \
496            --ipc=host \
497            --net="$CTR_CLH_NET" \
498            --mount type=tmpfs,destination=/tmp \
499            --volume /dev:/dev \
500            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
501            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
502            --env USER="root" \
503            --env CH_LIBC="${libc}" \
504            "$CTR_IMAGE" \
505            ./scripts/run_integration_tests_live_migration.sh "$@" || fix_dir_perms $? || exit $?
506    fi
507
508    if [ "$integration_rate_limiter" = true ]; then
509        say "Running 'rate limiter' integration tests for $target..."
510        $DOCKER_RUNTIME run \
511            --workdir "$CTR_CLH_ROOT_DIR" \
512            --rm \
513            --privileged \
514            --security-opt seccomp=unconfined \
515            --ipc=host \
516            --net="$CTR_CLH_NET" \
517            --mount type=tmpfs,destination=/tmp \
518            --volume /dev:/dev \
519            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
520            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
521            --env USER="root" \
522            --env CH_LIBC="${libc}" \
523            "$CTR_IMAGE" \
524            ./scripts/run_integration_tests_rate_limiter.sh "$@" || fix_dir_perms $? || exit $?
525    fi
526
527    if [ "$metrics" = true ]; then
528        say "Generating performance metrics for $target..."
529        $DOCKER_RUNTIME run \
530            --workdir "$CTR_CLH_ROOT_DIR" \
531            --rm \
532            --privileged \
533            --security-opt seccomp=unconfined \
534            --ipc=host \
535            --net="$CTR_CLH_NET" \
536            --mount type=tmpfs,destination=/tmp \
537            --volume /dev:/dev \
538            --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
539            --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
540            --env USER="root" \
541            --env CH_LIBC="${libc}" \
542            --env RUST_BACKTRACE="${RUST_BACKTRACE}" \
543            "$CTR_IMAGE" \
544            ./scripts/run_metrics.sh "$@" || fix_dir_perms $? || exit $?
545    fi
546
547    fix_dir_perms $?
548}
549
550build_container() {
551    ensure_build_dir
552
553    BUILD_DIR=/tmp/cloud-hypervisor/container/
554
555    mkdir -p $BUILD_DIR
556    cp "$CLH_DOCKERFILE" $BUILD_DIR
557
558    [ "$(uname -m)" = "aarch64" ] && TARGETARCH="arm64"
559    [ "$(uname -m)" = "x86_64" ] && TARGETARCH="amd64"
560
561    $DOCKER_RUNTIME build \
562        --target dev \
563        -t $CTR_IMAGE \
564        -f $BUILD_DIR/Dockerfile \
565        --build-arg TARGETARCH=$TARGETARCH \
566        $BUILD_DIR
567}
568
569cmd_build-container() {
570    while [ $# -gt 0 ]; do
571        case "$1" in
572        "-h" | "--help") {
573            cmd_help
574            exit 1
575        } ;;
576        "--") {
577            shift
578            break
579        } ;;
580        *)
581            die "Unknown build-container argument: $1. Please use --help for help."
582            ;;
583        esac
584        shift
585    done
586
587    build_container
588}
589
590cmd_shell() {
591    while [ $# -gt 0 ]; do
592        case "$1" in
593        "-h" | "--help") {
594            cmd_help
595            exit 1
596        } ;;
597        "--volumes")
598            shift
599            arg_vols="$1"
600            ;;
601        "--") {
602            shift
603            break
604        } ;;
605        *) ;;
606
607        esac
608        shift
609    done
610    ensure_build_dir
611    ensure_latest_ctr
612    process_volumes_args
613    say_warn "Starting a privileged shell prompt as root ..."
614    say_warn "WARNING: Your $CLH_ROOT_DIR folder will be bind-mounted in the container under $CTR_CLH_ROOT_DIR"
615    $DOCKER_RUNTIME run \
616        -ti \
617        --workdir "$CTR_CLH_ROOT_DIR" \
618        --rm \
619        --privileged \
620        --security-opt seccomp=unconfined \
621        --ipc=host \
622        --net="$CTR_CLH_NET" \
623        --tmpfs /tmp:exec \
624        --volume /dev:/dev \
625        --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" $exported_volumes \
626        --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \
627        --env USER="root" \
628        --entrypoint bash \
629        "$CTR_IMAGE"
630
631    fix_dir_perms $?
632}
633
634if [ $# = 0 ]; then
635    cmd_help
636    say_err "Please specify command to run!"
637    exit 1
638fi
639
640# Parse main command line args.
641#
642while [ $# -gt 0 ]; do
643    case "$1" in
644    -h | --help) {
645        cmd_help
646        exit 1
647    } ;;
648    --local) {
649        CTR_IMAGE_VERSION="local"
650        CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}"
651    } ;;
652    -*)
653        die "Unknown arg: $1. Please use \`$0 help\` for help."
654        ;;
655    *)
656        break
657        ;;
658    esac
659    shift
660done
661
662# $1 is now a command name. Check if it is a valid command and, if so,
663# run it.
664#
665declare -f "cmd_$1" >/dev/null
666ok_or_die "Unknown command: $1. Please use \`$0 help\` for help."
667
668cmd=cmd_$1
669shift
670
671$cmd "$@"
672