ProxMenux/scripts/configure_igpu_lxc.sh

205 lines
6.2 KiB
Bash
Raw Normal View History

2025-01-30 11:35:06 +01:00
#!/bin/bash
2025-01-30 12:10:42 +01:00
# ==========================================================
# ProxMenu - A menu-driven script for Proxmox VE management
# ==========================================================
# Author : MacRimi
# Copyright : (c) 2024 MacRimi
# License : MIT (https://raw.githubusercontent.com/MacRimi/ProxMenux/main/LICENSE)
2025-08-17 13:08:39 +02:00
# Version : 1.1
# Last Updated: 17/08/2025
2025-01-30 12:10:42 +01:00
# ==========================================================
2025-01-30 13:10:26 +01:00
# Description:
2025-02-06 11:38:42 +01:00
# This script automates the process of enabling and configuring Intel Integrated GPU (iGPU) support in Proxmox VE LXC containers.
# Its goal is to simplify the configuration of hardware-accelerated graphical capabilities within containers, allowing for efficient
# use of Intel iGPUs for tasks such as transcoding, rendering, and accelerating graphics-intensive applications.
2025-01-30 13:10:26 +01:00
# ==========================================================
2025-01-30 12:10:42 +01:00
2025-01-30 12:57:35 +01:00
# Configuration ============================================
2025-02-01 09:13:31 +01:00
REPO_URL="https://raw.githubusercontent.com/MacRimi/ProxMenux/main"
2025-01-30 12:57:35 +01:00
BASE_DIR="/usr/local/share/proxmenux"
2025-02-01 08:48:45 +01:00
UTILS_FILE="$BASE_DIR/utils.sh"
2025-01-30 12:57:35 +01:00
VENV_PATH="/opt/googletrans-env"
2025-02-01 08:48:45 +01:00
if [[ -f "$UTILS_FILE" ]]; then
source "$UTILS_FILE"
2025-01-30 12:57:35 +01:00
fi
2025-02-01 09:13:31 +01:00
load_language
initialize_cache
2025-05-05 00:20:09 +02:00
2025-01-30 12:57:35 +01:00
# ==========================================================
2025-01-30 17:10:59 +01:00
2025-08-17 13:08:39 +02:00
2025-01-30 12:57:35 +01:00
select_container() {
2025-02-04 18:45:00 +01:00
2025-01-30 12:57:35 +01:00
CONTAINERS=$(pct list | awk 'NR>1 {print $1, $3}' | xargs -n2)
if [ -z "$CONTAINERS" ]; then
msg_error "$(translate 'No containers available in Proxmox.')"
exit 1
fi
CONTAINER_ID=$(whiptail --title "$(translate 'Select Container')" \
2025-07-09 18:59:26 +02:00
--menu "$(translate 'Select the LXC container:')" 20 70 10 $CONTAINERS 3>&1 1>&2 2>&3)
2025-01-30 12:57:35 +01:00
if [ -z "$CONTAINER_ID" ]; then
msg_error "$(translate 'No container selected. Exiting.')"
exit 1
fi
if ! pct list | awk 'NR>1 {print $1}' | grep -qw "$CONTAINER_ID"; then
msg_error "$(translate 'Container with ID') $CONTAINER_ID $(translate 'does not exist. Exiting.')"
exit 1
fi
msg_ok "$(translate 'Container selected:') $CONTAINER_ID"
}
2025-02-01 09:13:31 +01:00
2025-08-17 13:08:39 +02:00
2025-02-01 09:13:31 +01:00
validate_container_id() {
if [ -z "$CONTAINER_ID" ]; then
msg_error "$(translate 'Container ID not defined. Make sure to select a container first.')"
exit 1
fi
2025-08-17 13:08:39 +02:00
2025-02-01 09:13:31 +01:00
if pct status "$CONTAINER_ID" | grep -q "running"; then
msg_info "$(translate 'Stopping the container before applying configuration...')"
pct stop "$CONTAINER_ID"
msg_ok "$(translate 'Container stopped.')"
fi
}
2025-08-16 18:12:14 +02:00
configure_lxc_for_igpu() {
2025-08-17 13:08:39 +02:00
validate_container_id
2025-03-04 17:37:46 +01:00
2025-08-17 13:08:39 +02:00
CONFIG_FILE="/etc/pve/lxc/${CONTAINER_ID}.conf"
[[ -f "$CONFIG_FILE" ]] || { msg_error "$(translate 'Configuration file for container') $CONTAINER_ID $(translate 'not found.')"; exit 1; }
if [[ ! -d /dev/dri ]]; then
modprobe i915 2>/dev/null || true
for _ in {1..5}; do
[[ -d /dev/dri ]] && break
sleep 1
done
fi
2025-03-04 17:37:46 +01:00
2025-08-17 13:08:39 +02:00
CT_TYPE=$(pct config "$CONTAINER_ID" | awk '/^unprivileged:/ {print $2}')
[[ -z "$CT_TYPE" ]] && CT_TYPE="0"
2025-03-04 17:37:46 +01:00
2025-08-17 13:08:39 +02:00
msg_info "$(translate 'Configuring Intel iGPU passthrough for container...')"
2025-03-04 17:37:46 +01:00
2025-08-17 13:08:39 +02:00
for rn in /dev/dri/renderD*; do
[[ -e "$rn" ]] || continue
chmod 660 "$rn" 2>/dev/null || true
chgrp render "$rn" 2>/dev/null || true
done
2025-01-30 12:57:35 +01:00
2025-08-17 13:08:39 +02:00
mapfile -t RENDER_NODES < <(find /dev/dri -maxdepth 1 -type c -name 'renderD*' 2>/dev/null || true)
mapfile -t CARD_NODES < <(find /dev/dri -maxdepth 1 -type c -name 'card*' 2>/dev/null || true)
FB_NODE=""
[[ -e /dev/fb0 ]] && FB_NODE="/dev/fb0"
2025-01-30 12:57:35 +01:00
2025-08-17 13:08:39 +02:00
if [[ ${#RENDER_NODES[@]} -eq 0 && ${#CARD_NODES[@]} -eq 0 && -z "$FB_NODE" ]]; then
msg_warn "$(translate 'No VA-API devices found on host (/dev/dri*, /dev/fb0). Is i915 loaded?')"
return 0
2025-01-30 12:57:35 +01:00
fi
2025-08-17 13:08:39 +02:00
if grep -q '^features:' "$CONFIG_FILE"; then
grep -Eq '^features:.*(^|,)\s*nesting=1(\s|,|$)' "$CONFIG_FILE" || sed -i 's/^features:\s*/&nesting=1, /' "$CONFIG_FILE"
else
echo "features: nesting=1" >> "$CONFIG_FILE"
2025-01-30 12:57:35 +01:00
fi
2025-02-01 09:13:31 +01:00
2025-08-17 13:08:39 +02:00
if [[ "$CT_TYPE" == "0" ]]; then
2025-02-01 09:13:31 +01:00
2025-08-17 13:08:39 +02:00
sed -i '/^lxc\.cgroup2\.devices\.allow:\s*c\s*226:/d' "$CONFIG_FILE"
sed -i '\|^lxc\.mount\.entry:\s*/dev/dri|d' "$CONFIG_FILE"
sed -i '\|^lxc\.mount\.entry:\s*/dev/fb0|d' "$CONFIG_FILE"
2025-08-16 17:34:34 +02:00
2025-08-17 13:08:39 +02:00
echo "lxc.cgroup2.devices.allow: c 226:* rwm" >> "$CONFIG_FILE"
echo "lxc.mount.entry: /dev/dri dev/dri none bind,optional,create=dir" >> "$CONFIG_FILE"
[[ -n "$FB_NODE" ]] && echo "lxc.mount.entry: /dev/fb0 dev/fb0 none bind,optional,create=file" >> "$CONFIG_FILE"
2025-08-16 17:34:34 +02:00
2025-08-17 13:08:39 +02:00
else
sed -i '/^dev[0-9]\+:/d' "$CONFIG_FILE"
idx=0
for c in "${CARD_NODES[@]}"; do
echo "dev${idx}: $c,gid=44" >> "$CONFIG_FILE"
idx=$((idx+1))
done
for r in "${RENDER_NODES[@]}"; do
echo "dev${idx}: $r,gid=104" >> "$CONFIG_FILE"
idx=$((idx+1))
done
2025-08-16 17:34:34 +02:00
fi
msg_ok "$(translate 'iGPU configuration added to container') $CONTAINER_ID."
2025-08-17 13:08:39 +02:00
2025-08-16 17:34:34 +02:00
}
2025-02-01 20:52:00 +01:00
install_igpu_in_container() {
2025-02-01 09:13:31 +01:00
2025-02-04 21:06:51 +01:00
msg_info2 "$(translate 'Installing iGPU drivers inside the container...')"
tput sc
LOG_FILE=$(mktemp)
2025-08-17 13:08:39 +02:00
2025-03-04 17:37:46 +01:00
pct start "$CONTAINER_ID" >/dev/null 2>&1
2025-02-04 21:06:51 +01:00
script -q -c "pct exec \"$CONTAINER_ID\" -- bash -c '
2025-02-01 21:36:33 +01:00
set -e
2025-08-17 13:08:39 +02:00
getent group video >/dev/null || groupadd -g 44 video
getent group render >/dev/null || groupadd -g 104 render
usermod -aG video,render root || true
2025-03-04 17:37:46 +01:00
apt-get update >/dev/null 2>&1
2025-02-04 21:06:51 +01:00
apt-get install -y va-driver-all ocl-icd-libopencl1 intel-opencl-icd vainfo intel-gpu-tools
2025-08-17 13:08:39 +02:00
chgrp video /dev/dri 2>/dev/null || true
chmod 755 /dev/dri 2>/dev/null || true
2025-02-04 21:06:51 +01:00
'" "$LOG_FILE"
if [ $? -eq 0 ]; then
tput rc
tput ed
rm -f "$LOG_FILE"
msg_ok "$(translate 'iGPU drivers installed inside the container.')"
else
tput rc
tput ed
msg_error "$(translate 'Failed to install iGPU drivers inside the container.')"
cat "$LOG_FILE"
rm -f "$LOG_FILE"
exit 1
fi
2025-01-30 12:57:35 +01:00
}
2025-02-01 09:13:31 +01:00
select_container
2025-06-10 14:18:05 +02:00
show_proxmenux_logo
2025-08-17 13:08:39 +02:00
msg_title "$(translate "Add HW iGPU acceleration to an LXC")"
2025-02-01 20:52:00 +01:00
configure_lxc_for_igpu
install_igpu_in_container
2025-02-01 09:13:31 +01:00
2025-03-04 17:37:46 +01:00
msg_success "$(translate 'iGPU configuration completed in container') $CONTAINER_ID."
2025-07-09 18:59:26 +02:00
echo -e
msg_success "$(translate "Press Enter to return to menu...")"
read -r