gpu things

This commit is contained in:
CanbiZ 2025-03-27 14:50:34 +01:00
parent 7a971b0bb3
commit c8110be760
3 changed files with 227 additions and 137 deletions

View File

@ -0,0 +1,61 @@
#!/usr/bin/env bash
# AMD GPU Helper Functions for Proxmox LXC / ROCm passthrough
# Author: CanbiZ
# License: MIT
set -euo pipefail
function exit_script() {
printf "⚠️ User exited script\n"
exit 0
}
function msg() {
local type="$1"
shift
case "$type" in
info) printf " \033[36m➤\033[0m %s\n" "$@" ;;
ok) printf " \033[32m✔\033[0m %s\n" "$@" ;;
warn) printf " \033[33m⚠\033[0m %s\n" "$@" >&2 ;;
err) printf " \033[31m✘\033[0m %s\n" "$@" >&2 ;;
esac
}
function amd_gpu_available() {
lspci | grep -qi 'VGA.*AMD' && [[ -e /dev/kfd ]]
}
function passthrough_amd_to_lxc() {
local ctid="$1"
local conf="/etc/pve/lxc/${ctid}.conf"
if ! amd_gpu_available; then
msg warn "No AMD GPU with ROCm support detected"
return 1
fi
{
echo "# AMD ROCm GPU"
echo "lxc.cgroup2.devices.allow: c 226:* rwm"
echo "lxc.cgroup2.devices.allow: c 238:* rwm"
echo "lxc.mount.entry: /dev/kfd dev/kfd none bind,optional,create=file"
echo "lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir"
} >>"$conf"
msg ok "AMD ROCm passthrough applied to CT $ctid"
return 0
}
function install_amd_tools_in_ct() {
local ctid="$1"
msg info "Installing AMD GPU tools in CT $ctid..."
pct exec "$ctid" -- bash -c \
"apt-get update && \
apt-get install -y rocm-smi rocm-utils && \
adduser \$(id -un 0) video && \
adduser \$(id -un 0) render" >/dev/null 2>&1 || true
msg ok "Installed ROCm tools inside CT $ctid"
}

View File

@ -0,0 +1,62 @@
#!/usr/bin/env bash
# Intel GPU Helper Functions for Proxmox LXC / VAAPI passthrough
# Author: CanbiZ
# License: MIT
set -euo pipefail
function exit_script() {
printf "⚠️ User exited script\n"
exit 0
}
function msg() {
local type="$1"
shift
case "$type" in
info) printf " \033[36m➤\033[0m %s\n" "$@" ;;
ok) printf " \033[32m✔\033[0m %s\n" "$@" ;;
warn) printf " \033[33m⚠\033[0m %s\n" "$@" >&2 ;;
err) printf " \033[31m✘\033[0m %s\n" "$@" >&2 ;;
esac
}
function intel_gpu_available() {
[[ -e /dev/dri/renderD128 ]] && lspci | grep -qi 'VGA.*Intel'
}
function passthrough_intel_to_lxc() {
local ctid="$1"
local conf="/etc/pve/lxc/${ctid}.conf"
if ! intel_gpu_available; then
msg warn "No Intel iGPU detected on host"
return 1
fi
{
echo "# Intel iGPU (VAAPI)"
echo "lxc.cgroup2.devices.allow: c 226:* rwm"
echo "lxc.cgroup2.devices.allow: c 29:0 rwm"
echo "lxc.mount.entry: /dev/fb0 dev/fb0 none bind,optional,create=file"
echo "lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir"
echo "lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file"
} >>"$conf"
msg ok "Intel VAAPI passthrough applied to CT $ctid"
return 0
}
function install_intel_tools_in_ct() {
local ctid="$1"
msg info "Installing Intel tools in CT $ctid..."
pct exec "$ctid" -- bash -c \
"apt-get update && \
apt-get install -y va-driver-all vainfo intel-gpu-tools ocl-icd-libopencl1 intel-opencl-icd && \
adduser \$(id -un 0) video && \
adduser \$(id -un 0) render" >/dev/null 2>&1
msg ok "Installed VAAPI & GPU tools inside CT $ctid"
}

View File

