Refactor VAAPI and NVIDIA passthrough setup for LXC
Some checks failed
Bump build.func Revision / bump-revision (push) Has been cancelled

Consolidates and simplifies VAAPI and NVIDIA passthrough logic by introducing unified GPU app detection and moving userland package installation into dedicated *_inside_setup functions. Updates build.func to use new function names and ensures setup is only performed for relevant apps and non-Alpine containers. Improves maintainability and clarity of passthrough.func.
This commit is contained in:
CanbiZ 2025-09-22 14:09:59 +02:00
parent 821c4f36f6
commit ddf5781e55
2 changed files with 51 additions and 76 deletions

View File

@ -2201,8 +2201,8 @@ EOF
source <(curl -fsSL https://git.community-scripts.org/community-scripts/ProxmoxVED/raw/branch/main/misc/passthrough.func) source <(curl -fsSL https://git.community-scripts.org/community-scripts/ProxmoxVED/raw/branch/main/misc/passthrough.func)
usb_handle_passthrough "$CTID" "$CT_TYPE" usb_handle_passthrough "$CTID" "$CT_TYPE"
vaapi_handle_passthrough "$CTID" "$CT_TYPE" "$APP" vaapi_select_and_apply "$CTID" "$CT_TYPE"
nvidia_handle_passthrough "$CTID" "$CT_TYPE" "$APP" nvidia_passthrough_to_lxc "$CTID" "$CT_TYPE"
# TUN device passthrough # TUN device passthrough
if [ "$ENABLE_TUN" == "yes" ]; then if [ "$ENABLE_TUN" == "yes" ]; then
@ -2276,9 +2276,10 @@ EOF
fi fi
msg_info "Customizing LXC Container" msg_info "Customizing LXC Container"
# Container erfolgreich gestartet if [ "$var_os" != "alpine" ]; then
vaapi_inside_setup "$CTID" "$CT_TYPE" vaapi_inside_setup "$CTID" "$CT_TYPE" "$APP"
nvidia_inside_setup "$CTID" "$CT_TYPE" nvidia_inside_setup "$CTID" "$CT_TYPE" "$APP"
fi
if [ "$var_os" == "alpine" ]; then if [ "$var_os" == "alpine" ]; then
sleep 3 sleep 3
pct exec "$CTID" -- /bin/sh -c 'cat <<EOF >/etc/apk/repositories pct exec "$CTID" -- /bin/sh -c 'cat <<EOF >/etc/apk/repositories

View File

@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# passthrough.func — host-side passthrough logic (VAAPI & NVIDIA) for LXC # passthrough.func — host-side passthrough logic (VAAPI & NVIDIA) for LXC
# This file ONLY touches host config (/etc/pve/lxc/<CTID>.conf) and whiptail. # This file ONLY touches host config (/etc/pve/lxc/<CTID>.conf) and whiptail.
# Inside-CT package setup lives in tools.func (hwaccel_setup_in_ct / nvidia_setup_in_ct). # Inside-CT package setup lives in *_inside_setup (called from build.func).
# --------------------------- Common helpers ----------------------------------- # --------------------------- Common helpers -----------------------------------
@ -15,6 +15,9 @@ _whiptail_dims() {
echo "$H $maxW" echo "$H $maxW"
} }
# Apps that benefit from GPU passthrough (VAAPI + NVIDIA)
_GPU_APPS=(immich Channels Emby ErsatzTV Frigate Jellyfin Plex Scrypted Tdarr Unmanic Ollama FileFlows "Open WebUI" Tunarr Debian)
# ------------------------------ USB ------------------------------------------- # ------------------------------ USB -------------------------------------------
usb_handle_passthrough() { usb_handle_passthrough() {
@ -112,18 +115,11 @@ vaapi_select_and_apply() {
n=$((${#items[@]} / 3)) n=$((${#items[@]} / 3))
read -r h w < <(_whiptail_dims "$n" "$maxlen") read -r h w < <(_whiptail_dims "$n" "$maxlen")
if [[ "$CTTYPE" == "0" ]]; then whiptail --title "VAAPI passthrough" --msgbox "\
whiptail --title "VAAPI passthrough" --msgbox "\
VAAPI passthrough will be enabled. VAAPI passthrough will be enabled.
• Privileged CT: full DRM access Privileged CT = full DRM access
• You may need to install drivers inside the CT (intel-media-driver, vainfo)." 12 "$w" Unprivileged CT = may be limited." 12 "$w"
else
whiptail --title "VAAPI passthrough (unprivileged)" --msgbox "\
Unprivileged CT: VAAPI may be limited.
If it fails, consider a privileged CT. You may still need drivers inside the CT." 12 "$w"
fi
local SELECTED local SELECTED
SELECTED="$( SELECTED="$(
@ -177,46 +173,34 @@ EOF
fi fi
} }
vaapi_handle_passthrough() { vaapi_inside_setup() {
local CTID="$1" CTTYPE="$2" APP="$3" local CTID="$1" CTTYPE="$2" APP="$3"
local VAAPI_APPS=(immich Channels Emby ErsatzTV Frigate Jellyfin Plex Scrypted Tdarr Unmanic Ollama FileFlows "Open WebUI" Tunarr Debian) local is_gpu_app=false
local is_vaapi_app=false for a in "${_GPU_APPS[@]}"; do [[ "$APP" == "$a" ]] && is_gpu_app=true && break; done
for a in "${VAAPI_APPS[@]}"; do [[ "$APP" == "$a" ]] && is_vaapi_app=true && break; done [[ "$CTTYPE" == "0" || "$is_gpu_app" == "true" ]] || return 0
if [[ "$CTTYPE" == "0" || "$is_vaapi_app" == "true" ]]; then msg_info "Installing VAAPI userland inside CT $CTID"
vaapi_select_and_apply "$CTID" "$CTTYPE" pct exec "$CTID" -- bash -lc '
. /etc/os-release
# ensure CT is running case "$VERSION_CODENAME" in
if ! pct status "$CTID" | grep -q running; then trixie|noble)
pct start "$CTID" apt-get update
for i in {1..10}; do apt-get install -y intel-media-va-driver-non-free ocl-icd-libopencl1 \
pct status "$CTID" | grep -q running && break mesa-opencl-icd mesa-va-drivers libvpl2 vainfo intel-gpu-tools
sleep 1 ;;
done *)
apt-get update
apt-get install -y va-driver-all ocl-icd-libopencl1 \
mesa-opencl-icd mesa-va-drivers vainfo intel-gpu-tools
;;
esac
if [[ "'"$CTTYPE"'" == "0" ]]; then
adduser "$(id -un)" video || true
adduser "$(id -un)" render || true
fi fi
'
# run apt install directly inside CT msg_ok "VAAPI setup done in CT $CTID"
pct exec "$CTID" -- bash -lc '
. /etc/os-release
case "$VERSION_CODENAME" in
trixie|noble)
apt-get update
apt-get install -y intel-media-va-driver-non-free ocl-icd-libopencl1 \
mesa-opencl-icd mesa-va-drivers libvpl2 vainfo intel-gpu-tools
;;
*)
apt-get update
apt-get install -y va-driver-all ocl-icd-libopencl1 \
mesa-opencl-icd mesa-va-drivers vainfo intel-gpu-tools
;;
esac
if [[ "'"$CTTYPE"'" == "0" ]]; then
adduser "$(id -un)" video || true
adduser "$(id -un)" render || true
fi
'
fi
} }
# ----------------------------- NVIDIA ----------------------------------------- # ----------------------------- NVIDIA -----------------------------------------
@ -240,7 +224,6 @@ nvidia_passthrough_to_lxc() {
return 0 return 0
fi fi
# optional: expose /dev/dri for apps probing VAAPI; harmless with NVIDIA
if [[ -d /dev/dri && "$CTTYPE" == "0" ]]; then if [[ -d /dev/dri && "$CTTYPE" == "0" ]]; then
echo "lxc.mount.entry: /dev/dri /dev/dri none bind,optional,create=dir" >>"$LXC_CONFIG" echo "lxc.mount.entry: /dev/dri /dev/dri none bind,optional,create=dir" >>"$LXC_CONFIG"
fi fi
@ -248,31 +231,22 @@ nvidia_passthrough_to_lxc() {
msg_ok "NVIDIA devices mapped to CT ${CTID}" msg_ok "NVIDIA devices mapped to CT ${CTID}"
} }
nvidia_handle_passthrough() { nvidia_inside_setup() {
local CTID="$1" CTTYPE="$2" APP="$3" local CTID="$1" CTTYPE="$2" APP="$3"
local is_gpu_app=false
for a in "${_GPU_APPS[@]}"; do [[ "$APP" == "$a" ]] && is_gpu_app=true && break; done
[[ "$CTTYPE" == "0" || "$is_gpu_app" == "true" ]] || return 0
compgen -G "/dev/nvidia*" >/dev/null || return 0 compgen -G "/dev/nvidia*" >/dev/null || return 0
msg_info "Installing NVIDIA userland inside CT $CTID"
if whiptail --title "NVIDIA passthrough" \ pct exec "$CTID" -- bash -lc '
--yesno "NVIDIA GPU detected. Map /dev/nvidia* into CT $CTID and install drivers inside?" 12 70; then . /etc/os-release
apt-get update
nvidia_passthrough_to_lxc "$CTID" "$CTTYPE" apt-get install -y nvidia-driver nvidia-utils libnvidia-encode1 libcuda1
if [[ "'"$CTTYPE"'" == "0" ]]; then
if ! pct status "$CTID" | grep -q running; then adduser "$(id -un)" video || true
pct start "$CTID"
for i in {1..10}; do
pct status "$CTID" | grep -q running && break
sleep 1
done
fi fi
'
pct exec "$CTID" -- bash -lc ' msg_ok "NVIDIA setup done in CT $CTID"
. /etc/os-release
apt-get update
apt-get install -y nvidia-driver nvidia-utils libnvidia-encode1 libcuda1
if [[ "'"$CTTYPE"'" == "0" ]]; then
adduser "$(id -un)" video || true
fi
'
fi
} }