diff --git a/json/clean-orphaned-lvm.json b/json/clean-orphaned-lvm.json new file mode 100644 index 00000000..09cb1563 --- /dev/null +++ b/json/clean-orphaned-lvm.json @@ -0,0 +1,39 @@ +{ + "name": "Proxmox Clean Orphaned LVM", + "slug": "clean_orphaned_lvm", + "categories": [ + 1 + ], + "date_created": "2025-01-29", + "type": "misc", + "updateable": false, + "privileged": false, + "interface_port": null, + "documentation": null, + "website": null, + "logo": "https://raw.githubusercontent.com/home-assistant/brands/master/core_integrations/proxmoxve/icon.png", + "description": "This script helps Proxmox users identify and remove orphaned LVM volumes that are no longer associated with any VM or LXC container. It scans all LVM volumes, detects unused ones, and provides an interactive prompt to delete them safely. System-critical volumes like root, swap, and data are excluded to prevent accidental deletion.", + "install_methods": [ + { + "type": "default", + "script": "misc/clean_orphaned_lvm.sh", + "resources": { + "cpu": null, + "ram": null, + "hdd": null, + "os": null, + "version": null + } + } + ], + "default_credentials": { + "username": null, + "password": null + }, + "notes": [ + { + "text": "Execute within the Proxmox shell", + "type": "info" + } + ] +} diff --git a/misc/clean-orphaned-lvm.sh b/misc/clean-orphaned-lvm.sh new file mode 100644 index 00000000..d8ad7820 --- /dev/null +++ b/misc/clean-orphaned-lvm.sh @@ -0,0 +1,83 @@ +#!/usr/bin/env bash + +# Copyright (c) 2021-2025 community-scripts ORG +# Author: MickLesk (CanbiZ) +# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE + +function header_info { + clear + cat <<"EOF" + ____ ________ ____ __ __ __ _ ____ ___ + / __ \_________ _ ______ ___ ____ _ __ / ____/ /__ ____ _____ / __ \_________ / /_ ____ _____ ___ ____/ / / /| | / / |/ /____ + / /_/ / ___/ __ \| |/_/ __ `__ \/ __ \| |/_/ / / / / _ \/ __ `/ __ \ / / / / ___/ __ \/ __ \/ __ `/ __ \/ _ \/ __ / / / | | / / /|_/ / ___/ + / ____/ / / /_/ /> < / /___/ / __/ /_/ / / / / / /_/ / / / /_/ / / / / /_/ / / / / __/ /_/ / / /__| |/ / / / (__ ) +/_/ /_/ \____/_/|_/_/ /_/ /_/\____/_/|_| \____/_/\___/\__,_/_/ /_/ \____/_/ / .___/_/ /_/\__,_/_/ /_/\___/\__,_/ /_____/___/_/ /_/____/ + /_/ +EOF +} + +# Function to check for orphaned LVM volumes +function find_orphaned_lvm { + echo -e "\nšŸ” Scanning for orphaned LVM volumes...\n" + + orphaned_volumes=() + while read -r lv vg size; do + container_id=$(echo "$lv" | grep -oE "[0-9]+" | head -1) + + # Exclude system-critical LVs + if [[ "$lv" == "data" || "$lv" == "root" || "$lv" == "swap" ]]; then + continue + fi + + # Check if the ID exists as a VM or LXC container + if [ -f "/etc/pve/lxc/${container_id}.conf" ] || [ -f "/etc/pve/qemu-server/${container_id}.conf" ]; then + continue + fi + + orphaned_volumes+=("$lv" "$vg" "$size") + done < <(lvs --noheadings -o lv_name,vg_name,lv_size --separator ' ' | awk '{print $1, $2, $3}') + + if [ ${#orphaned_volumes[@]} -eq 0 ]; then + echo -e "āœ… No orphaned LVM volumes found.\n" + exit 0 + fi + + # Display orphaned volumes + echo -e "ā— The following orphaned LVM volumes were found:\n" + printf "%-25s %-10s %-10s\n" "LV Name" "VG" "Size" + printf "%-25s %-10s %-10s\n" "-------------------------" "----------" "----------" + + for ((i = 0; i < ${#orphaned_volumes[@]}; i+=3)); do + printf "%-25s %-10s %-10s\n" "${orphaned_volumes[i]}" "${orphaned_volumes[i+1]}" "${orphaned_volumes[i+2]}" + done + echo "" +} + +# Function to delete selected volumes +function delete_orphaned_lvm { + for ((i = 0; i < ${#orphaned_volumes[@]}; i+=3)); do + lv="${orphaned_volumes[i]}" + vg="${orphaned_volumes[i+1]}" + size="${orphaned_volumes[i+2]}" + + read -p "ā“ Do you want to delete $lv (VG: $vg, Size: $size)? [y/N]: " confirm + if [[ "$confirm" =~ ^[Yy]$ ]]; then + echo -e "šŸ—‘ļø Deleting $lv from $vg..." + lvremove -f "$vg/$lv" + if [ $? -eq 0 ]; then + echo -e "āœ… Successfully deleted $lv.\n" + else + echo -e "āŒ Failed to delete $lv.\n" + fi + else + echo -e "āš ļø Skipping $lv.\n" + fi + done +} + +# Run script +header_info +find_orphaned_lvm +delete_orphaned_lvm + +echo -e "āœ… Cleanup process completed!\n"