Refactor GPU and USB passthrough setup in container scripts
Some checks failed
Bump build.func Revision / bump-revision (push) Has been cancelled

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.
This commit is contained in:
CanbiZ 2025-09-23 13:51:46 +02:00
parent a17aa696b6
commit 82c6e6e6fb
2 changed files with 62 additions and 8 deletions

View File

@ -2290,11 +2290,60 @@ EOF
fi fi
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 <<EOF
Types: deb deb-src
URIs: http://deb.debian.org/debian
Suites: trixie trixie-updates trixie-security
Components: non-free non-free-firmware
EOF
fi
DEBIAN_FRONTEND=noninteractive apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y \
intel-media-va-driver-non-free \
ocl-icd-libopencl1 \
mesa-opencl-icd \
mesa-va-drivers \
libvpl2 \
vainfo \
intel-gpu-tools
' || {
msg_error "VAAPI userland install failed"
return 1
}
msg_ok "Installed VAAPI userland"
fi
# NVIDIA inside (Debian/Ubuntu)
if [[ "${ENABLE_NVIDIA:-0}" -eq 1 ]]; then
msg_info "Installing NVIDIA userland inside CT ${CTID}"
pct exec "$CTID" -- bash -lc '
set -e
DEBIAN_FRONTEND=noninteractive apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y \
nvidia-driver \
nvidia-utils \
libnvidia-encode1 \
libcuda1
' || {
msg_error "NVIDIA userland install failed"
return 1
}
msg_ok "Installed NVIDIA userland"
fi
}
msg_info "Customizing LXC Container" msg_info "Customizing LXC Container"
#if [ "$var_os" != "alpine" ]; then gpu_inside_setup "$CTID"
# vaapi_inside_setup "$CTID" "$CT_TYPE" "$APP"
# 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

@ -97,8 +97,9 @@ usb_handle_passthrough() {
local CT_ID="$1" CT_TYPE="$2" local CT_ID="$1" CT_TYPE="$2"
local LXC_CONFIG="/etc/pve/lxc/${CT_ID}.conf" 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 <<EOF >>"$LXC_CONFIG" cat <<EOF >>"$LXC_CONFIG"
# USB passthrough # USB passthrough
lxc.cgroup2.devices.allow: a lxc.cgroup2.devices.allow: a
@ -221,11 +222,13 @@ vaapi_select_and_apply() {
done done
# Fallback only for privileged CTs # Fallback only for privileged CTs
[[ "$CT_TYPE" == "0" ]] && cat <<'EOF' >>"$LXC_CONFIG" if [[ "$CT_TYPE" == "0" ]]; then
cat <<'EOF' >>"$LXC_CONFIG"
# VAAPI fallback # VAAPI fallback
lxc.mount.entry: /dev/dri /dev/dri none bind,optional,create=dir lxc.mount.entry: /dev/dri /dev/dri none bind,optional,create=dir
lxc.cgroup2.devices.allow: c 226:* rwm lxc.cgroup2.devices.allow: c 226:* rwm
EOF EOF
fi
} }
# ----------------------------- NVIDIA ----------------------------------------- # ----------------------------- NVIDIA -----------------------------------------
@ -248,7 +251,9 @@ nvidia_passthrough_to_lxc() {
return 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}" msg_ok "NVIDIA devices mapped to CT ${CT_ID}"
} }