1#!/bin/bash
2
3#
4# DOCKER_CMD should be either `docker` or `podman`.
5#
6# if using (rootless) podman, remember to set /etc/subuid
7# and /etc/subgid.
8#
9docker_cmd=${DOCKER_CMD:-"docker"}
10[ "$DOCKER_CMD" = "podman" ] && userns_podman="--userns=keep-id" selinux=",z"
11
12einfo() {
13    echo "$*" >&2
14}
15
16die() {
17    echo "$*" >&2
18    exit 1
19}
20
21#
22# The caller is expected to override the CONTAINER environment
23# variable with the container they wish to launch.
24#
25BASE="registry.gitlab.com/xen-project/xen"
26case "_${CONTAINER}" in
27    _alpine) CONTAINER="${BASE}/alpine:3.18" ;;
28    _alpine-arm64v8) CONTAINER="${BASE}/alpine:3.18-arm64v8" ;;
29    _archlinux|_arch) CONTAINER="${BASE}/archlinux:current" ;;
30    _centos7) CONTAINER="${BASE}/centos:7" ;;
31    _fedora) CONTAINER="${BASE}/fedora:41-x86_64";;
32    _bullseye-ppc64le) CONTAINER="${BASE}/debian:11-ppc64le" ;;
33    _bookworm-ppc64le) CONTAINER="${BASE}/debian:12-ppc64le" ;;
34    _bookworm-riscv64) CONTAINER="${BASE}/debian:12-riscv64" ;;
35    _bookworm-x86_64-gcc-ibt) CONTAINER="${BASE}/debian:12-x86_64-gcc-ibt" ;;
36    _bookworm|_bookworm-x86_64|_) CONTAINER="${BASE}/debian:12-x86_64" ;;
37    _bookworm-i386|_bookworm-x86_32) CONTAINER="${BASE}/debian:12-x86_32" ;;
38    _bookworm-arm64v8-arm32-gcc) CONTAINER="${BASE}/debian:bookworm-arm64v8-arm32-gcc" ;;
39    _bookworm-arm64v8) CONTAINER="${BASE}/debian:bookworm-arm64v8" ;;
40    _bookworm-cppcheck) CONTAINER="${BASE}/debian:bookworm-cppcheck" ;;
41    _opensuse-leap|_leap) CONTAINER="${BASE}/opensuse:leap-15.6-x86_64" ;;
42    _opensuse-tumbleweed|_tumbleweed) CONTAINER="${BASE}/opensuse:tumbleweed-x86_64" ;;
43    _xenial) CONTAINER="${BASE}/ubuntu:16.04-x86_64" ;;
44    _bionic) CONTAINER="${BASE}/ubuntu:18.04-x86_64" ;;
45    _focal)  CONTAINER="${BASE}/ubuntu:20.04-x86_64" ;;
46    _jammy)  CONTAINER="${BASE}/ubuntu:22.04-x86_64" ;;
47    _noble)  CONTAINER="${BASE}/ubuntu:24.04-x86_64" ;;
48esac
49
50# Use this variable to control whether root should be used
51case "_${CONTAINER_UID0}" in
52    _1)   userarg="-u 0" ;;
53    _0|_) userarg="-u $(id -u) $userns_podman" ;;
54esac
55
56# Save the commands for future use
57cmd=("$@")
58
59# If no command was specified, just drop us into a shell if we're interactive
60[ $# -eq 0 ] && tty -s && cmd=("/bin/bash")
61
62# Are we in an interactive terminal?
63tty -s && termint=t
64
65#
66# Fetch the latest version of the container in hub.docker.com,
67# unless it's a newly created local copy.
68#
69if [[ "_${CONTAINER_NO_PULL}" != "_1" ]]; then
70    einfo "*** Ensuring ${CONTAINER} is up to date"
71    ${docker_cmd} pull ${CONTAINER} > /dev/null ||     \
72        die "Failed to update container"
73fi
74
75if hash greadlink > /dev/null 2>&1; then
76    READLINK=greadlink
77elif [[ $(uname -s) == "Darwin" ]]; then
78    echo "Unable to forward SSH agent without coreutils installed"
79    unset SSH_AUTH_SOCK
80else
81    READLINK=readlink
82fi
83
84# Ensure we've got what we need for SSH_AUTH_SOCK
85if [[ -n ${SSH_AUTH_SOCK} ]]; then
86    fullpath_sock=$(${READLINK} -f ${SSH_AUTH_SOCK} 2> /dev/null)
87    if [ $? -ne 0 ]; then
88        echo "Invalid SSH_AUTH_SOCK: ${SSH_AUTH_SOCK}"
89        unset SSH_AUTH_SOCK
90    else
91        SSH_AUTH_DIR=$(dirname ${fullpath_sock})
92        SSH_AUTH_NAME=$(basename ${fullpath_sock})
93    fi
94fi
95
96# Figure out the base of what we want as our sources
97# by using the top of the git repo
98if [[ -z ${CONTAINER_PATH} ]]; then
99    CONTAINER_PATH=$(git rev-parse --show-toplevel)
100fi
101
102# Kick off Docker
103einfo "*** Launching container ..."
104exec ${docker_cmd} run \
105    ${userarg} \
106    ${SSH_AUTH_SOCK:+-e SSH_AUTH_SOCK="/tmp/ssh-agent/${SSH_AUTH_NAME}"} \
107    -v "${CONTAINER_PATH}":/build:rw${selinux} \
108    -v "${HOME}/.ssh":/root/.ssh:ro \
109    ${SSH_AUTH_DIR:+-v "${SSH_AUTH_DIR}":/tmp/ssh-agent${selinux}} \
110    ${CONTAINER_ARGS} \
111    -${termint}i --rm -- \
112    ${CONTAINER} \
113    "${cmd[@]}"
114