#!/bin/sh # The first program in Equipo's CI box (scripts/ci-box.py run). It starts # as root, makes the box look like a runner host, and runs the command as # the job's own user: # # - /etc on a filesystem of its own. Containment lays an overlay over # /etc, and the kernel refuses one whose lower layer is the # container's own overlay root; # - a machine id, which a real host has and the image does not; # - a home directory, with the pinned programs where tests look for them, # and a git identity, without which git refuses to commit; # - a cgroup v2 subtree the user may write, at the path systemd gives a # user session. The cgroup move test moves a contained process between # two cgroups there, as a person on a runner host can. # # Tests never run as root: bubblewrap and file permissions behave # differently for root, and no runner runs as root. The one exception is a # rootless engine on a developer's machine, whose root is the caller. set -eu uid=${EQUIPO_BOX_UID:-0} gid=${EQUIPO_BOX_GID:-0} home=/home/ci if [ "$(id -u)" != 0 ]; then exec "$@" fi mkdir -p /run/equipo-etc mount -t tmpfs -o mode=0755 tmpfs /run/equipo-etc cp -a /etc/. /run/equipo-etc/ mount --bind /run/equipo-etc /etc [ -s /etc/machine-id ] || echo 0ec1b0c1e9015c1b0c5e9015c1b0c1e9 >/etc/machine-id grep -q '^ci:' /etc/passwd || [ "$uid" = 0 ] || echo "ci:x:$uid:$gid:Equipo CI:$home:/bin/sh" >>/etc/passwd grep -q '^ci:' /etc/group || [ "$gid" = 0 ] || echo "ci:x:$gid:" >>/etc/group mkdir -p "$home/.local/share/equipo/try" chown "$uid:$gid" "$home" "$home/.local" "$home/.local/share" "$home/.local/share/equipo" "$home/.local/share/equipo/try" base=/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service if mkdir -p "$base/ci.scope" 2>/dev/null && echo $$ >"$base/ci.scope/cgroup.procs" 2>/dev/null; then for file in "$base" "$base/cgroup.procs" "$base/cgroup.threads" "$base/cgroup.subtree_control" \ "$base/ci.scope" "$base/ci.scope/cgroup.procs" "$base/ci.scope/cgroup.threads" "$base/ci.scope/cgroup.subtree_control"; do chown "$uid:$gid" "$file" done else echo "ci-box: no writable cgroup v2 tree here; the cgroup move test will fail" >&2 fi printf '[user]\n\tname = Equipo CI\n\temail = ci@equipo.invalid\n' >"$home/.gitconfig" [ "$uid" != 0 ] || printf '[safe]\n\tdirectory = *\n' >>"$home/.gitconfig" chown "$uid:$gid" "$home/.gitconfig" export HOME="$home" if [ "$uid" = 0 ]; then exec "$@" fi export USER=ci LOGNAME=ci exec setpriv --reuid "$uid" --regid "$gid" --clear-groups -- "$@"