dev_container: Improve support for podman

Commit 95b80d1 made the dev container persistent between invocations,
but hard-coded `docker` again. This change fixes that and also adds
further improvements:
- Do not use `--privileged` with podman. If the rootless user has
  permissions to access `/dev/kvm`, so will the container.
- Map `/dev/vhost-net` and `/dev/vhost-vsock` as well.
- Use `BASH_SOURCE` to find this script's directory. As we're using Bash
  to start with, this is more robust than using plain `$0`.

BUG=None
TEST=Run `./tools/dev_container cargo build` with Podman and Docker

Change-Id: I05c699f327c8e1c4f3c4df9679ee92bf7e609e2e
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3295372
Reviewed-by: Dennis Kempin <denniskempin@google.com>
Tested-by: kokoro <noreply+kokoro@google.com>
Commit-Queue: Christian Blichmann <cblichmann@google.com>
This commit is contained in:
Christian Blichmann 2021-11-18 13:20:40 +01:00 committed by Commit Bot
parent 2d45b91e00
commit 1a2cfa90c3

View file

@ -21,7 +21,7 @@
# ./tools/dev_container --hermetic CMD
set -e
cd "$(dirname "$0")/.."
cd "$(dirname "${BASH_SOURCE[0]}")/.."
# Allow to override the container CLI tool, similar to the Makefile. Try
# "docker" first and fall back to "podman".
@ -41,13 +41,23 @@ if [ -t 1 ]; then
)
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/net/tun"
--device "/dev/kvm"
--volume "/dev/log:/dev/log"
--privileged
--device "/dev/net/tun"
--device "/dev/vhost-net"
--device "/dev/vhost-vsock"
"${PRIV_ARGS[@]}"
"gcr.io/crosvm-packages/crosvm_dev:$IMAGE_VERSION"
)
@ -62,7 +72,7 @@ docker_run() {
}
get_container_id() {
docker ps -q -f name="${CONTAINER_NAME}"
"${DOCKER}" ps -q -f name="${CONTAINER_NAME}"
}
docker_exec() {
@ -77,7 +87,7 @@ docker_exec() {
main() {
if [[ "$1" == "--stop" ]]; then
if [ -n "$(get_container_id)" ]; then
docker rm -f "$(get_container_id)" >/dev/null
"${DOCKER}" rm -f "$(get_container_id)" >/dev/null
fi
exit
fi