From ace869dc6fe4817398c51807f5e3da1e4c7b7714 Mon Sep 17 00:00:00 2001 From: CanbiZ <47820557+MickLesk@users.noreply.github.com> Date: Thu, 7 Aug 2025 14:03:15 +0200 Subject: [PATCH] Update fstrim.sh --- tools/pve/fstrim.sh | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/tools/pve/fstrim.sh b/tools/pve/fstrim.sh index 679d59a1..b3fef478 100644 --- a/tools/pve/fstrim.sh +++ b/tools/pve/fstrim.sh @@ -7,7 +7,8 @@ function header_info() { cat <<"EOF" _______ __ __ ______ _ / ____(_) /__ _______ _______/ /____ ____ ___ /_ __/____(_)___ ___ - / /_ / / / _ \/ ___/ / / / ___/ __/ _ \/ __ `__ \ / / / ___/ / / / / / / + / /_ / / / _ \/ ___/ / / / ___/ __/ _ \/ __ `__ \ / / / ___/ / __ `__ \ + / __/ / / / __(__ ) /_/ (__ ) /_/ __/ / / / / / / / / / / / / / / / / /_/ /_/_/\___/____/\__, /____/\__/\___/_/ /_/ /_/ /_/ /_/ /_/_/ /_/ /_/ /____/ EOF @@ -49,13 +50,13 @@ for LINE in "${CTLINES[@]}"; do ((${#STATUS} > MAX_STAT_LEN)) && MAX_STAT_LEN=${#STATUS} done -FMT="%-5s | %-${MAX_NAME_LEN}s | %-${MAX_STAT_LEN}s" +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" "$CTID" "$NAME" "$STATUS") + DESC=$(printf "$FMT" "$NAME" "$STATUS") EXCLUDE_MENU+=("$CTID" "$DESC" "OFF") if [[ "$STATUS" == "stopped" ]]; then STOPPED_MENU+=("$CTID" "$DESC" "OFF") @@ -71,12 +72,14 @@ read -ra EXCLUDED <<<$(echo "$excluded_containers_raw" | tr -d '"') TO_START=() if [ ${#STOPPED_MENU[@]} -gt 0 ]; then - echo "" - echo "Some containers are currently stopped." + + echo -e "${BL}[Info]${GN}Some containers are currently stopped.${CL}" for ((i = 0; i < ${#STOPPED_MENU[@]}; i += 3)); do CTID="${STOPPED_MENU[i]}" DESC="${STOPPED_MENU[i + 1]}" - read -rp "Temporarily start CT $DESC for fstrim? [y/N]: " answer + 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 @@ -90,14 +93,19 @@ done function trim_container() { local container="$1" + local name="$2" header_info echo -e "${BL}[Info]${GN} Trimming ${BL}$container${CL} \n" local before_trim after_trim before_trim=$(lvs --noheadings -o lv_name,data_percent | awk -v ctid="vm-${container}-disk-0" '$1 == ctid {gsub(/%/, "", $2); print $2}') echo -e "${RD}Data before trim $before_trim%${CL}" - pct fstrim "$container" + local fstrim_output + fstrim_output=$(pct fstrim "$container" 2>&1) after_trim=$(lvs --noheadings -o lv_name,data_percent | awk -v ctid="vm-${container}-disk-0" '$1 == ctid {gsub(/%/, "", $2); print $2}') echo -e "${GN}Data after trim $after_trim%${CL}" + + # Logging + echo "$(date '+%Y-%m-%d %H:%M:%S') | CTID=$container | Name=$name | Before=$before_trim% | After=$after_trim% | fstrim: $fstrim_output" >>"$LOGFILE" sleep 0.5 } @@ -131,10 +139,10 @@ for LINE in "${CTLINES[@]}"; do fi fi - trim_container "$CTID" + trim_container "$CTID" "$NAME" if [[ -n "${WAS_STOPPED[$CTID]:-}" ]]; then - read -rp "Stop CT $CTID ($NAME) again after trim? [Y/n]: " answer + 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}" @@ -150,4 +158,5 @@ done 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