crosvm/tools/dev_container
Anton Romanov bbec8ebf65 Use tmpfs for /tmp in dev container
This works around quirkiness of sparse file support in overlayfs.

Revert "sys_util: ignore seek_hole tests for kokoro uprev"
This reverts commit 3d0e51f71c.

BUG=b:208901617

TEST=./tools/dev_container --hermetic bash -c "cd common/sys_util; cargo test seek_"

Change-Id: Id4d809f09a71b5cd134b5eb9bf6a5f970e5503c8
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3319404
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Anton Romanov <romanton@google.com>
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Commit-Queue: Anton Romanov <romanton@google.com>
2021-12-07 17:44:17 +00:00

108 lines
2.5 KiB
Bash
Executable file

#!/usr/bin/env bash
# Copyright 2021 The Chromium OS Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# Usage:
#
# To get an interactive shell for development:
# ./tools/dev_container
#
# To run a command in the container, e.g. to run presubmits:
# ./tools/dev_container ./tools/presubmit
#
# The state of the container (including build artifacts) are preserved between
# calls. To stop the container call:
# ./tools/dev_container --stop
#
# The dev container can also be called with a fresh container for each call that
# is cleaned up afterwards (e.g. when run by Kokoro):
#
# ./tools/dev_container --hermetic CMD
set -e
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# Allow to override the container CLI tool, similar to the Makefile. Try
# "docker" first and fall back to "podman".
DOCKER=${DOCKER:-$(which docker || which podman)}
# Name to identify the running crosvm dev container
CONTAINER_NAME=crosvm_dev
IMAGE_VERSION=$(cat tools/impl/dev_container/version)
# Enable interactive mode when running in an interactive terminal.
TTY_ARGS=()
if [ -t 1 ]; then
TTY_ARGS+=(
--interactive
--tty
)
fi
# Podman will not share devices when `--privileged` is specified
PRIV_ARGS=()
if [ "${DOCKER}" != "${podman}" ]; then
PRIV_ARGS+=(
--privileged
)
fi
DOCKER_ARGS=(
"${TTY_ARGS[@]}"
--volume "$(pwd):/workspace:rw"
--device "/dev/kvm"
--volume "/dev/log:/dev/log"
--device "/dev/net/tun"
--device "/dev/vhost-net"
--device "/dev/vhost-vsock"
--mount "type=tmpfs,destination=/tmp"
"${PRIV_ARGS[@]}"
"gcr.io/crosvm-packages/crosvm_dev:$IMAGE_VERSION"
)
docker_run_detached() {
"${DOCKER}" run \
--detach --name "${CONTAINER_NAME}" \
"${DOCKER_ARGS[@]}" >/dev/null
}
docker_run() {
"${DOCKER}" run --rm "${DOCKER_ARGS[@]}" "$@"
}
get_container_id() {
"${DOCKER}" ps -q -f name="${CONTAINER_NAME}"
}
docker_exec() {
if [[ $# -gt 0 ]]; then
cmd=("$@")
else
cmd=(/bin/bash)
fi
"${DOCKER}" exec "${TTY_ARGS[@]}" "$(get_container_id)" "${cmd[@]}"
}
main() {
if [[ "$1" == "--stop" ]]; then
if [ -n "$(get_container_id)" ]; then
"${DOCKER}" rm -f "$(get_container_id)" >/dev/null
fi
exit
fi
if [[ "$1" == "--hermetic" ]]; then
shift
docker_run "$@"
else
if [ -z "$(get_container_id)" ]; then
docker_run_detached
fi
docker_exec "$@"
fi
}
main "$@"