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