@ -23,11 +23,19 @@
# - Container driver installation via APT # - Container driver installation via APT
# - User group assignments (video/render) # - User group assignments (video/render)
# - Interactive menu system via whiptail # - Interactive menu system via whiptail
#
#!/usr/bin/env bash # Proxmox LXC Hardware Passthrough & GPU Acceleration Setup
# https://github.com/community-scripts/ProxmoxVED
set -euo pipefail set -euo pipefail
TEMP_DIR=$(mktemp -d)
trap 'rm -rf $TEMP_DIR' EXIT
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() { function header_info() {
clear clear
cat <<"EOF" cat <<"EOF"
@ -45,66 +53,82 @@ function msg() {
local type="$1" local type="$1"
shift shift
case "$type" in case "$type" in
info) printf " \033[36m➤\033[0m %s\n" "$*" ;; info) printf " \033[36m➤\033[0m %s\n" "$@" ;;
ok) printf " \033[32m✔\033[0m %s\n" "$*" ;; ok) printf " \033[32m✔\033[0m %s\n" "$@" ;;
warn) printf " \033[33m⚠\033[0m %s\n" "$*" >&2 ;; warn) printf " \033[33m⚠\033[0m %s\n" "$@" >&2 ;;
err) printf " \033[31m✘\033[0m %s\n" "$*" >&2 ;; err) printf " \033[31m✘\033[0m %s\n" "$@" >&2 ;;
esac esac
} }
function detect_features() { function prompt_features() {
AVAILABLE_FEATURES=() local features=()
[[ -e /dev/ttyUSB0 || -e /dev/ttyACM0 ]] && AVAILABLE_FEATURES+=("usb" "USB Passthrough" OFF) printf "\nAvailable features:\n"
[[ -e /dev/dri/renderD128 ]] && AVAILABLE_FEATURES+=("intel" "Intel VAAPI GPU" OFF) if [[ -e /dev/ttyUSB0 || -e /dev/ttyACM0 ]]; then
[[ -e /dev/nvidia0 ]] && AVAILABLE_FEATURES+=("nvidia" "NVIDIA GPU" OFF) echo " [1] USB Passthrough"
[[ -e /dev/kfd ]] && AVAILABLE_FEATURES+=("amd" "AMD GPU (ROCm)" OFF) 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 [[ ${#AVAILABLE_FEATURES[@]} -eq 0 ]]; then if [[ ${#features[@]} -eq 0 ]]; then
msg warn "No supported hardware found on host system." msg err "No supported hardware found on host."
exit 1
fi
echo
read -rp "Enter number(s) separated by space (e.g. 1 3): " choices
SELECTED_FEATURES=()
for i in $choices; do
case "$i" in
1) SELECTED_FEATURES+=("usb") ;;
2) SELECTED_FEATURES+=("intel") ;;
3) SELECTED_FEATURES+=("nvidia") ;;
4) SELECTED_FEATURES+=("amd") ;;
esac
done
if [[ ${#SELECTED_FEATURES[@]} -eq 0 ]]; then
msg warn "No valid feature selected."
exit 1 exit 1
fi fi
} }
function select_hw_features() { function select_lxc_cts() {
SELECTED_FEATURES=$(whiptail --title "Hardware Options" --checklist \ mapfile -t containers < <(pct list | awk 'NR>1 {print $1 "|" $2}')
"Select hardware features to passthrough:" 20 60 10 \ if [[ ${#containers[@]} -eq 0 ]]; then
"${AVAILABLE_FEATURES[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || exit 1 msg warn "No LXC containers found."
[[ -z "$SELECTED_FEATURES" ]] && {
msg warn "No passthrough options selected."
exit 1
}
}
function select_lxc_targets() {
local opts=()
while IFS= read -r line; do
local id name conf
id=$(awk '{print $1}' <<<"$line")
name=$(awk '{print $2}' <<<"$line")
conf="/etc/pve/lxc/${id}.conf"
[[ -f "$conf" ]] && opts+=("$id" "$name (CTID: $id)" OFF)
done < <(pct list | tail -n +2)
if [[ ${#opts[@]} -eq 0 ]]; then
msg warn "No containers found. Make sure you have running LXCs."
exit 1 exit 1
fi fi
SELECTED_CTIDS=$(whiptail --title "Select LXC Containers" --checklist \ echo
"Choose container(s) to apply passthrough:" 20 60 10 \ echo "Available Containers:"
"${opts[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || exit 1 for entry in "${containers[@]}"; do
ctid="${entry%%|*}"
name="${entry##*|}"
echo " [$ctid] $name"
done
[[ -z "$SELECTED_CTIDS" ]] && { echo
read -rp "Enter container ID(s) separated by space: " SELECTED_CTIDS
if [[ -z "$SELECTED_CTIDS" ]]; then
msg warn "No containers selected." msg warn "No containers selected."
exit 1 exit 1
} fi
} }
function apply_usb() { function apply_usb_passthrough() {
local conf="$1" local conf="$1"
grep -q "ttyUSB\|ttyACM" <<<"$(ls /dev 2>/dev/null)" || return 1 grep -q "ttyUSB" "$conf" 2>/dev/null && return
grep -q "ttyUSB" "$conf" 2>/dev/null && return 0
cat <<EOF >>"$conf" cat <<EOF >>"$conf"
# USB Passthrough # USB Passthrough
lxc.cgroup2.devices.allow: a lxc.cgroup2.devices.allow: a
@ -119,116 +143,59 @@ lxc.mount.entry: /dev/ttyACM1 dev/ttyACM1 none bind,optional,create=
EOF EOF
} }
function apply_intel() {
local conf="$1"
[[ -e /dev/dri/renderD128 ]] || return 1
grep -q "renderD128" "$conf" 2>/dev/null && return 0
cat <<EOF >>"$conf"
# Intel VAAPI
lxc.cgroup2.devices.allow: c 226:* rwm
lxc.cgroup2.devices.allow: c 29:0 rwm
lxc.mount.entry: /dev/fb0 dev/fb0 none bind,optional,create=file
lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir
lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file
EOF
}
function apply_nvidia() {
local conf="$1"
[[ -e /dev/nvidia0 ]] || return 1
grep -q "nvidia0" "$conf" 2>/dev/null && return 0
cat <<EOF >>"$conf"
# NVIDIA GPU
lxc.cgroup2.devices.allow: c 195:* rwm
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/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
EOF
}
function apply_amd() {
local conf="$1"
[[ -e /dev/kfd ]] || return 1
grep -q "/dev/kfd" "$conf" 2>/dev/null && return 0
cat <<EOF >>"$conf"
# AMD ROCm GPU
lxc.cgroup2.devices.allow: c 226:* rwm
lxc.cgroup2.devices.allow: c 238:* rwm
lxc.mount.entry: /dev/kfd dev/kfd none bind,optional,create=file
lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir
EOF
}
function install_drivers() {
local ctid="$1"
for opt in $SELECTED_FEATURES; do
case "$opt" in
intel)
msg info "Installing Intel drivers/tools in CT $ctid..."
pct exec "$ctid" -- bash -c "
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y \
va-driver-all vainfo intel-gpu-tools \
ocl-icd-libopencl1 intel-opencl-icd >/dev/null
adduser root video >/dev/null 2>&1 || true
adduser root render >/dev/null 2>&1 || true
"
;;
nvidia)
msg info "Installing NVIDIA container tools in CT $ctid..."
pct exec "$ctid" -- bash -c "
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y \
nvidia-container-runtime nvidia-utils-525 >/dev/null 2>&1 || true
"
;;
amd)
msg info "Installing AMD ROCm tools in CT $ctid..."
pct exec "$ctid" -- bash -c "
apt-get update -qq
DEBIAN_FRONTEND=noninteractive apt-get install -y \
rocm-smi rocm-utils >/dev/null 2>&1 || true
"
;;
esac
done
}
function main() { function main() {
header_info header_info
detect_features prompt_features
select_hw_features select_lxc_cts
select_lxc_targets
local updated_cts=() local updated_cts=()
for ctid in $SELECTED_CTIDS; do for ctid in $SELECTED_CTIDS; do
local conf="/etc/pve/lxc/${ctid}.conf" local conf="/etc/pve/lxc/${ctid}.conf"
local updated=0 local updated=0
for opt in $SELECTED_FEATURES; do
case "$opt" in for feature in "${SELECTED_FEATURES[@]}"; do
usb) apply_usb "$conf" && updated=1 ;; case "$feature" in
intel) apply_intel "$conf" && updated=1 ;; usb)
nvidia) apply_nvidia "$conf" && updated=1 ;; msg info "Applying USB passthrough to CT $ctid..."
amd) apply_amd "$conf" && updated=1 ;; apply_usb_passthrough "$conf" && updated=1
;;
intel)
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 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 esac
done done
[[ "$updated" -eq 1 ]] && updated_cts+=("$ctid")
install_drivers "$ctid" if [[ "$updated" -eq 1 ]]; then
updated_cts+=("$ctid")
fi
done done
echo
if [[ ${#updated_cts[@]} -gt 0 ]]; then if [[ ${#updated_cts[@]} -gt 0 ]]; then
msg ok "Hardware passthrough updated in: ${updated_cts[*]}" msg ok "Updated: ${updated_cts[*]}"
if whiptail --yesno "Restart updated container(s)?\n${updated_cts[*]}" 10 60; then read -rp "Restart updated container(s)? [y/N]: " restart
if [[ "${restart,,}" == "y" ]]; then
for ctid in "${updated_cts[@]}"; do for ctid in "${updated_cts[@]}"; do
pct restart "$ctid" pct restart "$ctid"
msg ok "Restarted container $ctid"
done done
msg ok "Containers restarted: ${updated_cts[*]}"
else else
msg info "Please restart the container(s) manually." msg info "Manual restart required for: ${updated_cts[*]}"
fi fi
else else
msg warn "No passthrough or driver changes were applied." msg warn "No passthrough applied."
fi fi
} }