From 82c6e6e6fb144a5508b6a68ace4a6813d520f177 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Tue, 23 Sep 2025 13:51:46 +0200 Subject: [PATCH] Refactor GPU and USB passthrough setup in container scripts Introduced a unified gpu_inside_setup function in build.func to handle VAAPI and NVIDIA userland installation inside containers. Replaced bracket conditionals with if statements in passthrough.func for improved readability and maintainability, and made minor logic clarifications for privileged container checks. --- misc/build.func | 57 ++++++++++++++++++++++++++++++++++++++++--- misc/passthrough.func | 13 +++++++--- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/misc/build.func b/misc/build.func index e48e17b6..9f63fea5 100644 --- a/misc/build.func +++ b/misc/build.func @@ -2290,11 +2290,60 @@ EOF fi fi + gpu_inside_setup() { + local CTID="$1" + + # VAAPI inside (Debian/Ubuntu) + if [[ "${ENABLE_VAAPI:-0}" -eq 1 ]]; then + msg_info "Installing VAAPI userland inside CT ${CTID}" + pct exec "$CTID" -- bash -lc ' + set -e + . /etc/os-release || true + if [[ "${VERSION_CODENAME:-}" == "trixie" ]]; then + cat >/etc/apt/sources.list.d/non-free.sources </etc/apk/repositories diff --git a/misc/passthrough.func b/misc/passthrough.func index 9c19cdbe..6eab5f0b 100644 --- a/misc/passthrough.func +++ b/misc/passthrough.func @@ -97,8 +97,9 @@ usb_handle_passthrough() { local CT_ID="$1" CT_TYPE="$2" local LXC_CONFIG="/etc/pve/lxc/${CT_ID}.conf" - [[ "$CT_TYPE" != "0" ]] && return 0 # USB passthrough only for privileged CTs - + if [[ "$CT_TYPE" != "0" ]]; then + return 0 # USB passthrough only for privileged CTs + fi cat <>"$LXC_CONFIG" # USB passthrough lxc.cgroup2.devices.allow: a @@ -221,11 +222,13 @@ vaapi_select_and_apply() { done # Fallback only for privileged CTs - [[ "$CT_TYPE" == "0" ]] && cat <<'EOF' >>"$LXC_CONFIG" + if [[ "$CT_TYPE" == "0" ]]; then + cat <<'EOF' >>"$LXC_CONFIG" # VAAPI fallback lxc.mount.entry: /dev/dri /dev/dri none bind,optional,create=dir lxc.cgroup2.devices.allow: c 226:* rwm EOF + fi } # ----------------------------- NVIDIA ----------------------------------------- @@ -248,7 +251,9 @@ nvidia_passthrough_to_lxc() { return } - [[ -d /dev/dri && "$CT_TYPE" == "0" ]] && echo "lxc.mount.entry: /dev/dri /dev/dri none bind,optional,create=dir" >>"$LXC_CONFIG" + if [[ -d /dev/dri && "$CT_TYPE" == "0" ]]; then + echo "lxc.mount.entry: /dev/dri /dev/dri none bind,optional,create=dir" >>"$LXC_CONFIG" + fi msg_ok "NVIDIA devices mapped to CT ${CT_ID}" }