Update hw-acceleration.sh

This commit is contained in:
CanbiZ 2025-03-31 15:23:26 +02:00
parent 1551539cb5
commit df257af24e

View File

@ -1,20 +1,39 @@
#!/usr/bin/env bash
#
# Title: Proxmox LXC Hardware Passthrough & GPU Acceleration Setup
# Maintainer: https://github.com/community-scripts/ProxmoxVED
# Includes: gpu-intel.func, gpu-nvidia.func, gpu-amd.func
# Description: Enables hardware passthrough for USB, Intel, NVIDIA, AMD GPUs inside privileged LXC containers.
# Installs optional drivers/tools inside the container (vainfo, intel-gpu-tools, OpenCL, etc.)
# Only supports PRIVILEGED containers for GPU passthrough.
# License: MIT
# Author: MickLesk (CanbiZ)
# Repo: https://github.com/community-scripts/ProxmoxVED
#
# Usage: bash -c "$(wget -qLO - https://github.com/community-scripts/ProxmoxVED/raw/main/misc/hw-acceleration.sh)"
#
# Requires:
# - Proxmox VE 8.1+
# - Privileged LXC Containers
# - GPU device available on host
#
# Features:
# - USB Serial Passthrough
# - Intel VAAPI passthrough + (optional) non-free drivers
# - NVIDIA GPU passthrough for LXC (binds /dev/nvidia*)
# - AMD GPU passthrough (experimental)
# - Container driver installation via APT
# - User group assignments (video/render)
# - Interactive menu system via whiptail
#
# Proxmox LXC Hardware Passthrough & GPU Acceleration Setup
# https://github.com/community-scripts/ProxmoxVED
set -euo pipefail
TEMP_DIR=$(mktemp -d)
trap 'rm -rf $TEMP_DIR' EXIT
declare -a SELECTED_FEATURES
declare -a updated_cts
read -rp "Enter container ID(s) separated by space: " SELECTED_CTIDS
declare SELECTED_CTIDS=""
source <(wget -qO- https://github.com/community-scripts/ProxmoxVED/raw/main/scripts/tools/gpu-intel.func)
source <(wget -qO- https://github.com/community-scripts/ProxmoxVED/raw/main/scripts/tools/gpu-nvidia.func)
source <(wget -qO- https://github.com/community-scripts/ProxmoxVED/raw/main/scripts/tools/gpu-intel.func)
source <(wget -qO- https://github.com/community-scripts/ProxmoxVED/raw/main/scripts/tools/gpu-amd.func)
function header_info() {
@ -43,14 +62,31 @@ function msg() {
function prompt_features() {
local features=()
printf "\nAvailable passthrough options:\n"
[[ -e /dev/ttyUSB0 || -e /dev/ttyACM0 ]] && echo " [1] USB" && features+=("usb")
[[ -e /dev/dri/renderD128 ]] && echo " [2] Intel VAAPI" && features+=("intel")
[[ -e /dev/nvidia0 ]] && echo " [3] NVIDIA GPU" && features+=("nvidia")
[[ -e /dev/kfd ]] && echo " [4] AMD GPU" && features+=("amd")
[[ ${#features[@]} -eq 0 ]] && msg err "No supported hardware detected." && exit 1
printf "\nAvailable features:\n"
if [[ -e /dev/ttyUSB0 || -e /dev/ttyACM0 ]]; then
echo " [1] USB Passthrough"
features+=("usb")
fi
if [[ -e /dev/dri/renderD128 ]]; then
echo " [2] Intel iGPU (VAAPI)"
features+=("intel")
fi
if [[ -e /dev/nvidia0 ]]; then
echo " [3] NVIDIA GPU"
features+=("nvidia")
fi
if [[ -e /dev/kfd ]]; then
echo " [4] AMD GPU (ROCm)"
features+=("amd")
fi
if [[ ${#features[@]} -eq 0 ]]; then
msg err "No supported hardware found on host."
exit 1
fi
echo
read -rp "Select hardware (e.g. 1 3): " choices
read -rp "Enter number(s) separated by space (e.g. 1 3): " choices
SELECTED_FEATURES=()
for i in $choices; do
case "$i" in
@ -60,12 +96,20 @@ function prompt_features() {
4) SELECTED_FEATURES+=("amd") ;;
esac
done
[[ ${#SELECTED_FEATURES[@]} -eq 0 ]] && msg warn "No valid feature selected." && exit 1
if [[ ${#SELECTED_FEATURES[@]} -eq 0 ]]; then
msg warn "No valid feature selected."
exit 1
fi
}
function select_lxc_cts() {
mapfile -t containers < <(pct list | awk 'NR>1 {print $1 "|" $2}')
[[ ${#containers[@]} -eq 0 ]] && msg warn "No LXC containers found." && exit 1
if [[ ${#containers[@]} -eq 0 ]]; then
msg warn "No LXC containers found."
exit 1
fi
echo
echo "Available Containers:"
for entry in "${containers[@]}"; do
@ -73,21 +117,20 @@ function select_lxc_cts() {
name="${entry##*|}"
echo " [$ctid] $name"
done
echo
read -rp "Enter container ID(s) separated by space: " SELECTED_CTIDS
[[ -z "$SELECTED_CTIDS" ]] && msg warn "No containers selected." && exit 1
}
function is_alpine_container() {
local ctid="$1"
pct exec "$ctid" -- sh -c 'grep -qi alpine /etc/os-release' >/dev/null 2>&1
if [[ -z "$SELECTED_CTIDS" ]]; then
msg warn "No containers selected."
exit 1
fi
}
function apply_usb_passthrough() {
local conf="$1"
grep -q "ttyUSB" "$conf" 2>/dev/null && return
cat <<EOF >>"$conf"
# USB Serial Passthrough
# USB Passthrough
lxc.cgroup2.devices.allow: a
lxc.cap.drop:
lxc.cgroup2.devices.allow: c 188:* rwm
@ -103,20 +146,10 @@ EOF
function main() {
header_info
prompt_features
msg info "Features selected: ${SELECTED_FEATURES[*]}"
select_lxc_cts
msg info "Containers selected: ${SELECTED_CTIDS}"
local updated_cts=()
local intel_nonfree="no"
if [[ " ${SELECTED_FEATURES[*]} " =~ " intel " ]]; then
read -rp "Install non-free intel-media-va-driver (Debian only)? [y/N]: " confirm
if [[ "${confirm,,}" =~ ^(y|yes)$ ]]; then
intel_nonfree="yes"
fi
fi
for ctid in $SELECTED_CTIDS; do
local conf="/etc/pve/lxc/${ctid}.conf"
local updated=0
@ -124,45 +157,45 @@ function main() {
for feature in "${SELECTED_FEATURES[@]}"; do
case "$feature" in
usb)
msg info "Adding USB passthrough to CT $ctid..."
msg info "Applying USB passthrough to CT $ctid..."
apply_usb_passthrough "$conf" && updated=1
;;
intel)
msg info "Intel passthrough setup for CT $ctid"
passthrough_intel_to_lxc "$ctid" && install_intel_tools_in_ct "$ctid" "$intel_nonfree" && updated=1
;;
nvidia)
msg info "Validating NVIDIA setup..."
nvidia_validate_driver_version
nvidia_validate_cuda_version
local minor
minor=$(nvidia_select_gpu_minor)
nvidia_lxc_passthrough "$ctid" "$minor" && updated=1
msg info "Applying Intel VAAPI passthrough to CT $ctid..."
passthrough_intel_to_lxc "$ctid" && install_intel_tools_in_ct "$ctid" && updated=1
;;
amd)
msg info "Applying AMD passthrough to CT $ctid..."
msg info "Applying AMD GPU passthrough to CT $ctid..."
passthrough_amd_to_lxc "$ctid" && install_amd_tools_in_ct "$ctid" && updated=1
;;
nvidia)
msg info "Checking NVIDIA GPU on host..."
check_nvidia_driver_status && check_cuda_version
gpu_minor=$(select_nvidia_gpu) || continue
passthrough_nvidia_to_lxc "$ctid" "$gpu_minor" && updated=1
;;
esac
done
[[ "$updated" -eq 1 ]] && updated_cts+=("$ctid")
if [[ "$updated" -eq 1 ]]; then
updated_cts+=("$ctid")
fi
done
echo
if [[ ${#updated_cts[@]} -gt 0 ]]; then
msg ok "Updated containers: ${updated_cts[*]}"
read -rp "Restart updated container(s)? [y/N]: " confirm
if [[ "${confirm,,}" == "y" ]]; then
msg ok "Updated: ${updated_cts[*]}"
read -rp "Restart updated container(s)? [y/N]: " restart
if [[ "${restart,,}" == "y" ]]; then
for ctid in "${updated_cts[@]}"; do
pct reboot "$ctid"
msg ok "Restarted CT $ctid"
msg ok "Restarted container $ctid"
done
else
msg info "Restart skipped."
msg info "Manual restart required for: ${updated_cts[*]}"
fi
else
msg warn "No passthrough changes applied."
msg warn "No passthrough applied."
fi
}