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="v1" 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# Cargo paths 31# Full path to the cargo registry dir on the host. This appears on the host 32# because we want to persist the cargo registry across container invocations. 33# Otherwise, any rust crates from crates.io would be downloaded again each time 34# we build or test. 35CARGO_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_registry" 36 37# Full path to the cargo git registry on the host. This serves the same purpose 38# as CARGO_REGISTRY_DIR, for crates downloaded from GitHub repos instead of 39# crates.io. 40CARGO_GIT_REGISTRY_DIR="${CLH_BUILD_DIR}/cargo_git_registry" 41 42# Full path to the cargo target dir on the host. 43CARGO_TARGET_DIR="${CLH_BUILD_DIR}/cargo_target" 44 45# Send a decorated message to stdout, followed by a new line 46# 47say() { 48 [ -t 1 ] && [ -n "$TERM" ] \ 49 && echo "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \ 50 || echo "[$CLI_NAME] $*" 51} 52 53# Send a decorated message to stdout, without a trailing new line 54# 55say_noln() { 56 [ -t 1 ] && [ -n "$TERM" ] \ 57 && echo -n "$(tput setaf 2)[$CLI_NAME]$(tput sgr0) $*" \ 58 || echo "[$CLI_NAME] $*" 59} 60 61# Send a text message to stderr 62# 63say_err() { 64 [ -t 2 ] && [ -n "$TERM" ] \ 65 && echo "$(tput setaf 1)[$CLI_NAME] $*$(tput sgr0)" 1>&2 \ 66 || echo "[$CLI_NAME] $*" 1>&2 67} 68 69# Send a warning-highlighted text to stdout 70say_warn() { 71 [ -t 1 ] && [ -n "$TERM" ] \ 72 && echo "$(tput setaf 3)[$CLI_NAME] $*$(tput sgr0)" \ 73 || echo "[$CLI_NAME] $*" 74} 75 76# Exit with an error message and (optional) code 77# Usage: die [-c <error code>] <error message> 78# 79die() { 80 code=1 81 [[ "$1" = "-c" ]] && { 82 code="$2" 83 shift 2 84 } 85 say_err "$@" 86 exit $code 87} 88 89# Exit with an error message if the last exit code is not 0 90# 91ok_or_die() { 92 code=$? 93 [[ $code -eq 0 ]] || die -c $code "$@" 94} 95 96# Make sure the build/ dirs are available. Exit if we can't create them. 97# Upon returning from this call, the caller can be certain the build/ dirs exist. 98# 99ensure_build_dir() { 100 for dir in "$CLH_BUILD_DIR" \ 101 "$CLH_INTEGRATION_WORKLOADS" \ 102 "$CLH_CTR_BUILD_DIR" \ 103 "$CARGO_TARGET_DIR" \ 104 "$CARGO_REGISTRY_DIR" \ 105 "$CARGO_GIT_REGISTRY_DIR"; do 106 mkdir -p "$dir" || die "Error: cannot create dir $dir" 107 [ -x "$dir" ] && [ -w "$dir" ] || \ 108 { 109 say "Wrong permissions for $dir. Attempting to fix them ..." 110 chmod +x+w "$dir" 111 } || \ 112 die "Error: wrong permissions for $dir. Should be +x+w" 113 done 114} 115 116# Make sure we're using the latest dev container, by just pulling it. 117ensure_latest_ctr() { 118 $DOCKER_RUNTIME pull "$CTR_IMAGE" 119 120 ok_or_die "Error pulling container image. Aborting." 121} 122 123# Fix main directory permissions after a container ran as root. 124# Since the container ran as root, any files it creates will be owned by root. 125# This fixes that by recursively changing the ownership of /cloud-hypervisor to the 126# current user. 127# 128fix_dir_perms() { 129 # Yes, running Docker to get elevated privileges, just to chown some files 130 # is a dirty hack. 131 $DOCKER_RUNTIME run \ 132 --workdir "$CTR_CLH_ROOT_DIR" \ 133 --rm \ 134 --volume /dev:/dev \ 135 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 136 "$CTR_IMAGE" \ 137 chown -R "$(id -u):$(id -g)" "$CTR_CLH_ROOT_DIR" 138 139 return $1 140} 141 142cmd_help() { 143 echo "" 144 echo "Cloud Hypervisor $(basename $0)" 145 echo "Usage: $(basename $0) <command> [<command args>]" 146 echo "" 147 echo "Available commands:" 148 echo "" 149 echo " build [--debug|--release] [-- [<cargo args>]]" 150 echo " Build the Cloud Hypervisor binaries." 151 echo " --debug Build the debug binaries. This is the default." 152 echo " --release Build the release binaries." 153 echo "" 154 echo " tests [--unit|--cargo|--all] [-- [<cargo test args>]]" 155 echo " Run the Cloud Hypervisor tests." 156 echo " --unit Run the unit tests." 157 echo " --cargo Run the cargo tests." 158 echo " --integration Run the integration tests." 159 echo " --all Run all tests." 160 echo "" 161 echo " build-container [--type]" 162 echo " Build the Cloud Hypervisor container." 163 echo " --dev Build dev container. This is the default." 164 echo "" 165 echo " clean [<cargo args>]]" 166 echo " Remove the Cloud Hypervisor artifacts." 167 echo "" 168 echo " help" 169 echo " Display this help message." 170 echo "" 171} 172 173cmd_build() { 174 build="debug" 175 176 while [ $# -gt 0 ]; do 177 case "$1" in 178 "-h"|"--help") { cmd_help; exit 1; } ;; 179 "--debug") { build="debug"; } ;; 180 "--release") { build="release"; } ;; 181 "--") { shift; break; } ;; 182 *) 183 die "Unknown build argument: $1. Please use --help for help." 184 ;; 185 esac 186 shift 187 done 188 189 cargo_args=("$@") 190 [ $build = "release" ] && cargo_args+=("--release") 191 192 $DOCKER_RUNTIME run \ 193 --user "$(id -u):$(id -g)" \ 194 --workdir "$CTR_CLH_ROOT_DIR" \ 195 --rm \ 196 --volume /dev:/dev \ 197 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 198 "$CTR_IMAGE" \ 199 cargo build \ 200 --target-dir "$CTR_CLH_CARGO_TARGET" \ 201 "${cargo_args[@]}" && say "Binaries placed under $CLH_CARGO_TARGET/$build" 202} 203 204cmd_clean() { 205 cargo_args=("$@") 206 207 $DOCKER_RUNTIME run \ 208 --user "$(id -u):$(id -g)" \ 209 --workdir "$CTR_CLH_ROOT_DIR" \ 210 --rm \ 211 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 212 "$CTR_IMAGE" \ 213 cargo clean \ 214 --target-dir "$CTR_CLH_CARGO_TARGET" \ 215 "${cargo_args[@]}" 216 } 217 218cmd_tests() { 219 unit=false 220 cargo=false 221 integration=false 222 223 while [ $# -gt 0 ]; do 224 case "$1" in 225 "-h"|"--help") { cmd_help; exit 1; } ;; 226 "--unit") { unit=true; } ;; 227 "--cargo") { cargo=true; } ;; 228 "--integration") { integration=true; } ;; 229 "--all") { cargo=true; unit=true; integration=true; } ;; 230 "--") { shift; break; } ;; 231 *) 232 die "Unknown tests argument: $1. Please use --help for help." 233 ;; 234 esac 235 shift 236 done 237 238 if [ "$unit" = true ] ; then 239 say "Running unit tests..." 240 $DOCKER_RUNTIME run \ 241 --workdir "$CTR_CLH_ROOT_DIR" \ 242 --rm \ 243 --device /dev/kvm \ 244 --device /dev/net/tun \ 245 --cap-add net_admin \ 246 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 247 "$CTR_IMAGE" \ 248 ./scripts/run_unit_tests.sh "$@" || fix_dir_perms $? || exit $? 249 fi 250 251 if [ "$cargo" = true ] ; then 252 say "Running cargo tests..." 253 $DOCKER_RUNTIME run \ 254 --workdir "$CTR_CLH_ROOT_DIR" \ 255 --rm \ 256 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 257 "$CTR_IMAGE" \ 258 ./scripts/run_cargo_tests.sh || fix_dir_perms $? || exit $? 259 fi 260 261 if [ "$integration" = true ] ; then 262 say "Running integration tests..." 263 $DOCKER_RUNTIME run \ 264 --workdir "$CTR_CLH_ROOT_DIR" \ 265 --rm \ 266 --privileged \ 267 --security-opt seccomp=unconfined \ 268 --ipc=host \ 269 --net=host \ 270 --mount type=tmpfs,destination=/tmp \ 271 --volume /dev:/dev \ 272 --volume "$CLH_ROOT_DIR:$CTR_CLH_ROOT_DIR" \ 273 --volume "$CLH_INTEGRATION_WORKLOADS:$CTR_CLH_INTEGRATION_WORKLOADS" \ 274 --env USER="root" \ 275 "$CTR_IMAGE" \ 276 ./scripts/run_integration_tests.sh "$@" || fix_dir_perms $? || exit $? 277 fi 278 279 fix_dir_perms $? 280} 281 282cmd_build-container() { 283 container_type="dev" 284 285 while [ $# -gt 0 ]; do 286 case "$1" in 287 "-h"|"--help") { cmd_help; exit 1; } ;; 288 "--dev") { container_type="dev"; } ;; 289 "--") { shift; break; } ;; 290 *) 291 die "Unknown build-container argument: $1. Please use --help for help." 292 ;; 293 esac 294 shift 295 done 296 297 BUILD_DIR=/tmp/cloud-hypervisor/container/ 298 299 mkdir -p $BUILD_DIR 300 cp $CLH_DOCKERFILE $BUILD_DIR 301 302 $DOCKER_RUNTIME build \ 303 --target $container_type \ 304 -t $CTR_IMAGE \ 305 -f $BUILD_DIR/Dockerfile \ 306 $BUILD_DIR 307} 308 309# Parse main command line args. 310# 311while [ $# -gt 0 ]; do 312 case "$1" in 313 -h|--help) { cmd_help; exit 1; } ;; 314 -y|--unattended) { OPT_UNATTENDED=true; } ;; 315 -*) 316 die "Unknown arg: $1. Please use \`$0 help\` for help." 317 ;; 318 *) 319 break 320 ;; 321 esac 322 shift 323done 324 325# $1 is now a command name. Check if it is a valid command and, if so, 326# run it. 327# 328declare -f "cmd_$1" > /dev/null 329ok_or_die "Unknown command: $1. Please use \`$0 help\` for help." 330 331cmd=cmd_$1 332shift 333 334ensure_build_dir 335ensure_latest_ctr 336 337$cmd "$@" 338