-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathdev_cli.sh
More file actions
executable file
·525 lines (458 loc) · 15.3 KB
/
Copy pathdev_cli.sh
File metadata and controls
executable file
·525 lines (458 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/bin/bash
# Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# Copyright © 2020 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
CLI_NAME="Rust Hypervisor Firmware"
CTR_IMAGE_TAG="ghcr.io/cloud-hypervisor/rust-hypervisor-firmware"
CTR_IMAGE_VERSION="latest"
CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}"
DOCKER_RUNTIME="docker"
# Host paths
RHF_SCRIPTS_DIR=$(cd "$(dirname "$0")" && pwd)
RHF_ROOT_DIR=$(cd "${RHF_SCRIPTS_DIR}/.." && pwd)
RHF_BUILD_DIR="${RHF_ROOT_DIR}/build"
RHF_CARGO_TARGET="${RHF_BUILD_DIR}/cargo_target"
RHF_DOCKERFILE="${RHF_ROOT_DIR}/resources/Dockerfile"
RHF_CTR_BUILD_DIR="/tmp/rust-hypervisor-firmware/ctr-build"
RHF_WORKLOADS="${HOME}/workloads"
# Container paths
CTR_RHF_ROOT_DIR="/rust-hypervisor-firmware"
CTR_RHF_CARGO_BUILD_DIR="${CTR_RHF_ROOT_DIR}/build"
CTR_RHF_CARGO_TARGET="${CTR_RHF_CARGO_BUILD_DIR}/cargo_target"
CTR_RHF_WORKLOADS="/root/workloads"
# Container networking option
CTR_RHF_NET="bridge"
# Cargo paths
# Full path to the cargo registry dir on the host. This appears on the host
# because we want to persist the cargo registry across container invocations.
# Otherwise, any rust crates from crates.io would be downloaded again each time
# we build or test.
CARGO_REGISTRY_DIR="${RHF_BUILD_DIR}/cargo_registry"
# Full path to the cargo git registry on the host. This serves the same purpose
# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of
# crates.io.
CARGO_GIT_REGISTRY_DIR="${RHF_BUILD_DIR}/cargo_git_registry"
# Full path to the cargo target dir on the host.
CARGO_TARGET_DIR="${RHF_BUILD_DIR}/cargo_target"
# Send a decorated message to stdout, followed by a new line
#
say() {
[ -t 1 ] && [ -n "$TERM" ] \
&& echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \
|| echo "[$CLI_NAME] $*"
}
# Send a decorated message to stdout, without a trailing new line
#
say_noln() {
[ -t 1 ] && [ -n "$TERM" ] \
&& echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \
|| echo "[$CLI_NAME] $*"
}
# Send a text message to stderr
#
say_err() {
[ -t 2 ] && [ -n "$TERM" ] \
&& echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 \
|| echo "[$CLI_NAME] $*" 1>&2
}
# Send a warning-highlighted text to stdout
say_warn() {
[ -t 1 ] && [ -n "$TERM" ] \
&& echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" \
|| echo "[$CLI_NAME] $*"
}
# Exit with an error message and (optional) code
# Usage: die [-c <error code>] <error message>
#
die() {
code=1
[[ "$1" = "-c" ]] && {
code="$2"
shift 2
}
say_err "$@"
exit $code
}
# Exit with an error message if the last exit code is not 0
#
ok_or_die() {
code=$?
[[ $code -eq 0 ]] || die -c $code "$@"
}
# Fix main directory permissions after a container ran as root.
# Since the container ran as root, any files it creates will be owned by root.
# This fixes that by recursively changing the ownership of /cloud-hypervisor to the
# current user.
#
fix_dir_perms() {
# Yes, running Docker to get elevated privileges, just to chown some files
# is a dirty hack.
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--volume /dev:/dev \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
"$CTR_IMAGE" \
chown -R "$(id -u):$(id -g)" "$CTR_RHF_ROOT_DIR"
return $1
}
# Process exported volumes argument, separate the volumes and make docker compatible
# Sample input: --volumes /a:/a#/b:/b
# Sample output: --volume /a:/a --volume /b:/b
#
process_volumes_args() {
if [ -z "$arg_vols" ]; then
return
fi
exported_volumes=""
arr_vols=(${arg_vols//#/ })
for var in "${arr_vols[@]}"
do
parts=(${var//:/ })
if [[ ! -e "${parts[0]}" ]]; then
echo "The volume ${parts[0]} does not exist."
exit 1
fi
exported_volumes="$exported_volumes --volume $var"
done
}
# Make sure the build/ dirs are available. Exit if we can't create them.
# Upon returning from this call, the caller can be certain the build/ dirs exist.
#
ensure_build_dir() {
for dir in "$RHF_BUILD_DIR" \
"$RHF_WORKLOADS" \
"$CARGO_TARGET_DIR" \
"$CARGO_REGISTRY_DIR" \
"$CARGO_GIT_REGISTRY_DIR"; do
mkdir -p "$dir" || die "Error: cannot create dir $dir"
[ -x "$dir" ] && [ -w "$dir" ] || \
{
say "Wrong permissions for $dir. Attempting to fix them ..."
chmod +x+w "$dir"
} || \
die "Error: wrong permissions for $dir. Should be +x+w"
done
}
# Make sure we're using the latest dev container, by just pulling it.
ensure_latest_ctr() {
if [ "$CTR_IMAGE_VERSION" = "local" ]; then
build_container
else
arch="$(uname -m)"
[ "$arch" = "aarch64" ] && PLATFORM="linux/arm64"
[ "$arch" = "x86_64" ] && PLATFORM="linux/amd64"
$DOCKER_RUNTIME pull --platform "$PLATFORM" "$CTR_IMAGE"
ok_or_die "Error pulling container image. Aborting."
fi
}
build_container() {
arch="$(uname -m)"
ensure_build_dir
BUILD_DIR="$RHF_CTR_BUILD_DIR"
mkdir -p $BUILD_DIR
cp $RHF_DOCKERFILE $BUILD_DIR
[ "$arch" = "aarch64" ] && TARGETARCH="arm64"
[ "$arch" = "x86_64" ] && TARGETARCH="amd64"
RUST_TOOLCHAIN="$(rustup show active-toolchain | cut -d ' ' -f1)"
$DOCKER_RUNTIME build \
-t $CTR_IMAGE \
-f $BUILD_DIR/Dockerfile \
--build-arg TARGETARCH=$TARGETARCH \
--build-arg RUST_TOOLCHAIN=$RUST_TOOLCHAIN \
$BUILD_DIR
}
cmd_help() {
echo ""
echo "Rust Hypervisor Firmware $(basename $0)"
echo "Usage: $(basename $0) <command> [<command args>]"
echo ""
echo "Available commands:"
echo ""
echo " build [--debug|--release] [-- [<cargo args>]]"
echo " Build the Rust Hypervisor Firmware binary."
echo " --debug Build the debug binary. This is the default."
echo " --release Build the release binary."
echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol"
echo ""
echo " build-container"
echo " Build the Rust Hypervisor Firmware container."
echo ""
echo " clean [<cargo args>]]"
echo " Remove the Rust Hypervisor Firmware artifacts."
echo ""
echo " tests [--unit|--cargo|--all] [-- [<cargo test args>]]"
echo " Run the Rust Hypervisor Firmware tests."
echo " --unit Run the unit tests."
echo " --cargo Run the cargo tests."
echo " --integration Run the integration tests."
echo " --integration-coreboot Run the coreboot target integration tests."
echo " --integration-windows Run the Windows guest integration tests."
echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol"
echo " --all Run all tests."
echo ""
echo " shell"
echo " Run the development container into an interactive, privileged BASH shell."
echo " --volumes Hash separated volumes to be exported. Example --volumes /mnt:/mnt#/myvol:/myvol"
echo ""
echo " help"
echo " Display this help message."
echo ""
}
cmd_build() {
arch="$(uname -m)"
build="debug"
features_build=""
exported_device="/dev/kvm"
while [ $# -gt 0 ]; do
case "$1" in
"-h"|"--help") { cmd_help; exit 1; } ;;
"--debug") { build="debug"; } ;;
"--release") { build="release"; } ;;
"--volumes")
shift
arg_vols="$1"
;;
"--") { shift; break; } ;;
*)
die "Unknown build argument: $1. Please use --help for help."
;;
esac
shift
done
ensure_build_dir
ensure_latest_ctr
process_volumes_args
[ "$arch" = "aarch64" ] && target="aarch64-unknown-none.json"
[ "$arch" = "x86_64" ] && target="x86_64-unknown-none.json"
cargo_args=("$@")
[ $build = "release" ] && cargo_args+=("--release")
rustflags=""
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--user "$(id -u):$(id -g)" \
--rm \
--volume $exported_device \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
--env RUSTFLAGS="$rustflags" \
"$CTR_IMAGE" \
cargo build --target "$target" \
-Zbuild-std=core \
-Zbuild-std-features=compiler-builtins-mem \
--target-dir "$CTR_RHF_CARGO_TARGET" \
"${cargo_args[@]}" && say "Binary placed under $RHF_CARGO_TARGET/target/$build"
}
cmd_clean() {
cargo_args=("$@")
ensure_build_dir
ensure_latest_ctr
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
"$CTR_IMAGE" \
cargo clean \
--target-dir "$CTR_RHF_CARGO_TARGET" \
"${cargo_args[@]}"
}
cmd_tests() {
arch="$(uname -m)"
unit=false
cargo=false
integration=false
integration_coreboot=false
integration_windows=false
arg_vols=""
exported_device="/dev/kvm"
while [ $# -gt 0 ]; do
case "$1" in
"-h"|"--help") { cmd_help; exit 1; } ;;
"--unit") { unit=true; } ;;
"--cargo") { cargo=true; } ;;
"--integration") { integration=true; } ;;
"--integration-coreboot") { integration_coreboot=true; } ;;
"--integration-windows") { integration_windows=true; } ;;
"--volumes")
shift
arg_vols="$1"
;;
"--all") { cargo=true; unit=true; integration=true; } ;;
"--") { shift; break; } ;;
*)
die "Unknown tests argument: $1. Please use --help for help."
;;
esac
shift
done
if [ "$arch" = "aarch64" ] ; then
if [ "$integration_coreboot" = true ] ; then
die "coreboot integration test is not supported for aarch64."
fi
if [ "$integration_windows" = true ] ; then
die "Windows integration test is not supported for aarch64."
fi
fi
ensure_build_dir
ensure_latest_ctr
process_volumes_args
if [ "$unit" = true ] ; then
say "Running unit tests..."
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
--volume "$RHF_WORKLOADS:$CTR_RHF_WORKLOADS" \
"$CTR_IMAGE" \
./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $?
fi
if [ "$cargo" = true ] ; then
say "Running cargo tests..."
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
"$CTR_IMAGE" \
./scripts/run_cargo_tests.sh "$@" || fix_dir_perms $? || exit $?
fi
if [ "$integration" = true ] ; then
say "Running integration tests..."
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--privileged \
--security-opt seccomp=unconfined \
--ipc=host \
--net="$CTR_RHF_NET" \
--mount type=tmpfs,destination=/tmp \
--volume /dev:/dev \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
--volume "$RHF_WORKLOADS:$CTR_RHF_WORKLOADS" \
--env USER="root" \
"$CTR_IMAGE" \
./scripts/run_integration_tests.sh "$@" || fix_dir_perms $? || exit $?
fi
if [ "$integration_coreboot" = true ] ; then
say "Running coreboot integration tests..."
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--privileged \
--security-opt seccomp=unconfined \
--ipc=host \
--net="$CTR_RHF_NET" \
--mount type=tmpfs,destination=/tmp \
--volume /dev:/dev \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
--volume "$RHF_WORKLOADS:$CTR_RHF_WORKLOADS" \
--env USER="root" \
"$CTR_IMAGE" \
./scripts/run_coreboot_integration_tests.sh "$@" || fix_dir_perms $? || exit $?
fi
if [ "$integration_windows" = true ] ; then
say "Running Windows integration tests..."
$DOCKER_RUNTIME run \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--privileged \
--security-opt seccomp=unconfined \
--ipc=host \
--net="$CTR_RHF_NET" \
--mount type=tmpfs,destination=/tmp \
--volume /dev:/dev \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
--volume "$RHF_WORKLOADS:$CTR_RHF_WORKLOADS" \
--env USER="root" \
"$CTR_IMAGE" \
./scripts/run_integration_tests_windows.sh "$@" || fix_dir_perms $? || exit $?
fi
fix_dir_perms $?
}
cmd_build-container() {
arch="$(uname -m)"
while [ $# -gt 0 ]; do
case "$1" in
"-h"|"--help") { cmd_help; exit 1; } ;;
"--") { shift; break; } ;;
*)
die "Unknown build-container argument: $1. Please use --help for help."
;;
esac
shift
done
ensure_build_dir
BUILD_DIR="$RHF_CTR_BUILD_DIR"
mkdir -p $BUILD_DIR
cp $RHF_DOCKERFILE $BUILD_DIR
[ "$arch" = "aarch64" ] && TARGETARCH="arm64"
[ "$arch" = "x86_64" ] && TARGETARCH="amd64"
RUST_TOOLCHAIN="$(rustup show active-toolchain | cut -d ' ' -f1)"
$DOCKER_RUNTIME build \
-t $CTR_IMAGE \
-f $BUILD_DIR/Dockerfile \
--build-arg TARGETARCH=$TARGETARCH \
--build-arg RUST_TOOLCHAIN=$RUST_TOOLCHAIN \
$BUILD_DIR
}
cmd_shell() {
while [ $# -gt 0 ]; do
case "$1" in
"-h"|"--help") { cmd_help; exit 1; } ;;
"--volumes")
shift
arg_vols="$1"
;;
"--") { shift; break; } ;;
*)
;;
esac
shift
done
ensure_build_dir
ensure_latest_ctr
process_volumes_args
say_warn "Starting a privileged shell prompt as root ..."
say_warn "WARNING: Your $RHF_ROOT_DIR folder will be bind-mounted in the container under $CTR_RHF_ROOT_DIR"
$DOCKER_RUNTIME run \
-ti \
--workdir "$CTR_RHF_ROOT_DIR" \
--rm \
--privileged \
--security-opt seccomp=unconfined \
--ipc=host \
--net="$CTR_RHF_NET" \
--tmpfs /tmp:exec \
--volume /dev:/dev \
--volume "$RHF_ROOT_DIR:$CTR_RHF_ROOT_DIR" $exported_volumes \
--volume "$RHF_WORKLOADS:$CTR_RHF_WORKLOADS" \
--env USER="root" \
--entrypoint bash \
"$CTR_IMAGE"
fix_dir_perms $?
}
# Parse main command line args.
#
while [ $# -gt 0 ]; do
case "$1" in
-h|--help) { cmd_help; exit 1; } ;;
-l|--local) {
CTR_IMAGE_VERSION="local"
CTR_IMAGE="${CTR_IMAGE_TAG}:${CTR_IMAGE_VERSION}"
} ;;
-y|--unattended) { OPT_UNATTENDED=true; } ;;
-*)
die "Unknown arg: $1. Please use \`$0 help\` for help."
;;
*)
break
;;
esac
shift
done
# $1 is now a command name. Check if it is a valid command and, if so,
# run it.
#
declare -f "cmd_$1" > /dev/null
ok_or_die "Unknown command: $1. Please use \`$0 help\` for help."
cmd=cmd_$1
shift
$cmd "$@"