Update hw-acceleration.sh

This commit is contained in:
CanbiZ 2025-03-27 14:06:24 +01:00
parent 4b20355aed
commit bdf3e1c6ee

View File

@ -24,6 +24,8 @@
# - User group assignments (video/render) # - User group assignments (video/render)
# - Interactive menu system via whiptail # - Interactive menu system via whiptail
#!/usr/bin/env bash
set -euo pipefail set -euo pipefail
function header_info() { function header_info() {
@ -49,53 +51,53 @@ function msg() {
esac esac
} }
function select_lxc_container() { function select_hw_features() {
local lxc_list; local options=() local opts=(
if ! lxc_list=$(pct list | awk 'NR>1 {print $1}' | xargs -n1); then
msg err "Failed to fetch LXC containers"
exit 1
fi
for ctid in $lxc_list; do
if [[ -f "/etc/pve/lxc/${ctid}.conf" ]]; then
options+=("$ctid" "LXC ${ctid}" "OFF")
fi
done
if [[ ${#options[@]} -eq 0 ]]; then
msg warn "No containers found"
exit 1
fi
CTID=$(whiptail --title "Select LXC Container" --checklist \
"Choose container to apply hardware passthrough:" 15 50 5 \
"${options[@]}" 3>&1 1>&2 2>&3 | tr -d '"')
if [[ -z "$CTID" ]]; then
msg warn "No container selected"
exit 1
fi
LXC_CONFIG="/etc/pve/lxc/${CTID}.conf"
}
function select_hw_options() {
local options=(
"usb" "USB Passthrough" OFF "usb" "USB Passthrough" OFF
"intel" "Intel VAAPI GPU" OFF "intel" "Intel VAAPI GPU" OFF
"nvidia" "NVIDIA GPU" OFF "nvidia" "NVIDIA GPU" OFF
"amd" "AMD GPU (ROCm)" OFF "amd" "AMD GPU (ROCm)" OFF
) )
SELECTIONS=$(whiptail --title "Hardware Options" --checklist \ SELECTED_FEATURES=$(whiptail --title "Hardware Options" --checklist \
"Select hardware features to passthrough:" 20 50 10 \ "Select hardware features to passthrough:" 20 50 10 \
"${options[@]}" 3>&1 1>&2 2>&3 | tr -d '"') "${opts[@]}" 3>&1 1>&2 2>&3 | tr -d '"')
if [[ -z "$SELECTIONS" ]]; then if [[ -z "$SELECTED_FEATURES" ]]; then
msg warn "No hardware passthrough options selected" msg warn "No hardware passthrough options selected"
exit 1 exit 1
fi fi
} }
function add_usb_passthrough() { function select_lxc_targets() {
if ! ls /dev/ttyUSB* &>/dev/null && ! ls /dev/ttyACM* &>/dev/null; then local list; local opts=()
msg warn "No USB serial devices found" if ! list=$(pct list | awk 'NR>1 {print $1 "|" $2}' | xargs -n1); then
return msg err "Failed to get container list"
exit 1
fi fi
cat <<EOF >> "$LXC_CONFIG" while IFS="|" read -r id name; do
if [[ -f "/etc/pve/lxc/${id}.conf" ]]; then
opts+=("$id" "${name} (${id})" "OFF")
fi
done <<< "$list"
if [[ ${#opts[@]} -eq 0 ]]; then
msg warn "No containers found"
exit 1
fi
SELECTED_CTIDS=$(whiptail --title "Select LXC Containers" --checklist \
"Choose container(s) to apply passthrough:" 20 60 10 \
"${opts[@]}" 3>&1 1>&2 2>&3 | tr -d '"')
if [[ -z "$SELECTED_CTIDS" ]]; then
msg warn "No containers selected"
exit 1
fi
}
function apply_usb() {
local conf="$1"
if ! compgen -G "/dev/ttyUSB* /dev/ttyACM*" >/dev/null; then
msg warn "No USB serial devices found, skipping"
return 1
fi
cat <<EOF >> "$conf"
# USB Passthrough # USB Passthrough
lxc.cgroup2.devices.allow: a lxc.cgroup2.devices.allow: a
lxc.cap.drop: lxc.cap.drop:
@ -107,15 +109,16 @@ lxc.mount.entry: /dev/ttyUSB1 dev/ttyUSB1 none bind,optional,create=
lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file lxc.mount.entry: /dev/ttyACM0 dev/ttyACM0 none bind,optional,create=file
lxc.mount.entry: /dev/ttyACM1 dev/ttyACM1 none bind,optional,create=file lxc.mount.entry: /dev/ttyACM1 dev/ttyACM1 none bind,optional,create=file
EOF EOF
msg ok "USB passthrough added to $CTID" return 0
} }
function add_intel_gpu() { function apply_intel() {
local conf="$1"
if [[ ! -e /dev/dri/renderD128 ]]; then if [[ ! -e /dev/dri/renderD128 ]]; then
msg warn "Intel GPU not detected" msg warn "Intel GPU not detected, skipping"
return return 1
fi fi
cat <<EOF >> "$LXC_CONFIG" cat <<EOF >> "$conf"
# Intel VAAPI # Intel VAAPI
lxc.cgroup2.devices.allow: c 226:* rwm lxc.cgroup2.devices.allow: c 226:* rwm
lxc.cgroup2.devices.allow: c 29:0 rwm lxc.cgroup2.devices.allow: c 29:0 rwm
@ -123,15 +126,16 @@ 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 dev/dri none bind,optional,create=dir
lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file lxc.mount.entry: /dev/dri/renderD128 dev/dri/renderD128 none bind,optional,create=file
EOF EOF
msg ok "Intel VAAPI passthrough added to $CTID" return 0
} }
function add_nvidia_gpu() { function apply_nvidia() {
local conf="$1"
if [[ ! -e /dev/nvidia0 ]]; then if [[ ! -e /dev/nvidia0 ]]; then
msg warn "NVIDIA device not found" msg warn "NVIDIA device not found, skipping"
return return 1
fi fi
cat <<EOF >> "$LXC_CONFIG" cat <<EOF >> "$conf"
# NVIDIA GPU # NVIDIA GPU
lxc.cgroup2.devices.allow: c 195:* rwm lxc.cgroup2.devices.allow: c 195:* 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
@ -139,38 +143,47 @@ 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
msg ok "NVIDIA passthrough added to $CTID" return 0
} }
function add_amd_gpu() { function apply_amd() {
local conf="$1"
if [[ ! -e /dev/kfd ]]; then if [[ ! -e /dev/kfd ]]; then
msg warn "AMD ROCm device not detected" msg warn "AMD GPU not found, skipping"
return return 1
fi fi
cat <<EOF >> "$LXC_CONFIG" cat <<EOF >> "$conf"
# AMD ROCm GPU # AMD ROCm GPU
lxc.cgroup2.devices.allow: c 226:* rwm lxc.cgroup2.devices.allow: c 226:* rwm
lxc.cgroup2.devices.allow: c 238:* rwm lxc.cgroup2.devices.allow: c 238:* rwm
lxc.mount.entry: /dev/kfd dev/kfd none bind,optional,create=file lxc.mount.entry: /dev/kfd dev/kfd none bind,optional,create=file
lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir
EOF EOF
msg ok "AMD GPU passthrough added to $CTID" return 0
} }
function main() { function main() {
header_info header_info
select_lxc_container select_hw_features
select_hw_options select_lxc_targets
for opt in $SELECTIONS; do for ctid in $SELECTED_CTIDS; do
local conf="/etc/pve/lxc/${ctid}.conf"
local updated=0
for opt in $SELECTED_FEATURES; do
case "$opt" in case "$opt" in
usb) add_usb_passthrough ;; usb) apply_usb "$conf" && updated=1 ;;
intel) add_intel_gpu ;; intel) apply_intel "$conf" && updated=1 ;;
nvidia) add_nvidia_gpu ;; nvidia) apply_nvidia "$conf" && updated=1 ;;
amd) add_amd_gpu ;; amd) apply_amd "$conf" && updated=1 ;;
esac esac
done done
msg ok "Hardware passthrough updated in: $LXC_CONFIG" if [[ "$updated" -eq 1 ]]; then
printf "\nRestart container %s to apply changes.\n\n" "$CTID" msg ok "Hardware passthrough updated in: $conf"
printf "\nRestart container %s to apply changes.\n\n" "$ctid"
else
msg warn "No passthrough changes applied for container $ctid"
fi
done
} }
main main