Update fstrim.sh

This commit is contained in:
CanbiZ 2025-08-07 13:38:00 +02:00 committed by Push From Github
parent 41008b09fa
commit 453ac9f24e

View File

@ -7,8 +7,7 @@ function header_info() {
cat <<"EOF" cat <<"EOF"
_______ __ __ ______ _ _______ __ __ ______ _
/ ____(_) /__ _______ _______/ /____ ____ ___ /_ __/____(_)___ ___ / ____(_) /__ _______ _______/ /____ ____ ___ /_ __/____(_)___ ___
/ /_ / / / _ \/ ___/ / / / ___/ __/ _ \/ __ `__ \ / / / ___/ / / __ `__ \ / /_ / / / _ \/ ___/ / / / ___/ __/ _ \/ __ `__ \ / / / ___/ / / / / / /
/ __/ / / __(__ ) /_/ (__ ) /_/ __/ / / / / / / / / / / / / / / / / / /
/_/ /_/_/\___/____/\__, /____/\__/\___/_/ /_/ /_/ /_/ /_/ /_/_/ /_/ /_/ /_/ /_/_/\___/____/\__, /____/\__/\___/_/ /_/ /_/ /_/ /_/ /_/_/ /_/ /_/
/____/ /____/
EOF EOF
@ -24,7 +23,7 @@ echo "Loading..."
whiptail --backtitle "Proxmox VE Helper Scripts" \ whiptail --backtitle "Proxmox VE Helper Scripts" \
--title "About fstrim (LXC)" \ --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 --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\nSee: https://pve.proxmox.com/wiki/Shrinking_LXC_disks" 16 88
ROOT_FS=$(df -Th "/" | awk 'NR==2 {print $2}') ROOT_FS=$(df -Th "/" | awk 'NR==2 {print $2}')
if [ "$ROOT_FS" != "ext4" ]; then if [ "$ROOT_FS" != "ext4" ]; then
@ -37,36 +36,51 @@ NODE=$(hostname)
EXCLUDE_MENU=() EXCLUDE_MENU=()
STOPPED_MENU=() STOPPED_MENU=()
MAX_NAME_LEN=0 MAX_NAME_LEN=0
MAX_STAT_LEN=0
while read -r CTID STATUS _ NAME _; do # 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} ((${#NAME} > MAX_NAME_LEN)) && MAX_NAME_LEN=${#NAME}
done < <(pct list | awk 'NR>1') ((${#STATUS} > MAX_STAT_LEN)) && MAX_STAT_LEN=${#STATUS}
done
FMT="%-5s | %-${MAX_NAME_LEN}s | %-8s" FMT="%-5s | %-${MAX_NAME_LEN}s | %-${MAX_STAT_LEN}s"
while read -r CTID STATUS _ NAME _; do 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" "$CTID" "$NAME" "$STATUS")
EXCLUDE_MENU+=("$CTID" "$DESC" "OFF") EXCLUDE_MENU+=("$CTID" "$DESC" "OFF")
if [[ "$STATUS" == "stopped" ]]; then if [[ "$STATUS" == "stopped" ]]; then
STOPPED_MENU+=("$CTID" "$DESC" "OFF") STOPPED_MENU+=("$CTID" "$DESC" "OFF")
fi fi
done < <(pct list | awk 'NR>1') done
excluded_containers_raw=$(whiptail --backtitle "Proxmox VE Helper Scripts" \ excluded_containers_raw=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
--title "Containers on $NODE" \ --title "Containers on $NODE" \
--checklist "\nSelect containers to skip from trimming:\n" \ --checklist "\nSelect containers to skip from trimming:\n" \
20 $((MAX_NAME_LEN + 40)) 12 "${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 [ $? -ne 0 ] && exit
read -ra EXCLUDED <<<$(echo "$excluded_containers_raw" | tr -d '"') read -ra EXCLUDED <<<$(echo "$excluded_containers_raw" | tr -d '"')
TO_START=() TO_START=()
if [ ${#STOPPED_MENU[@]} -gt 0 ]; then if [ ${#STOPPED_MENU[@]} -gt 0 ]; then
selected=$(whiptail --backtitle "Proxmox VE Helper Scripts" \ echo ""
--title "Stopped LXC Containers" \ echo "Some containers are currently stopped."
--checklist "\nSome LXC containers are currently stopped.\nWhich ones do you want to temporarily start for the trim operation?\n(They can be stopped again afterwards)\n" \ for ((i = 0; i < ${#STOPPED_MENU[@]}; i += 3)); do
16 $((MAX_NAME_LEN + 40)) 8 "${STOPPED_MENU[@]}" 3>&1 1>&2 2>&3) CTID="${STOPPED_MENU[i]}"
[ $? -ne 0 ] && exit DESC="${STOPPED_MENU[i + 1]}"
read -ra TO_START <<<$(echo "$selected" | tr -d '"') read -rp "Temporarily start CT $DESC for fstrim? [y/N]: " answer
if [[ "$answer" =~ ^[Yy]$ ]]; then
TO_START+=("$CTID")
fi
done
fi fi
declare -A WAS_STOPPED declare -A WAS_STOPPED
@ -87,47 +101,48 @@ function trim_container() {
sleep 0.5 sleep 0.5
} }
for container in $(pct list | awk 'NR>1 {print $1}'); do for LINE in "${CTLINES[@]}"; do
if [[ " ${EXCLUDED[*]} " =~ " $container " ]]; then CTID=$(awk '{print $1}' <<<"$LINE")
STATUS=$(awk '{print $2}' <<<"$LINE")
NAME=$(awk '{print $3}' <<<"$LINE")
if [[ " ${EXCLUDED[*]} " =~ " $CTID " ]]; then
header_info header_info
echo -e "${BL}[Info]${GN} Skipping $container (excluded)${CL}" echo -e "${BL}[Info]${GN} Skipping $CTID ($NAME, excluded)${CL}"
sleep 0.5 sleep 0.5
continue continue
fi fi
if pct config "$container" | grep -q "template:"; then if pct config "$CTID" | grep -q "template:"; then
header_info header_info
echo -e "${BL}[Info]${GN} Skipping $container (template)${CL}\n" echo -e "${BL}[Info]${GN} Skipping $CTID ($NAME, template)${CL}\n"
sleep 0.5 sleep 0.5
continue continue
fi fi
STATUS=$(pct list | awk -v id="$container" '$1==id{print $2}')
if [[ "$STATUS" != "running" ]]; then if [[ "$STATUS" != "running" ]]; then
if [[ -n "${WAS_STOPPED[$container]:-}" ]]; then if [[ -n "${WAS_STOPPED[$CTID]:-}" ]]; then
header_info header_info
echo -e "${BL}[Info]${GN} Starting $container for trim...${CL}" echo -e "${BL}[Info]${GN} Starting $CTID ($NAME) for trim...${CL}"
pct start "$container" pct start "$CTID"
sleep 2 sleep 2
else else
header_info header_info
echo -e "${BL}[Info]${GN} Skipping $container (not running, not selected)${CL}" echo -e "${BL}[Info]${GN} Skipping $CTID ($NAME, not running, not selected)${CL}"
sleep 0.5 sleep 0.5
continue continue
fi fi
fi fi
trim_container "$container" trim_container "$CTID"
if [[ -n "${WAS_STOPPED[$container]:-}" ]]; then if [[ -n "${WAS_STOPPED[$CTID]:-}" ]]; then
if whiptail --backtitle "Proxmox VE Helper Scripts" \ read -rp "Stop CT $CTID ($NAME) again after trim? [Y/n]: " answer
--title "Stop container again?" \ if [[ ! "$answer" =~ ^[Nn]$ ]]; then
--yesno "Container $container was started for the trim operation.\n\nDo you want to stop it again now?" 10 60; then
header_info header_info
echo -e "${BL}[Info]${GN} Stopping $container again...${CL}" echo -e "${BL}[Info]${GN} Stopping $CTID ($NAME) again...${CL}"
pct stop "$container" pct stop "$CTID"
sleep 1 sleep 1
else else
header_info header_info
echo -e "${BL}[Info]${GN} Leaving $container running as requested.${CL}" echo -e "${BL}[Info]${GN} Leaving $CTID ($NAME) running as requested.${CL}"
sleep 1 sleep 1
fi fi
fi fi