Fix NVIDIA passthrough logic in container build
Some checks failed
Bump build.func Revision / bump-revision (push) Has been cancelled

Refactored the NVIDIA GPU passthrough configuration to ensure device entries and related settings are only added when /dev/nvidia* devices are present. This prevents passthrough configuration attempts when no NVIDIA devices are detected, improving reliability and error messaging.
This commit is contained in:
CanbiZ 2025-09-30 09:13:44 +02:00
parent 2d0b42ae45
commit e7936a686a

View File

@ -2198,6 +2198,7 @@ build_container() {
msg_info "Fix NVIDIA drivers on host, then recreate container or passthrough manually." msg_info "Fix NVIDIA drivers on host, then recreate container or passthrough manually."
return 0 return 0
fi fi
for d in /dev/nvidia* /dev/nvidiactl /dev/nvidia-modeset; do for d in /dev/nvidia* /dev/nvidiactl /dev/nvidia-modeset; do
[[ -e "$d" ]] && NVIDIA_DEVICES+=("$d") [[ -e "$d" ]] && NVIDIA_DEVICES+=("$d")
done done
@ -2205,37 +2206,37 @@ build_container() {
if [[ ${#NVIDIA_DEVICES[@]} -eq 0 ]]; then if [[ ${#NVIDIA_DEVICES[@]} -eq 0 ]]; then
msg_warn "NVIDIA GPU detected but no /dev/nvidia* devices found" msg_warn "NVIDIA GPU detected but no /dev/nvidia* devices found"
msg_warn "Please install NVIDIA drivers on host: apt install nvidia-driver" msg_warn "Please install NVIDIA drivers on host: apt install nvidia-driver"
fi else
if [[ "$CT_TYPE" == "0" ]]; then if [[ "$CT_TYPE" == "0" ]]; then
cat <<EOF >>"$LXC_CONFIG" cat <<EOF >>"$LXC_CONFIG"
# NVIDIA GPU Passthrough (privileged) # NVIDIA GPU Passthrough (privileged)
lxc.cgroup2.devices.allow: c 195:* rwm lxc.cgroup2.devices.allow: c 195:* rwm
lxc.cgroup2.devices.allow: c 243:* rwm lxc.cgroup2.devices.allow: c 243:* rwm
lxc.mount.entry: /dev/nvidia0 dev/nvidia0 none bind,optional,create=file lxc.mount.entry: /dev/nvidia0 dev/nvidia0 none bind,optional,create=file
lxc.mount.entry: /dev/nvidiactl dev/nvidiactl none bind,optional,create=file lxc.mount.entry: /dev/nvidiactl dev/nvidiactl none bind,optional,create=file
lxc.mount.entry: /dev/nvidia-uvm dev/nvidia-uvm none bind,optional,create=file lxc.mount.entry: /dev/nvidia-uvm dev/nvidia-uvm none bind,optional,create=file
lxc.mount.entry: /dev/nvidia-uvm-tools dev/nvidia-uvm-tools none bind,optional,create=file lxc.mount.entry: /dev/nvidia-uvm-tools dev/nvidia-uvm-tools none bind,optional,create=file
EOF EOF
fi
if [[ -e /dev/dri/renderD128 ]]; then if [[ -e /dev/dri/renderD128 ]]; then
echo "lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file" >>"$LXC_CONFIG" echo "lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file" >>"$LXC_CONFIG"
fi
export GPU_TYPE="NVIDIA"
export NVIDIA_DRIVER_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null | head -n1)
msg_ok "NVIDIA GPU passthrough configured (driver: ${NVIDIA_DRIVER_VERSION})"
else
msg_warn "NVIDIA passthrough only supported for privileged containers"
return 0
fi fi
fi
}
# Debug output export GPU_TYPE="NVIDIA"
msg_debug "Intel devices: ${INTEL_DEVICES[*]}" export NVIDIA_DRIVER_VERSION=$(nvidia-smi --query-gpu=driver_version --format=csv,noheader 2>/dev/null | head -n1)
msg_debug "AMD devices: ${AMD_DEVICES[*]}" msg_ok "NVIDIA GPU passthrough configured (driver: ${NVIDIA_DRIVER_VERSION})"
msg_debug "NVIDIA devices: ${NVIDIA_DEVICES[*]}" else
} msg_warn "NVIDIA passthrough only supported for privileged containers"
return 0
fi
fi
fi
# Debug output
msg_debug "Intel devices: ${INTEL_DEVICES[*]}"
msg_debug "AMD devices: ${AMD_DEVICES[*]}"
msg_debug "NVIDIA devices: ${NVIDIA_DEVICES[*]}"
}
# Configure USB passthrough for privileged containers # Configure USB passthrough for privileged containers