From a41497f90a632f90c158b7b8a6365c8d3986c312 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Fri, 8 Aug 2025 11:37:36 +0200 Subject: [PATCH] Refactor: FSTrim (Filesystem Trim) (#6660) --- tools/pve/fstrim.sh | 184 +++++++++++++++++++++++++++++++++----------- 1 file changed, 138 insertions(+), 46 deletions(-) diff --git a/tools/pve/fstrim.sh b/tools/pve/fstrim.sh index e31dc9479..4bd68f442 100644 --- a/tools/pve/fstrim.sh +++ b/tools/pve/fstrim.sh @@ -1,10 +1,6 @@ #!/usr/bin/env bash -# Copyright (c) 2021-2025 tteck -# Author: tteck (tteckster) -# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE - -set -o pipefail +set -eEuo pipefail function header_info() { clear @@ -17,81 +13,177 @@ function header_info() { /____/ EOF } -set -eEuo pipefail -BL=$(echo "\033[36m") -RD=$(echo "\033[01;31m") -CM='\xE2\x9C\x94\033' -GN=$(echo "\033[1;92m") -CL=$(echo "\033[m") + +BL="\033[36m" +RD="\033[01;31m" +GN="\033[1;92m" +CL="\033[m" + +LOGFILE="/var/log/fstrim.log" +touch "$LOGFILE" +chmod 600 "$LOGFILE" +echo -e "\n----- $(date '+%Y-%m-%d %H:%M:%S') | fstrim Run by $(whoami) on $(hostname) -----" >>"$LOGFILE" header_info echo "Loading..." +whiptail --backtitle "Proxmox VE Helper Scripts" \ + --title "About fstrim (LXC)" \ + --msgbox "The 'fstrim' command releases unused blocks back to the storage device. This only makes sense for containers on SSD, NVMe, Thin-LVM, or storage with discard/TRIM support.\n\nIf your root filesystem or container disks are on classic HDDs, thick LVM, or unsupported storage types, running fstrim will have no effect.\n\nRecommended:\n- Use fstrim only on SSD, NVMe, or thin-provisioned storage with discard enabled.\n- For ZFS, ensure 'autotrim=on' is set on your pool.\n" 16 88 + ROOT_FS=$(df -Th "/" | awk 'NR==2 {print $2}') if [ "$ROOT_FS" != "ext4" ]; then - echo "Root filesystem is not ext4. Exiting script." - exit 1 + whiptail --backtitle "Proxmox VE Helper Scripts" \ + --title "Warning" \ + --yesno "Root filesystem is not ext4 ($ROOT_FS).\nContinue anyway?" 12 80 || exit 1 fi -whiptail --backtitle "Proxmox VE Helper Scripts" \ - --title "Proxmox VE LXC Filesystem Trim" \ - --yesno "The LXC containers will undergo the fstrim command. Proceed?" 10 58 NODE=$(hostname) EXCLUDE_MENU=() -MSG_MAX_LENGTH=0 +STOPPED_MENU=() +MAX_NAME_LEN=0 +MAX_STAT_LEN=0 -while read -r TAG ITEM; do - OFFSET=2 - ((${#ITEM} + OFFSET > MSG_MAX_LENGTH)) && MSG_MAX_LENGTH=${#ITEM}+OFFSET - EXCLUDE_MENU+=("$TAG" "$ITEM " "OFF") -done < <(pct list | awk 'NR>1') +# Build arrays with one pct list +mapfile -t CTLINES < <(pct list | awk 'NR>1') + +for LINE in "${CTLINES[@]}"; do + CTID=$(awk '{print $1}' <<<"$LINE") + STATUS=$(awk '{print $2}' <<<"$LINE") + NAME=$(awk '{print $3}' <<<"$LINE") + ((${#NAME} > MAX_NAME_LEN)) && MAX_NAME_LEN=${#NAME} + ((${#STATUS} > MAX_STAT_LEN)) && MAX_STAT_LEN=${#STATUS} +done + +FMT="%-${MAX_NAME_LEN}s | %-${MAX_STAT_LEN}s" + +for LINE in "${CTLINES[@]}"; do + CTID=$(awk '{print $1}' <<<"$LINE") + STATUS=$(awk '{print $2}' <<<"$LINE") + NAME=$(awk '{print $3}' <<<"$LINE") + DESC=$(printf "$FMT" "$NAME" "$STATUS") + EXCLUDE_MENU+=("$CTID" "$DESC" "OFF") + if [[ "$STATUS" == "stopped" ]]; then + STOPPED_MENU+=("$CTID" "$DESC" "OFF") + fi +done excluded_containers_raw=$(whiptail --backtitle "Proxmox VE Helper Scripts" \ --title "Containers on $NODE" \ --checklist "\nSelect containers to skip from trimming:\n" \ - 16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3) + 20 $((MAX_NAME_LEN + MAX_STAT_LEN + 20)) 12 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3) +[ $? -ne 0 ] && exit +read -ra EXCLUDED <<<$(echo "$excluded_containers_raw" | tr -d '"') -if [ $? -ne 0 ]; then - exit +TO_START=() +if [ ${#STOPPED_MENU[@]} -gt 0 ]; then + for ((i = 0; i < ${#STOPPED_MENU[@]}; i += 3)); do + CTID="${STOPPED_MENU[i]}" + DESC="${STOPPED_MENU[i + 1]}" + if [[ " ${EXCLUDED[*]} " =~ " $CTID " ]]; then + continue + fi + header_info + echo -e "${BL}[Info]${GN} Container $CTID ($DESC) is currently stopped.${CL}" + read -rp "Temporarily start for fstrim? [y/N]: " answer + if [[ "$answer" =~ ^[Yy]$ ]]; then + TO_START+=("$CTID") + fi + done fi -excluded_containers=$(echo "$excluded_containers_raw" | tr -d '"') +declare -A WAS_STOPPED +for ct in "${TO_START[@]}"; do + WAS_STOPPED["$ct"]=1 +done function trim_container() { - local container=$1 + local container="$1" + local name="$2" header_info echo -e "${BL}[Info]${GN} Trimming ${BL}$container${CL} \n" - local before_trim - before_trim=$(lvs | awk -F '[[:space:]]+' 'NR>1 && (/Data%|'"vm-$container"'/) {gsub(/%/, "", $7); print $7}') - echo -e "${RD}Data before trim $before_trim%${CL}" + local before_trim after_trim + local lv_name="vm-${container}-disk-0" + if lvs --noheadings -o lv_name 2>/dev/null | grep -qw "$lv_name"; then + before_trim=$(lvs --noheadings -o lv_name,data_percent 2>/dev/null | awk -v ctid="$lv_name" '$1 == ctid {gsub(/%/, "", $2); print $2}') + [[ -n "$before_trim" ]] && echo -e "${RD}Data before trim $before_trim%${CL}" || echo -e "${RD}Data before trim: not available${CL}" + else + before_trim="" + echo -e "${RD}Data before trim: not available (non-LVM storage)${CL}" + fi - pct fstrim "$container" + local fstrim_output + fstrim_output=$(pct fstrim "$container" 2>&1) + if echo "$fstrim_output" | grep -qi "not supported"; then + echo -e "${RD}fstrim isnt supported on this storage!${CL}" + elif echo "$fstrim_output" | grep -Eq '([0-9]+(\.[0-9]+)?\s*[KMGT]?B)'; then + echo -e "${GN}fstrim result: $fstrim_output${CL}" + else + echo -e "${RD}fstrim result: $fstrim_output${CL}" + fi - local after_trim - after_trim=$(lvs | awk -F '[[:space:]]+' 'NR>1 && (/Data%|'"vm-$container"'/) {gsub(/%/, "", $7); print $7}') - echo -e "${GN}Data after trim $after_trim%${CL}" + if lvs --noheadings -o lv_name 2>/dev/null | grep -qw "$lv_name"; then + after_trim=$(lvs --noheadings -o lv_name,data_percent 2>/dev/null | awk -v ctid="$lv_name" '$1 == ctid {gsub(/%/, "", $2); print $2}') + [[ -n "$after_trim" ]] && echo -e "${GN}Data after trim $after_trim%${CL}" || echo -e "${GN}Data after trim: not available${CL}" + else + after_trim="" + echo -e "${GN}Data after trim: not available (non-LVM storage)${CL}" + fi - sleep 1.5 + # Logging + echo "$(date '+%Y-%m-%d %H:%M:%S') | CTID=$container | Name=$name | Before=${before_trim:-N/A}% | After=${after_trim:-N/A}% | fstrim: $fstrim_output" >>"$LOGFILE" + sleep 0.5 } -for container in $(pct list | awk '{if(NR>1) print $1}'); do - if [[ " ${excluded_containers} " =~ " $container " ]]; then +for LINE in "${CTLINES[@]}"; do + CTID=$(awk '{print $1}' <<<"$LINE") + STATUS=$(awk '{print $2}' <<<"$LINE") + NAME=$(awk '{print $3}' <<<"$LINE") + if [[ " ${EXCLUDED[*]} " =~ " $CTID " ]]; then header_info - echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}" - sleep 1 - else - template=$(pct config "$container" | grep -q "template:" && echo "true" || echo "false") - if [ "$template" == "true" ]; then + echo -e "${BL}[Info]${GN} Skipping $CTID ($NAME, excluded)${CL}" + sleep 0.5 + continue + fi + if pct config "$CTID" | grep -q "template:"; then + header_info + echo -e "${BL}[Info]${GN} Skipping $CTID ($NAME, template)${CL}\n" + sleep 0.5 + continue + fi + if [[ "$STATUS" != "running" ]]; then + if [[ -n "${WAS_STOPPED[$CTID]:-}" ]]; then header_info - echo -e "${BL}[Info]${GN} Skipping ${container} ${RD}$container is a template ${CL} \n" - sleep 1 + echo -e "${BL}[Info]${GN} Starting $CTID ($NAME) for trim...${CL}" + pct start "$CTID" + sleep 2 + else + header_info + echo -e "${BL}[Info]${GN} Skipping $CTID ($NAME, not running, not selected)${CL}" + sleep 0.5 continue fi - trim_container "$container" + fi + + trim_container "$CTID" "$NAME" + + if [[ -n "${WAS_STOPPED[$CTID]:-}" ]]; then + read -rp "Stop LXC $CTID ($NAME) again after trim? [Y/n]: " answer + if [[ ! "$answer" =~ ^[Nn]$ ]]; then + header_info + echo -e "${BL}[Info]${GN} Stopping $CTID ($NAME) again...${CL}" + pct stop "$CTID" + sleep 1 + else + header_info + echo -e "${BL}[Info]${GN} Leaving $CTID ($NAME) running as requested.${CL}" + sleep 1 + fi fi done -wait header_info echo -e "${GN}Finished, LXC Containers Trimmed.${CL} \n" +echo -e "${BL}If you want to see the complete log: cat $LOGFILE${CL}" +exit 0