• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright 2021 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5#
6# Runs a crosvm builder. Will use podman if available, falls back to docker.
7# Usage:
8# run_container.sh builder_name entry point args...
9#
10# The scratch or logs directory can be enabled by setting the env variables
11# CROSVM_BUILDER_SCRATCH_DIR or CROSVM_BUILDER_LOGS_DIR.
12
13crosvm_root=$(realpath "$(dirname $0)/..")
14cros_root=$(realpath "${crosvm_root}/../../..")
15
16if [ ! -d "${cros_root}/.repo" ]; then
17    echo "The CI builder must be run from a cros checkout. See ci/README.md"
18    exit 1
19fi
20
21# Parse parameters
22builder="$1"
23shift
24
25# User podman if available for root-less execution. Fall-back to docker.
26if which podman >/dev/null; then
27    run() {
28        # The run.oci.keep_original_groups flag allows us to access devices to
29        # which the calling user only has access via a group membership (i.e.
30        # /dev/kvm). See: https://github.com/containers/podman/issues/4477
31        podman run \
32            --runtime /usr/bin/crun \
33            --annotation run.oci.keep_original_groups=1 \
34            --security-opt label=disable \
35            "$@"
36    }
37else
38    run() {
39        docker run "$@"
40    }
41fi
42
43version=$(cat $(dirname $0)/image_tag)
44echo "Using builder: ${builder}:${version}"
45
46src="${cros_root}/src"
47echo "Using source directory: ${src} (Available at /workspace/src)"
48
49docker_args=(
50    --rm
51    --device /dev/kvm
52    --volume /dev/log:/dev/log
53    --volume "${src}":/workspace/src:rw
54)
55
56if [ ! -z "${CROSVM_BUILDER_SCRATCH_DIR}" ]; then
57    echo "Using scratch directory: ${CROSVM_BUILDER_SCRATCH_DIR}\
58 (Available at /workspace/scratch)"
59    mkdir -p "${CROSVM_BUILDER_SCRATCH_DIR}"
60    docker_args+=(
61        --volume "${CROSVM_BUILDER_SCRATCH_DIR}:/workspace/scratch:rw"
62    )
63fi
64
65if [ ! -z "${CROSVM_BUILDER_LOGS_DIR}" ]; then
66    echo "Using logs directory: ${CROSVM_BUILDER_LOGS_DIR}\
67 (Available at /workspace/logs)"
68    mkdir -p "${CROSVM_BUILDER_LOGS_DIR}"
69    docker_args+=(--volume "${CROSVM_BUILDER_LOGS_DIR}":/workspace/logs:rw)
70fi
71
72# Enable interactive mode when running in an interactive terminal.
73if [ -t 1 ]; then
74    docker_args+=(-it)
75fi
76
77echo ""
78run ${docker_args[@]} \
79    "gcr.io/crosvm-packages/${builder}:${version}" \
80    "$@"
81