Update fstrim.sh

This commit is contained in:
CanbiZ 2025-08-07 13:27:47 +02:00 committed by Push From Github
parent c6066d2755
commit 8496f217e3

View File

@ -1,8 +1,5 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# Copyright (c) 2021-2025 tteck
# License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
set -eEuo pipefail set -eEuo pipefail
function header_info() { function header_info() {
@ -10,8 +7,8 @@ function header_info() {
cat <<"EOF" cat <<"EOF"
_______ __ __ ______ _ _______ __ __ ______ _
/ ____(_) /__ _______ _______/ /____ ____ ___ /_ __/____(_)___ ___ / ____(_) /__ _______ _______/ /____ ____ ___ /_ __/____(_)___ ___
/ /_ / / / _ \/ ___/ / / / ___/ __/ _ \/ __ `__ \ / / / ___/ / __ `__ \ / /_ / / / _ \/ ___/ / / / ___/ __/ _ \/ __ `__ \ / / / ___/ / / __ `__ \
/ __/ / / / __(__ ) /_/ (__ ) /_/ __/ / / / / / / / / / / / / / / / / / __/ / / __(__ ) /_/ (__ ) /_/ __/ / / / / / / / / / / / / / / / / / /
/_/ /_/_/\___/____/\__, /____/\__/\___/_/ /_/ /_/ /_/ /_/ /_/_/ /_/ /_/ /_/ /_/_/\___/____/\__, /____/\__/\___/_/ /_/ /_/ /_/ /_/ /_/_/ /_/ /_/
/____/ /____/
EOF EOF
@ -37,42 +34,31 @@ if [ "$ROOT_FS" != "ext4" ]; then
fi fi
NODE=$(hostname) NODE=$(hostname)
EXCLUDE_MENU=()
STOPPED_MENU=()
MAX_NAME_LEN=0 MAX_NAME_LEN=0
declare -A NAMES
declare -A STATUS
for CTID in $(pct list | awk 'NR>1 {print $1}'); do while read -r CTID STATUS _ NAME _; do
NAME=$(pct config "$CTID" | awk -F': ' '/^hostname:/ {print $2}')
CTSTATUS=$(pct status "$CTID" | awk '{print $2}')
NAMES["$CTID"]="$NAME"
STATUS["$CTID"]="$CTSTATUS"
((${#NAME} > MAX_NAME_LEN)) && MAX_NAME_LEN=${#NAME} ((${#NAME} > MAX_NAME_LEN)) && MAX_NAME_LEN=${#NAME}
done done < <(pct list | awk 'NR>1')
FMT="%-5s | %-${MAX_NAME_LEN}s | %-8s" FMT="%-5s | %-${MAX_NAME_LEN}s | %-8s"
EXCLUDE_MENU=() while read -r CTID STATUS _ NAME _; do
for CTID in "${!NAMES[@]}"; do DESC=$(printf "$FMT" "$CTID" "$NAME" "$STATUS")
DESC=$(printf "$FMT" "$CTID" "${NAMES[$CTID]}" "${STATUS[$CTID]}")
EXCLUDE_MENU+=("$CTID" "$DESC" "OFF") EXCLUDE_MENU+=("$CTID" "$DESC" "OFF")
done if [[ "$STATUS" == "stopped" ]]; then
STOPPED_MENU+=("$CTID" "$DESC" "OFF")
fi
done < <(pct list | awk 'NR>1')
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 + 40)) 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 '"')
STOPPED_MENU=()
for CTID in "${!NAMES[@]}"; do
if [[ "${STATUS[$CTID]}" == "stopped" ]]; then
DESC=$(printf "$FMT" "$CTID" "${NAMES[$CTID]}" "${STATUS[$CTID]}")
STOPPED_MENU+=("$CTID" "$DESC" "OFF")
fi
done
TO_START=() TO_START=()
if [ ${#STOPPED_MENU[@]} -gt 0 ]; then if [ ${#STOPPED_MENU[@]} -gt 0 ]; then
selected=$(whiptail --backtitle "Proxmox VE Helper Scripts" \ selected=$(whiptail --backtitle "Proxmox VE Helper Scripts" \
@ -101,46 +87,47 @@ function trim_container() {
sleep 0.5 sleep 0.5
} }
for CTID in $(pct list | awk 'NR>1 {print $1}'); do for container in $(pct list | awk 'NR>1 {print $1}'); do
if [[ " ${EXCLUDED[*]} " =~ " $CTID " ]]; then if [[ " ${EXCLUDED[*]} " =~ " $container " ]]; then
header_info header_info
echo -e "${BL}[Info]${GN} Skipping $CTID (excluded)${CL}" echo -e "${BL}[Info]${GN} Skipping $container (excluded)${CL}"
sleep 0.5 sleep 0.5
continue continue
fi fi
if pct config "$CTID" | grep -q "template:"; then if pct config "$container" | grep -q "template:"; then
header_info header_info
echo -e "${BL}[Info]${GN} Skipping $CTID (template)${CL}\n" echo -e "${BL}[Info]${GN} Skipping $container (template)${CL}\n"
sleep 0.5 sleep 0.5
continue continue
fi fi
if [[ "${STATUS[$CTID]}" != "running" ]]; then STATUS=$(pct list | awk -v id="$container" '$1==id{print $2}')
if [[ -n "${WAS_STOPPED[$CTID]:-}" ]]; then if [[ "$STATUS" != "running" ]]; then
if [[ -n "${WAS_STOPPED[$container]:-}" ]]; then
header_info header_info
echo -e "${BL}[Info]${GN} Starting $CTID for trim...${CL}" echo -e "${BL}[Info]${GN} Starting $container for trim...${CL}"
pct start "$CTID" pct start "$container"
sleep 2 sleep 2
else else
header_info header_info
echo -e "${BL}[Info]${GN} Skipping $CTID (not running, not selected)${CL}" echo -e "${BL}[Info]${GN} Skipping $container (not running, not selected)${CL}"
sleep 0.5 sleep 0.5
continue continue
fi fi
fi fi
trim_container "$CTID" trim_container "$container"
if [[ -n "${WAS_STOPPED[$CTID]:-}" ]]; then if [[ -n "${WAS_STOPPED[$container]:-}" ]]; then
if whiptail --backtitle "Proxmox VE Helper Scripts" \ if whiptail --backtitle "Proxmox VE Helper Scripts" \
--title "Stop container again?" \ --title "Stop container again?" \
--yesno "Container $CTID (${NAMES[$CTID]}) was started for the trim operation.\n\nDo you want to stop it again now?" 10 60; 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 $CTID again...${CL}" echo -e "${BL}[Info]${GN} Stopping $container again...${CL}"
pct stop "$CTID" pct stop "$container"
sleep 1 sleep 1
else else
header_info header_info
echo -e "${BL}[Info]${GN} Leaving $CTID running as requested.${CL}" echo -e "${BL}[Info]${GN} Leaving $container running as requested.${CL}"
sleep 1 sleep 1
fi fi
fi fi