#!/usr/bin/env bash set -euo pipefail root={{ desktop_ai_media_root | quote }} build_root={{ desktop_ai_media_build_root | quote }} {% set manage_luks = desktop_ai_media_external_storage_enabled | default(false) and desktop_ai_media_external_luks_uuid | default('') %} {% if manage_luks %} encrypted={{ ('/dev/disk/by-uuid/' + desktop_ai_media_external_luks_uuid) | quote }} external_mount={{ desktop_ai_media_external_mountpoint | quote }} bind_fsroot={{ (desktop_ai_media_external_source | replace(desktop_ai_media_external_mountpoint, '', 1)) | quote }} fail() { echo "$*" >&2; exit 1; } read_mapper() { local devices mapping devices=$(lsblk -nrpo NAME,TYPE,MAJ:MIN -- "$encrypted") || fail "The configured LUKS volume is not available: $encrypted" mapping=$(awk '$2 == "crypt" {print $1, $3}' <<< "$devices") [[ "$mapping" != *$'\n'* ]] || fail "Multiple mappings for $encrypted; refusing to continue" read -r mapper mapper_id <<< "$mapping" } mount_info() { # Inspect the kernel mount table without following the automount path. findmnt -krnC --mountpoint "$1" --types noautofs --output MAJ:MIN,FSROOT || [[ $? == 1 ]] } volume_mounts() { [[ -n "$mapper_id" ]] || return 0 findmnt -krnC --source "$mapper_id" --output TARGET || [[ $? == 1 ]] } require_external_bind() { read_mapper [[ -n "$mapper_id" && "$(mount_info "$root")" == "$mapper_id $bind_fsroot" ]] || fail "Encrypted media storage is not mounted; run ai-media mount first" } manage_volume() { local action=$1 mapper mapper_id bind_unit automount_unit mounted bind jobs containers [[ $# == 1 ]] || fail "Usage: ai-media $action" bind_unit=$(systemd-escape -p --suffix=mount -- "$root") automount_unit=$(systemd-escape -p --suffix=automount -- "$root") read_mapper mounted=$(mount_info "$external_mount") bind=$(mount_info "$root") [[ -z "$mounted" || "$mounted" == "$mapper_id /" ]] || fail "Unexpected filesystem at $external_mount; refusing to continue" [[ -z "$bind" || "$bind" == "$mapper_id $bind_fsroot" ]] || fail "Unexpected filesystem at $root; refusing to continue" if [[ "$action" == mount ]]; then if [[ -z "$mapper" ]]; then udisksctl unlock --block-device "$encrypted" read_mapper [[ -n "$mapper" ]] || fail "No decrypted block device found after unlock" fi if [[ -z "$mounted" ]]; then [[ -z "$(volume_mounts)" ]] || fail "The LUKS volume is already mounted elsewhere" udisksctl mount --block-device "$mapper" fi [[ "$(mount_info "$external_mount")" == "$mapper_id /" ]] || fail "The LUKS volume is not mounted at the expected path: $external_mount" sudo systemctl start "$automount_unit" "$bind_unit" require_external_bind echo "Encrypted media storage mounted at $root" else jobs=$(systemctl --user list-units --type=service --state=active,activating,deactivating \ --no-legend --no-pager 'ai-media-*.service') containers=$(podman --remote=false ps -q \ --filter ancestor=localhost/ai-media-facefusion:managed \ --filter ancestor=localhost/ai-media-rvc:managed) [[ -z "$jobs" && -z "$containers" ]] || fail "Stop the running ai-media jobs and applications before unmounting" sudo systemctl stop ai-media-bind-restarter.service "$automount_unit" "$bind_unit" [[ -z "$(mount_info "$root")" ]] || fail "The media bind mount is still in use" mounted=$(volume_mounts) if [[ -n "$mounted" ]]; then [[ "$mounted" == "$external_mount" && "$(mount_info "$external_mount")" == "$mapper_id /" ]] || fail "The LUKS volume has other mounts; refusing to unmount or lock it" udisksctl unmount --block-device "$mapper" fi if [[ -n "$mapper" ]]; then [[ -z "$(volume_mounts)" ]] || fail "The LUKS volume is still mounted" udisksctl lock --block-device "$encrypted" fi echo "Encrypted media storage unmounted and locked" fi } {% endif %} app="${1:-}" case "$app" in {% if manage_luks %} mount|umount) manage_volume "$@"; exit ;; {% endif %} facefusion|rvc) shift ;; ffmpeg) app=facefusion; shift; set -- ffmpeg "$@" ;; preset) shift for path in "$root" "$root/input" "$root/output"; do [[ -d "$path" && "$(realpath -- "$path")" == "$path" ]] || { echo "Unsafe or missing media directory: $path" >&2; exit 1; } done {% if manage_luks %} require_external_bind {% endif %} python3 "$build_root/check_storage.py" "$root" exec python3 "$build_root/preset.py" "$root" "$(realpath -- "$0")" "$@" ;; verify) for tool in facefusion rvc; do podman --remote=false run --rm --pull=never --network=none --read-only \ --cap-drop=ALL --cap-add=SYS_CHROOT \ --security-opt=no-new-privileges --userns=keep-id --http-proxy=false \ --tmpfs "/tmp:rw,nosuid,nodev,size=4g,mode=1777" \ --tmpfs "/home/media:rw,nosuid,nodev,size=128m,mode=1777" \ --tmpfs "/work/state:rw,nosuid,nodev,size=128m,mode=1777" \ --tmpfs "/work/models:rw,nosuid,nodev,size=128m,mode=1777" \ "localhost/ai-media-$tool:managed" verify done exit 0 ;; *) echo "Usage: ai-media {facefusion|rvc|ffmpeg|preset|verify{% if manage_luks %}|mount|umount{% endif %}} [args ...]" >&2; exit 2 ;; esac # Fail closed if an existing data path was replaced by a symlink. for path in "$root" "$root/input" "$root/output" "$root/models" "$root/state/$app"; do [[ -d "$path" && "$(realpath -- "$path")" == "$path" ]] || { echo "Unsafe or missing media directory: $path" >&2; exit 1; } done {% if manage_luks %} require_external_bind {% endif %} python3 {{ (desktop_ai_media_build_root + "/check_storage.py") | quote }} "$root" # Run interactive sessions under the user manager, not the launching terminal. # No autostart or automatic restart: recovering a training run remains explicit. if [[ "$app" == rvc || "$app" == facefusion ]] && [[ $# -eq 0 ]]; then : "${XDG_RUNTIME_DIR:?Run this launcher from the GNOME Wayland session}" : "${WAYLAND_DISPLAY:?A Wayland session is required}" [[ "$WAYLAND_DISPLAY" != */* && -S "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" ]] || exit 1 active=$(podman --remote=false ps --filter "ancestor=localhost/ai-media-$app:managed" --format '{{ '{{.ID}}' }}') if [[ -n "$active" ]]; then echo "$app is already running; leave its training session open." >&2 exit 1 fi exec systemd-run --user --collect --unit="ai-media-$app" \ --property=Type=exec --property=Restart=no --property=Delegate=yes \ --setenv="XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR" --setenv="WAYLAND_DISPLAY=$WAYLAND_DISPLAY" \ -- /usr/bin/bash "$(realpath -- "$0")" "$app" --foreground fi if [[ "${1:-}" == --foreground ]]; then shift fi if [[ "$app" == rvc && "${1:-}" == cli ]]; then shift rvc_cli=1 set -- /opt/preset_rvc.py "$@" else rvc_cli=0 fi args=( run --rm --pull=never --network=none --read-only --userns=keep-id --http-proxy=false --cap-drop=ALL --security-opt=no-new-privileges --tmpfs "/tmp:rw,nosuid,nodev,size=8g,mode=1777" --tmpfs "/home/media:rw,nosuid,nodev,size=128m,mode=1777" --shm-size=512m --volume "$root/input:/work/input:ro,Z" --volume "$root/output:/work/output:rw,Z" --volume "$root/models:/work/models:ro,Z" --volume "$root/state/$app:/work/state:rw,Z" ) if [[ "$rvc_cli" == 1 ]]; then args+=(--volume "$build_root/preset_rvc.py:/opt/preset_rvc.py:ro,Z" --entrypoint python) fi if [[ "$rvc_cli" != 1 && "${1:-}" != cli && "${1:-}" != ffmpeg ]]; then : "${XDG_RUNTIME_DIR:?Run this launcher from the GNOME Wayland session}" : "${WAYLAND_DISPLAY:?A Wayland session is required}" [[ "$WAYLAND_DISPLAY" != */* && -S "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY" ]] || exit 1 # Share only the display socket, never the runtime directory, D-Bus, home # directory or host browser. Do not relabel the compositor's socket. args+=( --cap-add=SYS_CHROOT --security-opt label=disable --tmpfs "/run/media:rw,nosuid,nodev,size=1m,mode=1777" --volume "$XDG_RUNTIME_DIR/$WAYLAND_DISPLAY:/run/media/wayland-0:ro" --env WAYLAND_DISPLAY=wayland-0 ) if [[ -S "$XDG_RUNTIME_DIR/pulse/native" ]]; then args+=( --volume "$XDG_RUNTIME_DIR/pulse/native:/run/media/pulse-native:ro" --env PULSE_SERVER=unix:/run/media/pulse-native ) fi fi exec podman --remote=false "${args[@]}" "localhost/ai-media-$app:managed" "$@"