repositories / bugabinga.net
bugabinga.net
personal infrastructure for bugabinga!
owned by admin
scripts/remote/build-rootless-image.sh
Raw#!/usr/bin/env bash
set -euo pipefail
[[ "$#" -eq 2 ]] || {
echo "usage: build-rootless-image.sh USER IMAGE" >&2
exit 2
}
user="$1"
image="$2"
[[ "${user}" =~ ^[a-z_][a-z0-9_-]*$ && "${image}" =~ ^[A-Za-z0-9._/@:-]+$ ]] || {
echo "unsafe build arguments" >&2
exit 2
}
home_dir="$(getent passwd "${user}" | cut -d: -f6)"
uid="$(id -u "${user}")"
runtime="/run/user/${uid}"
build_dir="$(mktemp -d /tmp/bugabinga-image-build.XXXXXX)"
cleanup() {
rm -rf -- "${build_dir}"
}
trap cleanup EXIT
cd /tmp
[[ -n "${home_dir}" ]] || {
echo "user has no home directory: ${user}" >&2
exit 2
}
install -d -o "${user}" -g "${user}" -m 0700 "${runtime}"
chown "${user}:${user}" "${build_dir}"
runuser -u "${user}" -- tar -C "${build_dir}" -xf -
runuser -u "${user}" -- env \
HOME="${home_dir}" \
USER="${user}" \
LOGNAME="${user}" \
XDG_RUNTIME_DIR="${runtime}" \
DBUS_SESSION_BUS_ADDRESS="unix:path=${runtime}/bus" \
podman build -t "${image}" -f "${build_dir}/Containerfile" "${build_dir}"