This commit is contained in:
maurice
2026-08-28 16:37:53 +02:00
parent 7f342a7529
commit 500c6034dd
17 changed files with 213 additions and 268 deletions
+61 -104
View File
@@ -1,125 +1,82 @@
#!/bin/bash
# =============================================== #
# devc.sh v2.0; job79, maurice #
# Dev container enter script. Handles setting up #
# different dev containers, resuming sessions and #
# automatic container updates. #
# devc.sh v2.1; job79, maurice #
# Dev container entry script. #
# =============================================== #
set -eu
log() { printf '\e[%sm%s\e[0m %s\n' "${3:-36}" "${2:-}" "$1"; }
arg() { echo -n " $@"; }
set -euo pipefail
log() { echo -e "\e[36m○\e[0m $1"; }
die() { echo -e "\e[31mx\e[0m $1" && exit 1; }
# run_args returns the podman run arguments required for
# starting a new container.
# default_args configures standard container options.
default_args() {
arg "--name $name"
arg "--hostname $name"
run_opts+=(
"--name" "$name"
"--hostname" "$name"
"--network" "devc"
"--pull=newer" # Update image.
"--userns=keep-id" # Map host user.
"-v" "$name:/home/user:copy" # Persistent home volume.
"-v" "dnf-cache:/var/cache/libdnf5" # Cache dnf metadata.
)
# Pull newer container image if available.
# arg "--pull=newer"
# Unix sockets require SELinux label disable.
[[ -d /sys/fs/selinux ]] && run_opts+=("--security-opt" "label=disable")
# Use keep-id so the container user matches the host user.
arg "--userns=keep-id"
# Desktop integration (Wayland, SSH).
[[ -e "/run/user/$UID/wayland-0" ]] && run_opts+=("-v" "/run/user/$UID/wayland-0:/run/user/1000/wayland-0")
[[ -e "${SSH_AUTH_SOCK:-}" ]] && run_opts+=("-v" "$SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock")
# Disable selinux labeling so unix sockets can be mounted
# without problems.
arg "--security-opt label=disable"
# Mount the wayland socket. Required to get the system
# clipboard (wl-copy) and gui applications working.
[ -e "/run/user/$UID/wayland-0" ] && arg "-v /run/user/$UID/wayland-0:/run/user/1000/wayland-0"
# Mount the ssh socket to get ssh working.
[ -e "$SSH_AUTH_SOCK" ] && arg "-v $SSH_AUTH_SOCK:/run/user/1000/ssh-auth-sock"
# Make the user home dir a volume so it survives container
# restarts. Use copy to keep the files from the image.
arg "-v $name:/home/user:copy"
# If there is custom configuration for the container, load
# it here.
config_file="$(dirname "$(realpath "$0")")/containers/$name/config.sh"
[ -f "$config_file" ] && source "${config_file}"
# Load custom container config.
local config_file="${BASH_SOURCE[0]%/*}/container/config.sh"
[[ -f "$config_file" ]] && source "$config_file" || true
}
# param_args returns the podman run arguments based on the
# arguments provided to this script.
# param_args parses CLI arguments into podman run options.
param_args() {
while test $# -gt 0; do
while (($# > 0)); do
case "$1" in
-gpu) # Enable gpu acceleration.
arg "--device /dev/dri" ;;
-kvm) # Enable KVM
arg "--device /dev/kvm" ;;
-usb) # Enable USB access
arg "--device /dev/bus/usb" ;;
-host-spawn) # Enable spawning host commands from inside the container using host-spawn.
arg "-v /run/user/$UID/bus:/tmp/bus"
arg "-e HOST_HOME=$HOME" # Used to translate paths.
;;
-net) # Enable network 'dev-<container name>'' and 'devc'
arg "--network dev-$name --network devc"
;;
-mnt) # Mount directory.
shift
arg "-w /workdir/"
arg "-v $1:/workdir/$([ ! -d "$1" ] && echo $1)"
;;
-mdf) # Mount dev folder
arg "-v $HOME/dev:/home/user/dev"
;;
-x11) # Enable X11 support.
arg "-v /tmp/.X11-unix:/tmp/.X11-unix"
arg "-v $XAUTHORITY:/run/user/1000/.Xauthority:ro"
arg "-e DISPLAY=$DISPLAY"
arg "-e XAUTHORITY=/run/user/1000/.Xauthority"
;;
*) # Use unknown arguments a podman arguments.
arg "$1" ;;
-gpu) run_opts+=("--device" "/dev/dri") ;;
-kvm) run_opts+=("--device" "/dev/kvm") ;;
-usb) run_opts+=("--device" "/dev/bus/usb") ;;
-host-spawn) run_opts+=("-v" "/run/user/$UID/bus:/tmp/bus" "-e" "HOST_HOME=$HOME") ;;
-container-sock) run_opts+=("-v" "$XDG_RUNTIME_DIR/podman/podman.sock:/var/run/docker.sock") ;;
-x11) run_opts+=("-v" "/tmp/.X11-unix:/tmp/.X11-unix" "-v" "$XAUTHORITY:/run/user/1000/.Xauthority:ro" "-e" "DISPLAY=$DISPLAY" "-e" "XAUTHORITY=/run/user/1000/.Xauthority") ;;
-mnt) shift && run_opts+=("-w" "/mnt/" "-v" "$1:/mnt/$([[ -d "$1" ]] || echo "file")") ;;
*) run_opts+=("$1") ;;
esac
shift
done
}
### MAIN ###
# Get the devcontainer name from the first argument. If not
# provided, use the last used name when possible.
if [[ $# -gt 0 ]] && [[ ${1:-} != -* ]]; then
image="$1"
[[ "$image" != *:* ]] && image="$image:main"
echo "$image" >"$HOME/.local/share/devc-previous-container"
shift
elif [ -f "$HOME/.local/share/devc-previous-container" ]; then
image=$(<"$HOME/.local/share/devc-previous-container")
else
log "no container name specified" 'x' 31
exit 1
fi
name="${image%:*}"
main() {
local state_file="$HOME/.local/share/devc-previous-container"
local image="devc"
# Get container registry from the DEVC_REGISTRY env
# variable.
if [ -n "${DEVC_REGISTRY:-}" ]; then
registry="$DEVC_REGISTRY"
else
log "registry unknown; set the DEVC_REGISTRY environment variable" 'x' 31
exit 1
fi
# Resolve container name (CLI arg > Last used > Error).
if [[ $image && $image != -* ]]; then
shift
[[ $image != *:* ]] && image+=":main"
echo "$image" >"$state_file"
elif [[ -f $state_file ]]; then
image=$(<$state_file)
else
die "no container name specified"
fi
local name="${image%:*}"
# Get container command from the DEVC_COMMAND env variable
# if set, else use bash -l.
if [ -z "${DEVC_COMMAND:-}" ]; then
DEVC_COMMAND="bash -l"
fi
# Start/Restart if not running or if arguments change configuration.
if [[ -z "$(podman ps -q -f name="^$name$" -f status=running)" ]] || (($# > 0)); then
log "starting $image..."
# When container is not running or arguments are provided,
# recreate it.
if [ "$(podman container inspect "$name" -f {{.State.Running}} 2>&1)" != 'true' ] || [[ $# -gt 0 ]]; then
log "starting devcontainer..."
podman network create --ignore "dev-$name"
podman network create --ignore "devc"
podman container rm -f -t 0 "$name" 1>/dev/null
podman run -td $(default_args) $(param_args $@) "$registry/$image"
fi
default_args
param_args "$@"
podman exec --detach-keys "ctrl-@" -it "$name" ${DEVC_COMMAND:-}
[[ -n "${DEVC_REGISTRY:-}" ]] || die "registry unknown; set the DEVC_REGISTRY environment variable"
podman network create devc --ignore > /dev/null 2>&1
podman run --replace --stop-timeout 0 -td "${run_opts[@]}" "$DEVC_REGISTRY/$image"
fi
exec podman exec --detach-keys "ctrl-@,ctrl-@" -it "$name" ${DEVC_COMMAND:-bash -l}
}
main "$@"