Update lxc-delete.sh

This commit is contained in:
CanbiZ 2025-06-30 14:01:17 +02:00
parent 5e3ccb265f
commit e5b1668f68

View File

@ -44,10 +44,10 @@ if ! whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC De
echo -e "${RD}Aborted by user.${CL}" echo -e "${RD}Aborted by user.${CL}"
exit 0 exit 0
fi fi
NODE=$(hostname)
containers=$(pct list | tail -n +2 | awk '{print $0 " " $4}')
if [ -z "$containers" ]; then mapfile -t containers < <(pct list | tail -n +2)
if [ ${#containers[@]} -eq 0 ]; then
whiptail --title "LXC Container Delete" --msgbox "No LXC containers available!" 10 60 whiptail --title "LXC Container Delete" --msgbox "No LXC containers available!" 10 60
exit 1 exit 1
fi fi
@ -55,41 +55,39 @@ fi
menu_items=("ALL" "Delete ALL containers" "OFF") menu_items=("ALL" "Delete ALL containers" "OFF")
FORMAT="%-10s %-10s %-10s %-10s" FORMAT="%-10s %-10s %-10s %-10s"
while read -r container; do for line in "${containers[@]}"; do
container_id=$(echo $container | awk '{print $1}') container_id=$(echo "$line" | awk '{print $1}')
container_name=$(echo $container | awk '{print $2}') container_name=$(echo "$line" | awk '{print $2}')
container_status=$(echo $container | awk '{print $3}') container_status=$(echo "$line" | awk '{print $3}')
container_os=$(echo $container | awk '{print $4}') container_os=$(echo "$line" | awk '{print $4}')
protected=$(pct config $container_id | grep -i '^protection:' | awk '{print $2}') protected=$(pct config "$container_id" | awk '/^protection:/ {print $2}')
is_protected="No" is_protected="No"
[[ "$protected" == "1" ]] && is_protected="Yes" [[ "$protected" == "1" ]] && is_protected="Yes"
formatted_line=$(printf "$FORMAT" "$container_name" "$container_status" "$container_os" "$is_protected") formatted_line=$(printf "$FORMAT" "$container_name" "$container_status" "$container_os" "$is_protected")
menu_items+=("$container_id" "$formatted_line" "OFF") menu_items+=("$container_id" "$formatted_line" "OFF")
done < <(printf '%s\n' "$containers") done
CHOICES=$(whiptail --title "LXC Container Delete" \ CHOICES=$(whiptail --title "LXC Container Delete" \
--checklist "Select LXC containers to delete:\n\nNAME STATUS OS PROTECTED" 25 70 15 \ --checklist "Select LXC containers to delete:\n\nNAME STATUS OS PROTECTED" 25 70 15 \
"${menu_items[@]}" 3>&2 2>&1 1>&3) "${menu_items[@]}" 3>&2 2>&1 1>&3)
if [ -z "$CHOICES" ]; then if [ -z "$CHOICES" ]; then
whiptail --title "LXC Container Delete" \ whiptail --title "LXC Container Delete" --msgbox "No containers selected!" 10 60
--msgbox "No containers selected!" 10 60
exit 1 exit 1
fi fi
read -p "Delete containers manually or automatically? (Default: manual) m/a: " DELETE_MODE read -p "Delete containers manually or automatically? (Default: manual) m/a: " DELETE_MODE
DELETE_MODE=${DELETE_MODE:-m} DELETE_MODE=${DELETE_MODE:-m}
selected_ids=$(echo "$CHOICES" | tr -d '"' | tr -s ' ' '\n') selected_ids=$(echo "$CHOICES" | tr -d '"' | tr -s ' ' '\n')
# Wenn ALL ausgewählt ist, überschreiben # ALL ausgewählt
if echo "$selected_ids" | grep -q "^ALL$"; then if echo "$selected_ids" | grep -q "^ALL$"; then
selected_ids=$(echo "$containers" | awk '{print $1}') selected_ids=$(printf '%s\n' "${containers[@]}" | awk '{print $1}')
fi fi
for container_id in $selected_ids; do for container_id in $selected_ids; do
status=$(pct status "$container_id") status=$(pct status "$container_id")
protected=$(pct config "$container_id" | grep -i '^protection:' | awk '{print $2}') protected=$(pct config "$container_id" | awk '/^protection:/ {print $2}')
is_protected="No" is_protected="No"
[[ "$protected" == "1" ]] && is_protected="Yes" [[ "$protected" == "1" ]] && is_protected="Yes"
@ -98,7 +96,7 @@ for container_id in $selected_ids; do
continue continue
fi fi
if [ "$status" == "status: running" ]; then if [[ "$status" == "status: running" ]]; then
if [[ "$is_protected" == "Yes" && "$DELETE_MODE" == "m" ]]; then if [[ "$is_protected" == "Yes" && "$DELETE_MODE" == "m" ]]; then
read -p "⚠️ Container $container_id is PROTECTED. Delete anyway? (y/N): " CONFIRM_PROTECTED read -p "⚠️ Container $container_id is PROTECTED. Delete anyway? (y/N): " CONFIRM_PROTECTED
[[ ! "$CONFIRM_PROTECTED" =~ ^[Yy]$ ]] && { [[ ! "$CONFIRM_PROTECTED" =~ ^[Yy]$ ]] && {