Improve LXC template selection and cleanup hwaccel code
Some checks failed
Bump build.func Revision / bump-revision (push) Has been cancelled

Enhanced the LXC container creation process to prompt for available template versions if the requested template is missing, allowing users to select from available options interactively. Also refactored the hardware acceleration setup function for better readability and consistency, and made minor whitespace and formatting adjustments.
This commit is contained in:
CanbiZ 2025-10-21 14:12:26 +02:00
parent da4118d452
commit 9b624944c7
2 changed files with 109 additions and 41 deletions

View File

@ -3052,9 +3052,78 @@ create_lxc_container() {
TEMPLATE_BASE=$(awk -v s="$TEMPLATE_STORAGE" '$1==s {f=1} f && /path/ {print $2; exit}' /etc/pve/storage.cfg) TEMPLATE_BASE=$(awk -v s="$TEMPLATE_STORAGE" '$1==s {f=1} f && /path/ {print $2; exit}' /etc/pve/storage.cfg)
[[ -n "$TEMPLATE_BASE" ]] && TEMPLATE_PATH="$TEMPLATE_BASE/template/cache/$TEMPLATE" [[ -n "$TEMPLATE_BASE" ]] && TEMPLATE_PATH="$TEMPLATE_BASE/template/cache/$TEMPLATE"
fi fi
[[ -n "$TEMPLATE_PATH" ]] || { [[ -n "$TEMPLATE_PATH" ]] || {
msg_error "Unable to resolve template path for $TEMPLATE_STORAGE. Check storage type and permissions." if [[ -z "$TEMPLATE" ]]; then
exit 220 msg_error "Template ${PCT_OSTYPE} ${PCT_OSVERSION} not available"
# Get available versions
mapfile -t AVAILABLE_VERSIONS < <(
pveam available -section system 2>/dev/null |
grep "^${PCT_OSTYPE}-" |
sed -E 's/.*'"${PCT_OSTYPE}"'-([0-9]+\.[0-9]+).*/\1/' |
sort -V -u
)
if [[ ${#AVAILABLE_VERSIONS[@]} -gt 0 ]]; then
msg_info "Available versions:"
for i in "${!AVAILABLE_VERSIONS[@]}"; do
echo " [$((i + 1))] ${AVAILABLE_VERSIONS[$i]}"
done
echo ""
read -p "Select version [1-${#AVAILABLE_VERSIONS[@]}] or Enter to exit: " choice
if [[ "$choice" =~ ^[0-9]+$ ]] && [[ "$choice" -ge 1 ]] && [[ "$choice" -le ${#AVAILABLE_VERSIONS[@]} ]]; then
export var_version="${AVAILABLE_VERSIONS[$((choice - 1))]}"
export PCT_OSVERSION="$var_version"
msg_ok "Switched to ${PCT_OSTYPE} ${var_version}"
# Retry template search with new version
TEMPLATE_SEARCH="${PCT_OSTYPE}-${PCT_OSVERSION:-}"
mapfile -t LOCAL_TEMPLATES < <(
pveam list "$TEMPLATE_STORAGE" 2>/dev/null |
awk -v s="$TEMPLATE_SEARCH" -v p="$TEMPLATE_PATTERN" '$1 ~ s && $1 ~ p {print $1}' |
sed 's|.*/||' | sort -t - -k 2 -V
)
mapfile -t ONLINE_TEMPLATES < <(
pveam available -section system 2>/dev/null |
sed -n "s/.*\($TEMPLATE_SEARCH.*$TEMPLATE_PATTERN.*\)/\1/p" |
sort -t - -k 2 -V
)
ONLINE_TEMPLATE=""
[[ ${#ONLINE_TEMPLATES[@]} -gt 0 ]] && ONLINE_TEMPLATE="${ONLINE_TEMPLATES[-1]}"
if [[ ${#LOCAL_TEMPLATES[@]} -gt 0 ]]; then
TEMPLATE="${LOCAL_TEMPLATES[-1]}"
TEMPLATE_SOURCE="local"
else
TEMPLATE="$ONLINE_TEMPLATE"
TEMPLATE_SOURCE="online"
fi
TEMPLATE_PATH="$(pvesm path $TEMPLATE_STORAGE:vztmpl/$TEMPLATE 2>/dev/null || true)"
if [[ -z "$TEMPLATE_PATH" ]]; then
TEMPLATE_BASE=$(awk -v s="$TEMPLATE_STORAGE" '$1==s {f=1} f && /path/ {print $2; exit}' /etc/pve/storage.cfg)
[[ -n "$TEMPLATE_BASE" ]] && TEMPLATE_PATH="$TEMPLATE_BASE/template/cache/$TEMPLATE"
fi
[[ -n "$TEMPLATE_PATH" ]] || {
msg_error "Template still not found after version change"
exit 220
}
else
msg_info "Installation cancelled"
exit 0
fi
else
msg_error "No ${PCT_OSTYPE} templates available"
exit 220
fi
else
msg_error "Unable to resolve template path for $TEMPLATE_STORAGE"
exit 220
fi
} }
msg_ok "Template ${BL}$TEMPLATE${CL} [$TEMPLATE_SOURCE]" msg_ok "Template ${BL}$TEMPLATE${CL} [$TEMPLATE_SOURCE]"

View File

@ -1803,7 +1803,6 @@ function setup_gs() {
msg_ok "Installed Ghostscript $LATEST_VERSION_DOTTED" msg_ok "Installed Ghostscript $LATEST_VERSION_DOTTED"
} }
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Sets up Hardware Acceleration on debian or ubuntu. # Sets up Hardware Acceleration on debian or ubuntu.
# #
@ -1815,7 +1814,7 @@ function setup_gs() {
# Notes: # Notes:
# - Some things are fetched from intel repositories due to not being in debian repositories. # - Some things are fetched from intel repositories due to not being in debian repositories.
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
function setup_hwaccel () { function setup_hwaccel() {
msg_info "Setting Up Hardware Acceleration" msg_info "Setting Up Hardware Acceleration"
if ! command -v lspci &>/dev/null; then if ! command -v lspci &>/dev/null; then
@ -1845,45 +1844,45 @@ function setup_hwaccel () {
local in_ct="${CTTYPE:-0}" local in_ct="${CTTYPE:-0}"
case "$gpu_vendor" in case "$gpu_vendor" in
Intel) Intel)
msg_info "Detected Intel GPU — configuring Intel hardware acceleration" msg_info "Detected Intel GPU — configuring Intel hardware acceleration"
if [[ "$os_id" == "ubuntu" ]]; then if [[ "$os_id" == "ubuntu" ]]; then
$STD apt -y install intel-opencl-icd || msg_error "Failed to install intel-opencl-icd" $STD apt -y install intel-opencl-icd || msg_error "Failed to install intel-opencl-icd"
else else
fetch_and_deploy_gh_release "intel-igc-core-2" "intel/intel-graphics-compiler" "binary" "latest" "" "intel-igc-core-2_*_amd64.deb" fetch_and_deploy_gh_release "intel-igc-core-2" "intel/intel-graphics-compiler" "binary" "latest" "" "intel-igc-core-2_*_amd64.deb"
fetch_and_deploy_gh_release "intel-igc-opencl-2" "intel/intel-graphics-compiler" "binary" "latest" "" "intel-igc-opencl-2_*_amd64.deb" fetch_and_deploy_gh_release "intel-igc-opencl-2" "intel/intel-graphics-compiler" "binary" "latest" "" "intel-igc-opencl-2_*_amd64.deb"
fetch_and_deploy_gh_release "intel-libgdgmm12" "intel/compute-runtime" "binary" "latest" "" "libigdgmm12_*_amd64.deb" fetch_and_deploy_gh_release "intel-libgdgmm12" "intel/compute-runtime" "binary" "latest" "" "libigdgmm12_*_amd64.deb"
fetch_and_deploy_gh_release "intel-opencl-icd" "intel/compute-runtime" "binary" "latest" "" "intel-opencl-icd_*_amd64.deb" fetch_and_deploy_gh_release "intel-opencl-icd" "intel/compute-runtime" "binary" "latest" "" "intel-opencl-icd_*_amd64.deb"
fi fi
$STD apt -y install va-driver-all ocl-icd-libopencl1 vainfo intel-gpu-tools || msg_error "Failed to install GPU dependencies" $STD apt -y install va-driver-all ocl-icd-libopencl1 vainfo intel-gpu-tools || msg_error "Failed to install GPU dependencies"
;; ;;
AMD) AMD)
msg_info "Detected AMD GPU — configuring Mesa-based hardware acceleration" msg_info "Detected AMD GPU — configuring Mesa-based hardware acceleration"
$STD apt -y install mesa-va-drivers mesa-vdpau-drivers mesa-opencl-icd vainfo clinfo || msg_error "Failed to install AMD GPU dependencies" $STD apt -y install mesa-va-drivers mesa-vdpau-drivers mesa-opencl-icd vainfo clinfo || msg_error "Failed to install AMD GPU dependencies"
# For AMD CPUs without discrete GPU (APUs) # For AMD CPUs without discrete GPU (APUs)
if [[ "$cpu_vendor" == "AuthenticAMD" && "$gpu_vendor" != "AMD" ]]; then if [[ "$cpu_vendor" == "AuthenticAMD" && "$gpu_vendor" != "AMD" ]]; then
msg_info "Detected AMD CPU (APU) — enabling VAAPI via Mesa" msg_info "Detected AMD CPU (APU) — enabling VAAPI via Mesa"
$STD apt -y install libdrm-amdgpu1 firmware-amd-graphics || true $STD apt -y install libdrm-amdgpu1 firmware-amd-graphics || true
fi fi
;; ;;
NVIDIA) NVIDIA)
msg_info "Detected NVIDIA GPU — skipping automatic configuration (manual driver setup required)" msg_info "Detected NVIDIA GPU — skipping automatic configuration (manual driver setup required)"
msg_info "→ Please install proprietary drivers manually via: apt install nvidia-driver" msg_info "→ Please install proprietary drivers manually via: apt install nvidia-driver"
;; ;;
*) *)
# If no discrete GPU, but AMD CPU (e.g., Ryzen APU) # If no discrete GPU, but AMD CPU (e.g., Ryzen APU)
if [[ "$cpu_vendor" == "AuthenticAMD" ]]; then if [[ "$cpu_vendor" == "AuthenticAMD" ]]; then
msg_info "Detected AMD CPU without discrete GPU — installing Mesa OpenCL stack" msg_info "Detected AMD CPU without discrete GPU — installing Mesa OpenCL stack"
$STD apt -y install mesa-opencl-icd ocl-icd-libopencl1 clinfo || msg_error "Failed to install Mesa OpenCL stack" $STD apt -y install mesa-opencl-icd ocl-icd-libopencl1 clinfo || msg_error "Failed to install Mesa OpenCL stack"
else else
msg_error "No supported GPU vendor detected" msg_error "No supported GPU vendor detected"
return 1 return 1
fi fi
;; ;;
esac esac
if [[ "$in_ct" == "0" ]]; then if [[ "$in_ct" == "0" ]]; then
@ -2414,7 +2413,7 @@ function setup_mysql() {
SUITE="bookworm" SUITE="bookworm"
;; ;;
*) *)
SUITE="bookworm" # Fallback to bookworm for unknown Debian versions SUITE="bookworm" # Fallback to bookworm for unknown Debian versions
;; ;;
esac esac
else else
@ -2424,7 +2423,7 @@ function setup_mysql() {
# Stop existing MySQL if running # Stop existing MySQL if running
$STD systemctl stop mysql 2>/dev/null || true $STD systemctl stop mysql 2>/dev/null || true
# Only purge if MySQL is actually installed # Only purge if MySQL is actually installed
if dpkg -l 2>/dev/null | grep -q "^ii.*mysql-server"; then if dpkg -l 2>/dev/null | grep -q "^ii.*mysql-server"; then
$STD apt purge -y mysql-server* mysql-client* mysql-common 2>/dev/null || true $STD apt purge -y mysql-server* mysql-client* mysql-common 2>/dev/null || true