| 1 | #!/bin/sh |
| 2 | # The first program in Equipo's CI box (scripts/ci-box.py run). It starts |
| 3 | # as root, makes the box look like a runner host, and runs the command as |
| 4 | # the job's own user: |
| 5 | # |
| 6 | # - /etc on a filesystem of its own. Containment lays an overlay over |
| 7 | # /etc, and the kernel refuses one whose lower layer is the |
| 8 | # container's own overlay root; |
| 9 | # - a machine id, which a real host has and the image does not; |
| 10 | # - a home directory, with the pinned programs where tests look for them, |
| 11 | # and a git identity, without which git refuses to commit; |
| 12 | # - a cgroup v2 subtree the user may write, at the path systemd gives a |
| 13 | # user session. The cgroup move test moves a contained process between |
| 14 | # two cgroups there, as a person on a runner host can. |
| 15 | # |
| 16 | # Tests never run as root: bubblewrap and file permissions behave |
| 17 | # differently for root, and no runner runs as root. The one exception is a |
| 18 | # rootless engine on a developer's machine, whose root is the caller. |
| 19 | set -eu |
| 20 | |
| 21 | uid=${EQUIPO_BOX_UID:-0} |
| 22 | gid=${EQUIPO_BOX_GID:-0} |
| 23 | home=/home/ci |
| 24 | |
| 25 | if [ "$(id -u)" != 0 ]; then |
| 26 | exec "$@" |
| 27 | fi |
| 28 | |
| 29 | mkdir -p /run/equipo-etc |
| 30 | mount -t tmpfs -o mode=0755 tmpfs /run/equipo-etc |
| 31 | cp -a /etc/. /run/equipo-etc/ |
| 32 | mount --bind /run/equipo-etc /etc |
| 33 | [ -s /etc/machine-id ] || echo 0ec1b0c1e9015c1b0c5e9015c1b0c1e9 >/etc/machine-id |
| 34 | |
| 35 | grep -q '^ci:' /etc/passwd || [ "$uid" = 0 ] || echo "ci:x:$uid:$gid:Equipo CI:$home:/bin/sh" >>/etc/passwd |
| 36 | grep -q '^ci:' /etc/group || [ "$gid" = 0 ] || echo "ci:x:$gid:" >>/etc/group |
| 37 | mkdir -p "$home/.local/share/equipo/try" |
| 38 | chown "$uid:$gid" "$home" "$home/.local" "$home/.local/share" "$home/.local/share/equipo" "$home/.local/share/equipo/try" |
| 39 | |
| 40 | base=/sys/fs/cgroup/user.slice/user-$uid.slice/user@$uid.service |
| 41 | if mkdir -p "$base/ci.scope" 2>/dev/null && echo $$ >"$base/ci.scope/cgroup.procs" 2>/dev/null; then |
| 42 | for file in "$base" "$base/cgroup.procs" "$base/cgroup.threads" "$base/cgroup.subtree_control" \ |
| 43 | "$base/ci.scope" "$base/ci.scope/cgroup.procs" "$base/ci.scope/cgroup.threads" "$base/ci.scope/cgroup.subtree_control"; do |
| 44 | chown "$uid:$gid" "$file" |
| 45 | done |
| 46 | else |
| 47 | echo "ci-box: no writable cgroup v2 tree here; the cgroup move test will fail" >&2 |
| 48 | fi |
| 49 | |
| 50 | printf '[user]\n\tname = Equipo CI\n\temail = ci@equipo.invalid\n' >"$home/.gitconfig" |
| 51 | [ "$uid" != 0 ] || printf '[safe]\n\tdirectory = *\n' >>"$home/.gitconfig" |
| 52 | chown "$uid:$gid" "$home/.gitconfig" |
| 53 | export HOME="$home" |
| 54 | if [ "$uid" = 0 ]; then |
| 55 | exec "$@" |
| 56 | fi |
| 57 | export USER=ci LOGNAME=ci |
| 58 | exec setpriv --reuid "$uid" --regid "$gid" --clear-groups -- "$@" |
| 59 | |