Update disk-passthrough_ct.sh

This commit is contained in:
MacRimi 2025-07-30 18:43:54 +02:00
parent f8ebf03afd
commit df68154f10

View File

@ -1,13 +1,12 @@
#!/bin/bash
# ==========================================================
# 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)
# Version : 1.1
# Last Updated: 28/06/2025
# Version : 1.2
# Last Updated: 30/07/2025
# ==========================================================
# Description:
# This script allows users to assign physical disks to existing
@ -17,6 +16,7 @@
# - Identifies and displays unassigned physical disks.
# - Allows the user to select multiple disks and attach them to a CT.
# - Configures the selected disks for the CT and verifies the assignment.
# - Uses persistent device paths to avoid issues with device order changes.
# ==========================================================
# Configuration ============================================
@ -32,8 +32,66 @@ fi
load_language
initialize_cache
# Get OS codename for repository configuration
OS_CODENAME="$(grep "VERSION_CODENAME=" /etc/os-release | cut -d"=" -f 2 | xargs )"
# ==========================================================
# Function to get persistent device path
get_persistent_path() {
local device="$1"
local persistent_path=""
# Try by-id first (most reliable)
for path in /dev/disk/by-id/*; do
if [[ -e "$path" && "$(readlink -f "$path")" == "$device" ]]; then
# Prefer ata- or scsi- over wwn- for readability
if [[ "$path" =~ ata-|scsi- ]]; then
echo "$path"
return 0
elif [[ -z "$persistent_path" ]]; then
persistent_path="$path"
fi
fi
done
# Return the first found by-id path if any
if [[ -n "$persistent_path" ]]; then
echo "$persistent_path"
return 0
fi
# Try by-path as fallback
for path in /dev/disk/by-path/*; do
if [[ -e "$path" && "$(readlink -f "$path")" == "$device" ]]; then
echo "$path"
return 0
fi
done
# Fallback to original device if no persistent path found
msg_warn "$(translate "No persistent path found for") $device, $(translate "using direct path")"
echo "$device"
}
# Function to ensure repositories are properly configured
ensure_repositories() {
local sources_file="/etc/apt/sources.list"
local need_update=false
# Only verify the main repository with contrib and non-free
if ! grep -q "deb.*${OS_CODENAME}.*main" "$sources_file"; then
echo "deb http://deb.debian.org/debian ${OS_CODENAME} main contrib non-free non-free-firmware" >> "$sources_file"
need_update=true
fi
if [ "$need_update" = true ]; then
apt update >/dev/null 2>&1
fi
return 0
}
get_disk_info() {
local disk=$1
MODEL=$(lsblk -dn -o MODEL "$disk" | xargs)
@ -41,8 +99,10 @@ get_disk_info() {
echo "$MODEL" "$SIZE"
}
CT_LIST=$(pct list | awk 'NR>1 {print $1, $3}')
# Ensure repositories are configured
ensure_repositories
CT_LIST=$(pct list | awk 'NR>1 {print $1, $3}')
if [ -z "$CT_LIST" ]; then
whiptail --title "$(translate "Error")" --msgbox "$(translate "No CTs available in the system.")" 8 40
exit 1
@ -60,12 +120,11 @@ CTID=$(echo "$CTID" | tr -d '"')
clear
show_proxmenux_logo
echo -e
msg_info2 "$(translate "Add Disk") Passthrough $(translate "to a LXC")"
msg_title "$(translate "Add Disk") Passthrough $(translate "to a LXC")"
echo -e
msg_ok "$(translate "CT selected successfully.")"
CT_STATUS=$(pct status "$CTID" | awk '{print $2}')
if [ "$CT_STATUS" != "running" ]; then
msg_info "$(translate "Starting CT") $CTID..."
pct start "$CTID"
@ -79,10 +138,10 @@ if [ "$CT_STATUS" != "running" ]; then
fi
CONF_FILE="/etc/pve/lxc/$CTID.conf"
if grep -q '^unprivileged: 1' "$CONF_FILE"; then
if whiptail --title "$(translate "Privileged Container")" \
--yesno "$(translate "The selected container is unprivileged. A privileged container is required for direct device passthrough.")\\n\\n$(translate "Do you want to convert it to a privileged container now?")" 12 70; then
msg_info "$(translate "Stopping container") $CTID..."
pct shutdown "$CTID" &>/dev/null
for i in {1..10}; do
@ -91,15 +150,18 @@ if grep -q '^unprivileged: 1' "$CONF_FILE"; then
break
fi
done
if [ "$(pct status "$CTID" | awk '{print $2}')" == "running" ]; then
msg_error "$(translate "Failed to stop the container.")"
exit 1
fi
msg_ok "$(translate "Container stopped.")"
cp "$CONF_FILE" "$CONF_FILE.bak"
sed -i '/^unprivileged: 1/d' "$CONF_FILE"
echo "unprivileged: 0" >> "$CONF_FILE"
msg_ok "$(translate "Container successfully converted to privileged.")"
msg_info "$(translate "Starting container") $CTID..."
pct start "$CTID" &>/dev/null
sleep 2
@ -123,7 +185,6 @@ MOUNTED_DISKS=$(lsblk -ln -o NAME,MOUNTPOINT | awk '$2!="" {print "/dev/" $1}')
ZFS_DISKS=""
ZFS_RAW=$(zpool list -v -H 2>/dev/null | awk '{print $1}' | grep -v '^NAME$' | grep -v '^-' | grep -v '^mirror')
for entry in $ZFS_RAW; do
path=""
if [[ "$entry" == wwn-* || "$entry" == ata-* ]]; then
@ -133,6 +194,7 @@ for entry in $ZFS_RAW; do
elif [[ "$entry" == /dev/* ]]; then
path="$entry"
fi
if [ -n "$path" ]; then
base_disk=$(lsblk -no PKNAME "$path" 2>/dev/null)
if [ -n "$base_disk" ]; then
@ -140,7 +202,6 @@ for entry in $ZFS_RAW; do
fi
fi
done
ZFS_DISKS=$(echo "$ZFS_DISKS" | sort -u)
is_disk_in_use() {
@ -154,6 +215,7 @@ is_disk_in_use() {
return 0
fi
done < <(lsblk -ln -o NAME,FSTYPE "$disk" | tail -n +2)
if echo "$USED_DISKS" | grep -q "$disk" || echo "$ZFS_DISKS" | grep -q "$disk"; then
return 0
fi
@ -166,6 +228,7 @@ RAID_ACTIVE=$(grep -Po 'md\d+\s*:\s*active\s+raid[0-9]+' /proc/mdstat | awk '{pr
while read -r DISK; do
[[ "$DISK" =~ /dev/zd ]] && continue
INFO=($(get_disk_info "$DISK"))
MODEL="${INFO[@]::${#INFO[@]}-1}"
SIZE="${INFO[-1]}"
@ -193,6 +256,7 @@ while read -r DISK; do
USED_BY=""
REAL_PATH=$(readlink -f "$DISK")
CONFIG_DATA=$(grep -vE '^\s*#' /etc/pve/qemu-server/*.conf /etc/pve/lxc/*.conf 2>/dev/null)
if grep -Fq "$REAL_PATH" <<< "$CONFIG_DATA"; then
USED_BY="$(translate "In use")"
else
@ -220,8 +284,15 @@ while read -r DISK; do
SHOW_DISK=false
fi
# Check if disk is already assigned to this CT using persistent paths
if pct config "$CTID" | grep -vE '^\s*#|^description:' | grep -q "$DISK"; then
SHOW_DISK=false
else
# Also check persistent paths
PERSISTENT_DISK=$(get_persistent_path "$DISK")
if [[ "$PERSISTENT_DISK" != "$DISK" ]] && pct config "$CTID" | grep -vE '^\s*#|^description:' | grep -q "$PERSISTENT_DISK"; then
SHOW_DISK=false
fi
fi
if $SHOW_DISK; then
@ -229,6 +300,7 @@ while read -r DISK; do
[[ "$IS_RAID" == true ]] && LABEL+=" ⚠ RAID"
[[ "$IS_LVM" == true ]] && LABEL+=" ⚠ LVM"
[[ "$IS_ZFS" == true ]] && LABEL+=" ⚠ ZFS"
DESCRIPTION=$(printf "%-30s %10s%s" "$MODEL" "$SIZE" "$LABEL")
FREE_DISKS+=("$DISK" "$DESCRIPTION" "OFF")
fi
@ -270,6 +342,7 @@ msg_info "$(translate "Processing selected disks...")"
for DISK in $SELECTED; do
DISK=$(echo "$DISK" | tr -d '"')
DISK_INFO=$(get_disk_info "$DISK")
ASSIGNED_TO=""
RUNNING_CTS=""
RUNNING_VMS=""
@ -309,6 +382,7 @@ for DISK in $SELECTED; do
fi
cleanup
if lsblk "$DISK" | grep -q "raid" || grep -q "${DISK##*/}" /proc/mdstat; then
whiptail --title "$(translate "RAID Detected")" --msgbox "$(translate "The disk") $DISK_INFO $(translate "appears to be part of a") RAID. $(translate "For security reasons, the system cannot format it.")\\n\\n$(translate "If you are sure you want to use it, please remove the") RAID metadata $(translate "or format it manually using external tools.")\\n\\n$(translate "After that, run this script again to add it.")" 18 70
clear
@ -350,12 +424,14 @@ for DISK in $SELECTED; do
if [ $? -ne 0 ]; then
continue
fi
echo -e "$(translate "Creating partition table and partition...")"
parted -s "$DISK" mklabel gpt
parted -s "$DISK" mkpart primary 0% 100%
sleep 2
partprobe "$DISK"
sleep 2
PARTITION=$(lsblk -rno NAME "$DISK" | awk -v disk="$(basename "$DISK")" '$1 != disk {print $1; exit}')
if [ -n "$PARTITION" ]; then
PARTITION="/dev/$PARTITION"
@ -383,7 +459,6 @@ for DISK in $SELECTED; do
fi
whiptail --title "$(translate "WARNING")" --yesno "$(translate "WARNING: This operation will FORMAT the disk") $DISK_INFO $(translate "with") $FORMAT_TYPE.\\n\\n$(translate "ALL DATA ON THIS DISK WILL BE PERMANENTLY LOST!")\\n\\n$(translate "Are you sure you want to continue")" 15 70
if [ $? -ne 0 ]; then
whiptail --title "$(translate "Format Cancelled")" --msgbox "$(translate "Format operation cancelled. The disk will not be added.")" 8 60
continue
@ -444,32 +519,38 @@ for DISK in $SELECTED; do
fi
##############################################################################
# Get persistent path for the partition
PERSISTENT_PARTITION=$(get_persistent_path "$PARTITION")
# Apply passthrough with persistent path
CURRENT_FS=$(lsblk -no FSTYPE "$PARTITION" | xargs)
if [ "$CURRENT_FS" == "xfs" ] || [ "$FORMAT_TYPE" == "xfs" ]; then
RESULT=$(pct set "$CTID" -mp${INDEX} "$PARTITION,mp=$MOUNT_POINT,backup=0,ro=0" 2>&1)
RESULT=$(pct set "$CTID" -mp${INDEX} "$PERSISTENT_PARTITION,mp=$MOUNT_POINT,backup=0,ro=0" 2>&1)
else
RESULT=$(pct set "$CTID" -mp${INDEX} "$PARTITION,mp=$MOUNT_POINT,backup=0,ro=0,acl=1" 2>&1)
RESULT=$(pct set "$CTID" -mp${INDEX} "$PERSISTENT_PARTITION,mp=$MOUNT_POINT,backup=0,ro=0,acl=1" 2>&1)
fi
# Adjust permissions inside the CT
pct exec "$CTID" -- chmod -R 775 "$MOUNT_POINT" 2>/dev/null || true
pct exec "$CTID" -- chmod -R 777 "$MOUNT_POINT" 2>/dev/null || true
# Show confirmation with persistent identifier
msg_ok "$(translate "Assigned using") $PERSISTENT_PARTITION"
##############################################################################
if [ $? -eq 0 ]; then
MESSAGE="$(translate "The disk") $DISK_INFO $(translate "has been successfully added to CT") $CTID $(translate "as a mount point at") $MOUNT_POINT."
MESSAGE+="\\n$(translate "Using persistent path"): $PERSISTENT_PARTITION"
if [ -n "$ASSIGNED_TO" ]; then
MESSAGE+="\\n\\n$(translate "WARNING: This disk is also assigned to the following CT(s):")\\n$ASSIGNED_TO"
MESSAGE+="\\n$(translate "Make sure not to start CTs that share this disk at the same time to avoid data corruption.")"
fi
SUCCESS_MESSAGES+="$MESSAGE\\n\\n"
((DISKS_ADDED++))
else
ERROR_MESSAGES+="$(translate "Could not add disk") $DISK_INFO $(translate "to CT") $CTID.\\n$(translate "Error:") $RESULT\\n\\n"
fi
done
msg_ok "$(translate "Disk processing completed.")"
@ -485,5 +566,4 @@ fi
msg_success "$(translate "Press Enter to return to menu...")"
read -r
exit 0