64 lines
2.0 KiB
Bash
Executable File
64 lines
2.0 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Ensure script is run as root on the Proxmox host
|
|
if [ "$EUID" -ne 0 ]; then
|
|
echo "Please run as root"
|
|
exit
|
|
fi
|
|
|
|
# Get all LXC IDs
|
|
CONTAINERS=$(pct list | awk 'NR>1 {print $1}')
|
|
|
|
for VMID in $CONTAINERS; do
|
|
# Check if container is running
|
|
STATUS=$(pct status $VMID)
|
|
|
|
if [[ $STATUS == *"status: running"* ]]; then
|
|
echo "========================================================"
|
|
echo " CLEANING ARCH LXC: $VMID"
|
|
echo "========================================================"
|
|
|
|
# 1. Clean Arch Linux Packages
|
|
pct exec $VMID -- bash -c '
|
|
echo "--- Removing orphaned packages ---"
|
|
ORPHANS=$(pacman -Qtdq)
|
|
if [ -n "$ORPHANS" ]; then
|
|
pacman -Rns $ORPHANS --noconfirm
|
|
else
|
|
echo "No orphaned packages found."
|
|
fi
|
|
|
|
echo "--- Fixing and cleaning pacman cache ---"
|
|
# Remove those broken "download-" temporary files manually to stop the FD errors
|
|
rm -f /var/cache/pacman/pkg/download-* 2>/dev/null
|
|
|
|
# Use paccache if installed (cleans all but latest 3 versions),
|
|
# otherwise wipe the cache directory manually.
|
|
if command -v paccache >/dev/null 2>&1; then
|
|
paccache -r -k 0 --noconfirm
|
|
else
|
|
echo "paccache not found, performing manual cache wipe..."
|
|
# Keeping the directory but removing all files
|
|
find /var/cache/pacman/pkg/ -type f -delete
|
|
fi
|
|
'
|
|
|
|
# 2. Clean Podman
|
|
pct exec $VMID -- bash -c '
|
|
if command -v podman >/dev/null 2>&1; then
|
|
echo "--- Podman found. Pruning all unused images ---"
|
|
podman image prune -af
|
|
else
|
|
echo "--- Podman not installed. Skipping ---"
|
|
fi
|
|
'
|
|
|
|
echo "Finished cleaning $VMID"
|
|
echo ""
|
|
else
|
|
echo "Skipping $VMID (Container is powered off)"
|
|
fi
|
|
done
|
|
|
|
echo "All Arch containers processed."
|