Skip to content

Commit e379db8

Browse files
authored
Fix: FS-Trim Cancel / Error-Button (#1965)
1 parent 91ba5cd commit e379db8

File tree

1 file changed

+33
-14
lines changed

1 file changed

+33
-14
lines changed

misc/fstrim.sh

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
# Copyright (c) 2021-2025 tteck
44
# Author: tteck (tteckster)
5-
# License: MIT
6-
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
5+
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
6+
7+
set -o pipefail
78

89
function header_info() {
910
clear
@@ -16,11 +17,13 @@ function header_info() {
1617
/____/
1718
EOF
1819
}
20+
1921
BL=$(echo "\033[36m")
2022
RD=$(echo "\033[01;31m")
2123
CM='\xE2\x9C\x94\033'
2224
GN=$(echo "\033[1;92m")
2325
CL=$(echo "\033[m")
26+
2427
header_info
2528
echo "Loading..."
2629

@@ -29,50 +32,66 @@ if [ "$ROOT_FS" != "ext4" ]; then
2932
echo "Root filesystem is not ext4. Exiting script."
3033
exit 1
3134
fi
35+
whiptail --backtitle "Proxmox VE Helper Scripts" \
36+
--title "Proxmox VE LXC Filesystem Trim" \
37+
--yesno "The LXC containers will undergo the fstrim command. Proceed?" 10 58 || exit
3238

33-
whiptail --backtitle "Proxmox VE Helper Scripts" --title "Proxmox VE LXC Filesystem Trim" --yesno "The LXC containers will undergo the fstrim command. Proceed?" 10 58 || exit
3439
NODE=$(hostname)
3540
EXCLUDE_MENU=()
3641
MSG_MAX_LENGTH=0
42+
3743
while read -r TAG ITEM; do
3844
OFFSET=2
3945
((${#ITEM} + OFFSET > MSG_MAX_LENGTH)) && MSG_MAX_LENGTH=${#ITEM}+OFFSET
4046
EXCLUDE_MENU+=("$TAG" "$ITEM " "OFF")
4147
done < <(pct list | awk 'NR>1')
42-
excluded_containers=$(whiptail --backtitle "Proxmox VE Helper Scripts" --title "Containers on $NODE" --checklist "\nSelect containers to skip from trimming:\n" \
43-
16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3 | tr -d '"') || exit
48+
49+
excluded_containers_raw=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
50+
--title "Containers on $NODE" \
51+
--checklist "\nSelect containers to skip from trimming:\n" \
52+
16 $((MSG_MAX_LENGTH + 23)) 6 "${EXCLUDE_MENU[@]}" 3>&1 1>&2 2>&3)
53+
54+
if [ $? -ne 0 ]; then
55+
exit
56+
fi
57+
58+
excluded_containers=$(echo "$excluded_containers_raw" | tr -d '"')
4459

4560
function trim_container() {
4661
local container=$1
4762
header_info
4863
echo -e "${BL}[Info]${GN} Trimming ${BL}$container${CL} \n"
49-
local before_trim=$(lvs | awk -F '[[:space:]]+' 'NR>1 && (/Data%|'"vm-$container"'/) {gsub(/%/, "", $7); print $7}')
64+
65+
local before_trim
66+
before_trim=$(lvs | awk -F '[[:space:]]+' 'NR>1 && (/Data%|'"vm-$container"'/) {gsub(/%/, "", $7); print $7}')
5067
echo -e "${RD}Data before trim $before_trim%${CL}"
51-
pct fstrim $container
52-
local after_trim=$(lvs | awk -F '[[:space:]]+' 'NR>1 && (/Data%|'"vm-$container"'/) {gsub(/%/, "", $7); print $7}')
68+
69+
pct fstrim "$container"
70+
71+
local after_trim
72+
after_trim=$(lvs | awk -F '[[:space:]]+' 'NR>1 && (/Data%|'"vm-$container"'/) {gsub(/%/, "", $7); print $7}')
5373
echo -e "${GN}Data after trim $after_trim%${CL}"
74+
5475
sleep 1.5
5576
}
5677

57-
58-
5978
for container in $(pct list | awk '{if(NR>1) print $1}'); do
60-
if [[ " ${excluded_containers[@]} " =~ " $container " ]]; then
79+
if [[ " ${excluded_containers} " =~ " $container " ]]; then
6180
header_info
6281
echo -e "${BL}[Info]${GN} Skipping ${BL}$container${CL}"
6382
sleep 1
6483
else
65-
template=$(pct config $container | grep -q "template:" && echo "true" || echo "false")
84+
template=$(pct config "$container" | grep -q "template:" && echo "true" || echo "false")
6685
if [ "$template" == "true" ]; then
6786
header_info
6887
echo -e "${BL}[Info]${GN} Skipping ${container} ${RD}$container is a template ${CL} \n"
6988
sleep 1
7089
continue
7190
fi
72-
trim_container $container
91+
trim_container "$container"
7392
fi
7493
done
7594

7695
wait
7796
header_info
78-
echo -e "${GN} Finished, LXC Containers Trimmed. ${CL} \n"
97+
echo -e "${GN}Finished, LXC Containers Trimmed.${CL} \n"

0 commit comments

Comments
 (0)